|
| 1 | +name: Trademark CLA Approval |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [labeled] |
| 6 | + |
| 7 | +permissions: write-all |
| 8 | + |
| 9 | +jobs: |
| 10 | + process-cla-approval: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + if: github.event.label.name == 'cla-signed' |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Debug - Event info |
| 16 | + run: | |
| 17 | + echo "=== CLA APPROVAL DEBUG ===" |
| 18 | + echo "Event: ${{ github.event_name }}" |
| 19 | + echo "Action: ${{ github.event.action }}" |
| 20 | + echo "Label: ${{ github.event.label.name }}" |
| 21 | + echo "Added by: ${{ github.actor }}" |
| 22 | + echo "Issue number: ${{ github.event.issue.number }}" |
| 23 | + echo "Is PR: ${{ github.event.issue.pull_request != null }}" |
| 24 | + echo "=================================" |
| 25 | +
|
| 26 | + - name: Generate Token |
| 27 | + id: generate-token |
| 28 | + continue-on-error: true |
| 29 | + uses: actions/create-github-app-token@v1 |
| 30 | + with: |
| 31 | + app-id: "${{ secrets.WORKFLOW_AUTH_PUBLIC_APP_ID }}" |
| 32 | + private-key: "${{ secrets.WORKFLOW_AUTH_PUBLIC_PRIVATE_KEY }}" |
| 33 | + |
| 34 | + - name: Process CLA approval |
| 35 | + if: github.event.issue.pull_request != null |
| 36 | + uses: actions/github-script@v7 |
| 37 | + with: |
| 38 | + github-token: ${{ steps.generate-token.outputs.token || secrets.GITHUB_TOKEN }} |
| 39 | + script: | |
| 40 | + console.log('=== PROCESSING CLA APPROVAL ==='); |
| 41 | + console.log('Actor:', context.actor); |
| 42 | + console.log('PR/Issue number:', context.issue.number); |
| 43 | + |
| 44 | + // Check if this is actually a PR |
| 45 | + if (!context.payload.issue.pull_request) { |
| 46 | + console.log('This is not a PR, skipping...'); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + const prNumber = context.payload.issue.number; |
| 51 | + |
| 52 | + // Get PR details |
| 53 | + const { data: pr } = await github.rest.pulls.get({ |
| 54 | + owner: context.repo.owner, |
| 55 | + repo: context.repo.repo, |
| 56 | + pull_number: prNumber |
| 57 | + }); |
| 58 | + |
| 59 | + console.log('PR author:', pr.user.login); |
| 60 | + |
| 61 | + // Check if the person adding the label has the right permissions |
| 62 | + try { |
| 63 | + const { data: collaboration } = await github.rest.repos.getCollaboratorPermissionLevel({ |
| 64 | + owner: context.repo.owner, |
| 65 | + repo: context.repo.repo, |
| 66 | + username: context.actor |
| 67 | + }); |
| 68 | + |
| 69 | + console.log('Actor permission level:', collaboration.permission); |
| 70 | + |
| 71 | + // Only admin, maintain, or write permissions can approve CLA |
| 72 | + const isAuthorized = ['admin', 'maintain', 'write'].includes(collaboration.permission); |
| 73 | + console.log('Is authorized to approve CLA:', isAuthorized); |
| 74 | + |
| 75 | + if (!isAuthorized) { |
| 76 | + console.log('User does not have permission to approve CLA'); |
| 77 | + |
| 78 | + // Remove the label that was added by unauthorized user |
| 79 | + await github.rest.issues.removeLabel({ |
| 80 | + owner: context.repo.owner, |
| 81 | + repo: context.repo.repo, |
| 82 | + issue_number: prNumber, |
| 83 | + name: 'cla-signed' |
| 84 | + }); |
| 85 | + |
| 86 | + // Add a comment explaining why the label was removed |
| 87 | + await github.rest.issues.createComment({ |
| 88 | + owner: context.repo.owner, |
| 89 | + repo: context.repo.repo, |
| 90 | + issue_number: prNumber, |
| 91 | + body: `@${context.actor} Only repository maintainers can approve CLAs by adding the \`cla-signed\` label. The label has been removed.` |
| 92 | + }); |
| 93 | + |
| 94 | + console.log('Unauthorized approval attempt blocked'); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // Authorized - proceed with approval |
| 99 | + console.log('Processing authorized CLA approval...'); |
| 100 | + |
| 101 | + // Remove the blocking label |
| 102 | + try { |
| 103 | + await github.rest.issues.removeLabel({ |
| 104 | + owner: context.repo.owner, |
| 105 | + repo: context.repo.repo, |
| 106 | + issue_number: prNumber, |
| 107 | + name: 'cla-required' |
| 108 | + }); |
| 109 | + console.log('Removed cla-required label'); |
| 110 | + } catch (e) { |
| 111 | + console.log('cla-required label not found or already removed:', e.message); |
| 112 | + } |
| 113 | + |
| 114 | + // Check if confirmation comment already exists |
| 115 | + const comments = await github.rest.issues.listComments({ |
| 116 | + issue_number: prNumber, |
| 117 | + owner: context.repo.owner, |
| 118 | + repo: context.repo.repo, |
| 119 | + }); |
| 120 | + |
| 121 | + const confirmationExists = comments.data.some(comment => |
| 122 | + (comment.user.login === 'github-actions[bot]' || comment.user.type === 'Bot') && |
| 123 | + comment.body.includes('CLA Agreement Confirmed') |
| 124 | + ); |
| 125 | + |
| 126 | + if (!confirmationExists) { |
| 127 | + await github.rest.issues.createComment({ |
| 128 | + owner: context.repo.owner, |
| 129 | + repo: context.repo.repo, |
| 130 | + issue_number: prNumber, |
| 131 | + body: `## CLA Agreement Confirmed |
| 132 | + |
| 133 | + The trademark license agreement has been approved for @${pr.user.login}. |
| 134 | + |
| 135 | + **Status:** Approved |
| 136 | + **Date:** ${new Date().toISOString()} |
| 137 | + **Approved by:** @${context.actor} |
| 138 | + |
| 139 | + This PR is now unblocked and can proceed with normal review!` |
| 140 | + }); |
| 141 | + console.log('Posted confirmation comment'); |
| 142 | + } |
| 143 | + |
| 144 | + console.log('CLA approval completed successfully'); |
| 145 | + |
| 146 | + } catch (error) { |
| 147 | + console.error('Error processing CLA approval:', error); |
| 148 | + throw error; |
| 149 | + } |
| 150 | + |
| 151 | + console.log('=== END CLA APPROVAL PROCESSING ==='); |
0 commit comments