Skip to content

Commit 6679b04

Browse files
committed
Generate separate TS file with version mappings
1 parent 13e47f8 commit 6679b04

File tree

2 files changed

+64
-92
lines changed

2 files changed

+64
-92
lines changed

pr-checks/sync_back.test.ts

Lines changed: 45 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import { afterEach, beforeEach, describe, it } from "node:test";
1313
import {
1414
Options,
1515
scanGeneratedWorkflows,
16-
updateSyncTs,
16+
updateActionVersions,
1717
updateTemplateFiles,
1818
} from "./sync_back";
1919

2020
let testDir: string;
2121
let workflowDir: string;
2222
let checksDir: string;
23-
let syncTsPath: string;
23+
let actionVersionsTsPath: string;
2424

2525
const defaultOptions: Options = { verbose: false, force: false };
2626

@@ -32,8 +32,8 @@ beforeEach(() => {
3232
fs.mkdirSync(workflowDir, { recursive: true });
3333
fs.mkdirSync(checksDir, { recursive: true });
3434

35-
// Create sync.ts file path
36-
syncTsPath = path.join(testDir, "pr-checks", "sync.ts");
35+
// Create action-versions.ts file path
36+
actionVersionsTsPath = path.join(testDir, "pr-checks", "action-versions.ts");
3737
});
3838

3939
afterEach(() => {
@@ -130,83 +130,67 @@ jobs:
130130
});
131131
});
132132

133-
describe("updateSyncTs", () => {
134-
it("updates sync.ts file", () => {
135-
/** Test updating sync.ts file */
136-
const syncTsContent = `
137-
const steps = [
138-
{
139-
uses: "actions/setup-node@v4",
140-
with: { "node-version": "16" },
133+
describe("updateActionVersions", () => {
134+
it("updates action-versions.ts file", () => {
135+
/** Test updating action-versions.ts file */
136+
const actionVersionsTsContent = `
137+
export const ACTION_VERSIONS = {
138+
"actions/setup-node": {
139+
"version": "v4"
141140
},
142-
{
143-
uses: "actions/setup-go@v5",
144-
with: { "go-version": "1.19" },
145-
},
146-
];
147-
`;
141+
"actions/setup-go": {
142+
"version": "v5"
143+
}
144+
};
145+
`.trim();
148146

149-
fs.writeFileSync(syncTsPath, syncTsContent);
147+
fs.writeFileSync(actionVersionsTsPath, actionVersionsTsContent);
150148

151149
const actionVersions = {
152150
"actions/setup-node": { version: "v5" },
153151
"actions/setup-go": { version: "v6" },
154152
};
155153

156-
const result = updateSyncTs(defaultOptions, syncTsPath, actionVersions);
157-
assert.equal(result, true);
158-
159-
const updatedContent = fs.readFileSync(syncTsPath, "utf8");
160-
161-
assert.ok(updatedContent.includes('uses: "actions/setup-node@v5"'));
162-
assert.ok(updatedContent.includes('uses: "actions/setup-go@v6"'));
163-
});
164-
165-
it("strips comments from versions", () => {
166-
/** Test updating sync.ts file when versions have comments */
167-
const syncTsContent = `
168-
const steps = [
169-
{
170-
uses: "actions/setup-node@v4",
171-
with: { "node-version": "16" },
172-
},
173-
];
174-
`;
175-
176-
fs.writeFileSync(syncTsPath, syncTsContent);
177-
178-
const actionVersions = {
179-
"actions/setup-node": { version: "v5", comment: " Latest version" },
180-
};
181-
182-
const result = updateSyncTs(defaultOptions, syncTsPath, actionVersions);
154+
const result = updateActionVersions(
155+
defaultOptions,
156+
actionVersionsTsPath,
157+
actionVersions,
158+
);
183159
assert.equal(result, true);
184160

185-
const updatedContent = fs.readFileSync(syncTsPath, "utf8");
161+
const updatedContent = fs.readFileSync(actionVersionsTsPath, "utf8");
186162

187-
// sync.ts should get the version without comment
188-
assert.ok(updatedContent.includes('uses: "actions/setup-node@v5"'));
189-
assert.ok(!updatedContent.includes("# Latest version"));
163+
assert.ok(
164+
updatedContent.includes('"actions/setup-node": {\n "version": "v5"'),
165+
);
166+
assert.ok(
167+
updatedContent.includes('"actions/setup-go": {\n "version": "v6"'),
168+
);
190169
});
191170

192171
it("returns false when no changes are needed", () => {
193-
/** Test that updateSyncTs returns false when no changes are needed */
194-
const syncTsContent = `
195-
const steps = [
196-
{
197-
uses: "actions/setup-node@v5",
198-
with: { "node-version": "16" },
199-
},
200-
];
201-
`;
172+
/** Test that updateActionVersions returns false when no changes are needed */
173+
const actionVersionsTsContent = `
174+
export const ACTION_VERSIONS = {
175+
"actions/setup-node": {
176+
"version": "v5"
177+
}
178+
};
179+
`.trim();
202180

203-
fs.writeFileSync(syncTsPath, syncTsContent);
181+
fs.writeFileSync(actionVersionsTsPath, actionVersionsTsContent);
204182

205183
const actionVersions = {
206184
"actions/setup-node": { version: "v5" },
207185
};
208186

209-
const result = updateSyncTs(defaultOptions, syncTsPath, actionVersions);
187+
const result = updateActionVersions(
188+
defaultOptions,
189+
actionVersionsTsPath,
190+
actionVersions,
191+
);
192+
const updatedContent = fs.readFileSync(actionVersionsTsPath, "utf8");
193+
assert.equal(updatedContent, actionVersionsTsContent);
210194
assert.equal(result, false);
211195
});
212196
});

pr-checks/sync_back.ts

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import * as path from "path";
2727
const THIS_DIR = __dirname;
2828
const CHECKS_DIR = path.join(THIS_DIR, "checks");
2929
const WORKFLOW_DIR = path.join(THIS_DIR, "..", ".github", "workflows");
30-
const SYNC_TS_PATH = path.join(THIS_DIR, "sync.ts");
30+
const ACTION_VERSIONS_PATH = path.join(THIS_DIR, "action-versions.ts");
3131

3232
/** Command-line options for this program. */
3333
export type Options = {
@@ -126,46 +126,34 @@ export function scanGeneratedWorkflows(
126126
}
127127

128128
/**
129-
* Update hardcoded action versions in pr-checks/sync.ts
129+
* Update hardcoded action versions in pr-checks/action-versions.ts
130130
*
131131
* @param options - The command-line options.
132-
* @param syncTsPath - Path to sync.ts file
132+
* @param actionVersionsTsPath - Path to action-versions.ts file
133133
* @param actionVersions - Map of action names to versions (may include comments)
134134
* @returns True if the file was modified, false otherwise
135135
*/
136-
export function updateSyncTs(
136+
export function updateActionVersions(
137137
options: Options,
138-
syncTsPath: string,
138+
actionVersionsTsPath: string,
139139
actionVersions: Record<string, ActionVersion>,
140140
): boolean {
141-
if (!fs.existsSync(syncTsPath)) {
142-
throw new Error(`Could not find ${syncTsPath}`);
143-
}
141+
// Build content for the file.
142+
let newContent: string = `export const ACTION_VERSIONS = ${JSON.stringify(actionVersions, null, 2)};`;
144143

145-
let content = fs.readFileSync(syncTsPath, "utf8");
146-
const originalContent = content;
144+
if (fs.existsSync(actionVersionsTsPath)) {
145+
const content = fs.readFileSync(actionVersionsTsPath, "utf8");
147146

148-
// Update hardcoded action versions
149-
for (const [actionName, versionInfo] of Object.entries(actionVersions)) {
150-
// Note that this will break if we store an Action uses reference in a
151-
// variable - that's a risk we're happy to take since in that case the
152-
// PR checks will just fail.
153-
const escaped = actionName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
154-
const pattern = new RegExp(`(uses:\\s*")${escaped}@(?:[^"]+)(")`, "g");
155-
content = content.replace(
156-
pattern,
157-
`$1${actionName}@${versionInfo.version}$2`,
158-
);
147+
if (content === newContent && !options.force) {
148+
console.info(`No changes needed in ${actionVersionsTsPath}`);
149+
return false;
150+
}
159151
}
160152

161-
if (content !== originalContent || options.force) {
162-
fs.writeFileSync(syncTsPath, content, "utf8");
163-
console.info(`Updated ${syncTsPath}`);
164-
return true;
165-
} else {
166-
console.info(`No changes needed in ${syncTsPath}`);
167-
return false;
168-
}
153+
// Update hardcoded action versions
154+
fs.writeFileSync(actionVersionsTsPath, newContent, "utf8");
155+
console.info(`Updated ${actionVersionsTsPath}`);
156+
return true;
169157
}
170158

171159
/**
@@ -263,8 +251,8 @@ function main(): number {
263251
const modifiedFiles: string[] = [];
264252

265253
// Update sync.ts
266-
if (updateSyncTs(values, SYNC_TS_PATH, actionVersions)) {
267-
modifiedFiles.push(SYNC_TS_PATH);
254+
if (updateActionVersions(values, ACTION_VERSIONS_PATH, actionVersions)) {
255+
modifiedFiles.push(ACTION_VERSIONS_PATH);
268256
}
269257

270258
// Update template files

0 commit comments

Comments
 (0)