Repository navigation
ci: skip ai-rebase when its token is not configured (Refs #4461) #4730
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
Closed
KelboLive
wants to merge
2
commits into
obs-rust:master
from
KelboLive:ci/4461-ai-rebase-token-guard
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,89 @@ | ||
| #!/usr/bin/env -S uv run --script | ||
| """The optional ai-rebase workflow must skip when its token is unset (#4461). | ||
|
|
||
| uv run python -m unittest ci/test_ai_rebase_token_guard.py | ||
|
|
||
| `actions/checkout` fails with `Input required and not supplied: token` when | ||
| `secrets.AI_REBASE_TOKEN` is empty, so a master push armed the ai-rebase job, | ||
| paid for setup, and then died before its rebase logic (run 42724921146 of the | ||
| #4450 master push). The workflow is optional: with no token configured it must | ||
| skip its root job instead. Every root job whose body reads | ||
| `secrets.AI_REBASE_TOKEN` now carries `secrets.AI_REBASE_TOKEN != ''` in its | ||
| `if:`, so an unconfigured repository skips the whole job. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| ROOT = Path(__file__).resolve().parent.parent | ||
| WORKFLOW = ROOT / ".github" / "workflows" / "ai-rebase.yaml" | ||
|
|
||
| TOKEN_REF = "secrets.AI_REBASE_TOKEN" | ||
| GUARD = "secrets.AI_REBASE_TOKEN != ''" | ||
|
|
||
|
|
||
| def jobs(text: str) -> dict[str, str]: | ||
| """Each job's name and body (the lines below ` <name>:`).""" | ||
| body = text.split("\njobs:\n", 1)[1] | ||
| parts = re.split(r"^ ([A-Za-z0-9_-]+):\n", body, flags=re.M) | ||
| return dict(zip(parts[1::2], parts[2::2])) | ||
|
|
||
|
|
||
| def condition(job: str) -> str: | ||
| """The job's `if:`, with a folded block (`>-`) joined into one line.""" | ||
| m = re.search(r"^ if: *(.*)\n((?: .*\n)*)", job, re.M) | ||
| if not m: | ||
| return "" | ||
| lines = [m.group(1)] if m.group(1) not in (">-", ">", "|") else [] | ||
| lines += [line.strip() for line in m.group(2).splitlines()] | ||
| return " ".join(lines) | ||
|
|
||
|
|
||
| def unguarded_token_jobs(text: str) -> list[str]: | ||
| return [ | ||
| name | ||
| for name, job in jobs(text).items() | ||
| if TOKEN_REF in job and GUARD not in condition(job) | ||
| ] | ||
|
|
||
|
|
||
| WITH_GUARD = """\ | ||
| on: | ||
| push: | ||
| branches: [master] | ||
| jobs: | ||
| rebase: | ||
| if: >- | ||
| (github.event_name != 'schedule' || github.repository == 'obs-rust/obs-studio') && | ||
| secrets.AI_REBASE_TOKEN != '' | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout | ||
| with: | ||
| token: ${{ secrets.AI_REBASE_TOKEN }} | ||
| """ | ||
|
|
||
| WITHOUT_GUARD = WITH_GUARD.replace(" secrets.AI_REBASE_TOKEN != ''\n", "") | ||
|
|
||
|
|
||
| class ParserTest(unittest.TestCase): | ||
| def test_accepts_a_folded_guarded_condition(self): | ||
| self.assertEqual(unguarded_token_jobs(WITH_GUARD), []) | ||
|
|
||
| def test_reports_the_unguarded_token_job(self): | ||
| self.assertEqual(unguarded_token_jobs(WITHOUT_GUARD), ["rebase"]) | ||
|
|
||
| def test_ignores_jobs_without_the_token(self): | ||
| self.assertEqual(unguarded_token_jobs("on:\nx\njobs:\n other:\n runs-on: x\n"), []) | ||
|
|
||
|
|
||
| class WorkflowTest(unittest.TestCase): | ||
| def test_ai_rebase_skips_when_the_token_is_unset(self): | ||
| self.assertEqual(unguarded_token_jobs(WORKFLOW.read_text(encoding="utf-8")), []) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1]
secretsis unavailable injobs.<job_id>.if, so this condition makes the workflow invalid instead of skipping an unconfigured job. actionlint 1.7.11 reportscontext "secrets" is not allowed hereat 41:92. GitHub lists onlygithub,needs,varsandinputsfor this position in its context availability table. Keep the fork guard here and use a supported preflight or step-level guard; a boolean job environment flag must guard every setup/execution step, including checkout.