Skip to content

Commit

Permalink
fix: Revert "feat(gitlab): Improve performance for projects with a lo…
Browse files Browse the repository at this point in the history
…t of branches (renovatebot#3936)"

This reverts commit 9779d77.
  • Loading branch information
rarkins committed Jul 9, 2019
1 parent 9940590 commit cbf695b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 58 deletions.
1 change: 0 additions & 1 deletion lib/platform/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export interface IGotApiOptions {
useCache?: boolean;
hostType?: string;
body?: any;
query?: string | { [key: string]: string | number } | URLSearchParams;
}

export interface IGotApi<TOptions extends object = any> {
Expand Down
55 changes: 21 additions & 34 deletions lib/platform/gitlab/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import URL, { URLSearchParams } from 'url';
import URL from 'url';
import is from '@sindresorhus/is';

import api from './gl-got-wrapper';
Expand All @@ -22,8 +22,6 @@ const defaults = {
endpoint: 'https://gitlab.com/api/v4/',
};

let authorId: number;

export async function initPlatform({
endpoint,
token,
Expand All @@ -44,9 +42,7 @@ export async function initPlatform({
logger.info('Using default GitLab endpoint: ' + res.endpoint);
}
try {
const user = (await api.get(`user`, { token })).body;
res.gitAuthor = user.email;
authorId = user.id;
res.gitAuthor = (await api.get(`user`, { token })).body.email;
} catch (err) {
logger.info(
{ err },
Expand Down Expand Up @@ -207,13 +203,8 @@ export async function getBranchPr(branchName: string) {
if (!(await branchExists(branchName))) {
return null;
}
const query = new URLSearchParams({
per_page: '100',
state: 'opened',
source_branch: branchName,
});
const urlString = `projects/${config.repository}/merge_requests`;
const res = await api.get(urlString, { query, paginate: true });
const urlString = `projects/${config.repository}/merge_requests?state=opened&per_page=100`;
const res = await api.get(urlString, { paginate: true });
logger.debug(`Got res with ${res.body.length} results`);
let pr: any = null;
res.body.forEach((result: { source_branch: string }) => {
Expand Down Expand Up @@ -599,29 +590,25 @@ export async function ensureCommentRemoval(issueNo: number, topic: string) {
}
}

const mapPullRequests = (pr: {
iid: number;
source_branch: string;
title: string;
state: string;
created_at: string;
}) => ({
number: pr.iid,
branchName: pr.source_branch,
title: pr.title,
state: pr.state === 'opened' ? 'open' : pr.state,
createdAt: pr.created_at,
});

export async function getPrList() {
if (!config.prList) {
const query = new URLSearchParams({
per_page: '100',
author_id: `${authorId}`,
});
const urlString = `projects/${config.repository}/merge_requests`;
const res = await api.get(urlString, { query, paginate: true });
config.prList = res.body.map(mapPullRequests);
const urlString = `projects/${config.repository}/merge_requests?per_page=100`;
const res = await api.get(urlString, { paginate: true });
config.prList = res.body.map(
(pr: {
iid: number;
source_branch: string;
title: string;
state: string;
created_at: string;
}) => ({
number: pr.iid,
branchName: pr.source_branch,
title: pr.title,
state: pr.state === 'opened' ? 'open' : pr.state,
createdAt: pr.created_at,
})
);
}
return config.prList;
}
Expand Down
36 changes: 13 additions & 23 deletions test/platform/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,13 @@ describe('platform/gitlab', () => {
// branchExists
body: [],
} as any);
const pr = await gitlab.getBranchPr('some-branch');
const pr = await gitlab.getBranchPr('somebranch');
expect(pr).toBeNull();
});
it('should return the PR object', async () => {
await initRepo();
api.get.mockReturnValueOnce({
body: [{ iid: 91, source_branch: 'some-branch', state: 'opened' }],
body: [{ number: 91, source_branch: 'somebranch' }],
} as any);
api.get.mockReturnValueOnce({
body: {
Expand All @@ -284,7 +284,7 @@ describe('platform/gitlab', () => {
api.get.mockReturnValueOnce({ body: [] } as any); // get branch commit
api.get.mockReturnValueOnce({ body: [{ status: 'success' }] } as any); // get commit statuses
api.get.mockReturnValueOnce({ body: 'foo' } as any);
const pr = await gitlab.getBranchPr('some-branch');
const pr = await gitlab.getBranchPr('somebranch');
expect(pr).toMatchSnapshot();
});
});
Expand Down Expand Up @@ -633,7 +633,7 @@ describe('platform/gitlab', () => {
api.get.mockResolvedValueOnce({
body: [
{
iid: 1,
number: 1,
source_branch: 'branch-a',
title: 'branch a pr',
state: 'opened',
Expand All @@ -647,7 +647,7 @@ describe('platform/gitlab', () => {
api.get.mockReturnValueOnce({
body: [
{
iid: 1,
number: 1,
source_branch: 'branch-a',
title: 'branch a pr',
state: 'merged',
Expand All @@ -657,35 +657,25 @@ describe('platform/gitlab', () => {
const res = await gitlab.findPr('branch-a', null, '!open');
expect(res).toBeDefined();
});

it('returns true if open and with title', async () => {
it('caches pr list', async () => {
api.get.mockReturnValueOnce({
body: [
{
iid: 1,
number: 1,
source_branch: 'branch-a',
title: 'branch a pr',
state: 'opened',
},
],
} as any);
const res = await gitlab.findPr('branch-a', 'branch a pr', 'open');
let res = await gitlab.findPr('branch-a', null);
expect(res).toBeDefined();
});

it('returns true with title', async () => {
api.get.mockReturnValueOnce({
body: [
{
iid: 1,
source_branch: 'branch-a',
title: 'branch a pr',
state: 'opened',
},
],
} as any);
const res = await gitlab.findPr('branch-a', 'branch a pr');
res = await gitlab.findPr('branch-a', 'branch a pr');
expect(res).toBeDefined();
res = await gitlab.findPr('branch-a', 'branch a pr', 'open');
expect(res).toBeDefined();
res = await gitlab.findPr('branch-b');
expect(res).not.toBeDefined();
});
});
describe('createPr(branchName, title, body)', () => {
Expand Down

0 comments on commit cbf695b

Please sign in to comment.