Skip to content

Commit

Permalink
Merge pull request #41 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 3, 2023
2 parents 86eac8b + 287985d commit ea68b3a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 146 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
10 changes: 5 additions & 5 deletions src/branchprotection/BranchProtectionService.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/* 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 response: branchProtectionRepsponse = await octokit.rest.repos.getBranchProtection({
owner,
repo,
branch: 'main',
Expand All @@ -19,7 +20,6 @@ export class BranchProtectionService {
if (response.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 &&
Expand Down
75 changes: 75 additions & 0 deletions tests/branchprotection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import sinon, { SinonSandbox, SinonStub } from 'sinon';
import { expect } from 'chai';
import { BranchProtectionService } from '../src/branchprotection/BranchProtectionService';
describe('BranchProtectionService', () => {
let sandbox: SinonSandbox;
let warningStub: SinonStub;
let exportVariableStub: SinonStub;
let getOctokitStub: SinonStub;

beforeEach(() => {
sandbox = sinon.createSandbox();
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 () => {
getOctokitStub.returns({
rest: {
repos: {
getBranchProtection: sinon.stub().resolves({
data: {
enforce_admins: { enabled: true },
required_pull_request_reviews: { required_approving_review_count: 1 },
},
}),
},
},
});

await BranchProtectionService.getStateOfBranchProtection();
expect(warningStub.called).to.be.false;
expect(exportVariableStub.calledWith('numberOfReviewers', 1)).to.be.true;
});
it('should call warning when admins can byypass branch protection rules', async () => {
getOctokitStub.returns({
rest: {
repos: {
getBranchProtection: sinon.stub().resolves({
data: {
enforce_admins: { enabled: false },
required_pull_request_reviews: { required_approving_review_count: 1 },
},
}),
},
},
});

await BranchProtectionService.getStateOfBranchProtection();
expect(warningStub.called).to.be.true;
expect(exportVariableStub.calledWith('numberOfReviewers', 0)).to.be.true;
});
it('should call warning and set numberOfReviewers to 0 when github repo is private (status = 403)', async () => {
getOctokitStub.returns({
rest: {
repos: {
getBranchProtection: sinon.stub().rejects({
status: 403,
message: 'Forbidden',
}),
},
},
});

await BranchProtectionService.getStateOfBranchProtection();
expect(warningStub.called).to.be.true;
expect(exportVariableStub.calledWith('numberOfReviewers', 0)).to.be.true;
});
});
6 changes: 0 additions & 6 deletions tests/placeholdertests.test.ts

This file was deleted.

0 comments on commit ea68b3a

Please sign in to comment.