Skip to content

Commit fd3f108

Browse files
committed
Update sync-back script
This is intended as a workaround until #3556 is merged.
1 parent 87f4948 commit fd3f108

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

pr-checks/sync-back.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,41 @@ const steps = [
188188
const result = updateSyncTs(syncTsPath, actionVersions);
189189
assert.equal(result, false);
190190
});
191+
192+
await it("updates SHA-pinned pinnedUses references", () => {
193+
/** Test updating `pinnedUses(...)` references with new SHA and version */
194+
const syncTsContent = `
195+
const steps = [
196+
{
197+
uses: pinnedUses(
198+
"actions/setup-node",
199+
"0000000000000000000000000000000000000000",
200+
"v6.0.0",
201+
),
202+
},
203+
];
204+
`;
205+
206+
fs.writeFileSync(syncTsPath, syncTsContent);
207+
208+
const actionVersions = {
209+
"actions/setup-node": "48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0",
210+
};
211+
212+
const result = updateSyncTs(syncTsPath, actionVersions);
213+
assert.equal(result, true);
214+
215+
const updatedContent = fs.readFileSync(syncTsPath, "utf8");
216+
217+
assert.ok(
218+
updatedContent.includes('"48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e"'),
219+
);
220+
assert.ok(updatedContent.includes('"v6.4.0"'));
221+
assert.ok(
222+
!updatedContent.includes("0000000000000000000000000000000000000000"),
223+
);
224+
assert.ok(!updatedContent.includes('"v6.0.0"'));
225+
});
191226
});
192227

193228
describe("updateTemplateFiles", async () => {

pr-checks/sync-back.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ export function scanGeneratedWorkflows(
6868
/**
6969
* Update hardcoded action versions in pr-checks/sync.ts
7070
*
71+
* Handles both inline `uses: "owner/action@ref"` strings and SHA-pinned
72+
* references expressed via the `pinnedUses("owner/action", "<sha>", "version")`
73+
* helper.
74+
*
7175
* @param syncTsPath - Path to sync.ts file
7276
* @param actionVersions - Map of action names to versions (may include comments)
7377
* @returns True if the file was modified, false otherwise
@@ -87,18 +91,36 @@ export function updateSyncTs(
8791
for (const [actionName, versionWithComment] of Object.entries(
8892
actionVersions,
8993
)) {
90-
// Extract just the version part (before any comment) for sync.ts
91-
const version = versionWithComment.includes("#")
94+
// Split the scanned value into the ref (e.g. a commit SHA) and the optional
95+
// trailing version comment (e.g. `v6.0.3`).
96+
const ref = versionWithComment.includes("#")
9297
? versionWithComment.split("#")[0].trim()
9398
: versionWithComment.trim();
99+
const versionComment = versionWithComment.includes("#")
100+
? versionWithComment.split("#")[1].trim()
101+
: "";
102+
103+
const escaped = actionName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
94104

95105
// Look for patterns like uses: "actions/setup-node@v4"
96106
// Note that this will break if we store an Action uses reference in a
97107
// variable - that's a risk we're happy to take since in that case the
98108
// PR checks will just fail.
99-
const escaped = actionName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
100-
const pattern = new RegExp(`(uses:\\s*")${escaped}@(?:[^"]+)(")`, "g");
101-
content = content.replace(pattern, `$1${actionName}@${version}$2`);
109+
const usesPattern = new RegExp(`(uses:\\s*")${escaped}@(?:[^"]+)(")`, "g");
110+
content = content.replace(usesPattern, `$1${actionName}@${ref}$2`);
111+
112+
// Look for SHA-pinned references expressed via the `pinnedUses` helper, e.g.
113+
// `pinnedUses("actions/checkout", "<sha>", "v6.0.3")`, updating both the
114+
// pinned ref and the version comment.
115+
const pinnedPattern = new RegExp(
116+
`(pinnedUses\\(\\s*")${escaped}("\\s*,\\s*")[^"]*("\\s*,\\s*")([^"]*)(")`,
117+
"g",
118+
);
119+
content = content.replace(
120+
pinnedPattern,
121+
(_match, p1, p2, p3, oldVersion, p5) =>
122+
`${p1}${actionName}${p2}${ref}${p3}${versionComment || oldVersion}${p5}`,
123+
);
102124
}
103125

104126
if (content !== originalContent) {

0 commit comments

Comments
 (0)