-
Notifications
You must be signed in to change notification settings - Fork 445
Convert sync_back.py to TypeScript
#3529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6a6bd77
Add initial `sync_back.ts` script
mbg e1b83cc
Add `scanGeneratedWorkflows`
mbg f05cfae
Add `updateSyncTs`
mbg dd779fa
Add `updateTemplateFiles`
mbg 8eb0202
Port tests
mbg a6892dc
Use `sync_back.ts` in `rebuild` workflow
mbg aaed7b7
Merge remote-tracking branch 'origin/main' into mbg/ts/sync-back
mbg 24fa947
Update `pr-checks` to run new tests
mbg bf9bf1c
Remove python setup from `rebuild` workflow
mbg 77fc89c
Remove python files from `pr-checks`
mbg 0a5b95c
Update `pr-checks` README
mbg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Port tests
- Loading branch information
commit 8eb0202e9d4ba01c2d56e0ce47f216da4a368502
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| #!/usr/bin/env npx tsx | ||
|
|
||
| /* | ||
| Tests for the sync_back.ts script | ||
| */ | ||
|
|
||
| import * as assert from "node:assert/strict"; | ||
| import * as fs from "node:fs"; | ||
| import * as os from "node:os"; | ||
| import * as path from "node:path"; | ||
| import { afterEach, beforeEach, describe, it } from "node:test"; | ||
|
|
||
| import { | ||
| scanGeneratedWorkflows, | ||
| updateSyncTs, | ||
| updateTemplateFiles, | ||
| } from "./sync_back"; | ||
|
|
||
| let testDir: string; | ||
| let workflowDir: string; | ||
| let checksDir: string; | ||
| let syncTsPath: string; | ||
|
|
||
| beforeEach(() => { | ||
| /** Set up temporary directories and files for testing */ | ||
| testDir = fs.mkdtempSync(path.join(os.tmpdir(), "sync-back-test-")); | ||
| workflowDir = path.join(testDir, ".github", "workflows"); | ||
| checksDir = path.join(testDir, "pr-checks", "checks"); | ||
| fs.mkdirSync(workflowDir, { recursive: true }); | ||
| fs.mkdirSync(checksDir, { recursive: true }); | ||
|
|
||
| // Create sync.ts file path | ||
| syncTsPath = path.join(testDir, "pr-checks", "sync.ts"); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| /** Clean up temporary directories */ | ||
| fs.rmSync(testDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe("scanGeneratedWorkflows", () => { | ||
| it("basic workflow scanning", () => { | ||
| /** Test basic workflow scanning functionality */ | ||
| const workflowContent = ` | ||
| name: Test Workflow | ||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/setup-node@v5 | ||
| - uses: actions/setup-go@v6 | ||
| `; | ||
|
|
||
| fs.writeFileSync(path.join(workflowDir, "__test.yml"), workflowContent); | ||
|
|
||
| const result = scanGeneratedWorkflows(workflowDir); | ||
|
|
||
| assert.equal(result["actions/checkout"], "v4"); | ||
| assert.equal(result["actions/setup-node"], "v5"); | ||
| assert.equal(result["actions/setup-go"], "v6"); | ||
| }); | ||
|
|
||
| it("scanning workflows with version comments", () => { | ||
| /** Test scanning workflows with version comments */ | ||
| const workflowContent = ` | ||
| name: Test Workflow | ||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: ruby/setup-ruby@44511735964dcb71245e7e55f72539531f7bc0eb # v1.257.0 | ||
| - uses: actions/setup-python@v6 # Latest Python | ||
| `; | ||
|
|
||
| fs.writeFileSync(path.join(workflowDir, "__test.yml"), workflowContent); | ||
|
|
||
| const result = scanGeneratedWorkflows(workflowDir); | ||
|
|
||
| assert.equal(result["actions/checkout"], "v4"); | ||
| assert.equal( | ||
| result["ruby/setup-ruby"], | ||
| "44511735964dcb71245e7e55f72539531f7bc0eb # v1.257.0", | ||
| ); | ||
| assert.equal(result["actions/setup-python"], "v6 # Latest Python"); | ||
| }); | ||
|
|
||
| it("ignores local actions", () => { | ||
| /** Test that local actions (starting with ./) are ignored */ | ||
| const workflowContent = ` | ||
| name: Test Workflow | ||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: ./.github/actions/local-action | ||
| - uses: ./another-local-action@v1 | ||
| `; | ||
|
|
||
| fs.writeFileSync(path.join(workflowDir, "__test.yml"), workflowContent); | ||
|
|
||
| const result = scanGeneratedWorkflows(workflowDir); | ||
|
|
||
| assert.equal(result["actions/checkout"], "v4"); | ||
| assert.equal("./.github/actions/local-action" in result, false); | ||
| assert.equal("./another-local-action" in result, false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("updateSyncTs", () => { | ||
| it("updates sync.ts file", () => { | ||
| /** Test updating sync.ts file */ | ||
| const syncTsContent = ` | ||
| const steps = [ | ||
| { | ||
| uses: "actions/setup-node@v4", | ||
| with: { "node-version": "16" }, | ||
| }, | ||
| { | ||
| uses: "actions/setup-go@v5", | ||
| with: { "go-version": "1.19" }, | ||
| }, | ||
| ]; | ||
| `; | ||
|
|
||
| fs.writeFileSync(syncTsPath, syncTsContent); | ||
|
|
||
| const actionVersions = { | ||
| "actions/setup-node": "v5", | ||
| "actions/setup-go": "v6", | ||
| }; | ||
|
|
||
| const result = updateSyncTs(syncTsPath, actionVersions); | ||
| assert.equal(result, true); | ||
|
|
||
| const updatedContent = fs.readFileSync(syncTsPath, "utf8"); | ||
|
|
||
| assert.ok(updatedContent.includes('uses: "actions/setup-node@v5"')); | ||
| assert.ok(updatedContent.includes('uses: "actions/setup-go@v6"')); | ||
| }); | ||
|
|
||
| it("strips comments from versions", () => { | ||
| /** Test updating sync.ts file when versions have comments */ | ||
| const syncTsContent = ` | ||
| const steps = [ | ||
| { | ||
| uses: "actions/setup-node@v4", | ||
| with: { "node-version": "16" }, | ||
| }, | ||
| ]; | ||
| `; | ||
|
|
||
| fs.writeFileSync(syncTsPath, syncTsContent); | ||
|
|
||
| const actionVersions = { | ||
| "actions/setup-node": "v5 # Latest version", | ||
| }; | ||
|
|
||
| const result = updateSyncTs(syncTsPath, actionVersions); | ||
| assert.equal(result, true); | ||
|
|
||
| const updatedContent = fs.readFileSync(syncTsPath, "utf8"); | ||
|
|
||
| // sync.ts should get the version without comment | ||
| assert.ok(updatedContent.includes('uses: "actions/setup-node@v5"')); | ||
| assert.ok(!updatedContent.includes("# Latest version")); | ||
| }); | ||
|
|
||
| it("returns false when no changes are needed", () => { | ||
| /** Test that updateSyncTs returns false when no changes are needed */ | ||
| const syncTsContent = ` | ||
| const steps = [ | ||
| { | ||
| uses: "actions/setup-node@v5", | ||
| with: { "node-version": "16" }, | ||
| }, | ||
| ]; | ||
| `; | ||
|
|
||
| fs.writeFileSync(syncTsPath, syncTsContent); | ||
|
|
||
| const actionVersions = { | ||
| "actions/setup-node": "v5", | ||
| }; | ||
|
|
||
| const result = updateSyncTs(syncTsPath, actionVersions); | ||
| assert.equal(result, false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("updateTemplateFiles", () => { | ||
| it("updates template files", () => { | ||
| /** Test updating template files */ | ||
| const templateContent = ` | ||
| name: Test Template | ||
| steps: | ||
| - uses: actions/checkout@v3 | ||
| - uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: 16 | ||
| `; | ||
|
|
||
| const templatePath = path.join(checksDir, "test.yml"); | ||
| fs.writeFileSync(templatePath, templateContent); | ||
|
|
||
| const actionVersions = { | ||
| "actions/checkout": "v4", | ||
| "actions/setup-node": "v5 # Latest", | ||
| }; | ||
|
|
||
| const result = updateTemplateFiles(checksDir, actionVersions); | ||
| assert.equal(result.length, 1); | ||
| assert.ok(result.includes(templatePath)); | ||
|
|
||
| const updatedContent = fs.readFileSync(templatePath, "utf8"); | ||
|
|
||
| assert.ok(updatedContent.includes("uses: actions/checkout@v4")); | ||
| assert.ok(updatedContent.includes("uses: actions/setup-node@v5 # Latest")); | ||
| }); | ||
|
|
||
| it("preserves version comments", () => { | ||
| /** Test that updating template files preserves version comments */ | ||
| const templateContent = ` | ||
| name: Test Template | ||
| steps: | ||
| - uses: ruby/setup-ruby@44511735964dcb71245e7e55f72539531f7bc0eb # v1.256.0 | ||
| `; | ||
|
|
||
| const templatePath = path.join(checksDir, "test.yml"); | ||
| fs.writeFileSync(templatePath, templateContent); | ||
|
|
||
| const actionVersions = { | ||
| "ruby/setup-ruby": | ||
| "55511735964dcb71245e7e55f72539531f7bc0eb # v1.257.0", | ||
| }; | ||
|
|
||
| const result = updateTemplateFiles(checksDir, actionVersions); | ||
| assert.equal(result.length, 1); | ||
|
|
||
| const updatedContent = fs.readFileSync(templatePath, "utf8"); | ||
|
|
||
| assert.ok( | ||
| updatedContent.includes( | ||
| "uses: ruby/setup-ruby@55511735964dcb71245e7e55f72539531f7bc0eb # v1.257.0", | ||
| ), | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.