|
| 1 | +name: Check contribution |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + types: [opened, ready_for_review] |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + pull-requests: write |
| 12 | + issues: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + check: |
| 16 | + if: github.repository == 'ServiceNowDevProgram/ActionPack' |
| 17 | + runs-on: ubuntu-latest |
| 18 | + name: Check PR |
| 19 | + steps: |
| 20 | + - name: Shallow checkout |
| 21 | + uses: actions/checkout@v5 |
| 22 | + with: |
| 23 | + ref: ${{ github.event.pull_request.base.sha }} |
| 24 | + fetch-depth: 0 |
| 25 | + |
| 26 | + - name: Detect allowed file changes |
| 27 | + id: changes |
| 28 | + uses: tj-actions/changed-files@823fcebdb31bb35fdf2229d9f769b400309430d0 # v46 |
| 29 | + with: |
| 30 | + base_sha: ${{ github.event.pull_request.base.sha }} |
| 31 | + ref: ${{ github.event.pull_request.head.sha }} |
| 32 | + |
| 33 | + files: | |
| 34 | + b812ceb69337a210633378917cba10bc/checksum.txt |
| 35 | + b812ceb69337a210633378917cba10bc/update/sys_hub_action_type_definition_*.xml |
| 36 | +
|
| 37 | + - name: Handle inappropriate contribution |
| 38 | + if: steps.changes.outputs.only_changed != 'true' |
| 39 | + uses: actions/github-script@v8 |
| 40 | + with: |
| 41 | + script: | |
| 42 | + const {owner, repo} = context.repo; |
| 43 | + const pr = context.payload.pull_request.number; |
| 44 | + const label = 'non-compliant'; |
| 45 | +
|
| 46 | + // Ensure label exists (create if missing) |
| 47 | + try { |
| 48 | + await github.rest.issues.getLabel({owner, repo, name: label}); |
| 49 | + } catch (e) { |
| 50 | + if (e.status === 404) { |
| 51 | + await github.rest.issues.createLabel({owner, repo, name: label, color: 'B66B02', description: 'PR violates CONTRIBUTING rules'}); |
| 52 | + } else { |
| 53 | + throw e; |
| 54 | + } |
| 55 | + } |
| 56 | +
|
| 57 | + console.log(`Non-compliant: ${nonCompliant}`); |
| 58 | + console.log(`All changed: ${allChanged}`); |
| 59 | +
|
| 60 | + const body = [ |
| 61 | + '🚫 **Unexpected files changed in PR**', |
| 62 | + '', |
| 63 | + 'Thank you for your contribution. However, it seems that the file changes in this Pull Request are incorrect or invalid.', |
| 64 | + 'Please see the description of the unexpected file changes below. Contributions must be correct, valid, and align with the [CONTRIBUTING.md](CONTRIBUTING.md) for this repo.', |
| 65 | + 'This pull request modifies files *outside* the allowed paths/patterns.', |
| 66 | + '', |
| 67 | + '**Allowed patterns:**', |
| 68 | + '```', |
| 69 | + 'b812ceb69337a210633378917cba10bc/checksum.txt', |
| 70 | + 'b812ceb69337a210633378917cba10bc/update/sys_hub_action_type_definition_*.xml', |
| 71 | + '```', |
| 72 | + '', |
| 73 | + 'Closing this for now. Once you make additional changes, feel free to re-open this Pull Request or create a new one.', |
| 74 | + '', |
| 75 | + 'If you feel that this PR should be accepted in its current state, please reach out to Astrid Sapphire (SapphicFire) on GitHub, or in the SNDevs Slack Hacktoberfest channel.', |
| 76 | + ].join('\n'); |
| 77 | +
|
| 78 | + await github.rest.issues.addLabels({owner, repo, issue_number: pr, labels: [label]}); |
| 79 | + await github.rest.issues.createComment({owner, repo, issue_number: pr, body}); |
| 80 | + await github.rest.pulls.update({owner, repo, pull_number: pr, state: 'closed'}); |
| 81 | +
|
| 82 | + - name: Fail if non-compliant |
| 83 | + if: steps.changes.outputs.only_changed != 'true' |
| 84 | + run: | |
| 85 | + echo "Non-compliant file changes were made." |
| 86 | + exit 1 |
| 87 | +
|
| 88 | + - name: Handle appropriate contribution |
| 89 | + if: steps.changes.outputs.only_changed == 'true' |
| 90 | + uses: actions/github-script@v8 |
| 91 | + with: |
| 92 | + script: | |
| 93 | + const {owner, repo} = context.repo; |
| 94 | + const pr = context.payload.pull_request.number; |
| 95 | +
|
| 96 | + const body = [ |
| 97 | + '✅ **Valid PR for ActionPack**', |
| 98 | + '', |
| 99 | + 'Thank you for your contribution. This PR complies with the [CONTRIBUTING.md](CONTRIBUTING.md).', |
| 100 | + 'A maintainer will review this shortly. In the meantime, Happy Hacking!', |
| 101 | + ].join('\n'); |
| 102 | +
|
| 103 | + await github.rest.issues.createComment({owner, repo, issue_number: pr, body}); |
| 104 | +
|
| 105 | + - name: Succeed for compliant |
| 106 | + if: steps.changes.outputs.only_changed == 'true' |
| 107 | + run: | |
| 108 | + echo "Compliant file changes were made." |
| 109 | + exit 0 |
0 commit comments