|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# On PR open/update, runs `git blame` on every changed file and posts (or |
| 18 | +# edits) a single reviewer-suggestion comment split into two buckets: |
| 19 | +# • Committers — collaborators who can be formally review-requested. |
| 20 | +# The author uses `/request-review @login` to act on these. |
| 21 | +# • Non-committer contributors — have context but cannot be review- |
| 22 | +# requested; the author can @-mention them to notify. |
| 23 | +# The CI never sends a review request on its own. |
| 24 | +name: Suggest reviewers |
| 25 | +on: |
| 26 | + pull_request_target: |
| 27 | + types: [opened, synchronize, reopened] |
| 28 | + |
| 29 | +permissions: |
| 30 | + contents: read |
| 31 | + issues: write |
| 32 | + pull-requests: write |
| 33 | + |
| 34 | +jobs: |
| 35 | + suggest-reviewers: |
| 36 | + runs-on: ubuntu-latest |
| 37 | + steps: |
| 38 | + - uses: actions/checkout@v5 |
| 39 | + with: |
| 40 | + fetch-depth: 0 |
| 41 | + - uses: actions/github-script@v8 |
| 42 | + with: |
| 43 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 44 | + script: | |
| 45 | + const { execFileSync } = require('node:child_process'); |
| 46 | + const pull_number = context.payload.pull_request.number; |
| 47 | + const author = context.payload.pull_request.user.login; |
| 48 | + const { owner, repo } = context.repo; |
| 49 | +
|
| 50 | + const { data: pull } = await github.rest.pulls.get({ owner, repo, pull_number }); |
| 51 | +
|
| 52 | + try { |
| 53 | + execFileSync('git', ['fetch', 'origin', pull.base.ref], { encoding: 'utf8' }); |
| 54 | + } catch (e) { |
| 55 | + core.warning(`git fetch for base ref ${pull.base.ref} failed: ${e.message}`); |
| 56 | + } |
| 57 | +
|
| 58 | + const files = await github.paginate(github.rest.pulls.listFiles, { |
| 59 | + owner, repo, pull_number, per_page: 100, |
| 60 | + }); |
| 61 | +
|
| 62 | + // Parse `git blame -p` output to find the most-recent commit per file. |
| 63 | + function latestBlameCommit(blameOutput) { |
| 64 | + let latest = null; |
| 65 | + let current = null; |
| 66 | +
|
| 67 | + function finalizeCurrent() { |
| 68 | + if (!current || current.authorTime == null) return; |
| 69 | + if (!latest || current.authorTime > latest.authorTime) latest = current; |
| 70 | + } |
| 71 | +
|
| 72 | + for (const line of blameOutput.split(/\r?\n/)) { |
| 73 | + const header = line.match(/^([0-9a-f^]+)\s+\d+\s+\d+\s+\d+$/); |
| 74 | + if (header) { |
| 75 | + finalizeCurrent(); |
| 76 | + current = { sha: header[1].replace(/^\^/, ''), authorTime: null }; |
| 77 | + continue; |
| 78 | + } |
| 79 | + const authorTime = line.match(/^author-time\s+(\d+)$/); |
| 80 | + if (authorTime && current) current.authorTime = Number(authorTime[1]); |
| 81 | + } |
| 82 | +
|
| 83 | + finalizeCurrent(); |
| 84 | + return latest; |
| 85 | + } |
| 86 | +
|
| 87 | + // Count changed files touched per login; track collaborator status. |
| 88 | + const committerCounts = new Map(); // collaborators |
| 89 | + const nonCommitterCounts = new Map(); // non-collaborators with a GitHub login |
| 90 | +
|
| 91 | + for (const { filename, status, previous_filename } of files) { |
| 92 | + if (status === 'removed' || status === 'added') continue; |
| 93 | + const blamePath = status === 'renamed' ? previous_filename : filename; |
| 94 | +
|
| 95 | + let blameOutput; |
| 96 | + try { |
| 97 | + blameOutput = execFileSync( |
| 98 | + 'git', ['blame', '-p', pull.base.sha, '--', blamePath], |
| 99 | + { encoding: 'utf8' }, |
| 100 | + ); |
| 101 | + } catch (e) { |
| 102 | + core.warning(`git blame on ${filename} failed: ${e.message}`); |
| 103 | + continue; |
| 104 | + } |
| 105 | +
|
| 106 | + const latest = latestBlameCommit(blameOutput); |
| 107 | + if (!latest) continue; |
| 108 | +
|
| 109 | + let commit; |
| 110 | + try { |
| 111 | + ({ data: commit } = await github.rest.repos.getCommit({ owner, repo, ref: latest.sha })); |
| 112 | + } catch (e) { |
| 113 | + core.warning(`Commit lookup for ${latest.sha} failed: ${e.message}`); |
| 114 | + continue; |
| 115 | + } |
| 116 | +
|
| 117 | + const login = commit.author?.login ?? commit.committer?.login; |
| 118 | + if (!login) continue; |
| 119 | + if (login.toLowerCase() === author.toLowerCase()) continue; |
| 120 | +
|
| 121 | + const loginSource = commit.author?.login ? commit.author : commit.committer; |
| 122 | + if (loginSource?.type === 'Bot') continue; |
| 123 | +
|
| 124 | + let isCollaborator = false; |
| 125 | + try { |
| 126 | + await github.rest.repos.checkCollaborator({ owner, repo, username: login }); |
| 127 | + isCollaborator = true; |
| 128 | + } catch (_) { /* not a collaborator */ } |
| 129 | +
|
| 130 | + if (isCollaborator) { |
| 131 | + committerCounts.set(login, (committerCounts.get(login) ?? 0) + 1); |
| 132 | + } else { |
| 133 | + nonCommitterCounts.set(login, (nonCommitterCounts.get(login) ?? 0) + 1); |
| 134 | + } |
| 135 | + } |
| 136 | +
|
| 137 | + const MAX_EACH = 3; |
| 138 | +
|
| 139 | + const committers = [...committerCounts.entries()] |
| 140 | + .sort((a, b) => b[1] - a[1]) |
| 141 | + .slice(0, MAX_EACH) |
| 142 | + .map(([l]) => l); |
| 143 | +
|
| 144 | + const nonCommitters = [...nonCommitterCounts.entries()] |
| 145 | + .sort((a, b) => b[1] - a[1]) |
| 146 | + .slice(0, MAX_EACH) |
| 147 | + .map(([l]) => l); |
| 148 | +
|
| 149 | + const MARKER = '<!-- texera-reviewer-suggestion -->'; |
| 150 | +
|
| 151 | + let body = `${MARKER}\n`; |
| 152 | + body += `### Automated Reviewer Suggestions\n\n`; |
| 153 | + body += `Based on the \`git blame\` history of the changed files, we recommend the following reviewers:\n\n`; |
| 154 | +
|
| 155 | + if (committers.length) { |
| 156 | + body += `- **Committers with relevant context:** ${committers.map(l => `\`@${l}\``).join(', ')}\n`; |
| 157 | + body += ` You can request their reviews formally with \`/request-review ${committers.map(l => `@${l}`).join(' ')}\`.\n\n`; |
| 158 | + } |
| 159 | +
|
| 160 | + if (nonCommitters.length) { |
| 161 | + body += `- **Contributors with relevant context:** ${nonCommitters.map(l => `\`@${l}\``).join(', ')}\n`; |
| 162 | + body += ` You can notify them by mentioning ${nonCommitters.map(l => `\`@${l}\``).join(', ')} in a comment.\n`; |
| 163 | + } |
| 164 | +
|
| 165 | + if (!committers.length && !nonCommitters.length) { |
| 166 | + body += `- No candidates found from \`git blame\` history.\n`; |
| 167 | + } |
| 168 | +
|
| 169 | + // Update existing suggestion comment rather than posting a new one. |
| 170 | + const allComments = await github.paginate(github.rest.issues.listComments, { |
| 171 | + owner, repo, issue_number: pull_number, per_page: 100, |
| 172 | + }); |
| 173 | + const existing = allComments.find(c => c.body?.includes(MARKER)); |
| 174 | +
|
| 175 | + if (existing) { |
| 176 | + await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body }); |
| 177 | + core.info(`Updated reviewer suggestion comment on #${pull_number}`); |
| 178 | + } else { |
| 179 | + await github.rest.issues.createComment({ owner, repo, issue_number: pull_number, body }); |
| 180 | + core.info(`Posted reviewer suggestion comment on #${pull_number}`); |
| 181 | + } |
0 commit comments