|
| 1 | +name: Auto Assign Reviewers |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [ 'develop', 'release_**' ] |
| 6 | + types: [ opened, edited, reopened ] |
| 7 | + |
| 8 | +jobs: |
| 9 | + assign-reviewers: |
| 10 | + name: Assign Reviewers by Scope |
| 11 | + if: github.event_name == 'pull_request' |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Assign reviewers based on PR title scope |
| 16 | + uses: actions/github-script@v8 |
| 17 | + with: |
| 18 | + script: | |
| 19 | + const title = context.payload.pull_request.title; |
| 20 | + const prAuthor = context.payload.pull_request.user.login; |
| 21 | +
|
| 22 | + // ── Scope → Reviewer mapping ────────────────────────────── |
| 23 | + const scopeReviewers = { |
| 24 | + 'framework': ['xxo1shine', '317787106'], |
| 25 | + 'chainbase': ['halibobo1205', 'lvs0075'], |
| 26 | + 'db': ['halibobo1205', 'xxo1shine'], |
| 27 | + 'trie': ['halibobo1205', '317787106'], |
| 28 | + 'actuator': ['yanghang8612', 'lxcmyf'], |
| 29 | + 'consensus': ['lvs0075', 'xxo1shine'], |
| 30 | + 'protocol': ['lvs0075', 'waynercheung'], |
| 31 | + 'common': ['xxo1shine', 'lxcmyf'], |
| 32 | + 'crypto': ['Federico2014', '3for'], |
| 33 | + 'net': ['317787106', 'xxo1shine'], |
| 34 | + 'vm': ['yanghang8612', 'CodeNinjaEvan'], |
| 35 | + 'tvm': ['yanghang8612', 'CodeNinjaEvan'], |
| 36 | + 'jsonrpc': ['0xbigapple', 'bladehan1'], |
| 37 | + 'rpc': ['317787106', 'Sunny6889'], |
| 38 | + 'http': ['Sunny6889', 'bladehan1'], |
| 39 | + 'event': ['xxo1shine', '0xbigapple'], |
| 40 | + 'config': ['317787106', 'halibobo1205'], |
| 41 | + 'backup': ['xxo1shine', '317787106'], |
| 42 | + 'lite': ['bladehan1', 'halibobo1205'], |
| 43 | + 'toolkit': ['halibobo1205', 'Sunny6889'], |
| 44 | + 'plugins': ['halibobo1205', 'Sunny6889'], |
| 45 | + 'docker': ['3for', 'kuny0707'], |
| 46 | + 'test': ['bladehan1', 'lxcmyf'], |
| 47 | + 'metrics': ['halibobo1205', 'Sunny6889'], |
| 48 | + 'ci': ['bladehan1', 'halibobo1205'], |
| 49 | + }; |
| 50 | + const defaultReviewers = ['halibobo1205', '317787106']; |
| 51 | +
|
| 52 | + // ── Normalize helper ───────────────────────────────────── |
| 53 | + // Strip spaces, hyphens, underscores and lower-case so that |
| 54 | + // "VM", " json rpc ", "chain-base", "Json_Rpc" all normalize |
| 55 | + // to their canonical key form ("vm", "jsonrpc", "chainbase"). |
| 56 | + const normalize = s => s.toLowerCase().replace(/[\s\-_]/g, ''); |
| 57 | +
|
| 58 | + // ── Extract scope from conventional commit title ────────── |
| 59 | + // Format: type(scope): description |
| 60 | + // Also supports: type(scope1,scope2): description |
| 61 | + const scopeMatch = title.match(/^\w+\(([^)]+)\):/); |
| 62 | + const rawScope = scopeMatch ? scopeMatch[1] : null; |
| 63 | +
|
| 64 | + core.info(`PR title : ${title}`); |
| 65 | + core.info(`Raw scope: ${rawScope || '(none)'}`); |
| 66 | +
|
| 67 | + // ── Determine reviewers ─────────────────────────────────── |
| 68 | + // 1. Split by comma to support multi-scope: feat(vm,rpc): ... |
| 69 | + // 2. Normalize each scope token |
| 70 | + // 3. Match against keys: exact match first, then contains match |
| 71 | + // (longest key wins to avoid "net" matching inside "jsonrpc") |
| 72 | + let matched = new Set(); |
| 73 | + let matchedScopes = []; |
| 74 | +
|
| 75 | + if (rawScope) { |
| 76 | + const tokens = rawScope.split(',').map(s => normalize(s.trim())); |
| 77 | + // Pre-sort keys by length descending so longer keys match first |
| 78 | + const sortedKeys = Object.keys(scopeReviewers) |
| 79 | + .sort((a, b) => b.length - a.length); |
| 80 | +
|
| 81 | + for (const token of tokens) { |
| 82 | + if (!token) continue; |
| 83 | + // Exact match |
| 84 | + if (scopeReviewers[token]) { |
| 85 | + matchedScopes.push(token); |
| 86 | + scopeReviewers[token].forEach(r => matched.add(r)); |
| 87 | + continue; |
| 88 | + } |
| 89 | + // Contains match: token contains a key, or key contains token |
| 90 | + // Prefer longest key that matches |
| 91 | + const found = sortedKeys.find(k => token.includes(k) || k.includes(token)); |
| 92 | + if (found) { |
| 93 | + matchedScopes.push(`${token}→${found}`); |
| 94 | + scopeReviewers[found].forEach(r => matched.add(r)); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +
|
| 99 | + let reviewers = matched.size > 0 |
| 100 | + ? [...matched] |
| 101 | + : defaultReviewers; |
| 102 | +
|
| 103 | + core.info(`Matched scopes: ${matchedScopes.length > 0 ? matchedScopes.join(', ') : '(none — using default)'}`); |
| 104 | + core.info(`Candidate reviewers: ${reviewers.join(', ')}`); |
| 105 | +
|
| 106 | + // Exclude the PR author from the reviewer list |
| 107 | + reviewers = reviewers.filter(r => r.toLowerCase() !== prAuthor.toLowerCase()); |
| 108 | +
|
| 109 | + if (reviewers.length === 0) { |
| 110 | + core.info('No eligible reviewers after excluding PR author. Skipping.'); |
| 111 | + return; |
| 112 | + } |
| 113 | +
|
| 114 | + core.info(`Assigning reviewers: ${reviewers.join(', ')}`); |
| 115 | +
|
| 116 | + // ── Request reviews ─────────────────────────────────────── |
| 117 | + try { |
| 118 | + await github.rest.pulls.requestReviewers({ |
| 119 | + owner: context.repo.owner, |
| 120 | + repo: context.repo.repo, |
| 121 | + pull_number: context.payload.pull_request.number, |
| 122 | + reviewers: reviewers, |
| 123 | + }); |
| 124 | + core.info('Reviewers assigned successfully.'); |
| 125 | + } catch (error) { |
| 126 | + // If a reviewer is not a collaborator the API returns 422; |
| 127 | + // log the error but do not fail the workflow. |
| 128 | + core.warning(`Failed to assign some reviewers: ${error.message}`); |
| 129 | + } |
0 commit comments