diff --git a/lib/plugin/GitBase.js b/lib/plugin/GitBase.js index fd541782..291ded89 100644 --- a/lib/plugin/GitBase.js +++ b/lib/plugin/GitBase.js @@ -50,11 +50,24 @@ class GitBase extends Plugin { return !_.includes(remoteUrlOrName, '/'); } - getRemoteUrl() { - const remoteNameOrUrl = this.options.pushRepo || 'origin'; + async getRemoteUrl() { + const remoteNameOrUrl = this.options.pushRepo || (await this.getRemote()) || 'origin'; return this.isRemoteName(remoteNameOrUrl) ? this.exec(`git config --get remote.${remoteNameOrUrl}.url`, { options }).catch(() => null) - : Promise.resolve(remoteNameOrUrl); + : remoteNameOrUrl; + } + + async getRemote() { + const branchName = await this.getBranchName(); + return branchName ? await this.getRemoteForBranch(branchName) : null; + } + + getBranchName() { + return this.exec('git rev-parse --abbrev-ref HEAD', { options }).catch(() => null); + } + + getRemoteForBranch(branch) { + return this.exec(`git config --get branch.${branch}.remote`, { options }).catch(() => null); } fetch() { diff --git a/lib/plugin/git/Git.js b/lib/plugin/git/Git.js index 149f0a65..91c6c3dd 100644 --- a/lib/plugin/git/Git.js +++ b/lib/plugin/git/Git.js @@ -76,10 +76,6 @@ class Git extends GitBase { return Boolean(branch); } - getBranchName() { - return this.exec('git rev-parse --abbrev-ref HEAD', { options }).catch(() => null); - } - tagExists(tag) { return this.exec(`git show-ref --tags --quiet --verify -- refs/tags/${tag}`, { options }).then( () => true, diff --git a/test/git.init.js b/test/git.init.js index dc320cb2..1a4b3425 100644 --- a/test/git.init.js +++ b/test/git.init.js @@ -78,6 +78,13 @@ test.serial('should not throw if there are no tags', async t => { await t.notThrowsAsync(gitClient.init()); }); +test.serial('should not throw if origin remote is renamed', async t => { + const options = { git }; + const gitClient = factory(Git, { options }); + sh.exec('git remote rename origin upstream'); + await t.notThrowsAsync(gitClient.init()); +}); + test.serial('should get the latest tag after fetch', async t => { const log = new Log(); const shell = new Shell({ container: { log } }); diff --git a/test/github.js b/test/github.js index 7f8ce98f..50af0b6d 100644 --- a/test/github.js +++ b/test/github.js @@ -64,6 +64,27 @@ test('should release and upload assets', async t => { exec.restore(); }); +test('should release to non-origin upstream', async t => { + const github = factory(GitHub, { options: { github: { tokenRef } } }); + const exec = sinon.stub(github.shell, 'exec').callThrough(); + exec.withArgs('git rev-parse --abbrev-ref HEAD').resolves(`test`); + exec.withArgs('git config --get branch.test.remote').resolves(`upstream`); + exec.withArgs('git config --get remote.upstream.url').resolves(`https://github.example.org/user/repo`); + exec.withArgs('git describe --tags --abbrev=0').resolves(`1.0.0`); + + const remote = { api: 'https://github.example.org/api/v3', host: 'github.example.org' }; + interceptAuthentication(remote); + interceptCollaborator(remote); + interceptDraft(Object.assign({ body: { tag_name: '1.0.1', name: '', prerelease: false, draft: true } }, remote)); + interceptPublish(Object.assign({ body: { draft: false, tag_name: '1.0.1' } }, remote)); + + await runTasks(github); + + t.true(github.isReleased); + t.is(github.getReleaseUrl(), `https://github.example.org/user/repo/releases/tag/1.0.1`); + exec.restore(); +}); + test('should release to enterprise host', async t => { const github = factory(GitHub, { options: { github: { tokenRef } } }); const exec = sinon.stub(github.shell, 'exec').callThrough();