|
| 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 | + // Classify the PR author as internal (Microsoft) vs external using a |
| 29 | + // cascade of signals. The GITHUB_TOKEN is an *installation* token, not |
| 30 | + // a user identity in the 'microsoft' or 'microsoft-foundry' orgs, so |
| 31 | + // the org-membership checks below can only confirm *public* members. |
| 32 | + // Most Microsoft employees default to private membership, so we also |
| 33 | + // fall back to a username pattern and a public-profile heuristic. |
| 34 | + // Contributors with no public Microsoft signal anywhere will still be |
| 35 | + // misclassified as external; the external-tier message below carries a |
| 36 | + // universal caveat pointing self-aware internal contributors at the |
| 37 | + // private staging repo, so that failure mode is self-correcting. |
| 38 | + async function classifyAuthor(login) { |
| 39 | + // Signal 1: microsoft-foundry org membership (public members only). |
| 40 | + try { |
| 41 | + const res = await github.rest.orgs.checkMembershipForUser({ |
| 42 | + org: 'microsoft-foundry', |
| 43 | + username: login, |
| 44 | + }); |
| 45 | + if (res.status === 204) return 'microsoft-foundry org member (public)'; |
| 46 | + } catch {} |
| 47 | +
|
| 48 | + // Signal 2: direct collaborator on this repo (team-based access is |
| 49 | + // typically not visible to GITHUB_TOKEN here). |
| 50 | + try { |
| 51 | + const res = await github.rest.repos.checkCollaborator({ |
| 52 | + owner: context.repo.owner, |
| 53 | + repo: context.repo.repo, |
| 54 | + username: login, |
| 55 | + }); |
| 56 | + if (res.status === 204) return 'repo collaborator'; |
| 57 | + } catch {} |
| 58 | +
|
| 59 | + // Signal 3: microsoft org membership (public members only). |
| 60 | + try { |
| 61 | + const res = await github.rest.orgs.checkMembershipForUser({ |
| 62 | + org: 'microsoft', |
| 63 | + username: login, |
| 64 | + }); |
| 65 | + if (res.status === 204) return 'microsoft org member (public)'; |
| 66 | + } catch {} |
| 67 | +
|
| 68 | + // Signal 4: username pattern. Matches 'ms', 'msft', or 'microsoft' |
| 69 | + // as a whole token bounded by start/end/'-'/'_'. Catches handles like |
| 70 | + // 'aprilk-ms', 'mitsha-microsoft', 'brandom-msft' without false- |
| 71 | + // positiving 'cosmos', 'awesome', etc. |
| 72 | + if (/(^|[-_])(ms|msft|microsoft)([-_]|$)/i.test(login)) { |
| 73 | + return 'username pattern'; |
| 74 | + } |
| 75 | +
|
| 76 | + // Signal 5: public profile heuristic. Strict regex on `email`, plus |
| 77 | + // a normalized whole-string match on `company` against a small allow |
| 78 | + // list. We deliberately do NOT scan `bio` ΓÇö phrases like |
| 79 | + // 'ex-Microsoft' or 'Microsoft MVP' would produce false positives. |
| 80 | + try { |
| 81 | + const { data: profile } = await github.rest.users.getByUsername({ username: login }); |
| 82 | + const email = (profile.email || '').trim(); |
| 83 | + if (/@([a-z0-9-]+\.)?microsoft\.com$/i.test(email)) { |
| 84 | + return 'profile email (@microsoft.com)'; |
| 85 | + } |
| 86 | + const normalizedCompany = (profile.company || '') |
| 87 | + .trim() |
| 88 | + .toLowerCase() |
| 89 | + .replace(/^@/, '') |
| 90 | + .replace(/[.,]+$/, ''); |
| 91 | + const acceptedCompanies = new Set([ |
| 92 | + 'microsoft', |
| 93 | + 'microsoft corporation', |
| 94 | + 'microsoft corp', |
| 95 | + 'msft', |
| 96 | + ]); |
| 97 | + if (acceptedCompanies.has(normalizedCompany)) { |
| 98 | + return 'profile company'; |
| 99 | + } |
| 100 | + } catch {} |
| 101 | +
|
| 102 | + return null; |
| 103 | + } |
| 104 | +
|
| 105 | + const matchedSignal = await classifyAuthor(author); |
| 106 | + const isInternal = matchedSignal !== null; |
| 107 | +
|
| 108 | + console.log(`Author: ${author}, isInternal: ${isInternal}, signal: ${matchedSignal || 'none'}`); |
| 109 | +
|
| 110 | + let body; |
| 111 | + if (isInternal) { |
| 112 | + body = [ |
| 113 | + `👋 Thanks for your contribution, @${author}!`, |
| 114 | + '', |
| 115 | + 'This repository is read-only. If you are contributing on behalf of Microsoft, please submit your PR to the private staging repository instead:', |
| 116 | + '', |
| 117 | + '👉 **[foundry-samples-pr](https://github.com/microsoft-foundry/foundry-samples-pr)**', |
| 118 | + '', |
| 119 | + 'See [CONTRIBUTING.md](https://github.com/microsoft-foundry/foundry-samples/blob/main/CONTRIBUTING.md) for full instructions.', |
| 120 | + ].join('\n'); |
| 121 | + } else { |
| 122 | + body = [ |
| 123 | + `👋 Thanks for your interest in contributing, @${author}!`, |
| 124 | + '', |
| 125 | + '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.', |
| 126 | + '', |
| 127 | + '_If you are a Microsoft-internal contributor, please submit your PR through **[foundry-samples-pr](https://github.com/microsoft-foundry/foundry-samples-pr)** instead._', |
| 128 | + '', |
| 129 | + 'See [CONTRIBUTING.md](https://github.com/microsoft-foundry/foundry-samples/blob/main/CONTRIBUTING.md) for more details.', |
| 130 | + ].join('\n'); |
| 131 | + } |
| 132 | +
|
| 133 | + // Skip if the bot already commented (idempotent on re-runs). We |
| 134 | + // match on the staging-repo slug "microsoft-foundry/foundry-samples-pr", |
| 135 | + // which both the internal- and external-tier messages above include |
| 136 | + // and is structurally specific to this workflow ΓÇö generic phrases |
| 137 | + // like "This repository" can collide with unrelated bot comments |
| 138 | + // and silently suppress the redirect. |
| 139 | + const comments = await github.rest.issues.listComments({ |
| 140 | + owner: context.repo.owner, |
| 141 | + repo: context.repo.repo, |
| 142 | + issue_number: pr.number, |
| 143 | + }); |
| 144 | + const alreadyCommented = comments.data.some(c => |
| 145 | + c.user.login === 'github-actions[bot]' && |
| 146 | + c.body.includes('microsoft-foundry/foundry-samples-pr') |
| 147 | + ); |
| 148 | + if (alreadyCommented) { |
| 149 | + console.log('Bot already commented on this PR, skipping.'); |
| 150 | + return; |
| 151 | + } |
| 152 | +
|
| 153 | + await github.rest.issues.createComment({ |
| 154 | + owner: context.repo.owner, |
| 155 | + repo: context.repo.repo, |
| 156 | + issue_number: pr.number, |
| 157 | + body, |
| 158 | + }); |
| 159 | +
|
| 160 | + await github.rest.pulls.update({ |
| 161 | + owner: context.repo.owner, |
| 162 | + repo: context.repo.repo, |
| 163 | + pull_number: pr.number, |
| 164 | + state: 'closed', |
| 165 | + }); |
0 commit comments