diff --git a/lib/platform/github/index.js b/lib/platform/github/index.js index b9bd1bc3584b6fb1a7b457bbfa034990b56ae7d0..20027f44ff011ff01eab16fb6014dab015087313 100644 --- a/lib/platform/github/index.js +++ b/lib/platform/github/index.js @@ -91,7 +91,6 @@ async function initRepo(repoName, token, endpoint) { config.defaultBranch = res.body.default_branch; config.baseBranch = config.defaultBranch; logger.debug(`${repoName} default branch = ${config.baseBranch}`); - config.baseCommitSHA = await getBranchCommit(config.baseBranch); if (res.body.allow_rebase_merge) { config.mergeMethod = 'rebase'; } else if (res.body.allow_squash_merge) { @@ -132,6 +131,13 @@ async function initRepo(repoName, token, endpoint) { return platformConfig; } +async function getBaseCommitSHA() { + if (!config.baseCommitSHA) { + config.baseCommitSHA = await getBranchCommit(config.baseBranch); + } + return config.baseCommitSHA; +} + async function getBranchProtection(branchName) { const res = await get( `repos/${config.repoName}/branches/${ @@ -146,11 +152,11 @@ async function getBranchProtection(branchName) { return res.body; } -async function setBaseBranch(branchName) { +function setBaseBranch(branchName) { if (branchName) { logger.debug(`Setting baseBranch to ${branchName}`); config.baseBranch = branchName; - config.baseCommitSHA = await getBranchCommit(config.baseBranch); + delete config.baseCommitSHA; } } @@ -243,7 +249,7 @@ async function isBranchStale(branchName) { const parentSha = commitDetails.parents[0].sha; logger.debug(`parentSha=${parentSha}`); // Return true if the SHAs don't match - return parentSha !== config.baseCommitSHA; + return parentSha !== (await getBaseCommitSHA()); } // Returns the Pull Request for a branch. Null if not exists. @@ -344,7 +350,7 @@ async function mergeBranch(branchName, mergeType) { throw new Error(`Unsupported branch merge type: ${mergeType}`); } // Update base commit - config.baseCommitSHA = await getBranchCommit(config.baseBranch); + delete config.baseCommitSHA; // Delete branch await deleteBranch(branchName); } @@ -569,7 +575,7 @@ async function getPr(prNo) { pr.canRebase = true; } } - if (!pr.base || pr.base.sha !== config.baseCommitSHA) { + if (!pr.base || pr.base.sha !== (await getBaseCommitSHA())) { pr.isStale = true; } } @@ -647,7 +653,7 @@ async function mergePr(prNo, branchName) { } logger.info('Automerging succeeded'); // Update base branch SHA - config.baseCommitSHA = await getBranchCommit(config.baseBranch); + delete config.baseCommitSHA; // Delete branch await deleteBranch(branchName); return true; @@ -711,11 +717,12 @@ async function commitFilesToBranch( // Internal branch operations // Creates a new branch with provided commit -async function createBranch(branchName, commit = config.baseCommitSHA) { +async function createBranch(branchName, commit) { + const sha = commit || (await getBaseCommitSHA()); await get.post(`repos/${config.repoName}/git/refs`, { body: { ref: `refs/heads/${branchName}`, - sha: commit, + sha, }, }); } diff --git a/test/platform/github/__snapshots__/index.spec.js.snap b/test/platform/github/__snapshots__/index.spec.js.snap index 7f6cd6010ea9c260b413b7c7460315aefce605b5..c48888d881838d3805a4304ea497fd10bd420b9f 100644 --- a/test/platform/github/__snapshots__/index.spec.js.snap +++ b/test/platform/github/__snapshots__/index.spec.js.snap @@ -46,9 +46,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -73,9 +70,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -100,9 +94,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -127,9 +118,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -154,9 +142,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -181,9 +166,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -208,9 +190,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -294,9 +273,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -479,9 +455,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -518,9 +491,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -545,9 +515,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -572,9 +539,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -753,9 +717,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -785,9 +746,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -817,9 +775,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -881,9 +836,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -892,9 +844,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], ] `; @@ -934,9 +883,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -948,9 +894,6 @@ Array [ Array [ "repos/some/repo/git/refs/heads/thebranchname", ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], ] `; @@ -991,9 +934,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -1037,9 +977,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -1085,9 +1022,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -1117,9 +1051,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/master", - ], Array [ "repos/some/repo/branches/master/protection/required_status_checks", Object { @@ -1128,9 +1059,6 @@ Array [ }, }, ], - Array [ - "repos/some/repo/git/refs/heads/some-branch", - ], ] `; diff --git a/test/platform/github/index.spec.js b/test/platform/github/index.spec.js index 8a8ce7541bea6133efda937e51f74b64b294cf81..cab7d68745d4a59b5ac4648189b28b276f10db81 100644 --- a/test/platform/github/index.spec.js +++ b/test/platform/github/index.spec.js @@ -63,14 +63,6 @@ describe('platform/github', () => { allow_merge_commit: true, }, })); - // getBranchCommit - get.mockImplementationOnce(() => ({ - body: { - object: { - sha: '1234', - }, - }, - })); // getBranchProtection get.mockImplementationOnce(() => ({ body: { @@ -127,14 +119,6 @@ describe('platform/github', () => { allow_merge_commit: true, }, })); - // getBranchCommit - get.mockImplementationOnce(() => ({ - body: { - object: { - sha: '1234', - }, - }, - })); // getBranchProtection get.mockImplementationOnce(() => ({ body: { @@ -160,14 +144,6 @@ describe('platform/github', () => { allow_merge_commit: true, }, })); - // getBranchCommit - get.mockImplementationOnce(() => ({ - body: { - object: { - sha: '1234', - }, - }, - })); // getBranchProtection get.mockImplementationOnce(() => ({ body: { @@ -193,14 +169,6 @@ describe('platform/github', () => { allow_merge_commit: true, }, })); - // getBranchCommit - get.mockImplementationOnce(() => ({ - body: { - object: { - sha: '1234', - }, - }, - })); // getBranchProtection get.mockImplementationOnce(() => ({ body: { @@ -223,14 +191,6 @@ describe('platform/github', () => { default_branch: 'master', }, })); - // getBranchCommit - get.mockImplementationOnce(() => ({ - body: { - object: { - sha: '1234', - }, - }, - })); // getBranchProtection get.mockImplementationOnce(() => ({ body: { @@ -253,14 +213,6 @@ describe('platform/github', () => { default_branch: 'master', }, })); - // getBranchCommit - get.mockImplementationOnce(() => ({ - body: { - object: { - sha: '1234', - }, - }, - })); // getBranchProtection get.mockImplementationOnce(() => ({ body: { @@ -283,14 +235,6 @@ describe('platform/github', () => { default_branch: 'master', }, })); - // getBranchCommit - get.mockImplementationOnce(() => ({ - body: { - object: { - sha: '1234', - }, - }, - })); // getBranchProtection get.mockImplementationOnce(() => Promise.reject({ @@ -313,14 +257,6 @@ describe('platform/github', () => { default_branch: 'master', }, })); - // getBranchCommit - get.mockImplementationOnce(() => ({ - body: { - object: { - sha: '1234', - }, - }, - })); // getBranchProtection get.mockImplementationOnce(() => Promise.reject({ @@ -343,14 +279,6 @@ describe('platform/github', () => { default_branch: 'master', }, })); - // getBranchCommit - get.mockImplementationOnce(() => ({ - body: { - object: { - sha: '1234', - }, - }, - })); // getBranchProtection get.mockImplementationOnce(() => Promise.reject({ @@ -558,6 +486,14 @@ describe('platform/github', () => { ], }, })); + // getBranchCommit + get.mockImplementationOnce(() => ({ + body: { + object: { + sha: '1234', + }, + }, + })); expect(await github.isBranchStale('thebranchname')).toBe(false); }); it('should return true if SHA different from master', async () => { @@ -580,6 +516,14 @@ describe('platform/github', () => { ], }, })); + // getBranchCommit + get.mockImplementationOnce(() => ({ + body: { + object: { + sha: '1234', + }, + }, + })); expect(await github.isBranchStale('thebranchname')).toBe(true); }); }); @@ -1010,6 +954,14 @@ describe('platform/github', () => { get.mockImplementationOnce(() => ({ body, })); + // getBranchCommit + get.mockImplementationOnce(() => ({ + body: { + object: { + sha: '1234', + }, + }, + })); const pr = await github.getPr(1234); expect(pr).toMatchSnapshot(); }); @@ -1034,6 +986,14 @@ describe('platform/github', () => { }, ], })); + // getBranchCommit + get.mockImplementationOnce(() => ({ + body: { + object: { + sha: '1234', + }, + }, + })); const pr = await github.getPr(1234); expect(pr).toMatchSnapshot(); }); @@ -1065,6 +1025,14 @@ describe('platform/github', () => { {}, ], })); + // getBranchCommit + get.mockImplementationOnce(() => ({ + body: { + object: { + sha: '1234', + }, + }, + })); const pr = await github.getPr(1234); expect(pr).toMatchSnapshot(); }); @@ -1097,6 +1065,14 @@ describe('platform/github', () => { }, ], })); + // getBranchCommit + get.mockImplementationOnce(() => ({ + body: { + object: { + sha: '1234', + }, + }, + })); const pr = await github.getPr(1234); expect(pr.canRebase).toBe(true); expect(pr).toMatchSnapshot(); @@ -1146,10 +1122,8 @@ describe('platform/github', () => { expect(await github.mergePr(pr)).toBe(true); expect(get.put.mock.calls).toHaveLength(1); expect(get.delete.mock.calls).toHaveLength(1); - expect(get.mock.calls).toHaveLength(4); + expect(get.mock.calls).toHaveLength(2); }); - }); - describe('mergePr(prNo)', () => { it('should handle merge error', async () => { await initRepo('some/repo', 'token'); const pr = { @@ -1164,7 +1138,7 @@ describe('platform/github', () => { expect(await github.mergePr(pr)).toBe(false); expect(get.put.mock.calls).toHaveLength(1); expect(get.delete.mock.calls).toHaveLength(0); - expect(get.mock.calls).toHaveLength(3); + expect(get.mock.calls).toHaveLength(2); }); }); describe('mergePr(prNo) - autodetection', () => {