-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51b633c
commit 066f0ec
Showing
3 changed files
with
267 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |