Revert "fix: ensureIssue logic (#2957)"

This reverts commit 83b4bead48.
This commit is contained in:
Rhys Arkins 2018-12-16 17:17:33 +01:00
parent 7d4cd7ca60
commit 0956c058d0
2 changed files with 29 additions and 61 deletions

View file

@ -602,7 +602,7 @@ async function getIssueList() {
logger.debug('Retrieving issueList'); logger.debug('Retrieving issueList');
const res = await get( const res = await get(
`repos/${config.parentRepo || `repos/${config.parentRepo ||
config.repository}/issues?filter=created&state=all&per_page=100&sort=created&direction=asc`, config.repository}/issues?filter=created&state=all&per_page=100&sort=updated`,
{ paginate: true, useCache: false } { paginate: true, useCache: false }
); );
// istanbul ignore if // istanbul ignore if
@ -647,46 +647,37 @@ async function ensureIssue(title, body, once = false) {
logger.debug(`ensureIssue()`); logger.debug(`ensureIssue()`);
try { try {
const issueList = await getIssueList(); const issueList = await getIssueList();
const issues = issueList.filter(i => i.title === title); const issues = issueList.filter(i => i.title === title).reverse();
if (issues.length) { if (issues.length) {
if (once && issues[0].state === 'closed') { if (issues.length > 1) {
// Close all other matching issues and return
for (const issue of issues.slice(1)) { for (const issue of issues.slice(1)) {
if (issue.state === 'open') { if (issue.state === 'open') {
logger.warn('Closing duplicate issue ' + issue.number); logger.warn('Closing duplicate issue ' + issue.number);
await closeIssue(issue.number); await closeIssue(issue.number);
} }
} }
}
const [issue] = issues;
if (once && issue.state === 'closed') {
logger.debug('Issue is closed - skipping');
return null; return null;
} }
const issue =
issues.find(i => i.state === 'open') || issues[issues.length - 1];
logger.info({ issue });
for (const i of issues) {
if (i.state === 'open' && i.number !== issue.number) {
logger.warn('Closing duplicate issue ' + i.number);
await closeIssue(i.number);
}
}
const issueBody = (await get( const issueBody = (await get(
`repos/${config.parentRepo || config.repository}/issues/${issue.number}` `repos/${config.parentRepo || config.repository}/issues/${issue.number}`
)).body.body; )).body.body;
if (issueBody === body && issue.state === 'open') { if (issueBody !== body) {
logger.info('Issue is open and up to date - nothing to do'); logger.info('Issue updated');
return null;
}
logger.info('Patching issue');
await get.patch( await get.patch(
`repos/${config.parentRepo || config.repository}/issues/${ `repos/${config.parentRepo || config.repository}/issues/${
issue.number issue.number
}`, }`,
{ {
body: { body, state: 'open' }, body: { body },
} }
); );
logger.info('Issue updated');
return 'updated'; return 'updated';
} }
} else {
await get.post(`repos/${config.parentRepo || config.repository}/issues`, { await get.post(`repos/${config.parentRepo || config.repository}/issues`, {
body: { body: {
title, title,
@ -697,6 +688,7 @@ async function ensureIssue(title, body, once = false) {
// reset issueList so that it will be fetched again as-needed // reset issueList so that it will be fetched again as-needed
delete config.issueList; delete config.issueList;
return 'created'; return 'created';
}
} catch (err) /* istanbul ignore next */ { } catch (err) /* istanbul ignore next */ {
if (err.message.startsWith('Issues are disabled for this repo')) { if (err.message.startsWith('Issues are disabled for this repo')) {
logger.info( logger.info(

View file

@ -1077,30 +1077,6 @@ describe('platform/github', () => {
const res = await github.ensureIssue('title-1', 'new-content', once); const res = await github.ensureIssue('title-1', 'new-content', once);
expect(res).toEqual(null); expect(res).toEqual(null);
}); });
it('closes others if ensuring only once', async () => {
get.mockImplementationOnce(() => ({
body: [
{
number: 1,
title: 'title-1',
state: 'closed',
},
{
number: 2,
title: 'title-2',
state: 'open',
},
{
number: 3,
title: 'title-1',
state: 'open',
},
],
}));
const once = true;
const res = await github.ensureIssue('title-1', 'new-content', once);
expect(res).toEqual(null);
});
it('updates issue', async () => { it('updates issue', async () => {
get.mockReturnValueOnce({ get.mockReturnValueOnce({
body: [ body: [