Skip to content

Commit

Permalink
fix(platform): Filter out non-Renovate PRs with same branch (#5211)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov authored and rarkins committed Jan 27, 2020
1 parent a761e95 commit 514fdbd
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 99 deletions.
6 changes: 5 additions & 1 deletion lib/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ interface LocalRepoConfig {
storage: GitStorage;
parentRepo: string;
baseCommitSHA: string | null;
forkMode?: boolean;
forkToken?: string;
closedPrList: PrList | null;
openPrList: PrList | null;
Expand Down Expand Up @@ -367,6 +368,8 @@ export async function initRepo({
config.prList = null;
config.openPrList = null;
config.closedPrList = null;

config.forkMode = !!forkMode;
if (forkMode) {
logger.info('Bot is in forkMode');
config.forkToken = forkToken;
Expand Down Expand Up @@ -1015,7 +1018,8 @@ export async function findPr({
p =>
p.branchName === branchName &&
(!prTitle || p.title === prTitle) &&
matchesState(p.state, state)
matchesState(p.state, state) &&
(config.forkMode || config.repository === p.sourceRepo) // #5188
);
if (pr) {
logger.debug(`Found PR #${pr.number}`);
Expand Down
57 changes: 57 additions & 0 deletions test/platform/github/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ Object {
"displayNumber": "Pull Request #91",
"head": Object {
"ref": "somebranch",
"repo": Object {
"full_name": "some/repo",
},
},
"isModified": false,
"isStale": true,
Expand All @@ -184,6 +187,60 @@ Object {
}
`;

exports[`platform/github getBranchPr(branchName) should return the PR object in fork mode 1`] = `
Array [
Array [
"repos/some/repo",
],
Array [
"repos/some/repo/git/refs/heads/master",
],
Array [
"user/repos?per_page=100",
Object {
"paginate": true,
"token": "abc123",
},
],
Array [
"repos/some/repo/pulls?per_page=100&state=all",
Object {
"paginate": true,
},
],
Array [
"repos/some/repo/pulls/90",
],
Array [
"repos/forked/repo/git/refs/heads/master",
],
]
`;

exports[`platform/github getBranchPr(branchName) should return the PR object in fork mode 2`] = `
Object {
"additions": 1,
"base": Object {
"sha": "1234",
},
"branchName": "somebranch",
"commits": 1,
"deletions": 1,
"displayNumber": "Pull Request #90",
"head": Object {
"ref": "somebranch",
"repo": Object {
"full_name": "other/repo",
},
},
"isModified": false,
"isStale": true,
"number": 90,
"sha": undefined,
"state": "open",
}
`;

exports[`platform/github getPr(prNo) should return PR from closed graphql result 1`] = `
Object {
"body": "dummy body",
Expand Down
223 changes: 125 additions & 98 deletions test/platform/github/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs-extra';
import { GotApi, GotResponse } from '../../../lib/platform/common';
import { GotApi, GotResponse, RepoConfig } from '../../../lib/platform/common';
import {
REPOSITORY_DISABLED,
REPOSITORY_NOT_FOUND,
Expand Down Expand Up @@ -179,7 +179,11 @@ describe('platform/github', () => {
});
});

function initRepo(args: { repository: string; token?: string }) {
function initRepo(args: {
repository: string;
token?: string;
forkMode?: boolean;
}) {
// repo info
api.get.mockImplementationOnce(
() =>
Expand All @@ -204,6 +208,59 @@ describe('platform/github', () => {
} as any);
}

function forkInitRepo(args: any, repo?: string): Promise<RepoConfig> {
// repo info
api.get.mockImplementationOnce(
() =>
({
body: {
owner: {
login: 'theowner',
},
default_branch: 'master',
allow_rebase_merge: true,
allow_squash_merge: true,
allow_merge_commit: true,
},
} as any)
);
// api.getBranchCommit
api.get.mockImplementationOnce(
() =>
({
body: {
object: {
sha: '1234',
},
},
} as any)
);
// api.getRepos
api.get.mockImplementationOnce(
() =>
({
body: repo
? [
{
full_name: repo,
},
]
: [],
} as any)
);
// api.getBranchCommit
api.post.mockImplementationOnce(() => {
return {
body: repo
? {
full_name: repo,
}
: {},
} as any;
});
return github.initRepo(args);
}

describe('initRepo', () => {
it('should throw err if disabled in renovate.json', async () => {
// repo info
Expand Down Expand Up @@ -257,107 +314,20 @@ describe('platform/github', () => {
expect(config).toMatchSnapshot();
});
it('should forks when forkMode', async () => {
function forkInitRepo(args: any) {
// repo info
api.get.mockImplementationOnce(
() =>
({
body: {
owner: {
login: 'theowner',
},
default_branch: 'master',
allow_rebase_merge: true,
allow_squash_merge: true,
allow_merge_commit: true,
},
} as any)
);
// api.getBranchCommit
api.get.mockImplementationOnce(
() =>
({
body: {
object: {
sha: '1234',
},
},
} as any)
);
// api.getRepos
api.get.mockImplementationOnce(
() =>
({
body: [],
} as any)
);
// api.getBranchCommit
api.post.mockImplementationOnce(
() =>
({
body: {},
} as any)
);
return github.initRepo(args);
}
const config = await forkInitRepo({
repository: 'some/repo',
forkMode: true,
});
expect(config).toMatchSnapshot();
});
it('should update fork when forkMode', async () => {
function forkInitRepo(args: any) {
// repo info
api.get.mockImplementationOnce(
() =>
({
body: {
owner: {
login: 'theowner',
},
default_branch: 'master',
allow_rebase_merge: true,
allow_squash_merge: true,
allow_merge_commit: true,
},
} as any)
);
// api.getBranchCommit
api.get.mockImplementationOnce(
() =>
({
body: {
object: {
sha: '1234',
},
},
} as any)
);
// api.getRepos
api.get.mockImplementationOnce(
() =>
({
body: [
{
full_name: 'forked_repo',
},
],
} as any)
);
// fork
api.post.mockImplementationOnce(
() =>
({
body: { full_name: 'forked_repo' },
} as any)
);
return github.initRepo(args);
}
const config = await forkInitRepo({
repository: 'some/repo',
forkMode: true,
});
const config = await forkInitRepo(
{
repository: 'some/repo',
forkMode: true,
},
'forked_repo'
);
expect(config).toMatchSnapshot();
});
it('should squash', async () => {
Expand Down Expand Up @@ -555,7 +525,18 @@ describe('platform/github', () => {
api.get.mockImplementationOnce(
() =>
({
body: [{ number: 91, head: { ref: 'somebranch' }, state: 'open' }],
body: [
{
number: 90,
head: { ref: 'somebranch', repo: { full_name: 'other/repo' } },
state: 'open',
},
{
number: 91,
head: { ref: 'somebranch', repo: { full_name: 'some/repo' } },
state: 'open',
},
],
} as any)
);
api.get.mockImplementationOnce(
Expand All @@ -569,7 +550,53 @@ describe('platform/github', () => {
base: {
sha: '1234',
},
head: { ref: 'somebranch' },
head: { ref: 'somebranch', repo: { full_name: 'some/repo' } },
state: 'open',
},
} as any)
);
api.get.mockResolvedValue({ body: { object: { sha: '12345' } } } as any);
const pr = await github.getBranchPr('somebranch');
expect(api.get.mock.calls).toMatchSnapshot();
expect(pr).toMatchSnapshot();
});
it('should return the PR object in fork mode', async () => {
await forkInitRepo(
{
repository: 'some/repo',
forkMode: true,
},
'forked/repo'
);
api.get.mockImplementationOnce(
() =>
({
body: [
{
number: 90,
head: { ref: 'somebranch', repo: { full_name: 'other/repo' } },
state: 'open',
},
{
number: 91,
head: { ref: 'somebranch', repo: { full_name: 'some/repo' } },
state: 'open',
},
],
} as any)
);
api.get.mockImplementationOnce(
() =>
({
body: {
number: 90,
additions: 1,
deletions: 1,
commits: 1,
base: {
sha: '1234',
},
head: { ref: 'somebranch', repo: { full_name: 'other/repo' } },
state: 'open',
},
} as any)
Expand Down

0 comments on commit 514fdbd

Please sign in to comment.