Skip to content

Commit

Permalink
fixes sc core approval checker (#780)
Browse files Browse the repository at this point in the history
* fixes sc core approval checker

* fix if clause

---------

Co-authored-by: Ed Zynda <[email protected]>
  • Loading branch information
0xDEnYO and ezynda3 authored Aug 28, 2024
1 parent e59b1c8 commit 0edaeca
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions .github/workflows/ensureSCCoreDevApproval.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@
name: SC Core Dev Approval Check

on:
push:
pull_request:
types: [opened, synchronize, reopened]

jobs:
core-dev-approval:
if: ${{ github.event.pull_request.draft == false }}
if: ${{ github.event.pull_request.draft == false }} # will only run once the PR is in "Ready for Review" state
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 ##### Fetch all history for all branches

- name: Get SC Core Dev Team Members
- name: Get smart-contract-core Team Members
env:
GH_PAT: ${{ secrets.GIT_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down Expand Up @@ -50,20 +44,22 @@ jobs:
#### check if any members were returned
if [[ -z $MEMBERS ]]; then
echo -e "\033[31mERROR: Could not retrieve team members of group $TEAM_SLUG\033[0m"
echo "CONTINUE=false" >> $GITHUB_ENV
echo "CONTINUE=false" >> "$GITHUB_ENV"
exit 1
fi
echo "Team members of smart contract core: "
echo "The following Github users are members of team smart-contract-core: "
echo "$MEMBERS"
echo -e "$MEMBERS" > sc_core_dev_members.txt
echo "CONTINUE=true" >> $GITHUB_ENV
echo "CONTINUE=true" >> "$GITHUB_ENV"
- name: Check if PR is approved by at least one SC core dev
id: check-core-dev-approval
if: env.CONTINUE == 'true'
uses: actions/github-script@v7
env:
PR_NUMBER: ${{ github.event.number }}
with:
script: |
const fs = require('fs');
Expand All @@ -75,40 +71,49 @@ jobs:
};
const coreDevsFile = 'sc_core_dev_members.txt';
// Read handles from file
const coreDevs = fs.readFileSync(coreDevsFile, 'utf-8').split(/\r?\n/).filter(Boolean);
// get all reviewers that have approved this PR
const { data: reviews } = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: process.env.PR_NUMBER,
});
// make sure that reviews are available
if(!(await github.pulls)) {
if(!reviews || reviews.length === 0) {
console.log(`${colors.red}Could not get reviewers of this PR from Github. Are there any reviews yet?${colors.reset}`);
console.log(`${colors.red}Check failed.${colors.reset}`);
core.setFailed("Required approval is missing");
return
}
// get all reviewers that have approved this PR
const { data: reviews } = await github.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});
// Filter to only include reviews that have "APPROVED" status
const approvedReviews = reviews.filter(review => review.state === 'APPROVED');
// extract the git login handles of all reviewers
if(!approvedReviews.length === 0) {
console.log(`${colors.red}Could not find any reviews with approval.${colors.reset}`);
console.log(`${colors.red}Cannot continue. Check failed.${colors.reset}`);
core.setFailed("Required approval is missing");
return
}
// extract the git login handles of all reviewers that approved this PR
const reviewerHandles = approvedReviews.map(review => review.user.login);
console.log(`This PR has been reviewed by the following git members: ${reviewerHandles}`)
if(approvedReviews.length === 0)
console.log(`${colors.red}This PR has no approvals${colors.reset}`);
else
console.log(`This PR has been approved by the following git members: ${reviewerHandles}`);
// check if at least one of these reviewers is member in smart-contract-core group
if (reviewerHandles.some((handle) => coreDevs.includes(handle))) {
console.log(`${colors.green}The current PR is approved by a member of the $TEAM_SLUG group.${colors.reset}`);
console.log(`${colors.green}The current PR is approved by a member of the smart-contract-core group.${colors.reset}`);
console.log(`${colors.green}Check passed.${colors.reset}`);
core.setOutput('approved', 'true');
} else {
console.log(`${colors.red}The PR requires a missing approval by a member of the $TEAM_SLUG group.${colors.reset}`);
console.log(`${colors.red}Find group members here: https://github.com/orgs/lifinance/teams/smart-contract-core.${colors.reset}`);
console.log(`${colors.red}The PR requires a missing approval by a member of the smart-contract-core group (https://github.com/orgs/lifinance/teams/smart-contract-core).${colors.reset}`);
console.log(`${colors.red}Check failed.${colors.reset}`);
core.setFailed("Required approval is missing");
}

0 comments on commit 0edaeca

Please sign in to comment.