diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts index 314c30c3dd06ec83481b448c7f1e90ab8f72492a..79cac92d967c52bb233be105a799d22c7c339800 100644 --- a/lib/platform/github/index.ts +++ b/lib/platform/github/index.ts @@ -75,6 +75,7 @@ interface LocalRepoConfig { storage: GitStorage; parentRepo: string; baseCommitSHA: string | null; + forkMode?: boolean; forkToken?: string; closedPrList: PrList | null; openPrList: PrList | null; @@ -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; @@ -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}`); diff --git a/test/platform/github/__snapshots__/index.spec.ts.snap b/test/platform/github/__snapshots__/index.spec.ts.snap index cbf78d72c5c3450a1263cb53ed099f6b05856c28..15d00a595cb00601d2d719d40cd9b9487fff54b2 100644 --- a/test/platform/github/__snapshots__/index.spec.ts.snap +++ b/test/platform/github/__snapshots__/index.spec.ts.snap @@ -175,6 +175,9 @@ Object { "displayNumber": "Pull Request #91", "head": Object { "ref": "somebranch", + "repo": Object { + "full_name": "some/repo", + }, }, "isModified": false, "isStale": true, @@ -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", diff --git a/test/platform/github/index.spec.ts b/test/platform/github/index.spec.ts index d8976f711e15aeb18195e303d1595d32114d27d2..66557f198aa5d1ea25b282b0ba90edc42d8abc6b 100644 --- a/test/platform/github/index.spec.ts +++ b/test/platform/github/index.spec.ts @@ -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, @@ -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( () => @@ -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 @@ -257,49 +314,6 @@ 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, @@ -307,57 +321,13 @@ describe('platform/github', () => { 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 () => { @@ -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( @@ -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)