Skip to content

Commit

Permalink
Merge pull request #3 from joshdales/new-config-structure
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdales authored Feb 21, 2023
2 parents 7b1327b + 56347d5 commit 8943ca2
Show file tree
Hide file tree
Showing 9 changed files with 502 additions and 144 deletions.
8 changes: 7 additions & 1 deletion __mocks__/@actions/github.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
export const context = {
payload: {
pull_request: {
number: 123
number: 123,
head: {
ref: 'head-branch-name'
},
base: {
ref: 'base-branch-name'
}
}
},
repo: {
Expand Down
155 changes: 155 additions & 0 deletions __tests__/branch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import {
getBranchName,
checkBranch,
toBranchMatchConfig,
BranchMatchConfig
} from '../src/branch';
import * as github from '@actions/github';

jest.mock('@actions/core');
jest.mock('@actions/github');

describe('getBranchName', () => {
describe('when the pull requests base branch is requested', () => {
it('returns the base branch name', () => {
const result = getBranchName('base');
expect(result).toEqual('base-branch-name');
});
});

describe('when the pull requests head branch is requested', () => {
it('returns the head branch name', () => {
const result = getBranchName('head');
expect(result).toEqual('head-branch-name');
});
});

describe('when no branch is specified', () => {
it('returns the head branch name', () => {
const result = getBranchName();
expect(result).toEqual('head-branch-name');
});
});
});

describe('checkBranch', () => {
beforeEach(() => {
github.context.payload.pull_request!.head = {
ref: 'test/feature/123'
};
github.context.payload.pull_request!.base = {
ref: 'main'
};
});

describe('when a single pattern is provided', () => {
describe('and the pattern matches the head branch', () => {
it('returns true', () => {
const result = checkBranch(['^test']);
expect(result).toBe(true);
});
});

describe('and the pattern does not match the head branch', () => {
it('returns false', () => {
const result = checkBranch(['^feature/']);
expect(result).toBe(false);
});
});
});

describe('when multiple patterns are provided', () => {
describe('and at least one pattern matches', () => {
it('returns true', () => {
const result = checkBranch(['^test/', '^feature/']);
expect(result).toBe(true);
});
});

describe('and all patterns match', () => {
it('returns true', () => {
const result = checkBranch(['^test/', '/feature/']);
expect(result).toBe(true);
});
});

describe('and no patterns match', () => {
it('returns false', () => {
const result = checkBranch(['^feature/', '/test$']);
expect(result).toBe(false);
});
});
});

describe('when the branch to check is specified as the base branch', () => {
describe('and the pattern matches the base branch', () => {
it('returns true', () => {
const result = checkBranch(['^main$'], 'base');
expect(result).toBe(true);
});
});
});
});

describe('toBranchMatchConfig', () => {
describe('when there are no branch keys in the config', () => {
const config = {'changed-files': [{any: ['testing']}]};
it('returns an empty object', () => {
const result = toBranchMatchConfig(config);
expect(result).toMatchObject({});
});
});

describe('when the config contains a head-branch option', () => {
const config = {'head-branch': ['testing']};
it('sets headBranch in the matchConfig', () => {
const result = toBranchMatchConfig(config);
expect(result).toMatchObject<BranchMatchConfig>({
headBranch: ['testing']
});
});

describe('and the matching option is a string', () => {
const stringConfig = {'head-branch': 'testing'};

it('sets headBranch in the matchConfig', () => {
const result = toBranchMatchConfig(stringConfig);
expect(result).toMatchObject<BranchMatchConfig>({
headBranch: ['testing']
});
});
});
});

describe('when the config contains a base-branch option', () => {
const config = {'base-branch': ['testing']};
it('sets headBranch in the matchConfig', () => {
const result = toBranchMatchConfig(config);
expect(result).toMatchObject<BranchMatchConfig>({
baseBranch: ['testing']
});
});

describe('and the matching option is a string', () => {
const stringConfig = {'base-branch': 'testing'};

it('sets headBranch in the matchConfig', () => {
const result = toBranchMatchConfig(stringConfig);
expect(result).toMatchObject<BranchMatchConfig>({
baseBranch: ['testing']
});
});
});
});

describe('when the config contains both a base-branch and head-branch option', () => {
const config = {'base-branch': ['testing'], 'head-branch': ['testing']};
it('sets headBranch in the matchConfig', () => {
const result = toBranchMatchConfig(config);
expect(result).toMatchObject<BranchMatchConfig>({
baseBranch: ['testing'],
headBranch: ['testing']
});
});
});
});
8 changes: 4 additions & 4 deletions __tests__/fixtures/branches.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
test-branch:
- branch: "test/**"
- head-branch: "^test/"

feature-branch:
- branch: "*/feature/*"
- head-branch: "/feature/"

bug-branch:
- branch: "{bug,fix}/*"
- head-branch: "^bug/|fix/"

array-branch:
- branch: ["array/*"]
- head-branch: ["^array/"]
3 changes: 2 additions & 1 deletion __tests__/fixtures/only_pdfs.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
touched-a-pdf-file:
- any: ['*.pdf']
- changed-files:
- any: ['*.pdf']
8 changes: 6 additions & 2 deletions __tests__/labeler.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {checkGlobs} from '../src/labeler';
import {checkGlobs, MatchConfig} from '../src/labeler';

import * as core from '@actions/core';

Expand All @@ -10,7 +10,11 @@ beforeAll(() => {
});
});

const matchConfig = [{any: ['*.txt']}];
// I have to double cast here as this is what the output from js-yaml looks like which then gets
// transformed in toMatchConfig
const matchConfig = [
{'changed-files': [{any: ['*.txt']}]}
] as unknown as MatchConfig[];

describe('checkGlobs', () => {
it('returns true when our pattern does match changed files', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('run', () => {
expect(removeLabelMock).toHaveBeenCalledTimes(0);
});

it('adds labels based on the branch names that match the glob pattern', async () => {
it('adds labels based on the branch names that match the regexp pattern', async () => {
github.context.payload.pull_request!.head = {ref: 'test/testing-time'};
usingLabelerConfigYaml('branches.yml');
await run();
Expand All @@ -118,7 +118,7 @@ describe('run', () => {
});
});

it('adds multiple labels based on branch names that match different glob patterns', async () => {
it('adds multiple labels based on branch names that match different regexp patterns', async () => {
github.context.payload.pull_request!.head = {
ref: 'test/feature/123'
};
Expand Down
Loading

0 comments on commit 8943ca2

Please sign in to comment.