Skip to content

Commit

Permalink
Merge pull request #30 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 cad4f30 + 0705f99 commit 3b51977
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/branchprotection/BranchProtectionService.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
/* eslint-disable @typescript-eslint/typedef */
import * as core from '@actions/core';
import * as github from '@actions/github';
import { Octokit } from 'octokit';
// import { Octokit } from 'octokit';
export class BranchProtectionService {
public static async getStateOfBranchProtection(): Promise<void> {
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 = new Octokit({ auth: token });
const response = await octokit.request(`GET /repos/${owner}/${repo}/branches/main/protection`, {
owner,
repo,
branch: 'main',
});
console.log(response.data);
// const octokit = github.getOctokit(token);
// const response = await octokit.rest.repos.getBranchProtection({
// const octokit = new Octokit({ auth: token });
// const response = await octokit.request(`GET /repos/${owner}/${repo}/branches/main/protection`, {
// owner,
// repo,
// branch: 'main',
// });
if (response.data.enforce_admins?.enabled === false) {
const headers = {
Authorization: `token ${token}`,
'Content-Type': 'application/json',
};

const branchProtectionUrl = `https://api.github.com/repos/${owner}/${repo}/branches/main/protection`;
const response = await fetch(branchProtectionUrl, {
method: 'GET',
headers,
});
const data = await response.json();
console.log('Data:', data);
if (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;
const numberOfReviewers: number = data.required_pull_request_reviews?.required_approving_review_count || 0;

if (numberOfReviewers >= 1 && response.data.enforce_admins?.enabled === true) {
if (numberOfReviewers >= 1 && 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);
Expand Down

0 comments on commit 3b51977

Please sign in to comment.