2017-02-14 07:08:40 +00:00
|
|
|
const logger = require('winston');
|
|
|
|
const prWorker = require('../../lib/workers/pr');
|
|
|
|
const defaultConfig = require('../../lib/config/defaults').getConfig();
|
|
|
|
|
|
|
|
logger.remove(logger.transports.Console);
|
|
|
|
|
|
|
|
const getChangeLog = jest.fn();
|
|
|
|
getChangeLog.mockReturnValue('Mocked changelog');
|
|
|
|
|
|
|
|
describe('workers/pr', () => {
|
2017-04-20 11:01:23 +00:00
|
|
|
describe('checkAutoMerge(pr, config)', () => {
|
|
|
|
let config;
|
|
|
|
let pr;
|
|
|
|
beforeEach(() => {
|
|
|
|
config = Object.assign({}, defaultConfig);
|
|
|
|
pr = {
|
|
|
|
head: {
|
|
|
|
ref: 'somebranch',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
config.api = {
|
|
|
|
mergePr: jest.fn(),
|
|
|
|
getBranchStatus: jest.fn(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
it('should not automerge if not configured', async () => {
|
|
|
|
await prWorker.checkAutoMerge(pr, config);
|
|
|
|
expect(config.api.mergePr.mock.calls.length).toBe(0);
|
|
|
|
});
|
|
|
|
it('should automerge if any and pr is mergeable', async () => {
|
|
|
|
config.automerge = 'any';
|
|
|
|
pr.mergeable = true;
|
|
|
|
config.api.getBranchStatus.mockReturnValueOnce('success');
|
|
|
|
await prWorker.checkAutoMerge(pr, config);
|
|
|
|
expect(config.api.mergePr.mock.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
it('should not automerge if any and pr is mergeable but branch status is not success', async () => {
|
|
|
|
config.automerge = 'any';
|
|
|
|
pr.mergeable = true;
|
|
|
|
config.api.getBranchStatus.mockReturnValueOnce('pending');
|
|
|
|
await prWorker.checkAutoMerge(pr, config);
|
|
|
|
expect(config.api.mergePr.mock.calls.length).toBe(0);
|
|
|
|
});
|
|
|
|
it('should not automerge if any and pr is mergeable but unstable', async () => {
|
|
|
|
config.automerge = 'any';
|
|
|
|
pr.mergeable = true;
|
|
|
|
pr.mergeable_state = 'unstable';
|
|
|
|
await prWorker.checkAutoMerge(pr, config);
|
|
|
|
expect(config.api.mergePr.mock.calls.length).toBe(0);
|
|
|
|
});
|
|
|
|
it('should not automerge if any and pr is unmergeable', async () => {
|
|
|
|
config.automerge = 'any';
|
|
|
|
pr.mergeable = false;
|
|
|
|
await prWorker.checkAutoMerge(pr, config);
|
|
|
|
expect(config.api.mergePr.mock.calls.length).toBe(0);
|
|
|
|
});
|
|
|
|
it('should automerge if minor and upgradeType is minor', async () => {
|
|
|
|
config.automerge = 'minor';
|
|
|
|
config.upgradeType = 'minor';
|
|
|
|
pr.mergeable = true;
|
|
|
|
config.api.getBranchStatus.mockReturnValueOnce('success');
|
|
|
|
await prWorker.checkAutoMerge(pr, config);
|
|
|
|
expect(config.api.mergePr.mock.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
it('should not automerge if minor and upgradeType is major', async () => {
|
|
|
|
config.automerge = 'minor';
|
|
|
|
config.upgradeType = 'major';
|
|
|
|
pr.mergeable = true;
|
|
|
|
await prWorker.checkAutoMerge(pr, config);
|
|
|
|
expect(config.api.mergePr.mock.calls.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
2017-06-05 08:21:02 +00:00
|
|
|
describe('ensurePr(upgrades)', () => {
|
2017-02-14 07:08:40 +00:00
|
|
|
let config;
|
|
|
|
let existingPr;
|
|
|
|
beforeEach(() => {
|
|
|
|
config = Object.assign({}, defaultConfig);
|
|
|
|
config.api = {
|
|
|
|
createPr: jest.fn(() => ({ displayNumber: 'New Pull Request' })),
|
|
|
|
};
|
|
|
|
existingPr = {
|
|
|
|
title: 'Update dependency dummy to version 1.1.0',
|
2017-06-02 20:40:00 +00:00
|
|
|
body:
|
2017-06-05 08:21:02 +00:00
|
|
|
'This Pull Request updates dependency dummy from version `1.0.0` to `1.1.0`\n\nNo changelog available',
|
2017-02-14 07:08:40 +00:00
|
|
|
displayNumber: 'Existing PR',
|
|
|
|
};
|
|
|
|
});
|
|
|
|
it('should return null if check fails', async () => {
|
|
|
|
config.api.getBranchPr = jest.fn(() => {
|
|
|
|
throw new Error('oops');
|
|
|
|
});
|
2017-06-05 08:21:02 +00:00
|
|
|
const pr = await prWorker.ensurePr([config]);
|
2017-02-14 07:08:40 +00:00
|
|
|
expect(pr).toBe(null);
|
|
|
|
});
|
2017-04-20 10:11:56 +00:00
|
|
|
it('should return null if waiting for success', async () => {
|
|
|
|
config.api.getBranchStatus = jest.fn(() => 'failed');
|
|
|
|
config.prCreation = 'status-success';
|
2017-06-05 08:21:02 +00:00
|
|
|
const pr = await prWorker.ensurePr([config]);
|
2017-04-20 10:11:56 +00:00
|
|
|
expect(pr).toBe(null);
|
|
|
|
});
|
|
|
|
it('should create PR if success', async () => {
|
|
|
|
config.api.getBranchStatus = jest.fn(() => 'success');
|
|
|
|
config.api.getBranchPr = jest.fn();
|
|
|
|
config.prCreation = 'status-success';
|
2017-06-05 08:21:02 +00:00
|
|
|
const pr = await prWorker.ensurePr([config]);
|
2017-04-20 10:11:56 +00:00
|
|
|
expect(pr).toMatchObject({ displayNumber: 'New Pull Request' });
|
|
|
|
});
|
|
|
|
it('should return null if waiting for not pending', async () => {
|
|
|
|
config.api.getBranchStatus = jest.fn(() => 'pending');
|
|
|
|
config.prCreation = 'not-pending';
|
2017-06-05 08:21:02 +00:00
|
|
|
const pr = await prWorker.ensurePr([config]);
|
2017-04-20 10:11:56 +00:00
|
|
|
expect(pr).toBe(null);
|
|
|
|
});
|
|
|
|
it('should create PR if no longer pending', async () => {
|
|
|
|
config.api.getBranchStatus = jest.fn(() => 'failed');
|
|
|
|
config.api.getBranchPr = jest.fn();
|
|
|
|
config.prCreation = 'not-pending';
|
2017-06-05 08:21:02 +00:00
|
|
|
const pr = await prWorker.ensurePr([config]);
|
2017-04-20 10:11:56 +00:00
|
|
|
expect(pr).toMatchObject({ displayNumber: 'New Pull Request' });
|
|
|
|
});
|
2017-02-14 07:08:40 +00:00
|
|
|
it('should create new branch if none exists', async () => {
|
|
|
|
config.api.getBranchPr = jest.fn();
|
2017-06-05 08:21:02 +00:00
|
|
|
const pr = await prWorker.ensurePr([config]);
|
2017-02-14 07:08:40 +00:00
|
|
|
expect(pr).toMatchObject({ displayNumber: 'New Pull Request' });
|
|
|
|
});
|
|
|
|
it('should add labels to new PR', async () => {
|
|
|
|
config.api.getBranchPr = jest.fn();
|
|
|
|
config.api.addLabels = jest.fn();
|
|
|
|
config.labels = ['foo'];
|
2017-06-05 08:21:02 +00:00
|
|
|
const pr = await prWorker.ensurePr([config]);
|
2017-02-14 07:08:40 +00:00
|
|
|
expect(pr).toMatchObject({ displayNumber: 'New Pull Request' });
|
|
|
|
expect(config.api.addLabels.mock.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
it('should add not labels to new PR if empty', async () => {
|
|
|
|
config.api.getBranchPr = jest.fn();
|
|
|
|
config.api.addLabels = jest.fn();
|
|
|
|
config.labels = [];
|
2017-06-05 08:21:02 +00:00
|
|
|
const pr = await prWorker.ensurePr([config]);
|
2017-02-14 07:08:40 +00:00
|
|
|
expect(pr).toMatchObject({ displayNumber: 'New Pull Request' });
|
|
|
|
expect(config.api.addLabels.mock.calls.length).toBe(0);
|
|
|
|
});
|
|
|
|
it('should add assignees and reviewers to new PR', async () => {
|
|
|
|
config.api.getBranchPr = jest.fn();
|
|
|
|
config.api.addAssignees = jest.fn();
|
|
|
|
config.api.addReviewers = jest.fn();
|
|
|
|
config.assignees = ['bar'];
|
|
|
|
config.reviewers = ['baz'];
|
2017-06-05 08:21:02 +00:00
|
|
|
const pr = await prWorker.ensurePr([config]);
|
2017-02-14 07:08:40 +00:00
|
|
|
expect(pr).toMatchObject({ displayNumber: 'New Pull Request' });
|
|
|
|
expect(config.api.addAssignees.mock.calls.length).toBe(1);
|
2017-04-21 05:23:36 +00:00
|
|
|
expect(config.api.addReviewers.mock.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
it('should not add assignees and reviewers to new PR if automerging any', async () => {
|
|
|
|
config.api.getBranchPr = jest.fn();
|
|
|
|
config.api.addAssignees = jest.fn();
|
|
|
|
config.api.addReviewers = jest.fn();
|
|
|
|
config.assignees = ['bar'];
|
|
|
|
config.reviewers = ['baz'];
|
|
|
|
config.automerge = 'any';
|
2017-06-05 08:21:02 +00:00
|
|
|
const pr = await prWorker.ensurePr([config]);
|
2017-04-21 05:23:36 +00:00
|
|
|
expect(pr).toMatchObject({ displayNumber: 'New Pull Request' });
|
|
|
|
expect(config.api.addAssignees.mock.calls.length).toBe(0);
|
|
|
|
expect(config.api.addReviewers.mock.calls.length).toBe(0);
|
|
|
|
});
|
|
|
|
it('should not add assignees and reviewers to new PR if automerging minor', async () => {
|
|
|
|
config.api.getBranchPr = jest.fn();
|
|
|
|
config.api.addAssignees = jest.fn();
|
|
|
|
config.api.addReviewers = jest.fn();
|
|
|
|
config.assignees = ['bar'];
|
|
|
|
config.reviewers = ['baz'];
|
|
|
|
config.upgradeType = 'minor';
|
|
|
|
config.automerge = 'minor';
|
2017-06-05 08:21:02 +00:00
|
|
|
const pr = await prWorker.ensurePr([config]);
|
2017-04-21 05:23:36 +00:00
|
|
|
expect(pr).toMatchObject({ displayNumber: 'New Pull Request' });
|
|
|
|
expect(config.api.addAssignees.mock.calls.length).toBe(0);
|
|
|
|
expect(config.api.addReviewers.mock.calls.length).toBe(0);
|
|
|
|
});
|
|
|
|
it('should add assignees and reviewers to new PR if automerging minor and its major', async () => {
|
|
|
|
config.api.getBranchPr = jest.fn();
|
|
|
|
config.api.addAssignees = jest.fn();
|
|
|
|
config.api.addReviewers = jest.fn();
|
|
|
|
config.assignees = ['bar'];
|
|
|
|
config.reviewers = ['baz'];
|
|
|
|
config.upgradeType = 'major';
|
|
|
|
config.automerge = 'minor';
|
2017-06-05 08:21:02 +00:00
|
|
|
const pr = await prWorker.ensurePr([config]);
|
2017-04-21 05:23:36 +00:00
|
|
|
expect(pr).toMatchObject({ displayNumber: 'New Pull Request' });
|
|
|
|
expect(config.api.addAssignees.mock.calls.length).toBe(1);
|
2017-02-14 07:08:40 +00:00
|
|
|
expect(config.api.addReviewers.mock.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
it('should return unmodified existing PR', async () => {
|
|
|
|
config.depName = 'dummy';
|
|
|
|
config.currentVersion = '1.0.0';
|
|
|
|
config.newVersion = '1.1.0';
|
|
|
|
config.api.getBranchPr = jest.fn(() => existingPr);
|
|
|
|
config.api.updatePr = jest.fn();
|
2017-06-05 08:21:02 +00:00
|
|
|
const pr = await prWorker.ensurePr([config]);
|
2017-02-14 07:08:40 +00:00
|
|
|
expect(pr).toMatchObject(existingPr);
|
|
|
|
});
|
|
|
|
it('should return modified existing PR', async () => {
|
|
|
|
config.depName = 'dummy';
|
|
|
|
config.currentVersion = '1.0.0';
|
|
|
|
config.newVersion = '1.2.0';
|
|
|
|
config.api.getBranchPr = jest.fn(() => existingPr);
|
|
|
|
config.api.updatePr = jest.fn();
|
2017-06-05 08:21:02 +00:00
|
|
|
const pr = await prWorker.ensurePr([config]);
|
2017-02-14 07:08:40 +00:00
|
|
|
const updatedPr = Object.assign(existingPr, {
|
2017-06-02 20:40:00 +00:00
|
|
|
body:
|
2017-06-05 08:21:02 +00:00
|
|
|
'This Pull Request updates dependency dummy from version `1.0.0` to `1.2.0`\n\nNo changelog available',
|
2017-02-14 07:08:40 +00:00
|
|
|
});
|
|
|
|
expect(pr).toMatchObject(updatedPr);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|