|
| 1 | +# This workflow checks for common PR submission issues and provides helpful feedback |
| 2 | +# - PRs from fork's main branch (helpful workflow suggestion) |
| 3 | +# - Organization account submissions (critical - blocks CI due to GitHub limitation) |
| 4 | +# |
| 5 | +# Security Note: This workflow uses pull_request_target which runs in the context |
| 6 | +# of the base repository, not the fork. User-controlled data (PR titles, descriptions, |
| 7 | +# branch names, usernames) must be sanitized before use to prevent script injection. |
| 8 | + |
| 9 | +name: PR Submission Checks |
| 10 | + |
| 11 | +on: |
| 12 | + pull_request: |
| 13 | + types: [opened, reopened, synchronize] |
| 14 | + branches: |
| 15 | + - main |
| 16 | + |
| 17 | +permissions: {} |
| 18 | + |
| 19 | +concurrency: |
| 20 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} |
| 21 | + cancel-in-progress: true |
| 22 | + |
| 23 | +jobs: |
| 24 | + check-pr-submission: |
| 25 | + name: Check PR submission best practices |
| 26 | + runs-on: ubuntu-latest |
| 27 | + permissions: |
| 28 | + contents: read |
| 29 | + pull-requests: write # to post a message to PR |
| 30 | + |
| 31 | + steps: |
| 32 | + - name: Check if PR is from main branch of fork |
| 33 | + id: check-main-branch |
| 34 | + uses: actions/github-script@v8 |
| 35 | + with: |
| 36 | + script: | |
| 37 | + const pr = context.payload.pull_request; |
| 38 | +
|
| 39 | + // Get PR details from context (these are all user-controlled) |
| 40 | + const headRef = pr.head.ref; |
| 41 | + const headRepo = pr.head.repo.full_name; |
| 42 | + const baseRepo = pr.base.repo.full_name; |
| 43 | + const isFork = headRepo !== baseRepo; |
| 44 | + const isFromMain = headRef === 'main'; |
| 45 | + const isFromOrg = pr.head.repo.owner.type === 'Organization'; |
| 46 | + const headOwnerLogin = pr.head.repo.owner.login; |
| 47 | + const baseOwnerLogin = pr.base.repo.owner.login; |
| 48 | +
|
| 49 | + // Log details - no user input in template literals for security |
| 50 | + console.log('PR number:', pr.number); |
| 51 | + console.log('Head branch:', headRef); |
| 52 | + console.log('Head repo:', headRepo); |
| 53 | + console.log('Base repo:', baseRepo); |
| 54 | + console.log('Is fork:', isFork); |
| 55 | + console.log('From main branch:', isFromMain); |
| 56 | + console.log('Owner type:', pr.head.repo.owner.type); |
| 57 | + console.log('Head owner login:', headOwnerLogin); |
| 58 | + console.log('Base owner login:', baseOwnerLogin); |
| 59 | +
|
| 60 | + core.setOutput('is_fork', isFork); |
| 61 | + core.setOutput('is_from_main', isFromMain); |
| 62 | + core.setOutput('is_from_org', isFromOrg); |
| 63 | + core.setOutput('head_ref', headRef); |
| 64 | + core.setOutput('head_repo', headRepo); |
| 65 | + core.setOutput('head_owner_login', headOwnerLogin); |
| 66 | + core.setOutput('base_owner_login', baseOwnerLogin); |
| 67 | +
|
| 68 | + return { |
| 69 | + isFork, |
| 70 | + isFromMain, |
| 71 | + isFromOrg, |
| 72 | + needsComment: isFork && isFromMain |
| 73 | + }; |
| 74 | +
|
| 75 | + - name: Post comment about main branch submission |
| 76 | + if: steps.check-main-branch.outputs.is_fork == 'true' && steps.check-main-branch.outputs.is_from_main == 'true' |
| 77 | + uses: actions/github-script@v8 |
| 78 | + with: |
| 79 | + script: | |
| 80 | + const pr = context.payload.pull_request; |
| 81 | +
|
| 82 | + // Check if we already posted this comment |
| 83 | + const comments = await github.rest.issues.listComments({ |
| 84 | + owner: context.repo.owner, |
| 85 | + repo: context.repo.repo, |
| 86 | + issue_number: pr.number |
| 87 | + }); |
| 88 | +
|
| 89 | + const botComment = comments.data.find(comment => |
| 90 | + comment.user.type === 'Bot' && |
| 91 | + comment.body.includes('submitted from the main branch') |
| 92 | + ); |
| 93 | +
|
| 94 | + if (botComment) { |
| 95 | + console.log('Comment about main branch already exists, skipping'); |
| 96 | + return; |
| 97 | + } |
| 98 | +
|
| 99 | + // Sanitize username - GitHub usernames can only contain alphanumeric characters and hyphens |
| 100 | + const username = pr.user.login.replace(/[^a-zA-Z0-9-]/g, ''); |
| 101 | +
|
| 102 | + // Post helpful comment |
| 103 | + const commentBody = `Hi @${username}! 👋 |
| 104 | +
|
| 105 | + Thank you for your contribution to voc4cat! |
| 106 | +
|
| 107 | + > [!WARNING] |
| 108 | + > We noticed that this pull request was submitted from the \`main\` branch of your fork. |
| 109 | + > While this works, it can cause issues: |
| 110 | +
|
| 111 | + - It makes it harder to keep your fork updated with upstream changes |
| 112 | + - You won't be able to work on multiple PRs at once |
| 113 | + - Future contributions may be complicated by merge conflicts |
| 114 | +
|
| 115 | + This PR can still be merged, but please use feature branches going forward! |
| 116 | +
|
| 117 | + For more information, see our [How to contribute](https://nfdi4cat.github.io/voc4cat/docs_usage/how-to-contribute.html) guide. |
| 118 | +
|
| 119 | + *If you have any questions, please don't hesitate to ask!* 🚀`; |
| 120 | +
|
| 121 | + await github.rest.issues.createComment({ |
| 122 | + owner: context.repo.owner, |
| 123 | + repo: context.repo.repo, |
| 124 | + issue_number: pr.number, |
| 125 | + body: commentBody |
| 126 | + }); |
| 127 | +
|
| 128 | + - name: Post info about organization account |
| 129 | + if: steps.check-main-branch.outputs.is_from_org == 'true' && steps.check-main-branch.outputs.is_fork == 'true' && steps.check-main-branch.outputs.head_owner_login != steps.check-main-branch.outputs.base_owner_login |
| 130 | + uses: actions/github-script@v8 |
| 131 | + with: |
| 132 | + script: | |
| 133 | + const pr = context.payload.pull_request; |
| 134 | +
|
| 135 | + // Check if we already posted this comment |
| 136 | + const comments = await github.rest.issues.listComments({ |
| 137 | + owner: context.repo.owner, |
| 138 | + repo: context.repo.repo, |
| 139 | + issue_number: pr.number |
| 140 | + }); |
| 141 | +
|
| 142 | + const botComment = comments.data.find(comment => |
| 143 | + comment.user.type === 'Bot' && |
| 144 | + comment.body.includes('organization account') |
| 145 | + ); |
| 146 | +
|
| 147 | + if (botComment) { |
| 148 | + console.log('Comment about organization account already exists, skipping'); |
| 149 | + return; |
| 150 | + } |
| 151 | +
|
| 152 | + // Sanitize username - GitHub usernames can only contain alphanumeric characters and hyphens |
| 153 | + const username = pr.user.login.replace(/[^a-zA-Z0-9-]/g, ''); |
| 154 | +
|
| 155 | + const commentBody = `Hi @${username}! 👋 |
| 156 | +
|
| 157 | + > [!CAUTION] |
| 158 | + > We noticed that this pull request comes from a fork under an organization account, |
| 159 | + > which will prevent our CI workflow from working correctly. |
| 160 | +
|
| 161 | + GitHub does not allow the "Allow edits from maintainers" option for forks in organizations (see [discussion](https://github.com/orgs/community/discussions/5634)). |
| 162 | + Our CI needs this permission to commit turtle files and clean up Excel files. |
| 163 | +
|
| 164 | + **This PR cannot be merged as-is.** Please: |
| 165 | + 1. Fork voc4cat to your personal GitHub account |
| 166 | + 2. Create a feature branch with your changes |
| 167 | + 3. Submit a new PR from your personal fork |
| 168 | + 4. Close this PR |
| 169 | +
|
| 170 | + Sorry for the inconvenience - this is a GitHub limitation, not our choice!`; |
| 171 | +
|
| 172 | + await github.rest.issues.createComment({ |
| 173 | + owner: context.repo.owner, |
| 174 | + repo: context.repo.repo, |
| 175 | + issue_number: pr.number, |
| 176 | + body: commentBody |
| 177 | + }); |
| 178 | +
|
0 commit comments