|
| 1 | +name: Pull Request CI |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: |
| 6 | + - opened |
| 7 | + - reopened |
| 8 | + - ready_for_review |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + members: read |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + pr-comment: |
| 17 | + name: Handle misdirected PRs |
| 18 | + runs-on: ubuntu-latest |
| 19 | + if: github.event.pull_request.draft == false |
| 20 | + env: |
| 21 | + # Teams to apply this workflow for: |
| 22 | + TEAM_SLUGS: '["TeamX", "TeamY"]' |
| 23 | + # The PR comment: |
| 24 | + COMMENT_BODY: | |
| 25 | + hej @${{ github.event.pull_request.user.login }} |
| 26 | + # TODO: finish comment body |
| 27 | + steps: |
| 28 | + - name: Check team membership |
| 29 | + id: team_check |
| 30 | + uses: actions/github-script@v7 |
| 31 | + with: |
| 32 | + script: | |
| 33 | + const traineeTeamSlugs = JSON.parse(process.env.TEAM_SLUGS); |
| 34 | + const authorUsername = context.payload.pull_request.user.login; |
| 35 | + const curriculumCrewTeamSlug = 'curriculum-crew'; |
| 36 | + |
| 37 | + try { |
| 38 | + await github.rest.teams.getMembershipForUserInOrg({ |
| 39 | + org: context.repo.owner, |
| 40 | + team_slug: curriculumCrewTeamSlug, |
| 41 | + authorUsername, |
| 42 | + }); |
| 43 | + core.info(`User ${authorUsername} is a member of ${curriculumCrewTeamSlug} team. Skipping workflow.`); |
| 44 | + core.setOutput("is_curriculum_crew", "true"); |
| 45 | + return; |
| 46 | + } catch (error) { |
| 47 | + if (error.status === 404) { |
| 48 | + core.setOutput("is_curriculum_crew", "false"); |
| 49 | + } else { |
| 50 | + throw error; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + for (const teamSlug of traineeTeamSlugs) { |
| 55 | + try { |
| 56 | + await github.rest.teams.getMembershipForUserInOrg({ |
| 57 | + org: context.repo.owner, |
| 58 | + team_slug: teamSlug, |
| 59 | + authorUsername, |
| 60 | + }); |
| 61 | + core.info(`✅ User ${authorUsername} is a member of team ${teamSlug}.`); |
| 62 | + core.setOutput("is_trainee", "true"); |
| 63 | + return; |
| 64 | + } catch (error) { |
| 65 | + if (error.status === 404) { |
| 66 | + continue; |
| 67 | + } else { |
| 68 | + throw error; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + core.info(`User ${authorUsername} is not a member of any configured team.`); |
| 74 | + - name: Comment on PR and close |
| 75 | + if: steps.team_check.outputs.is_trainee == 'true' && steps.team_check.outputs.is_curriculum_crew == 'false' |
| 76 | + uses: actions/github-script@v7 |
| 77 | + with: |
| 78 | + script: | |
| 79 | + await github.rest.issues.createComment({ |
| 80 | + owner: context.repo.owner, |
| 81 | + repo: context.repo.repo, |
| 82 | + issue_number: context.payload.pull_request.number, |
| 83 | + body: process.env.COMMENT_BODY, |
| 84 | + }); |
| 85 | + |
| 86 | + await github.rest.pulls.update({ |
| 87 | + owner: context.repo.owner, |
| 88 | + repo: context.repo.repo, |
| 89 | + pull_number: context.payload.pull_request.number, |
| 90 | + state: 'closed', |
| 91 | + }); |
| 92 | + |
| 93 | + core.info(`PR #${context.payload.pull_request.number} has been commented on and closed.`); |
| 94 | +
|
0 commit comments