Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create enforce-approval-voting.yml #7

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions .github/enforceApproval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// .github/enforceApproval.js

const { Octokit } = require("@octokit/rest");
const core = require("@actions/core");

(async () => {
try {
// Initialize Octokit with the GitHub token
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });

// Get repository details from environment variables
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
const pull_number = parseInt(process.env.GITHUB_REF.split('/').pop(), 10);

// Fetch organization members
const org = owner; // Assuming the owner is the organization
let members = [];
let page = 1;
const per_page = 100;
while (true) {
const { data } = await octokit.orgs.listMembers({
org,
per_page,
page,
});
members = members.concat(data);
if (data.length < per_page) break;
page++;
}

const totalMembers = members.length;
const requiredApprovals = Math.ceil(totalMembers / 2);

// Fetch pull request reviews
let reviews = [];
page = 1;
while (true) {
const { data } = await octokit.pulls.listReviews({
owner,
repo,
pull_number,
per_page,
page,
});
reviews = reviews.concat(data);
if (data.length < per_page) break;
page++;
}

// Filter for 'APPROVED' reviews and ensure uniqueness by user
const approvedUsers = new Set();
reviews.forEach(review => {
if (review.state === 'APPROVED') {
approvedUsers.add(review.user.login);
}
});

const approvalCount = approvedUsers.size;

console.log(`Total Organization Members: ${totalMembers}`);
console.log(`Required Approvals: ${requiredApprovals}`);
console.log(`Current Approvals: ${approvalCount}`);

if (approvalCount < requiredApprovals) {
core.setFailed(`Pull request requires at least ${requiredApprovals} approvals, but only ${approvalCount} were provided.`);
} else {
console.log("Approval requirement met.");
}
} catch (error) {
core.setFailed(`Error enforcing approval requirements: ${error.message}`);
}
})();
15 changes: 15 additions & 0 deletions .github/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "github-action-scripts",
"version": "1.0.0",
"description": "Scripts for GitHub Actions workflows",
"main": "enforceApproval.js",
"scripts": {
"enforce": "node enforceApproval.js"
},
"dependencies": {
"@actions/core": "^1.10.0",
"@octokit/rest": "^18.0.0"
},
"author": "",
"license": "ISC"
}
34 changes: 34 additions & 0 deletions .github/workflows/enforce-approval-voting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Enforce Custom Approval Requirements

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

jobs:
approval-check:
runs-on: ubuntu-latest

steps:
# Step 1: Checkout the repository
- name: Checkout Repository
uses: actions/checkout@v3

# Step 2: Set up Node.js environment
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16' # Ensure this matches your script's Node.js version requirement

# Step 3: Install Dependencies within .github/
- name: Install Dependencies
working-directory: .github
run: |
npm install

# Step 4: Execute the Enforcement Script
- name: Enforce Approval Requirement
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
node enforceApproval.js
working-directory: .github
Loading