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

Feature/fixed branchprotection zero #28

Merged
merged 4 commits into from
Oct 2, 2023
Merged
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
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