Revert "fix: Revert "feat(github): use graphql instead of rest api for issue list (#3808)""

This reverts commit dc194219d9.
This commit is contained in:
Rhys Arkins 2019-05-30 16:33:34 +02:00
parent a91e3d39f3
commit 2243b54f60
2 changed files with 299 additions and 134 deletions

View file

@ -631,27 +631,75 @@ async function setBranchStatus(
// Issue // Issue
/* istanbul ignore next */
async function getGraphqlIssues(afterCursor = null) {
const url = 'graphql';
const headers = {
accept: 'application/vnd.github.merge-info-preview+json',
};
// prettier-ignore
const query = `
query {
repository(owner: "${config.repositoryOwner}", name: "${config.repositoryName}") {
issues(first: 100, after:${afterCursor}, orderBy: {field: UPDATED_AT, direction: DESC}, filterBy: {createdBy: "${config.renovateUsername}"}) {
pageInfo {
startCursor
hasNextPage
}
nodes {
number
state
title
body
}
}
}
}
`;
const options = {
headers,
body: JSON.stringify({ query }),
json: false,
};
try {
const res = JSON.parse((await get.post(url, options)).body);
if (!res.data) {
logger.info({ query, res }, 'No graphql res.data');
return [false, [], null];
}
const cursor = res.data.repository.issues.pageInfo.hasNextPage
? res.data.repository.issues.pageInfo.startCursor
: null;
return [true, res.data.repository.issues.nodes, cursor];
} catch (err) {
logger.warn({ query, err }, 'getGraphqlIssues error');
return [false, [], null];
}
}
async function getIssueList() { async function getIssueList() {
if (!config.issueList) { if (!config.issueList) {
logger.debug('Retrieving issueList'); logger.debug('Retrieving issueList');
const res = await get(
`repos/${config.parentRepo || config.repository}/issues?creator=${ let [success, issues, cursor] = await getGraphqlIssues();
config.renovateUsername config.issueList = [];
}&state=all&per_page=100&sort=created&direction=asc`, while (success) {
{ paginate: 'all', useCache: false } for (const issue of issues) {
); issue.state = issue.state.toLowerCase();
// istanbul ignore if config.issueList.push(issue);
if (!is.array(res.body)) { }
logger.warn({ responseBody: res.body }, 'Could not retrieve issue list');
return []; if (!cursor) {
break;
}
// istanbul ignore next
[success, issues, cursor] = await getGraphqlIssues(cursor);
} }
config.issueList = res.body
.filter(issue => !issue.pull_request)
.map(i => ({
number: i.number,
state: i.state,
title: i.title,
}));
logger.debug('Retrieved ' + config.issueList.length + ' issues'); logger.debug('Retrieved ' + config.issueList.length + ' issues');
} }
return config.issueList; return config.issueList;

View file

@ -716,20 +716,33 @@ describe('platform/github', () => {
expect(res).toBeNull(); expect(res).toBeNull();
}); });
it('finds issue', async () => { it('finds issue', async () => {
get.mockReturnValueOnce({ get.post.mockImplementationOnce(() => ({
body: [ body: JSON.stringify({
{ data: {
number: 1, repository: {
title: 'title-1', issues: {
state: 'open', pageInfo: {
startCursor: null,
hasNextPage: false,
endCursor: null,
}, },
nodes: [
{ {
number: 2, number: 2,
title: 'title-2',
state: 'open', state: 'open',
title: 'title-2',
},
{
number: 1,
state: 'open',
title: 'title-1',
}, },
], ],
}); },
},
},
}),
}));
get.mockReturnValueOnce({ body: { body: 'new-content' } }); get.mockReturnValueOnce({ body: { body: 'new-content' } });
const res = await github.findIssue('title-2'); const res = await github.findIssue('title-2');
expect(res).not.toBeNull(); expect(res).not.toBeNull();
@ -737,137 +750,228 @@ describe('platform/github', () => {
}); });
describe('ensureIssue()', () => { describe('ensureIssue()', () => {
it('creates issue', async () => { it('creates issue', async () => {
get.mockImplementationOnce(() => ({ get.post.mockImplementationOnce(() => ({
body: [ body: JSON.stringify({
{ data: {
number: 1, repository: {
title: 'title-1', issues: {
state: 'open', pageInfo: {
startCursor: null,
hasNextPage: false,
endCursor: null,
}, },
nodes: [
{ {
number: 2, number: 2,
title: 'title-2',
state: 'open', state: 'open',
title: 'title-2',
},
{
number: 1,
state: 'open',
title: 'title-1',
}, },
], ],
},
},
},
}),
})); }));
const res = await github.ensureIssue('new-title', 'new-content'); const res = await github.ensureIssue('new-title', 'new-content');
expect(res).toEqual('created'); expect(res).toEqual('created');
}); });
it('creates issue if not ensuring only once', async () => { it('creates issue if not ensuring only once', async () => {
get.mockImplementationOnce(() => ({ get.post.mockImplementationOnce(() => ({
body: [ body: JSON.stringify({
{ data: {
number: 1, repository: {
title: 'title-1', issues: {
state: 'closed', pageInfo: {
startCursor: null,
hasNextPage: false,
endCursor: null,
}, },
nodes: [
{ {
number: 2, number: 2,
title: 'title-2',
state: 'open', state: 'open',
title: 'title-2',
},
{
number: 1,
state: 'closed',
title: 'title-1',
}, },
], ],
},
},
},
}),
})); }));
const res = await github.ensureIssue('title-1', 'new-content'); const res = await github.ensureIssue('title-1', 'new-content');
expect(res).toBeNull(); expect(res).toBeNull();
}); });
it('does not create issue if ensuring only once', async () => { it('does not create issue if ensuring only once', async () => {
get.mockImplementationOnce(() => ({ get.post.mockImplementationOnce(() => ({
body: [ body: JSON.stringify({
{ data: {
number: 1, repository: {
title: 'title-1', issues: {
state: 'closed', pageInfo: {
startCursor: null,
hasNextPage: false,
endCursor: null,
}, },
nodes: [
{ {
number: 2, number: 2,
title: 'title-2',
state: 'open', state: 'open',
title: 'title-2',
},
{
number: 1,
state: 'closed',
title: 'title-1',
}, },
], ],
},
},
},
}),
})); }));
const once = true; const once = true;
const res = await github.ensureIssue('title-1', 'new-content', once); const res = await github.ensureIssue('title-1', 'new-content', once);
expect(res).toBeNull(); expect(res).toBeNull();
}); });
it('closes others if ensuring only once', async () => { it('closes others if ensuring only once', async () => {
get.mockImplementationOnce(() => ({ get.post.mockImplementationOnce(() => ({
body: [ body: JSON.stringify({
data: {
repository: {
issues: {
pageInfo: {
startCursor: null,
hasNextPage: false,
endCursor: null,
},
nodes: [
{ {
number: 1, number: 3,
state: 'open',
title: 'title-1', title: 'title-1',
state: 'closed',
}, },
{ {
number: 2, number: 2,
title: 'title-2',
state: 'open', state: 'open',
title: 'title-2',
}, },
{ {
number: 3, number: 1,
state: 'closed',
title: 'title-1', title: 'title-1',
state: 'open',
}, },
], ],
},
},
},
}),
})); }));
const once = true; const once = true;
const res = await github.ensureIssue('title-1', 'new-content', once); const res = await github.ensureIssue('title-1', 'new-content', once);
expect(res).toBeNull(); expect(res).toBeNull();
}); });
it('updates issue', async () => { it('updates issue', async () => {
get.mockReturnValueOnce({ get.post.mockImplementationOnce(() => ({
body: [ body: JSON.stringify({
{ data: {
number: 1, repository: {
title: 'title-1', issues: {
state: 'open', pageInfo: {
startCursor: null,
hasNextPage: false,
endCursor: null,
}, },
nodes: [
{ {
number: 2, number: 2,
title: 'title-2',
state: 'open', state: 'open',
title: 'title-2',
},
{
number: 1,
state: 'open',
title: 'title-1',
}, },
], ],
}); },
},
},
}),
}));
get.mockReturnValueOnce({ body: { body: 'new-content' } }); get.mockReturnValueOnce({ body: { body: 'new-content' } });
const res = await github.ensureIssue('title-2', 'newer-content'); const res = await github.ensureIssue('title-2', 'newer-content');
expect(res).toEqual('updated'); expect(res).toEqual('updated');
}); });
it('skips update if unchanged', async () => { it('skips update if unchanged', async () => {
get.mockReturnValueOnce({ get.post.mockImplementationOnce(() => ({
body: [ body: JSON.stringify({
{ data: {
number: 1, repository: {
title: 'title-1', issues: {
state: 'open', pageInfo: {
startCursor: null,
hasNextPage: false,
endCursor: null,
}, },
nodes: [
{ {
number: 2, number: 2,
title: 'title-2',
state: 'open', state: 'open',
title: 'title-2',
},
{
number: 1,
state: 'open',
title: 'title-1',
}, },
], ],
}); },
},
},
}),
}));
get.mockReturnValueOnce({ body: { body: 'newer-content' } }); get.mockReturnValueOnce({ body: { body: 'newer-content' } });
const res = await github.ensureIssue('title-2', 'newer-content'); const res = await github.ensureIssue('title-2', 'newer-content');
expect(res).toBeNull(); expect(res).toBeNull();
}); });
it('deletes if duplicate', async () => { it('deletes if duplicate', async () => {
get.mockReturnValueOnce({ get.post.mockImplementationOnce(() => ({
body: [ body: JSON.stringify({
{ data: {
number: 1, repository: {
title: 'title-1', issues: {
state: 'open', pageInfo: {
startCursor: null,
hasNextPage: false,
endCursor: null,
}, },
nodes: [
{ {
number: 2, number: 2,
title: 'title-1',
state: 'open', state: 'open',
title: 'title-1',
},
{
number: 1,
state: 'open',
title: 'title-1',
}, },
], ],
}); },
},
},
}),
}));
get.mockReturnValueOnce({ body: { body: 'newer-content' } }); get.mockReturnValueOnce({ body: { body: 'newer-content' } });
const res = await github.ensureIssue('title-1', 'newer-content'); const res = await github.ensureIssue('title-1', 'newer-content');
expect(res).toBeNull(); expect(res).toBeNull();
@ -875,19 +979,32 @@ describe('platform/github', () => {
}); });
describe('ensureIssueClosing()', () => { describe('ensureIssueClosing()', () => {
it('closes issue', async () => { it('closes issue', async () => {
get.mockImplementationOnce(() => ({ get.post.mockImplementationOnce(() => ({
body: [ body: JSON.stringify({
{ data: {
number: 1, repository: {
title: 'title-1', issues: {
state: 'open', pageInfo: {
startCursor: null,
hasNextPage: false,
endCursor: null,
}, },
nodes: [
{ {
number: 2, number: 2,
title: 'title-2',
state: 'open', state: 'open',
title: 'title-2',
},
{
number: 1,
state: 'open',
title: 'title-1',
}, },
], ],
},
},
},
}),
})); }));
await github.ensureIssueClosing('title-2'); await github.ensureIssueClosing('title-2');
}); });