From 8fe1b9c659c96f012e251db604eaa5365b7cd3a9 Mon Sep 17 00:00:00 2001 From: Yamila Moreno Date: Mon, 23 Nov 2026 12:56:49 +0100 Subject: [PATCH] :wrench: Improve PRs validations Signed-off-by: Yamila Moreno --- .github/workflows/prs-pre-validations.yml | 119 ++++++++++++---------- 1 file changed, 64 insertions(+), 55 deletions(-) diff --git a/.github/workflows/prs-pre-validations.yml b/.github/workflows/prs-pre-validations.yml index 3c9f736a1b..ae1a590958 100644 --- a/.github/workflows/prs-pre-validations.yml +++ b/.github/workflows/prs-pre-validations.yml @@ -1,8 +1,8 @@ name: PRs Pre-Validations -# pull_request_target is required so the job gets a write token for PRs from -# forks. It is safe here: the workflow never checks out or runs PR code, it -# only reads ids from the event payload and calls the GitHub API. +# pull_request_target is required so the GitHub App secrets are available +# for PRs from forks. It is safe here: the workflow never checks out or runs +# PR code, it only reads ids from the event payload and calls the GitHub API. on: pull_request_target: types: @@ -17,45 +17,25 @@ jobs: name: Validate public PR runs-on: ubuntu-latest timeout-minutes: 5 - permissions: - contents: read - issues: write - pull-requests: write steps: + - name: Generate GitHub App token + id: validation-app-token + uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 + with: + client-id: ${{ secrets.TRIAGE_APP_CLIENT_ID }} + private-key: ${{ secrets.TRIAGE_APP_PRIVATE_KEY }} + # The App needs Organization > Members: read to see private members. + # Its repository permissions cover the comment and the close. + permission-issues: write + permission-members: read + permission-pull-requests: write + - name: Close public PR that does not meet the validation rules uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 with: + github-token: ${{ steps.validation-app-token.outputs.token }} script: | - const ASSOCIATIONS_WITH_ACCESS = new Set(['COLLABORATOR', 'MEMBER', 'OWNER']); - const CONTRIBUTION_GUIDE_URL = 'https://github.com/penpot/penpot/blob/develop/CONTRIBUTING.md'; - const REASONS = { - NO_LINKED_ISSUE: [ - 'Thank you for taking the time to contribute to Penpot!', - '', - 'This pull request was closed automatically because it is not linked to any issue yet. Please open an issue describing the change, or find an existing one, link it to this pull request and reopen it. We will be happy to review it then.' - ].join('\n'), - NEEDS_TRIAGE: [ - 'Thank you for your interest in improving Penpot.', - '', - 'This pull request was closed automatically because it is linked to an issue labelled `needs triage`. Our product team is still reviewing that issue, so it is not open for contributions yet. Once a maintainer confirms that the issue is ready, please reopen this pull request.' - ].join('\n'), - INTERNAL_ISSUE: [ - 'Thank you for wanting to help with Penpot.', - '', - 'This pull request was closed automatically because it is linked to an issue labelled `internal`. Internal issues are not open to outside contributions. Please choose an issue that is explicitly open for contributions, or ask in the issue first, and then reopen this pull request.' - ].join('\n') - }; - const GUIDE_FOOTER = [ - '', - 'If you are unsure where to start, our [contribution guide](' + - CONTRIBUTION_GUIDE_URL + - ') explains how we triage issues and when they become open for contributions.' - ].join('\n'); - const pr = context.payload.pull_request; - if (!pr) { - throw new Error('This workflow only runs for pull_request_target events.'); - } if (pr.draft) { console.log(`PR #${pr.number} is a draft; skipping.`); return; @@ -64,29 +44,33 @@ jobs: console.log(`PR #${pr.number} was opened by a bot; skipping.`); return; } - const { owner, repo } = context.repo; - - // Use the API instead of the webhook, because - // the API reports their real relationship to the repository. - const { data: currentPR } = await github.rest.pulls.get({ - owner, - repo, - pull_number: pr.number - }); - const association = currentPR.author_association; - console.log(`PR #${pr.number} author association from API: ${association}.`); - - // Exemption labels such as `no issue required` do not apply to - // public contributors: the author classification wins. - if (ASSOCIATIONS_WITH_ACCESS.has(association)) { - console.log(`PR #${pr.number} was opened by ${association}; skipping.`); - return; - } - if (association === 'MANNEQUIN') { + if (pr.author_association === 'MANNEQUIN') { console.log(`PR #${pr.number} was opened by a migrated placeholder user; skipping.`); return; } + // author_association hides private organization membership from + // the workflow token. Ask the organization with the App token. + // A failed check is treated as external, so the public + // contributor rules still apply. + let isMember = false; + try { + const response = await github.rest.orgs.checkMembershipForUser({ + owner: context.repo.owner, + username: pr.user.login + }); + isMember = response.status === 204; + } catch (error) { + console.log(`Membership check failed for @${pr.user.login}: ${error.message}`); + } + + if (isMember) { + console.log(`PR #${pr.number} was opened by ${pr.user.login}, an organization member; skipping.`); + return; + } + console.log(`PR #${pr.number} was opened by ${pr.user.login}, not an organization member.`); + + const { owner, repo } = context.repo; const result = await github.graphql( `query($owner: String!, $repo: String!, $number: Int!) { repository(owner: $owner, name: $repo) { @@ -133,6 +117,31 @@ jobs: return; } + const CONTRIBUTION_GUIDE_URL = 'https://github.com/penpot/penpot/blob/develop/CONTRIBUTING.md'; + const REASONS = { + NO_LINKED_ISSUE: [ + 'Thank you for taking the time to contribute to Penpot!', + '', + 'This pull request was closed automatically because it is not linked to any issue yet. Please open an issue describing the change, or find an existing one, link it to this pull request and reopen it. We will be happy to review it then.' + ].join('\n'), + NEEDS_TRIAGE: [ + 'Thank you for your interest in improving Penpot.', + '', + 'This pull request was closed automatically because it is linked to an issue labelled `needs triage`. Our product team is still reviewing that issue, so it is not open for contributions yet. Once a maintainer confirms that the issue is ready, please reopen this pull request.' + ].join('\n'), + INTERNAL_ISSUE: [ + 'Thank you for wanting to help with Penpot.', + '', + 'This pull request was closed automatically because it is linked to an issue labelled `internal`. Internal issues are not open to outside contributions. Please choose an issue that is explicitly open for contributions, or ask in the issue first, and then reopen this pull request.' + ].join('\n') + }; + const GUIDE_FOOTER = [ + '', + 'If you are unsure where to start, our [contribution guide](' + + CONTRIBUTION_GUIDE_URL + + ') explains how we triage issues and when they become open for contributions.' + ].join('\n'); + const body = reasons.map((reason) => REASONS[reason]).join('\n\n') + GUIDE_FOOTER; await github.rest.issues.createComment({ owner,