Skip to content

Commit

Permalink
Merge pull request #28 from Omegapoint/feature/FixedBranchprotectionZero
Browse files Browse the repository at this point in the history
Feature/fixed branchprotection zero
  • Loading branch information
jonathanbokvad authored Oct 2, 2023
2 parents 4582da6 + dfca0a5 commit 8610ddf
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 27 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
51 changes: 27 additions & 24 deletions src/branchprotection/BranchProtectionService.ts
Original file line number Diff line number Diff line change
@@ -1,33 +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');
const octokit: any = github.getOctokit(token);

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(
'Branch protection is enabled, number of reviewers are:',
response.data.required_pull_request_reviews?.['required_approving_review_count']
);
core.exportVariable(
'numberOfReviewers',
response.data.required_pull_request_reviews?.['required_approving_review_count']
);
})
.catch((error: any) => {
console.log('Branch protections is not enabled for repository: ' + repo);
console.log('Error:', error?.message);
});
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');
}

const numberOfReviewers: number =
response.data.required_pull_request_reviews?.required_approving_review_count || 0;

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);
}
}
}
2 changes: 1 addition & 1 deletion src/cydigconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"teamName": "name-of-your-team",
"teamName": "Test-GitHub",
"usingAzure": true,
"threatModeling": {
"date": "date-of-threat-modeling",
Expand Down

0 comments on commit 8610ddf

Please sign in to comment.