-
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.
Merge pull request #28 from Omegapoint/feature/FixedBranchprotectionZero
Feature/fixed branchprotection zero
- Loading branch information
Showing
4 changed files
with
268 additions
and
27 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,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); | ||
} | ||
} | ||
} |
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