feat: retrieve and cache PR list for better performance

This commit is contained in:
Rhys Arkins 2017-10-17 11:01:21 +02:00
parent e310887f36
commit 78cff771af
3 changed files with 73 additions and 139 deletions

View file

@ -30,6 +30,7 @@ module.exports = {
addReviewers, addReviewers,
addLabels, addLabels,
// PR // PR
getPrList,
findPr, findPr,
createPr, createPr,
getPr, getPr,
@ -147,6 +148,7 @@ async function initRepo(repoName, token, endpoint, repoLogger) {
} }
config.repoName = repoName; config.repoName = repoName;
config.fileList = null; config.fileList = null;
config.prList = null;
const platformConfig = {}; const platformConfig = {};
try { try {
const res = await get(`repos/${repoName}`, { const res = await get(`repos/${repoName}`, {
@ -464,22 +466,32 @@ async function addLabels(issueNo, labels) {
// Pull Request // Pull Request
async function getPrList() {
if (!config.prList) {
const res = await get(
`repos/${config.repoName}/pulls?per_page=100&state=all`
);
config.prList = res.body.map(pr => ({
number: pr.number,
branchName: pr.head.ref,
title: pr.title,
state: pr.state,
closed_at: pr.closed_at,
}));
}
return config.prList;
}
async function findPr(branchName, prTitle, state = 'all') { async function findPr(branchName, prTitle, state = 'all') {
logger.debug(`findPr(${branchName}, ${state})`); logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`);
const urlString = `repos/${config.repoName}/pulls?head=${config.owner}:${branchName}&state=${state}`; const prList = await module.exports.getPrList();
logger.debug(`findPr urlString: ${urlString}`); const pr = prList.filter(
const res = await get(urlString); p =>
let pr = null; p.branchName === branchName &&
res.body.forEach(result => { (!prTitle || p.title === prTitle) &&
if (!prTitle || result.title === prTitle) { (state === 'all' || p.state === state)
pr = result; )[0];
if (pr.state === 'closed') { return pr ? { ...pr, isClosed: pr.state === 'closed' } : undefined;
pr.isClosed = true;
}
pr.displayNumber = `Pull Request #${pr.number}`;
}
});
return pr;
} }
// Creates PR and returns PR number // Creates PR and returns PR number

View file

@ -440,106 +440,6 @@ Array [
] ]
`; `;
exports[`api/github findPr(branchName, prTitle, state) should return a PR object 1`] = `
Array [
Array [
"repos/some/repo",
Object {
"headers": Object {
"accept": "application/vnd.github.loki-preview+json",
},
},
],
Array [
"repos/some/repo/git/refs/heads/master",
],
Array [
"repos/some/repo/branches/master/protection/required_status_checks",
Object {
"headers": Object {
"accept": "application/vnd.github.loki-preview+json",
},
},
],
Array [
"repos/some/repo/pulls?head=theowner:master&state=all",
],
]
`;
exports[`api/github findPr(branchName, prTitle, state) should return a PR object 2`] = `
Object {
"displayNumber": "Pull Request #42",
"number": 42,
"state": "open",
"title": "PR Title",
}
`;
exports[`api/github findPr(branchName, prTitle, state) should return null if no PR's are found 1`] = `
Array [
Array [
"repos/some/repo",
Object {
"headers": Object {
"accept": "application/vnd.github.loki-preview+json",
},
},
],
Array [
"repos/some/repo/git/refs/heads/master",
],
Array [
"repos/some/repo/branches/master/protection/required_status_checks",
Object {
"headers": Object {
"accept": "application/vnd.github.loki-preview+json",
},
},
],
Array [
"repos/some/repo/pulls?head=theowner:master&state=all",
],
]
`;
exports[`api/github findPr(branchName, prTitle, state) should set the isClosed attribute of the PR to true if the PR is closed 1`] = `
Array [
Array [
"repos/some/repo",
Object {
"headers": Object {
"accept": "application/vnd.github.loki-preview+json",
},
},
],
Array [
"repos/some/repo/git/refs/heads/master",
],
Array [
"repos/some/repo/branches/master/protection/required_status_checks",
Object {
"headers": Object {
"accept": "application/vnd.github.loki-preview+json",
},
},
],
Array [
"repos/some/repo/pulls?head=theowner:master&state=all",
],
]
`;
exports[`api/github findPr(branchName, prTitle, state) should set the isClosed attribute of the PR to true if the PR is closed 2`] = `
Object {
"displayNumber": "Pull Request #42",
"isClosed": true,
"number": 42,
"state": "closed",
"title": "PR Title",
}
`;
exports[`api/github getAllPrs() maps results to simple array 1`] = ` exports[`api/github getAllPrs() maps results to simple array 1`] = `
Array [ Array [
Object { Object {

View file

@ -959,32 +959,54 @@ describe('api/github', () => {
}); });
}); });
describe('findPr(branchName, prTitle, state)', () => { describe('findPr(branchName, prTitle, state)', () => {
it('should return a PR object', async () => { it('returns true if no title and all state', async () => {
await initRepo('some/repo', 'token'); get.mockReturnValueOnce({
get.mockImplementationOnce(() => ({ body: [
body: [{ title: 'PR Title', state: 'open', number: 42 }], {
})); number: 1,
const pr = await github.findPr('master', 'PR Title'); head: { ref: 'branch-a' },
expect(get.mock.calls).toMatchSnapshot(); title: 'branch a pr',
expect(pr).toMatchSnapshot(); state: 'open',
},
],
});
const res = await github.findPr('branch-a', null);
expect(res).toBeDefined();
}); });
it("should return null if no PR's are found", async () => { it('returns isClosed if closed', async () => {
await initRepo('some/repo', 'token'); get.mockReturnValueOnce({
get.mockImplementationOnce(() => ({ body: [
body: [], {
})); number: 1,
const pr = await github.findPr('master', 'PR Title'); head: { ref: 'branch-a' },
expect(get.mock.calls).toMatchSnapshot(); title: 'branch a pr',
expect(pr).toBe(null); state: 'closed',
closed_at: '2017-01-01',
},
],
});
const res = await github.findPr('branch-a', null);
expect(res.isClosed).toBe(true);
}); });
it('should set the isClosed attribute of the PR to true if the PR is closed', async () => { it('caches pr list', async () => {
await initRepo('some/repo', 'token'); get.mockReturnValueOnce({
get.mockImplementationOnce(() => ({ body: [
body: [{ title: 'PR Title', state: 'closed', number: 42 }], {
})); number: 1,
const pr = await github.findPr('master'); head: { ref: 'branch-a' },
expect(get.mock.calls).toMatchSnapshot(); title: 'branch a pr',
expect(pr).toMatchSnapshot(); state: 'open',
},
],
});
let res = await github.findPr('branch-a', null);
expect(res).toBeDefined();
res = await github.findPr('branch-a', 'branch a pr');
expect(res).toBeDefined();
res = await github.findPr('branch-a', 'branch a pr', 'open');
expect(res).toBeDefined();
res = await github.findPr('branch-b');
expect(res).not.toBeDefined();
}); });
}); });
describe('createPr(branchName, title, body)', () => { describe('createPr(branchName, title, body)', () => {