Skip to content

Commit

Permalink
refactor project
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanbokvad committed Oct 3, 2023
1 parent a7b918a commit 03700bb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 149 deletions.
138 changes: 4 additions & 134 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"@octokit/rest": "^20.0.2",
"@vercel/ncc": "^0.36.1",
"joi": "^17.10.1"
},
"devDependencies": {
"@octokit/types": "^12.0.0",
"@types/chai": "^4.3.6",
"@types/mocha": "^10.0.1",
"@types/node": "^20.5.7",
Expand Down
18 changes: 9 additions & 9 deletions src/branchprotection/BranchProtectionService.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
/* eslint-disable @typescript-eslint/typedef */
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as core from '@actions/core';
import * as github from '@actions/github';
import { Endpoints } from '@octokit/types';
import { GitHub } from '@actions/github/lib/utils';
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 = github.getOctokit(token);
const response = await octokit.rest.repos.getBranchProtection({
const octokit: InstanceType<typeof GitHub> = github.getOctokit(token);
type branchProtectionRepsponse = Endpoints['GET /repos/{owner}/{repo}/branches/{branch}/protection']['response'];
const data: branchProtectionRepsponse['data'] = await octokit.rest.repos.getBranchProtection({
owner,
repo,
branch: 'main',
});

if (response.data.enforce_admins?.enabled === false) {
if (data.enforce_admins?.enabled === false) {
core.warning('Branch protection can be overridden by admins and is therefore counted as not enabled');
}

let numberOfReviewers: number = 0;
if (
response.data.enforce_admins?.enabled === true &&
response.data.required_pull_request_reviews?.required_approving_review_count
data.enforce_admins?.enabled === true &&
data.required_pull_request_reviews?.required_approving_review_count
) {
numberOfReviewers = response.data.required_pull_request_reviews?.required_approving_review_count;
numberOfReviewers = data.required_pull_request_reviews?.required_approving_review_count;
console.log('Branch protection is enabled, number of reviewers:', numberOfReviewers);
} else {
console.log('Branch protection is not enabled for repository:', repo);
Expand Down
9 changes: 4 additions & 5 deletions tests/branchprotection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,27 @@ describe('BranchProtectionService', () => {
let exportVariableStub: SinonStub;
let getOctokitStub: SinonStub;

beforeEach(() => {
beforeEach(() => {
sandbox = sinon.createSandbox();
getInputStub = sandbox.stub(core, 'getInput');
warningStub = sandbox.stub(core, 'warning');
exportVariableStub = sandbox.stub(core, 'exportVariable');
getOctokitStub = sandbox.stub(github, 'getOctokit');
});
});

afterEach(() => {
sandbox.restore();
});

it('should handle successful branch protection retrieval', async () => {
getInputStub.withArgs('PAT-token').returns('your-pat-token'); // Replace with your token

getOctokitStub.returns({
rest: {
repos: {
getBranchProtection: sinon.stub().resolves({
data: {
enforce_admins: { enabled: true },
required_pull_request_reviews: { required_approving_review_count: 2 },
required_pull_request_reviews: { required_approving_review_count: 1 },
},
}),
},
Expand All @@ -42,7 +41,7 @@ describe('BranchProtectionService', () => {

expect(getInputStub.calledWith('PAT-token')).to.be.true;
expect(warningStub.called).to.be.false;
expect(exportVariableStub.calledWith('numberOfReviewers', 2)).to.be.true;
expect(exportVariableStub.calledWith('numberOfReviewers', 1)).to.be.true;
});

// Add more test cases for different scenarios as needed
Expand Down

0 comments on commit 03700bb

Please sign in to comment.