|
| 1 | +name: API Diff Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize] |
| 6 | + paths: |
| 7 | + - 'pkg/**/*.go' |
| 8 | + - 'rpc/**/*.go' |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + pull-requests: write |
| 13 | + issues: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + apidiff: |
| 17 | + runs-on: ubuntu-24.04 |
| 18 | + name: API Diff Check |
| 19 | + steps: |
| 20 | + - name: Checkout |
| 21 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 22 | + |
| 23 | + - name: Set up Go |
| 24 | + uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0 |
| 25 | + with: |
| 26 | + go-version-file: go.mod |
| 27 | + cache: false |
| 28 | + |
| 29 | + # Ensure the base commit exists locally when checkout uses depth=1 (default). |
| 30 | + - name: Fetch base commit |
| 31 | + run: | |
| 32 | + BASE_REF="${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha }}" |
| 33 | + if [ -n "$BASE_REF" ]; then |
| 34 | + git fetch --depth=1 origin "$BASE_REF" |
| 35 | + fi |
| 36 | +
|
| 37 | + # NOTE: go-apidiff is not managed in go.mod because installing it via `go get -tool` |
| 38 | + # would cause `mage tool:install` to attempt building it on Windows, which currently |
| 39 | + # fails due to platform-specific issues. |
| 40 | + - name: Run go-apidiff |
| 41 | + id: apidiff |
| 42 | + continue-on-error: true |
| 43 | + uses: joelanford/go-apidiff@60c4206be8f84348ebda2a3e0c3ac9cb54b8f685 # v0.8.3 |
| 44 | + with: |
| 45 | + version: v0.8.3 |
| 46 | + |
| 47 | + - name: Add apidiff label |
| 48 | + if: ${{ steps.apidiff.outputs.semver-type == 'major' }} |
| 49 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 50 | + with: |
| 51 | + script: | |
| 52 | + const label = 'apidiff'; |
| 53 | + await github.rest.issues.addLabels({ |
| 54 | + owner: context.repo.owner, |
| 55 | + repo: context.repo.repo, |
| 56 | + issue_number: context.issue.number, |
| 57 | + labels: [label], |
| 58 | + }); |
| 59 | +
|
| 60 | + - name: Comment API diff |
| 61 | + if: ${{ steps.apidiff.outputs.semver-type == 'major' }} |
| 62 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 63 | + env: |
| 64 | + APIDIFF_OUTPUT: ${{ steps.apidiff.outputs.output }} |
| 65 | + SEMVER_TYPE: ${{ steps.apidiff.outputs.semver-type }} |
| 66 | + with: |
| 67 | + script: | |
| 68 | + const header = '## 📊 API Changes Detected'; |
| 69 | + const diff = process.env.APIDIFF_OUTPUT.trim(); |
| 70 | + const semver = process.env.SEMVER_TYPE || 'unknown'; |
| 71 | + const body = [ |
| 72 | + header, |
| 73 | + '', |
| 74 | + `Semver impact: \`${semver}\``, |
| 75 | + '', |
| 76 | + '```', |
| 77 | + diff, |
| 78 | + '```', |
| 79 | + ].join('\n'); |
| 80 | +
|
| 81 | + const { data: comments } = await github.rest.issues.listComments({ |
| 82 | + owner: context.repo.owner, |
| 83 | + repo: context.repo.repo, |
| 84 | + issue_number: context.issue.number, |
| 85 | + }); |
| 86 | +
|
| 87 | + const existing = comments.find(comment => |
| 88 | + comment.user.type === 'Bot' && |
| 89 | + comment.body.startsWith(header), |
| 90 | + ); |
| 91 | +
|
| 92 | + if (existing) { |
| 93 | + await github.rest.issues.updateComment({ |
| 94 | + owner: context.repo.owner, |
| 95 | + repo: context.repo.repo, |
| 96 | + comment_id: existing.id, |
| 97 | + body, |
| 98 | + }); |
| 99 | + } else { |
| 100 | + await github.rest.issues.createComment({ |
| 101 | + owner: context.repo.owner, |
| 102 | + repo: context.repo.repo, |
| 103 | + issue_number: context.issue.number, |
| 104 | + body, |
| 105 | + }); |
| 106 | + } |
| 107 | +
|
| 108 | + # Attempt to request the premium reviewers; needs org-scoped token because GITHUB_TOKEN lacks read:org. |
| 109 | + - name: Request trivy-premium review |
| 110 | + if: ${{ steps.apidiff.outputs.semver-type == 'major' }} |
| 111 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 112 | + with: |
| 113 | + github-token: ${{ secrets.ORG_REPO_TOKEN }} |
| 114 | + script: | |
| 115 | + try { |
| 116 | + await github.rest.pulls.requestReviewers({ |
| 117 | + owner: context.repo.owner, |
| 118 | + repo: context.repo.repo, |
| 119 | + pull_number: context.issue.number, |
| 120 | + team_reviewers: ['trivy-premium'], |
| 121 | + }); |
| 122 | + console.log('Requested review from aquasecurity/trivy-premium team'); |
| 123 | + } catch (error) { |
| 124 | + core.error(`Failed to request trivy-premium reviewers: ${error.message}`); |
| 125 | + throw error; |
| 126 | + } |
0 commit comments