Skip to content

Commit

Permalink
refactor project
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanbokvad committed Oct 2, 2023
1 parent 51b633c commit 066f0ec
Show file tree
Hide file tree
Showing 3 changed files with 267 additions and 29 deletions.
241 changes: 239 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"@octokit/rest": "^20.0.2",
"@vercel/ncc": "^0.36.1",
"joi": "^17.10.1"
},
Expand Down
54 changes: 27 additions & 27 deletions src/branchprotection/BranchProtectionService.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
/* eslint-disable @typescript-eslint/typedef */
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as core from '@actions/core';
import * as github from '@actions/github';
export class BranchProtectionService {
public static async getStateOfBranchProtection(): Promise<void> {
console.log('\n Running branch protection control');

const { owner, repo }: { owner: string; repo: string } = github.context.repo;
const token: string = core.getInput('PAT-token');
// eslint-disable-next-line @typescript-eslint/typedef
const octokit = github.getOctokit(token);
let numberOfReviewers: number = 0;
await octokit.rest.repos
.getBranchProtection({
owner: owner,
repo: repo,
try {
console.log('\n Running branch protection control');
const { owner, repo }: { owner: string; repo: string } = github.context.repo;
const token: string = core.getInput('PAT-token');
const octokit = github.getOctokit(token);
const response = await octokit.rest.repos.getBranchProtection({
owner,
repo,
branch: 'main',
})
.then((response: any) => {
console.log('Response', response.data);
if (!response.data.enforce_admins?.enabled) {
console.log('Branch protection can be overridden by admins, and is therefore counted as not enabled');
return;
}
if (response.data.required_pull_request_reviews?.['required_approving_review_count'] >= 1) {
numberOfReviewers = response.data.required_pull_request_reviews?.['required_approving_review_count'];
console.log('Branch protection is enabled, number of reviewers are:', numberOfReviewers);
}
})
.catch((error: any) => {
console.log('Error:', error?.message);
});
if (numberOfReviewers <= 0) console.log('Branch protections is not enabled for repository: ' + repo);
core.exportVariable('numberOfReviewers', numberOfReviewers);
const numberOfReviewers: number =
response.data.required_pull_request_reviews?.required_approving_review_count || 0;
console.log('Response', response.data);

if (response.data.enforce_admins?.enabled === false) {
console.log('Branch protection can be overridden by admins and is therefore counted as not enabled');
}

if (numberOfReviewers >= 1 && response.data.enforce_admins?.enabled === true) {
console.log('Branch protection is enabled, number of reviewers:', numberOfReviewers);
} else {
console.log('Branch protection is not enabled for repository:', repo);
}

core.exportVariable('numberOfReviewers', numberOfReviewers);
} catch (error) {
console.log('Error:', error.message);
}
}
}

0 comments on commit 066f0ec

Please sign in to comment.