|
| 1 | +name: CLA Label Sync |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created, edited] |
| 6 | + pull_request_target: |
| 7 | + types: [opened, synchronize, reopened] |
| 8 | + |
| 9 | +permissions: |
| 10 | + pull-requests: write |
| 11 | + issues: write |
| 12 | + contents: read |
| 13 | + |
| 14 | +jobs: |
| 15 | + sync-label: |
| 16 | + # Only run for PRs (issue_comment fires for issues too) |
| 17 | + if: >- |
| 18 | + github.event_name == 'pull_request_target' || |
| 19 | + (github.event.issue.pull_request != null) |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - name: Sync "awaiting cla" label |
| 23 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 #v9.0.0 |
| 24 | + env: |
| 25 | + AWAITING_LABEL: 'awaiting cla' |
| 26 | + # Bot login that posts the CLA comment. Common values: |
| 27 | + # 'github-actions[bot]', 'CLAassistant', 'cla-assistant[bot]' |
| 28 | + CLA_BOT_LOGINS: 'CLAassistant' |
| 29 | + # Regex (case-insensitive) that matches an UNSIGNED CLA comment |
| 30 | + NOT_SIGNED_REGEX: 'cla-assistant.io/pull/badge/not_signed' |
| 31 | + # Regex (case-insensitive) that matches a SIGNED CLA comment |
| 32 | + SIGNED_REGEX: 'cla-assistant.io/pull/badge/signed' |
| 33 | + with: |
| 34 | + script: | |
| 35 | + const awaitingLabel = process.env.AWAITING_LABEL; |
| 36 | + const botLogins = process.env.CLA_BOT_LOGINS.split(',').map(s => s.trim().toLowerCase()); |
| 37 | + const notSigned = new RegExp(process.env.NOT_SIGNED_REGEX, 'i'); |
| 38 | + const signed = new RegExp(process.env.SIGNED_REGEX, 'i'); |
| 39 | +
|
| 40 | + // Resolve PR number for either trigger |
| 41 | + const prNumber = context.eventName === 'pull_request_target' |
| 42 | + ? context.payload.pull_request.number |
| 43 | + : context.payload.issue.number; |
| 44 | +
|
| 45 | + const { owner, repo } = context.repo; |
| 46 | +
|
| 47 | + // Pull the full comment history to find the latest CLA bot comment |
| 48 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 49 | + owner, repo, issue_number: prNumber, per_page: 100, |
| 50 | + }); |
| 51 | +
|
| 52 | + const claComments = comments.filter(c => |
| 53 | + botLogins.includes((c.user?.login || '').toLowerCase()) && |
| 54 | + (notSigned.test(c.body) || signed.test(c.body)) |
| 55 | + ); |
| 56 | +
|
| 57 | + if (claComments.length === 0) { |
| 58 | + core.info('No CLA Assistant comment found yet; nothing to do.'); |
| 59 | + return; |
| 60 | + } |
| 61 | +
|
| 62 | + const latest = claComments[claComments.length - 1]; |
| 63 | + const isSigned = signed.test(latest.body) && !notSigned.test(latest.body); |
| 64 | +
|
| 65 | + core.info(`Latest CLA comment (id ${latest.id}) => signed=${isSigned}`); |
| 66 | +
|
| 67 | + // Current labels |
| 68 | + const { data: issue } = await github.rest.issues.get({ |
| 69 | + owner, repo, issue_number: prNumber, |
| 70 | + }); |
| 71 | + const hasLabel = issue.labels.some(l => |
| 72 | + (typeof l === 'string' ? l : l.name) === awaitingLabel |
| 73 | + ); |
| 74 | +
|
| 75 | + if (isSigned && hasLabel) { |
| 76 | + await github.rest.issues.removeLabel({ |
| 77 | + owner, repo, issue_number: prNumber, name: awaitingLabel, |
| 78 | + }).catch(e => core.warning(`removeLabel failed: ${e.message}`)); |
| 79 | + core.info(`Removed "${awaitingLabel}".`); |
| 80 | + } else if (!isSigned && !hasLabel) { |
| 81 | + await github.rest.issues.addLabels({ |
| 82 | + owner, repo, issue_number: prNumber, labels: [awaitingLabel], |
| 83 | + }); |
| 84 | + core.info(`Added "${awaitingLabel}".`); |
| 85 | + } else { |
| 86 | + core.info('Label already in the correct state.'); |
| 87 | + } |
0 commit comments