Skip to content

Commit 9f7dece

Browse files
committed
fix: sync/refresh ordering
1 parent d263096 commit 9f7dece

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

scripts/__tests__/sync.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {GitHub} from '../src/github'
99
import env from '../src/env'
1010
import {Resource} from '../src/resources/resource'
1111
import {RepositoryFile} from '../src/resources/repository-file'
12+
import {refresh} from '../src/refresh'
1213

1314
test('sync', async () => {
1415
const yamlConfig = new config.Config('{}')
@@ -77,8 +78,10 @@ test('sync new repository file', async () => {
7778
const expectedYamlConfig = new config.Config(YAML.stringify(yamlSource))
7879

7980
await sync(tfConfig, yamlConfig)
80-
8181
yamlConfig.format()
82+
expect(yamlConfig.toString().trim()).toEqual('{}')
8283

84+
await refresh(tfConfig, yamlConfig)
85+
yamlConfig.format()
8386
expect(yamlConfig.toString()).toEqual(expectedYamlConfig.toString())
8487
})

scripts/src/actions/fix-yaml-config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ const uninitialisedRepositoryNames = [
1111
]
1212

1313
addFileToAllRepos(
14-
'.github/workflows/stale.yml',
1514
'.github/workflows/stale.yml',
16-
(repository: Repository) => !uninitialisedRepositoryNames.includes(repository.name)
15+
'.github/workflows/stale.yml',
16+
(repository: Repository) =>
17+
!uninitialisedRepositoryNames.includes(repository.name)
1718
)
1819
format()

scripts/src/main.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
import 'reflect-metadata'
22
import {sync} from './sync'
3+
import {refresh} from './refresh'
34
import {State} from './terraform/state'
45
import {Config} from './yaml/config'
56

67
async function run(): Promise<void> {
78
const state = await State.New()
89
const config = Config.FromPath()
910

11+
// This step calls GitHub API to get the latest state of the world
12+
// and updates the TF state to match. It only updates the resources,
13+
// not their attributes.
1014
await sync(state, config)
15+
config.save()
1116

17+
// This step calls Terraform to get the latest state of the world
18+
// and updates the TF state to match. It updates the resource attributes.
19+
// It requires config to be saved first because it needs to know about
20+
// all the resources.
21+
await refresh(state, config)
1222
config.save()
1323
}
1424

scripts/src/refresh.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {State} from './terraform/state'
2+
import {Config} from './yaml/config'
3+
4+
export async function refresh(state: State, config: Config): Promise<void> {
5+
await state.refresh()
6+
7+
const refreshedResources = state.getAllResources()
8+
config.sync(refreshedResources)
9+
}

scripts/src/sync.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export async function sync(state: State, config: Config): Promise<void> {
1414
}
1515

1616
await state.sync(resources)
17-
await state.refresh()
1817

1918
const syncedResources = state.getAllResources()
2019
config.sync(syncedResources)

0 commit comments

Comments
 (0)