Skip to content
Snippets Groups Projects
Commit e310887f authored by Rhys Arkins's avatar Rhys Arkins
Browse files

refactor: remove checkForClosedPr

replace with findPr
parent 08120967
Branches
No related tags found
No related merge requests found
...@@ -31,7 +31,6 @@ module.exports = { ...@@ -31,7 +31,6 @@ module.exports = {
addLabels, addLabels,
// PR // PR
findPr, findPr,
checkForClosedPr,
createPr, createPr,
getPr, getPr,
getAllPrs, getAllPrs,
...@@ -463,6 +462,8 @@ async function addLabels(issueNo, labels) { ...@@ -463,6 +462,8 @@ async function addLabels(issueNo, labels) {
}); });
} }
// Pull Request
async function findPr(branchName, prTitle, state = 'all') { async function findPr(branchName, prTitle, state = 'all') {
logger.debug(`findPr(${branchName}, ${state})`); logger.debug(`findPr(${branchName}, ${state})`);
const urlString = `repos/${config.repoName}/pulls?head=${config.owner}:${branchName}&state=${state}`; const urlString = `repos/${config.repoName}/pulls?head=${config.owner}:${branchName}&state=${state}`;
...@@ -481,25 +482,6 @@ async function findPr(branchName, prTitle, state = 'all') { ...@@ -481,25 +482,6 @@ async function findPr(branchName, prTitle, state = 'all') {
return pr; return pr;
} }
// Pull Request
async function checkForClosedPr(branchName, prTitle) {
logger.debug(`checkForClosedPr(${branchName}, ${prTitle})`);
const url = `repos/${config.repoName}/pulls?state=closed&head=${config.owner}:${branchName}`;
try {
const res = await get(url);
// Return true if any of the titles match exactly
return res.body.some(
pr =>
pr.title === prTitle &&
pr.head.label === `${config.owner}:${branchName}`
);
} catch (err) {
// Sometimes this fails with a 401 for no apparently reason, so we catch and reeturn false
logger.info({ err }, 'Error checking for closed PR. Continuing..');
return false;
}
}
// Creates PR and returns PR number // Creates PR and returns PR number
async function createPr(branchName, title, body, useDefaultBranch) { async function createPr(branchName, title, body, useDefaultBranch) {
const base = useDefaultBranch ? config.defaultBranch : config.baseBranch; const base = useDefaultBranch ? config.defaultBranch : config.baseBranch;
......
...@@ -25,7 +25,6 @@ module.exports = { ...@@ -25,7 +25,6 @@ module.exports = {
addLabels, addLabels,
// PR // PR
findPr, findPr,
checkForClosedPr,
createPr, createPr,
getPr, getPr,
updatePr, updatePr,
...@@ -366,13 +365,6 @@ async function findPr(branchName, prTitle, state = 'all') { ...@@ -366,13 +365,6 @@ async function findPr(branchName, prTitle, state = 'all') {
} }
// Pull Request // Pull Request
async function checkForClosedPr(branchName, prTitle) {
const pr = await findPr(branchName, prTitle, 'closed');
if (pr) {
return true;
}
return false;
}
async function createPr(branchName, title, body, useDefaultBranch) { async function createPr(branchName, title, body, useDefaultBranch) {
const targetBranch = useDefaultBranch const targetBranch = useDefaultBranch
......
...@@ -14,30 +14,21 @@ async function prAlreadyExisted(config) { ...@@ -14,30 +14,21 @@ async function prAlreadyExisted(config) {
logger.debug('recreateClosed is false'); logger.debug('recreateClosed is false');
// Return if same PR already existed // Return if same PR already existed
// Check for current PR title format // Check for current PR title format
if (await config.api.checkForClosedPr(config.branchName, config.prTitle)) { const pr = config.api.findPr(config.branchName, config.prTitle, 'closed');
if (pr) {
logger.debug('Found closed PR with current title'); logger.debug('Found closed PR with current title');
// this code exists to ignore mistakenly closed PRs which occurred due to a bug // this code exists to ignore mistakenly closed PRs which occurred due to a bug
// TODO: Remove this by end of October 2017 or in v10 // TODO: Remove this by end of October 2017 or in v10
const pr = await config.api.findPr(
config.branchName,
config.prTitle,
'closed'
);
if (pr) {
const closedAt = moment(pr.closed_at); const closedAt = moment(pr.closed_at);
const problemStart = moment('2017-10-15T20:00:00Z'); const problemStart = moment('2017-10-15T20:00:00Z');
const problemStopped = moment('2017-10-16T06:00:00Z'); const problemStopped = moment('2017-10-16T06:00:00Z');
if ( if (problemStart.isBefore(closedAt) && closedAt.isBefore(problemStopped)) {
problemStart.isBefore(closedAt) &&
closedAt.isBefore(problemStopped)
) {
logger.info( logger.info(
{ closedAt, problemStart, problemStopped }, { closedAt, problemStart, problemStopped },
'Ignoring mistakenly closed PR' 'Ignoring mistakenly closed PR'
); );
return false; return false;
} }
}
return true; return true;
} }
// Check for legacy PR title format // Check for legacy PR title format
...@@ -45,7 +36,7 @@ async function prAlreadyExisted(config) { ...@@ -45,7 +36,7 @@ async function prAlreadyExisted(config) {
const legacyPrTitle = config.prTitle const legacyPrTitle = config.prTitle
.replace(/to v(\d+)$/, 'to version $1.x') // Major .replace(/to v(\d+)$/, 'to version $1.x') // Major
.replace(/to v(\d+)/, 'to version $1'); // Non-major .replace(/to v(\d+)/, 'to version $1'); // Non-major
if (await config.api.checkForClosedPr(config.branchName, legacyPrTitle)) { if (await config.api.findPr(config.branchName, legacyPrTitle, 'closed')) {
logger.debug('Found closed PR with legacy title'); logger.debug('Found closed PR with legacy title');
return true; return true;
} }
......
...@@ -987,34 +987,6 @@ describe('api/github', () => { ...@@ -987,34 +987,6 @@ describe('api/github', () => {
expect(pr).toMatchSnapshot(); expect(pr).toMatchSnapshot();
}); });
}); });
describe('checkForClosedPr(branchName, prTitle)', () => {
[
['some-branch', 'foo', true],
['some-branch', 'bar', false],
['some-branch', 'bop', false],
].forEach(([branch, title, expected], i) => {
it(`should return true if a closed PR is found - ${i}`, async () => {
await initRepo('some/repo', 'token');
get.mockImplementationOnce(() => ({
body: [
{ title: 'foo', head: { label: 'theowner:some-branch' } },
{ title: 'bar', head: { label: 'theowner:some-other-branch' } },
{ title: 'baz', head: { label: 'theowner:some-branch' } },
],
}));
const res = await github.checkForClosedPr(branch, title);
expect(res).toBe(expected);
});
});
it(`should return false if error thrown`, async () => {
await initRepo('some/repo', 'token');
get.mockImplementationOnce(() => {
throw new Error('fail');
});
const res = await github.checkForClosedPr('some-branch', 'some-title');
expect(res).toBe(false);
});
});
describe('createPr(branchName, title, body)', () => { describe('createPr(branchName, title, body)', () => {
it('should create and return a PR object', async () => { it('should create and return a PR object', async () => {
await initRepo('some/repo', 'token'); await initRepo('some/repo', 'token');
......
...@@ -541,31 +541,6 @@ describe('api/gitlab', () => { ...@@ -541,31 +541,6 @@ describe('api/gitlab', () => {
expect(pr.number).toBe(2); expect(pr.number).toBe(2);
}); });
}); });
describe('checkForClosedPr(branchName, prTitle)', () => {
it('returns true if pr exists', async () => {
get.mockReturnValueOnce({
body: [
{
source_branch: 'some-branch',
id: 1,
},
{
source_branch: 'some-branch',
id: 2,
},
],
});
const res = await gitlab.checkForClosedPr('some-branch');
expect(res).toBe(true);
});
it('returns false if pr does not exist', async () => {
get.mockReturnValueOnce({
body: [],
});
const res = await gitlab.checkForClosedPr('some-branch');
expect(res).toBe(false);
});
});
describe('createPr(branchName, title, body)', () => { describe('createPr(branchName, title, body)', () => {
it('returns the PR', async () => { it('returns the PR', async () => {
get.post.mockReturnValueOnce({ get.post.mockReturnValueOnce({
......
...@@ -10,7 +10,7 @@ describe('workers/branch/check-existing', () => { ...@@ -10,7 +10,7 @@ describe('workers/branch/check-existing', () => {
beforeEach(() => { beforeEach(() => {
config = { config = {
...defaultConfig, ...defaultConfig,
api: { checkForClosedPr: jest.fn(), findPr: jest.fn() }, api: { findPr: jest.fn() },
logger, logger,
branchName: 'some-branch', branchName: 'some-branch',
prTitle: 'some-title', prTitle: 'some-title',
...@@ -19,26 +19,25 @@ describe('workers/branch/check-existing', () => { ...@@ -19,26 +19,25 @@ describe('workers/branch/check-existing', () => {
it('returns false if recreating closed PRs', async () => { it('returns false if recreating closed PRs', async () => {
config.recreateClosed = true; config.recreateClosed = true;
expect(await prAlreadyExisted(config)).toBe(false); expect(await prAlreadyExisted(config)).toBe(false);
expect(config.api.checkForClosedPr.mock.calls.length).toBe(0); expect(config.api.findPr.mock.calls.length).toBe(0);
}); });
it('returns false if both checks miss', async () => { it('returns false if both checks miss', async () => {
config.recreatedClosed = true; config.recreatedClosed = true;
expect(await prAlreadyExisted(config)).toBe(false); expect(await prAlreadyExisted(config)).toBe(false);
expect(config.api.checkForClosedPr.mock.calls.length).toBe(2); expect(config.api.findPr.mock.calls.length).toBe(2);
}); });
it('returns true if first check hits', async () => { it('returns true if first check hits', async () => {
config.api.checkForClosedPr.mockReturnValueOnce(true); config.api.findPr.mockReturnValueOnce({});
expect(await prAlreadyExisted(config)).toBe(true); expect(await prAlreadyExisted(config)).toBe(true);
expect(config.api.checkForClosedPr.mock.calls.length).toBe(1); expect(config.api.findPr.mock.calls.length).toBe(1);
}); });
it('returns true if second check hits', async () => { it('returns true if second check hits', async () => {
config.api.checkForClosedPr.mockReturnValueOnce(false); config.api.findPr.mockReturnValueOnce(null);
config.api.checkForClosedPr.mockReturnValueOnce(true); config.api.findPr.mockReturnValueOnce({});
expect(await prAlreadyExisted(config)).toBe(true); expect(await prAlreadyExisted(config)).toBe(true);
expect(config.api.checkForClosedPr.mock.calls.length).toBe(2); expect(config.api.findPr.mock.calls.length).toBe(2);
}); });
it('returns false if mistaken', async () => { it('returns false if mistaken', async () => {
config.api.checkForClosedPr.mockReturnValueOnce(true);
config.api.findPr.mockReturnValueOnce({ config.api.findPr.mockReturnValueOnce({
closed_at: '2017-10-15T21:28:07.000Z', closed_at: '2017-10-15T21:28:07.000Z',
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment