From d5f27e43c6a5b2658377847169b4ccfdfb9ddd5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Bokvad=20Engar=C3=A5s?= Date: Mon, 2 Oct 2023 17:14:04 +0200 Subject: [PATCH 1/2] refactor project --- .../BranchProtectionService.ts | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/branchprotection/BranchProtectionService.ts b/src/branchprotection/BranchProtectionService.ts index b3a3c7ee..2f22e9aa 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,32 @@ 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, + }); + console.log('Response:', response); + 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); From 0705f997b2c44122d6e1f1f7a786553914e7bd83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Bokvad=20Engar=C3=A5s?= Date: Mon, 2 Oct 2023 17:16:14 +0200 Subject: [PATCH 2/2] refactor project --- src/branchprotection/BranchProtectionService.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/branchprotection/BranchProtectionService.ts b/src/branchprotection/BranchProtectionService.ts index 2f22e9aa..6e1a8b68 100644 --- a/src/branchprotection/BranchProtectionService.ts +++ b/src/branchprotection/BranchProtectionService.ts @@ -25,7 +25,6 @@ export class BranchProtectionService { method: 'GET', headers, }); - console.log('Response:', response); const data = await response.json(); console.log('Data:', data); if (data.enforce_admins?.enabled === false) {