diff --git a/src/branchprotection/BranchProtectionService.ts b/src/branchprotection/BranchProtectionService.ts index b3a3c7ee..6e1a8b68 100644 --- a/src/branchprotection/BranchProtectionService.ts +++ b/src/branchprotection/BranchProtectionService.ts @@ -1,7 +1,7 @@ /* 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 { try { @@ -9,27 +9,31 @@ export class BranchProtectionService { 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);