|
| 1 | +name: Redirect Pull Requests |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened] |
| 6 | + |
| 7 | +permissions: |
| 8 | + pull-requests: write |
| 9 | + |
| 10 | +jobs: |
| 11 | + redirect: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Check org membership and redirect |
| 15 | + uses: actions/github-script@v7 |
| 16 | + with: |
| 17 | + script: | |
| 18 | + const pr = context.payload.pull_request; |
| 19 | + const author = pr.user.login; |
| 20 | +
|
| 21 | + // Allow PRs from trusted automation bots (e.g., repo sync) |
| 22 | + const allowedBots = ['foundry-samples-repo-sync[bot]']; |
| 23 | + if (allowedBots.includes(author)) { |
| 24 | + console.log(`Skipping redirect for allowed bot: ${author}`); |
| 25 | + return; |
| 26 | + } |
| 27 | +
|
| 28 | + // Check if author is a Microsoft contributor using multiple signals. |
| 29 | + // The GITHUB_TOKEN can only see *public* members of the 'microsoft' org |
| 30 | + // (since this repo is in the 'microsoft-foundry' org), so we cascade |
| 31 | + // through several checks to catch contributors with private membership. |
| 32 | + let isInternal = false; |
| 33 | + let matchedSignal = null; |
| 34 | +
|
| 35 | + // Signal 1: Check microsoft-foundry org membership (full visibility via GITHUB_TOKEN) |
| 36 | + try { |
| 37 | + const res = await github.rest.orgs.checkMembershipForUser({ |
| 38 | + org: 'microsoft-foundry', |
| 39 | + username: author, |
| 40 | + }); |
| 41 | + if (res.status === 204) { |
| 42 | + isInternal = true; |
| 43 | + matchedSignal = 'microsoft-foundry org member'; |
| 44 | + } |
| 45 | + } catch { |
| 46 | + // 404 or 302 means not a member |
| 47 | + } |
| 48 | +
|
| 49 | + // Signal 2: Check if author is a collaborator on this repo |
| 50 | + if (!isInternal) { |
| 51 | + try { |
| 52 | + const res = await github.rest.repos.checkCollaborator({ |
| 53 | + owner: context.repo.owner, |
| 54 | + repo: context.repo.repo, |
| 55 | + username: author, |
| 56 | + }); |
| 57 | + if (res.status === 204) { |
| 58 | + isInternal = true; |
| 59 | + matchedSignal = 'repo collaborator'; |
| 60 | + } |
| 61 | + } catch { |
| 62 | + // 404 means not a collaborator |
| 63 | + } |
| 64 | + } |
| 65 | +
|
| 66 | + // Signal 3: Check microsoft org membership (catches public members only) |
| 67 | + if (!isInternal) { |
| 68 | + try { |
| 69 | + const res = await github.rest.orgs.checkMembershipForUser({ |
| 70 | + org: 'microsoft', |
| 71 | + username: author, |
| 72 | + }); |
| 73 | + if (res.status === 204) { |
| 74 | + isInternal = true; |
| 75 | + matchedSignal = 'microsoft org member (public)'; |
| 76 | + } |
| 77 | + } catch { |
| 78 | + // 404 or 302 means not a member (or private membership not visible) |
| 79 | + } |
| 80 | + } |
| 81 | +
|
| 82 | + console.log(`Author: ${author}, isInternal: ${isInternal}, signal: ${matchedSignal || 'none'}`); |
| 83 | +
|
| 84 | + let body; |
| 85 | + if (isInternal) { |
| 86 | + body = [ |
| 87 | + `👋 Thanks for your contribution, @${author}!`, |
| 88 | + '', |
| 89 | + 'This repository is read-only. As a Microsoft contributor, please submit your PR to the private staging repository instead:', |
| 90 | + '', |
| 91 | + '👉 **[foundry-samples-pr](https://github.com/microsoft-foundry/foundry-samples-pr)**', |
| 92 | + '', |
| 93 | + 'See [CONTRIBUTING.md](https://github.com/microsoft-foundry/foundry-samples/blob/main/CONTRIBUTING.md) for full instructions.', |
| 94 | + ].join('\n'); |
| 95 | + } else { |
| 96 | + body = [ |
| 97 | + `👋 Thanks for your interest in contributing, @${author}!`, |
| 98 | + '', |
| 99 | + 'This repository does not accept pull requests directly. If you\'d like to report a bug, suggest an improvement, or propose a new sample, please **[open an issue](https://github.com/microsoft-foundry/foundry-samples/issues/new)** instead.', |
| 100 | + '', |
| 101 | + 'See [CONTRIBUTING.md](https://github.com/microsoft-foundry/foundry-samples/blob/main/CONTRIBUTING.md) for more details.', |
| 102 | + ].join('\n'); |
| 103 | + } |
| 104 | +
|
| 105 | + // Skip if the bot already commented (idempotent on re-runs) |
| 106 | + const comments = await github.rest.issues.listComments({ |
| 107 | + owner: context.repo.owner, |
| 108 | + repo: context.repo.repo, |
| 109 | + issue_number: pr.number, |
| 110 | + }); |
| 111 | + const alreadyCommented = comments.data.some(c => |
| 112 | + c.user.login === 'github-actions[bot]' && |
| 113 | + c.body.includes('This repository') |
| 114 | + ); |
| 115 | + if (alreadyCommented) { |
| 116 | + console.log('Bot already commented on this PR, skipping.'); |
| 117 | + return; |
| 118 | + } |
| 119 | +
|
| 120 | + await github.rest.issues.createComment({ |
| 121 | + owner: context.repo.owner, |
| 122 | + repo: context.repo.repo, |
| 123 | + issue_number: pr.number, |
| 124 | + body, |
| 125 | + }); |
| 126 | +
|
| 127 | + await github.rest.pulls.update({ |
| 128 | + owner: context.repo.owner, |
| 129 | + repo: context.repo.repo, |
| 130 | + pull_number: pr.number, |
| 131 | + state: 'closed', |
| 132 | + }); |
0 commit comments