Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build-desktop-app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 58 additions & 0 deletions tests/test_reusable_workflow_permissions.py
Original file line number Diff line number Diff line change
@@ -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}"
Loading