Skip to content

Commit 7b8e4ce

Browse files
committed
Maintain action versions by workflow in sync-back.ts
This avoids failures on PRs where `sync-back.ts` is run as part of the "Rebuild" job if there are already version inconsistencies on the base branch.
1 parent c050421 commit 7b8e4ce

2 files changed

Lines changed: 120 additions & 31 deletions

File tree

‎pr-checks/sync-back.test.ts‎

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,18 @@ jobs:
5656

5757
const result = scanGeneratedWorkflows(workflowDir);
5858

59-
assert.equal(result["actions/checkout"], "v4");
60-
assert.equal(result["actions/setup-node"], "v5");
61-
assert.equal(result["actions/setup-go"], "v6");
59+
assert.deepEqual(
60+
result.actionVersions["actions/checkout"],
61+
new Set(["v4"]),
62+
);
63+
assert.deepEqual(
64+
result.actionVersions["actions/setup-node"],
65+
new Set(["v5"]),
66+
);
67+
assert.deepEqual(
68+
result.actionVersions["actions/setup-go"],
69+
new Set(["v6"]),
70+
);
6271
});
6372

6473
await it("scanning workflows with version comments", () => {
@@ -78,12 +87,18 @@ jobs:
7887

7988
const result = scanGeneratedWorkflows(workflowDir);
8089

81-
assert.equal(result["actions/checkout"], "v4");
82-
assert.equal(
83-
result["ruby/setup-ruby"],
84-
"44511735964dcb71245e7e55f72539531f7bc0eb # v1.257.0",
90+
assert.deepEqual(
91+
result.actionVersions["actions/checkout"],
92+
new Set(["v4"]),
93+
);
94+
assert.deepEqual(
95+
result.actionVersions["ruby/setup-ruby"],
96+
new Set(["44511735964dcb71245e7e55f72539531f7bc0eb # v1.257.0"]),
97+
);
98+
assert.deepEqual(
99+
result.actionVersions["actions/setup-python"],
100+
new Set(["v6 # Latest Python"]),
85101
);
86-
assert.equal(result["actions/setup-python"], "v6 # Latest Python");
87102
});
88103

89104
await it("ignores local actions", () => {
@@ -103,9 +118,15 @@ jobs:
103118

104119
const result = scanGeneratedWorkflows(workflowDir);
105120

106-
assert.equal(result["actions/checkout"], "v4");
107-
assert.equal("./.github/actions/local-action" in result, false);
108-
assert.equal("./another-local-action" in result, false);
121+
assert.deepEqual(
122+
result.actionVersions["actions/checkout"],
123+
new Set(["v4"]),
124+
);
125+
assert.equal(
126+
"./.github/actions/local-action" in result.actionVersions,
127+
false,
128+
);
129+
assert.equal("./another-local-action" in result.actionVersions, false);
109130
});
110131
});
111132

@@ -128,8 +149,8 @@ const steps = [
128149
fs.writeFileSync(syncTsPath, syncTsContent);
129150

130151
const actionVersions = {
131-
"actions/setup-node": "v5",
132-
"actions/setup-go": "v6",
152+
"actions/setup-node": new Set(["v5"]),
153+
"actions/setup-go": new Set(["v6"]),
133154
};
134155

135156
const result = updateSyncTs(syncTsPath, actionVersions);
@@ -155,7 +176,7 @@ const steps = [
155176
fs.writeFileSync(syncTsPath, syncTsContent);
156177

157178
const actionVersions = {
158-
"actions/setup-node": "v5 # Latest version",
179+
"actions/setup-node": new Set(["v5 # Latest version"]),
159180
};
160181

161182
const result = updateSyncTs(syncTsPath, actionVersions);
@@ -182,7 +203,7 @@ const steps = [
182203
fs.writeFileSync(syncTsPath, syncTsContent);
183204

184205
const actionVersions = {
185-
"actions/setup-node": "v5",
206+
"actions/setup-node": new Set(["v5"]),
186207
};
187208

188209
const result = updateSyncTs(syncTsPath, actionVersions);
@@ -206,7 +227,9 @@ const steps = [
206227
fs.writeFileSync(syncTsPath, syncTsContent);
207228

208229
const actionVersions = {
209-
"actions/setup-node": "48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0",
230+
"actions/setup-node": new Set([
231+
"48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0",
232+
]),
210233
};
211234

212235
const result = updateSyncTs(syncTsPath, actionVersions);
@@ -244,8 +267,9 @@ steps:
244267
"actions/checkout": "v4",
245268
"actions/setup-node": "v5 # Latest",
246269
};
270+
const byWorkflow = { ["test.yml"]: actionVersions };
247271

248-
const result = updateTemplateFiles(checksDir, actionVersions);
272+
const result = updateTemplateFiles(checksDir, byWorkflow);
249273
assert.equal(result.length, 1);
250274
assert.ok(result.includes(templatePath));
251275

@@ -269,8 +293,9 @@ steps:
269293
const actionVersions = {
270294
"ruby/setup-ruby": "55511735964dcb71245e7e55f72539531f7bc0eb # v1.257.0",
271295
};
296+
const byWorkflow = { ["test.yml"]: actionVersions };
272297

273-
const result = updateTemplateFiles(checksDir, actionVersions);
298+
const result = updateTemplateFiles(checksDir, byWorkflow);
274299
assert.equal(result.length, 1);
275300

276301
const updatedContent = fs.readFileSync(templatePath, "utf8");

‎pr-checks/sync-back.ts‎

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ const CHECKS_DIR = path.join(PR_CHECKS_DIR, "checks");
2727
const WORKFLOW_DIR = path.join(REPO_ROOT, ".github", "workflows");
2828
const SYNC_TS_PATH = path.join(PR_CHECKS_DIR, "sync.ts");
2929

30+
/** Type alias for action names. */
31+
type ActionName = string;
32+
33+
/** Type alias for action versions. May include a comment. */
34+
type ActionVersion = string;
35+
36+
/** Mappings of {@link ActionName}s to {@link ActionVersion}s. */
37+
type ActionVersions = Record<ActionName, ActionVersion>;
38+
39+
/** Mappings of workflow names to {@link ActionVersions}. */
40+
type ActionVersionsByWorkflow = Record<string, ActionVersions>;
41+
42+
/** The result of {@link scanGeneratedWorkflows}. */
43+
type ScanWorkflowResult = {
44+
/** A mapping of {@link ActionName}s to all encountered {@link ActionVersion}s. */
45+
actionVersions: Record<ActionName, Set<ActionVersion>>;
46+
47+
/** A mapping of workflow names to {@link ActionVersions}. */
48+
actionVersionsByWorkflow: ActionVersionsByWorkflow;
49+
};
50+
3051
/**
3152
* Scan generated workflow files to extract the latest action versions.
3253
*
@@ -35,8 +56,9 @@ const SYNC_TS_PATH = path.join(PR_CHECKS_DIR, "sync.ts");
3556
*/
3657
export function scanGeneratedWorkflows(
3758
workflowDir: string,
38-
): Record<string, string> {
39-
const actionVersions: Record<string, string> = {};
59+
): ScanWorkflowResult {
60+
const actionVersionsByWorkflow: ActionVersionsByWorkflow = {};
61+
const actionVersions: Record<ActionName, Set<ActionVersion>> = {};
4062

4163
const generatedFiles = fs
4264
.readdirSync(workflowDir)
@@ -46,6 +68,10 @@ export function scanGeneratedWorkflows(
4668
for (const filePath of generatedFiles) {
4769
const content = fs.readFileSync(filePath, "utf8");
4870

71+
// Get the basename of the workflow and drop the leading `__` from it.
72+
const workflowName = path.basename(filePath).slice(2);
73+
actionVersionsByWorkflow[workflowName] = {};
74+
4975
// Find all action uses in the file, including potential comments
5076
// This pattern captures: action_name@version_with_possible_comment
5177
const pattern = /uses:\s+([^/\s]+\/[^@\s]+)@([^@\n]+)/g;
@@ -57,13 +83,26 @@ export function scanGeneratedWorkflows(
5783

5884
// Only track non-local actions (those with / but not starting with ./)
5985
if (!actionName.startsWith("./")) {
60-
// Assume that version numbers are consistent (this should be the case on a Dependabot update PR)
61-
actionVersions[actionName] = versionWithComment;
86+
// Record the version of the action for this workflow.
87+
actionVersionsByWorkflow[workflowName][actionName] = versionWithComment;
88+
89+
// Record this version for the action so that we can write it to `sync.ts` if needed.
90+
// Usually we'd expect there to be exactly one version per action across all workflows,
91+
// but sometimes these can go out-of-sync if e.g. a workflow is added in a branch and a
92+
// Dependabot PR is merged in the meantime that changes the version of an action that is
93+
// used in the new workflow. Different versions of the same action across different
94+
// workflows is not fundamentally a problem, but we may need to pick one for `sync.ts`
95+
// so we warn there if we have multiple versions of an action.
96+
if (actionVersions[actionName] === undefined) {
97+
actionVersions[actionName] = new Set([versionWithComment]);
98+
} else if (!actionVersions[actionName].has(versionWithComment)) {
99+
actionVersions[actionName].add(versionWithComment);
100+
}
62101
}
63102
}
64103
}
65104

66-
return actionVersions;
105+
return { actionVersions, actionVersionsByWorkflow };
67106
}
68107

69108
/**
@@ -79,7 +118,7 @@ export function scanGeneratedWorkflows(
79118
*/
80119
export function updateSyncTs(
81120
syncTsPath: string,
82-
actionVersions: Record<string, string>,
121+
actionVersions: Record<ActionName, Set<ActionVersion>>,
83122
): boolean {
84123
if (!fs.existsSync(syncTsPath)) {
85124
throw new Error(`Could not find ${syncTsPath}`);
@@ -89,9 +128,18 @@ export function updateSyncTs(
89128
const originalContent = content;
90129

91130
// Update hardcoded action versions
92-
for (const [actionName, versionWithComment] of Object.entries(
131+
for (const [actionName, versionsWithComments] of Object.entries(
93132
actionVersions,
94133
)) {
134+
// We usually expect `versionsWithComments` to contain one version. If there are multiple,
135+
// we arbitrarily pick one and warn about it.
136+
const versionsWithCommentsArray = Array.from(versionsWithComments);
137+
const versionWithComment = versionsWithCommentsArray[0];
138+
if (versionsWithComments.size > 1) {
139+
console.warn(
140+
`Inconsistent versions for ${actionName}. Arbitrarily picking '${versionWithComment}'. All found: ${versionsWithCommentsArray.join(", ")}`,
141+
);
142+
}
95143
// Split the scanned value into the ref (e.g. a commit SHA) and the optional
96144
// trailing version comment (e.g. `v6.0.3`).
97145
const ref = versionWithComment.includes("#")
@@ -138,12 +186,12 @@ export function updateSyncTs(
138186
* Update action versions in template files in pr-checks/checks/
139187
*
140188
* @param checksDir - Path to pr-checks/checks directory
141-
* @param actionVersions - Map of action names to versions (may include comments)
189+
* @param actionVersionsByWorkflow - Map of workflows to maps of action names to versions (may include comments)
142190
* @returns List of files that were modified
143191
*/
144192
export function updateTemplateFiles(
145193
checksDir: string,
146-
actionVersions: Record<string, string>,
194+
actionVersionsByWorkflow: ActionVersionsByWorkflow,
147195
): string[] {
148196
const modifiedFiles: string[] = [];
149197

@@ -156,6 +204,16 @@ export function updateTemplateFiles(
156204
let content = fs.readFileSync(filePath, "utf8");
157205
const originalContent = content;
158206

207+
const workflowName = path.basename(filePath);
208+
const actionVersions = actionVersionsByWorkflow[workflowName];
209+
210+
if (actionVersions === undefined) {
211+
console.info(
212+
`Found no action versions for workflow template '${filePath}'.`,
213+
);
214+
continue;
215+
}
216+
159217
// Update action versions
160218
for (const [actionName, versionWithComment] of Object.entries(
161219
actionVersions,
@@ -191,12 +249,15 @@ function main(): number {
191249
const verbose = values.verbose ?? false;
192250

193251
console.info("Scanning generated workflows for latest action versions...");
194-
const actionVersions = scanGeneratedWorkflows(WORKFLOW_DIR);
252+
const { actionVersions, actionVersionsByWorkflow } =
253+
scanGeneratedWorkflows(WORKFLOW_DIR);
195254

196255
if (verbose) {
197256
console.info("Found action versions:");
198-
for (const [action, version] of Object.entries(actionVersions)) {
199-
console.info(` ${action}@${version}`);
257+
for (const [action, versions] of Object.entries(actionVersions)) {
258+
for (const version of versions) {
259+
console.info(` ${action}@${version}`);
260+
}
200261
}
201262
}
202263

@@ -215,7 +276,10 @@ function main(): number {
215276
}
216277

217278
// Update template files
218-
const templateModified = updateTemplateFiles(CHECKS_DIR, actionVersions);
279+
const templateModified = updateTemplateFiles(
280+
CHECKS_DIR,
281+
actionVersionsByWorkflow,
282+
);
219283
modifiedFiles.push(...templateModified);
220284

221285
if (modifiedFiles.length > 0) {

0 commit comments

Comments
 (0)