|
| 1 | +name: Protect workflow baseline |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: |
| 6 | + - opened |
| 7 | + - synchronize |
| 8 | + - reopened |
| 9 | + - labeled |
| 10 | + - unlabeled |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: write |
| 14 | + pull-requests: write |
| 15 | + |
| 16 | +concurrency: |
| 17 | + group: workflow-baseline-${{ github.event.pull_request.number }} |
| 18 | + cancel-in-progress: true |
| 19 | + |
| 20 | +jobs: |
| 21 | + restore-canonical-workflows: |
| 22 | + name: Restore canonical workflow files |
| 23 | + if: >- |
| 24 | + github.event.pull_request.head.repo.full_name == github.repository && |
| 25 | + !contains(github.event.pull_request.labels.*.name, 'workflow-change-approved') |
| 26 | + runs-on: ubuntu-latest |
| 27 | + |
| 28 | + steps: |
| 29 | + - name: Check out pull-request branch |
| 30 | + uses: actions/checkout@v4 |
| 31 | + with: |
| 32 | + repository: ${{ github.event.pull_request.head.repo.full_name }} |
| 33 | + ref: ${{ github.event.pull_request.head.ref }} |
| 34 | + fetch-depth: 0 |
| 35 | + token: ${{ secrets.OVVO_SYNC_TOKEN || github.token }} |
| 36 | + |
| 37 | + - name: Restore workflow baseline from default branch |
| 38 | + shell: bash |
| 39 | + env: |
| 40 | + BASE_REF: ${{ github.event.repository.default_branch }} |
| 41 | + HEAD_REF: ${{ github.event.pull_request.head.ref }} |
| 42 | + run: | |
| 43 | + set -euo pipefail |
| 44 | +
|
| 45 | + git fetch origin \ |
| 46 | + "${BASE_REF}:refs/remotes/origin/${BASE_REF}" \ |
| 47 | + --force |
| 48 | +
|
| 49 | + protected_workflows=( |
| 50 | + ".github/workflows/workflow-baseline.yml" |
| 51 | + ".github/workflows/native-backend-ci.yml" |
| 52 | + ".github/workflows/inspect-r-api-update.yml" |
| 53 | + ".github/workflows/docs.yml" |
| 54 | + ".github/workflows/release.yml" |
| 55 | + ) |
| 56 | +
|
| 57 | + mkdir -p .github/workflows |
| 58 | +
|
| 59 | + for path in "${protected_workflows[@]}"; do |
| 60 | + git show "origin/${BASE_REF}:${path}" > "${path}" |
| 61 | + done |
| 62 | +
|
| 63 | + while IFS= read -r -d '' path; do |
| 64 | + case "${path}" in |
| 65 | + ".github/workflows/workflow-baseline.yml"|\ |
| 66 | + ".github/workflows/native-backend-ci.yml"|\ |
| 67 | + ".github/workflows/inspect-r-api-update.yml"|\ |
| 68 | + ".github/workflows/docs.yml"|\ |
| 69 | + ".github/workflows/release.yml") |
| 70 | + ;; |
| 71 | + *) |
| 72 | + echo "Removing non-baseline workflow: ${path}" |
| 73 | + rm -f "${path}" |
| 74 | + ;; |
| 75 | + esac |
| 76 | + done < <( |
| 77 | + find .github/workflows \ |
| 78 | + -maxdepth 1 \ |
| 79 | + -type f \ |
| 80 | + \( -name '*.yml' -o -name '*.yaml' \) \ |
| 81 | + -print0 |
| 82 | + ) |
| 83 | +
|
| 84 | + git config user.name "github-actions[bot]" |
| 85 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 86 | +
|
| 87 | + git add -A .github/workflows |
| 88 | +
|
| 89 | + if git diff --cached --quiet; then |
| 90 | + echo "Workflow files already match the canonical baseline." |
| 91 | + exit 0 |
| 92 | + fi |
| 93 | +
|
| 94 | + git commit -m "Restore protected workflow baseline" |
| 95 | + git push origin "HEAD:${HEAD_REF}" |
| 96 | +
|
| 97 | + - name: Record enforcement result |
| 98 | + if: always() |
| 99 | + shell: bash |
| 100 | + run: | |
| 101 | + { |
| 102 | + echo "## Workflow baseline enforcement" |
| 103 | + echo |
| 104 | + echo "The default branch is the source of truth for GitHub Actions." |
| 105 | + echo "Normal fix PRs cannot add, delete, or modify workflow YAML." |
| 106 | + echo |
| 107 | + echo "Apply the \`workflow-change-approved\` label only for an intentional workflow change." |
| 108 | + } >> "${GITHUB_STEP_SUMMARY}" |
0 commit comments