diff --git a/.github/scripts/tests/test_pr_status_labels_workflow.py b/.github/scripts/tests/test_pr_status_labels_workflow.py new file mode 100644 index 000000000..b2ae559a8 --- /dev/null +++ b/.github/scripts/tests/test_pr_status_labels_workflow.py @@ -0,0 +1,55 @@ +import re +from pathlib import Path +from typing import Any + +import pytest +import yaml + +WORKFLOW_PATH = Path(__file__).parents[2] / "workflows" / "pr-status-labels.yml" +TEST_PARSER_PATH = Path(__file__).parents[2] / "workflows" / "test-parser.yml" + + +@pytest.fixture(name="workflow") +def workflow_fixture() -> dict[Any, Any]: + return yaml.safe_load(WORKFLOW_PATH.read_text()) + + +def test_runs_on_events_that_change_status(workflow: dict[Any, Any]) -> None: + # PyYAML reads the bare key `on` as the boolean True. + assert workflow[True] == { + "pull_request_target": { + "types": ["opened", "reopened", "ready_for_review", "synchronize"] + } + } + + +def test_calls_shared_workflow_pinned_to_commit(workflow: dict[Any, Any]) -> None: + uses = workflow["jobs"]["pr-status-labels"]["uses"] + + assert re.fullmatch( + r"aws/aws-durable-execution-ci/\.github/workflows/pr-status-labels\.yml" + r"@[0-9a-f]{40}", + uses, + ) + + +def test_grants_only_label_permissions(workflow: dict[Any, Any]) -> None: + assert workflow["permissions"] == {} + assert workflow["jobs"]["pr-status-labels"]["permissions"] == { + "contents": "read", + "pull-requests": "write", + } + + +def test_passes_no_secrets(workflow: dict[Any, Any]) -> None: + # pull_request_target runs for fork pull requests with the base + # repository's token, so the job gets no secrets. + assert "secrets" not in workflow["jobs"]["pr-status-labels"] + + +def test_workflow_triggers_script_tests() -> None: + parser = yaml.safe_load(TEST_PARSER_PATH.read_text()) + path = ".github/workflows/pr-status-labels.yml" + + assert path in parser[True]["pull_request"]["paths"] + assert path in parser[True]["push"]["paths"] diff --git a/.github/workflows/pr-status-labels.yml b/.github/workflows/pr-status-labels.yml new file mode 100644 index 000000000..5e818b530 --- /dev/null +++ b/.github/workflows/pr-status-labels.yml @@ -0,0 +1,14 @@ +name: PR Status Labels + +on: + pull_request_target: + types: [opened, reopened, ready_for_review, synchronize] + +permissions: {} + +jobs: + pr-status-labels: + permissions: + contents: read + pull-requests: write + uses: aws/aws-durable-execution-ci/.github/workflows/pr-status-labels.yml@c28fdc7df300996309e282ee6bfe54658ba53351 # v0.7.0 diff --git a/.github/workflows/test-parser.yml b/.github/workflows/test-parser.yml index e241d81b4..1e68ce19a 100644 --- a/.github/workflows/test-parser.yml +++ b/.github/workflows/test-parser.yml @@ -14,6 +14,7 @@ on: - '.github/workflows/closed-issue-comment.yml' - '.github/workflows/issue-triage.yml' - '.github/workflows/notify.yml' + - '.github/workflows/pr-status-labels.yml' - '.github/workflows/stale-issue-closer.yml' - '.github/workflows/opentelemetry-conformance-tests.yml' - '.github/workflows/test-parser.yml' @@ -34,6 +35,7 @@ on: - '.github/workflows/closed-issue-comment.yml' - '.github/workflows/issue-triage.yml' - '.github/workflows/notify.yml' + - '.github/workflows/pr-status-labels.yml' - '.github/workflows/stale-issue-closer.yml' - '.github/workflows/opentelemetry-conformance-tests.yml' - '.github/workflows/test-parser.yml' @@ -80,6 +82,7 @@ jobs: .github/scripts/tests/test_opentelemetry_conformance_workflow.py \ .github/scripts/tests/test_parse_sdk_branch.py \ .github/scripts/tests/test_parse_testing_version.py \ + .github/scripts/tests/test_pr_status_labels_workflow.py \ .github/scripts/tests/test_release_workflow_dependencies.py \ .github/scripts/tests/e2e/test_ecr_release_workflow.py \ .github/scripts/tests/test_shared_workflow_runners.py