Skip to content

Commit

Permalink
Try to use branch's upstream remote when releasing (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl committed Mar 6, 2020
1 parent 424f59a commit 0d1a81f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
19 changes: 16 additions & 3 deletions lib/plugin/GitBase.js
Expand Up @@ -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() {
Expand Down
4 changes: 0 additions & 4 deletions lib/plugin/git/Git.js
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions test/git.init.js
Expand Up @@ -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 } });
Expand Down
21 changes: 21 additions & 0 deletions test/github.js
Expand Up @@ -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();
Expand Down

0 comments on commit 0d1a81f

Please sign in to comment.