diff --git a/.github/workflows/build-desktop-app.yaml b/.github/workflows/build-desktop-app.yaml index dcc49187..d8ecf0ab 100644 --- a/.github/workflows/build-desktop-app.yaml +++ b/.github/workflows/build-desktop-app.yaml @@ -568,8 +568,11 @@ jobs: publish-web-image: needs: release if: github.event_name == 'release' && !cancelled() && needs.release.result == 'success' + # Every permission a job of build-and-test.yml asks for has to be granted + # here, or GitHub refuses to start the whole run (startup_failure). permissions: contents: read packages: write + pull-requests: read uses: ./.github/workflows/build-and-test.yml secrets: inherit diff --git a/tests/test_reusable_workflow_permissions.py b/tests/test_reusable_workflow_permissions.py new file mode 100644 index 00000000..35d32288 --- /dev/null +++ b/tests/test_reusable_workflow_permissions.py @@ -0,0 +1,58 @@ +"""A job that calls a reusable workflow must grant every permission the called +workflow's jobs ask for. GitHub otherwise refuses to start the whole calling +run (startup_failure), which once kept a release's desktop installers from +being built at all.""" + +from pathlib import Path + +import pytest +import yaml + +WORKFLOWS = Path(".github/workflows") +LEVELS = {"none": 0, "read": 1, "write": 2} + + +def _load(path): + return yaml.safe_load(path.read_text(encoding="utf-8")) + + +def _perms(block): + if block is None: + return {} + if isinstance(block, str): # read-all / write-all + return {"*": block.split("-")[0]} + return dict(block) + + +def _calls(): + for path in sorted(WORKFLOWS.glob("*.y*ml")): + doc = _load(path) + top = _perms(doc.get("permissions")) + for name, job in (doc.get("jobs") or {}).items(): + uses = job.get("uses", "") + if uses.startswith("./.github/workflows/"): + granted = _perms(job["permissions"]) if "permissions" in job else top + yield path.name, name, Path(uses), granted + + +def _requested(called): + doc = _load(called) + wanted = dict(_perms(doc.get("permissions"))) + for job in (doc.get("jobs") or {}).values(): + for scope, level in _perms(job.get("permissions")).items(): + if LEVELS[level] > LEVELS.get(wanted.get(scope, "none"), 0): + wanted[scope] = level + return wanted + + +CALLS = list(_calls()) + + +@pytest.mark.parametrize("caller,job,called,granted", CALLS, ids=[f"{c}:{j}" for c, j, _, _ in CALLS]) +def test_caller_grants_what_the_called_workflow_needs(caller, job, called, granted): + missing = { + scope: level + for scope, level in _requested(called).items() + if LEVELS[level] > LEVELS.get(granted.get(scope, granted.get("*", "none")), 0) + } + assert not missing, f"{caller} job {job} calls {called.name} without granting {missing}"