From e23c2463d51cb098961e4e7e58fc264fa96df087 Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 18 Jan 2022 18:21:29 +0100 Subject: [PATCH] chore: move out script into one file instead because args were too long Signed-off-by: blam --- .github/workflows/goalie-cron.yaml | 88 +-------------------------- scripts/goalie-labels.js | 98 ++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 86 deletions(-) create mode 100644 scripts/goalie-labels.js diff --git a/.github/workflows/goalie-cron.yaml b/.github/workflows/goalie-cron.yaml index 277692beaf..24b037546a 100644 --- a/.github/workflows/goalie-cron.yaml +++ b/.github/workflows/goalie-cron.yaml @@ -18,101 +18,17 @@ jobs: application_private_key: ${{ secrets.BACKSTAGE_WORKFLOW_MEMBER_READ_PRIVATE_KEY }} organization: backstage - - uses: actions/github-script@v5 - id: get-all-open-prs - with: - github-token: ${{ steps.get_workflow_token.outputs.token }} - script: | - const allPrs = await github.paginate(github.rest.pulls.list, { - owner: context.repo.owner, - repo: context.repo.repo, - state: 'open', - }); - - return allPrs; - - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: 14 - run: npm install codeowners - - uses: actions/github-script@v5 - id: get-all-group-members - with: - github-token: ${{ steps.get_workflow_token.outputs.token }} - script: | - // Get all teams and their respective members - const {data: teams} = await github.request('GET /orgs/{org}/teams', { - org: context.repo.owner, - }) - - const groupMembers = await Promise.all( - teams.map( - async (team) => { - const { data } = await github.rest.teams.listMembersInOrg({ - org: context.repo.owner, - team_slug: team.slug, - }); - - return { team: `@backstage/${team.slug}`, data }; - } - ) - ) - - return groupMembers; - - uses: actions/github-script@v5 id: fix-labels with: github-token: ${{ steps.get_workflow_token.outputs.token }} script: | - const allPullRequests = ${{ steps.get-all-open-prs.outputs.result }} - const groupMembers = ${{ steps.get-all-group-members.outputs.result }} - const Codeowners = require('codeowners'); - const codeowners = new Codeowners(); - - for (const pullRequest of allPullRequests) { - const changedFiles = await github.paginate(github.rest.pulls.listFiles, { - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: pullRequest.number, - }); - - const allReviews = await github.paginate(github.rest.pulls.listReviews, { - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: pullRequest.number, - }); - - const expectedReviewers = new Set(); - - for (const file of changedFiles) { - expectedReviewers.add(...codeowners.getOwner(file.filename)); - } - - const hasReviewed = new Set(); - - // For each reviewer in the group, check to see if they have a review set - for (const reviewer of expectedReviewers) { - const members = groupMembers.find(member => member.team === reviewer); - if (members) { - // then we are dealing with a group - const hasMemberReview = allReviews.some(review => members.data.some(member => member.login === review.user.login)); - if (hasMemberReview) { - hasReviewed.add(reviewer); - } - } else { - // reviewer is a person - const hasReview = allReviews.some(review => reviewer === `@${review.user.login}`); - if (hasReview) { - hasReviewed.add(reviewer); - } - } - } - - if (hasReviewed.size === expectedReviewers.size) { - console.log('should remove label'); - } else { console.log('should add label')} - } + const script = require('./scripts/goalie-labels.js') + await script({github, context, core}) diff --git a/scripts/goalie-labels.js b/scripts/goalie-labels.js new file mode 100644 index 0000000000..02d2e4a332 --- /dev/null +++ b/scripts/goalie-labels.js @@ -0,0 +1,98 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +module.exports = async ({ github, context, core }) => { + // first get all open pull requests + const allPullRequests = await github.paginate(github.rest.pulls.list, { + owner: context.repo.owner, + repo: context.repo.repo, + state: 'open', + }); + + const { data: teams } = await github.request('GET /orgs/{org}/teams', { + org: context.repo.owner, + }); + + const groupMembers = await Promise.all( + teams.map(async team => { + const { data } = await github.rest.teams.listMembersInOrg({ + org: context.repo.owner, + team_slug: team.slug, + }); + + return { team: `@backstage/${team.slug}`, data }; + }), + ); + + for (const pullRequest of allPullRequests) { + // Go through each file changed and go through each codeowner entry and use minimatch to see if the file matches + // strip the backstage group from the name? + // If it does match push the owner to a list of reviewers + // check to see the reviews and if there is at least one matching reviewer from those group + const changedFiles = await github.paginate(github.rest.pulls.listFiles, { + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: pullRequest.number, + }); + + const allReviews = await github.paginate(github.rest.pulls.listReviews, { + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: pullRequest.number, + }); + + const expectedReviewers = new Set(); + + for (const file of changedFiles) { + expectedReviewers.add(...codeowners.getOwner(file.filename)); + } + + const hasReviewed = new Set(); + + // For each reviewer in the group, check to see if they have a review set + for (const reviewer of expectedReviewers) { + const members = groupMembers.find(member => member.team === reviewer); + if (members) { + // then we are dealing with a group + const hasMemberReview = allReviews.some(review => + members.data.some(member => member.login === review.user.login), + ); + if (hasMemberReview) { + hasReviewed.add(reviewer); + } + } else { + // reviewer is a person + const hasReview = allReviews.some( + review => reviewer === `@${review.user.login}`, + ); + if (hasReview) { + hasReviewed.add(reviewer); + } + } + } + + if (hasReviewed.size === expectedReviewers.size) { + console.log('should remove label'); + // await github.rest.issues.removeLabel({ + // issue_number: context.issue.number, + // owner: context.repo.owner, + // repo: context.repo.repo, + // name: 'awaiting-review' + // }).catch(() => {}); + } else { + console.log('should add label'); + } + } +};