|
| 1 | +name: API Diff Check |
| 2 | + |
| 3 | +on: |
| 4 | + # SECURITY: Using pull_request_target to support fork PRs with write permissions. |
| 5 | + # PR code is checked out but only for static analysis - it is never executed. |
| 6 | + # If modifying this workflow, ensure PR code is never executed and user inputs are not used unsafely. |
| 7 | + pull_request_target: |
| 8 | + types: [opened, synchronize] |
| 9 | + paths: |
| 10 | + - 'pkg/**/*.go' |
| 11 | + - 'rpc/**/*.go' |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: read |
| 15 | + pull-requests: write |
| 16 | + issues: write |
| 17 | + |
| 18 | +jobs: |
| 19 | + apidiff: |
| 20 | + runs-on: ubuntu-24.04 |
| 21 | + name: API Diff Check |
| 22 | + steps: |
| 23 | + # Check if PR has conflicts. When conflicts exist, the merge commit becomes |
| 24 | + # frozen at an old state and apidiff cannot run correctly. |
| 25 | + - name: Check for merge conflicts |
| 26 | + env: |
| 27 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 28 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 29 | + # pull_request_target and mergeability are processed asynchronously. |
| 30 | + # As a result, it’s possible that we start the check before GitHub has finished calculating the mergeability. |
| 31 | + # To handle this, a retry mechanism has been added — it waits for 2 seconds after each attempt. |
| 32 | + # If mergeable_state isn’t obtained after 5 attempts, an error is returned. |
| 33 | + run: | |
| 34 | + MAX=5 |
| 35 | + for i in $(seq 1 "$MAX"); do |
| 36 | + state=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq .mergeable_state) |
| 37 | + echo "mergeable_state=$state" |
| 38 | + |
| 39 | + if [ "$state" = "dirty" ]; then |
| 40 | + echo "::error::This PR has merge conflicts. Please resolve conflicts before running apidiff." |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + |
| 44 | + if [ -n "$state" ] && [ "$state" != "unknown" ] && [ "$state" != "null" ]; then |
| 45 | + break |
| 46 | + fi |
| 47 | + |
| 48 | + if [ "$i" -lt "$MAX" ] && { [ -z "$state" ] || [ "$state" = "unknown" ] || [ "$state" = "null" ]; }; then |
| 49 | + echo "::error::Could not determine mergeability after $i tries." |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | + |
| 53 | + sleep 2 |
| 54 | + done |
| 55 | +
|
| 56 | + # Checkout PR merge commit to compare against base branch |
| 57 | + # This ensures we compare the actual merge result with the base branch, |
| 58 | + # avoiding false positives when PR is not rebased with latest main |
| 59 | + - name: Checkout |
| 60 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 61 | + with: |
| 62 | + ref: refs/pull/${{ github.event.pull_request.number }}/merge |
| 63 | + |
| 64 | + - name: Set up Go |
| 65 | + uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0 |
| 66 | + with: |
| 67 | + go-version-file: go.mod |
| 68 | + check-latest: true # Ensure we use the latest Go patch version |
| 69 | + cache: false |
| 70 | + |
| 71 | + # Ensure the base commit exists locally for go-apidiff to compare against. |
| 72 | + # Even though we checkout the merge commit, go-apidiff needs the base ref to exist. |
| 73 | + # Use base.ref instead of base.sha, since base.sha is outdated (not updated after every commit). |
| 74 | + # cf. https://github.com/orgs/community/discussions/59677 |
| 75 | + - name: Fetch base commit |
| 76 | + id: fetch_base |
| 77 | + run: | |
| 78 | + set -euo pipefail |
| 79 | + BASE_REF="${{ github.event.pull_request.base.ref || github.event.merge_group.base_ref }}" |
| 80 | + if [ -z "${BASE_REF:-}" ]; then |
| 81 | + echo "::error::BASE_REF is empty (no base ref in event payload)"; exit 1 |
| 82 | + fi |
| 83 | + |
| 84 | + git fetch --depth=1 origin "$BASE_REF" |
| 85 | + |
| 86 | + BASE_SHA="$(git rev-parse "origin/$BASE_REF")" |
| 87 | + if [ -z "${BASE_SHA:-}" ]; then |
| 88 | + echo "::error::BASE_SHA is empty (failed to resolve origin/$BASE_REF)"; exit 1 |
| 89 | + fi |
| 90 | + echo "base_sha=$BASE_SHA" >> "$GITHUB_OUTPUT" |
| 91 | + |
| 92 | + # NOTE: go-apidiff is not managed in go.mod because installing it via `go get -tool` |
| 93 | + # would cause `mage tool:install` to attempt building it on Windows, which currently |
| 94 | + # fails due to platform-specific issues. |
| 95 | + - name: Run go-apidiff |
| 96 | + id: apidiff |
| 97 | + continue-on-error: true |
| 98 | + uses: joelanford/go-apidiff@60c4206be8f84348ebda2a3e0c3ac9cb54b8f685 # v0.8.3 |
| 99 | + with: |
| 100 | + base-ref: ${{ steps.fetch_base.outputs.base_sha }} |
| 101 | + version: v0.8.3 |
| 102 | + |
| 103 | + - name: Add apidiff label |
| 104 | + if: ${{ steps.apidiff.outputs.semver-type == 'major' }} |
| 105 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 106 | + with: |
| 107 | + script: | |
| 108 | + const label = 'apidiff'; |
| 109 | + await github.rest.issues.addLabels({ |
| 110 | + owner: context.repo.owner, |
| 111 | + repo: context.repo.repo, |
| 112 | + issue_number: context.issue.number, |
| 113 | + labels: [label], |
| 114 | + }); |
| 115 | +
|
| 116 | + - name: Comment API diff |
| 117 | + if: ${{ steps.apidiff.outputs.semver-type == 'major' }} |
| 118 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 119 | + env: |
| 120 | + APIDIFF_OUTPUT: ${{ steps.apidiff.outputs.output }} |
| 121 | + SEMVER_TYPE: ${{ steps.apidiff.outputs.semver-type }} |
| 122 | + with: |
| 123 | + script: | |
| 124 | + const header = '## 📊 API Changes Detected'; |
| 125 | + const diff = process.env.APIDIFF_OUTPUT.trim(); |
| 126 | + const semver = process.env.SEMVER_TYPE || 'unknown'; |
| 127 | + const body = [ |
| 128 | + header, |
| 129 | + '', |
| 130 | + `Semver impact: \`${semver}\``, |
| 131 | + '', |
| 132 | + '```', |
| 133 | + diff, |
| 134 | + '```', |
| 135 | + ].join('\n'); |
| 136 | +
|
| 137 | + const { data: comments } = await github.rest.issues.listComments({ |
| 138 | + owner: context.repo.owner, |
| 139 | + repo: context.repo.repo, |
| 140 | + issue_number: context.issue.number, |
| 141 | + }); |
| 142 | +
|
| 143 | + const existing = comments.find(comment => |
| 144 | + comment.user.type === 'Bot' && |
| 145 | + comment.body.startsWith(header), |
| 146 | + ); |
| 147 | +
|
| 148 | + if (existing) { |
| 149 | + await github.rest.issues.updateComment({ |
| 150 | + owner: context.repo.owner, |
| 151 | + repo: context.repo.repo, |
| 152 | + comment_id: existing.id, |
| 153 | + body, |
| 154 | + }); |
| 155 | + } else { |
| 156 | + await github.rest.issues.createComment({ |
| 157 | + owner: context.repo.owner, |
| 158 | + repo: context.repo.repo, |
| 159 | + issue_number: context.issue.number, |
| 160 | + body, |
| 161 | + }); |
| 162 | + } |
| 163 | +
|
| 164 | + # Attempt to request the premium reviewers; needs org-scoped token because GITHUB_TOKEN lacks read:org. |
| 165 | + - name: Request trivy-premium review |
| 166 | + if: ${{ steps.apidiff.outputs.semver-type == 'major' }} |
| 167 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 168 | + with: |
| 169 | + github-token: ${{ secrets.ORG_REPO_TOKEN }} |
| 170 | + script: | |
| 171 | + try { |
| 172 | + await github.rest.pulls.requestReviewers({ |
| 173 | + owner: context.repo.owner, |
| 174 | + repo: context.repo.repo, |
| 175 | + pull_number: context.issue.number, |
| 176 | + team_reviewers: ['trivy-premium'], |
| 177 | + }); |
| 178 | + console.log('Requested review from aquasecurity/trivy-premium team'); |
| 179 | + } catch (error) { |
| 180 | + core.error(`Failed to request trivy-premium reviewers: ${error.message}`); |
| 181 | + throw error; |
| 182 | + } |
0 commit comments