Skip to content

Commit

Permalink
Use git.pushRepo for GitHub Release (fixes #632)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Mar 27, 2020
1 parent 12e109d commit 2ab8aaf
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/plugin/GitBase.js
Expand Up @@ -8,7 +8,7 @@ const changelogFallback = 'git log --pretty=format:"* %s (%h)"';

class GitBase extends Plugin {
async init() {
this.remoteUrl = this.options.remoteUrl || (await this.getRemoteUrl());
this.remoteUrl = await this.getRemoteUrl();
if (!this.remoteUrl) {
throw new GitRemoteUrlError();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/plugin/GitRelease.js
Expand Up @@ -11,7 +11,7 @@ class GitRelease extends GitBase {
}

getInitialOptions(options, namespace) {
const gitOptions = _.pick(options.git, ['tagName', 'remoteUrl', 'changelog']);
const gitOptions = _.pick(options.git, ['tagName', 'pushRepo', 'changelog']);
return _.defaults(options[namespace], gitOptions);
}

Expand Down
35 changes: 26 additions & 9 deletions test/github.js
Expand Up @@ -13,12 +13,12 @@ const {
} = require('./stub/github');

const tokenRef = 'GITHUB_TOKEN';
const remoteUrl = 'git://github.com:user/repo';
const pushRepo = 'git://github.com:user/repo';
const host = 'github.com';

test.serial('should validate token', async t => {
const tokenRef = 'MY_GITHUB_TOKEN';
const options = { github: { release: true, tokenRef, remoteUrl } };
const options = { github: { release: true, tokenRef, pushRepo } };
const github = factory(GitHub, { options });
delete process.env[tokenRef];

Expand All @@ -36,7 +36,7 @@ test('should release and upload assets', async t => {
const asset = 'file1';
const options = {
github: {
remoteUrl,
pushRepo,
tokenRef,
release: true,
releaseName: 'Release ${tagName}',
Expand Down Expand Up @@ -91,7 +91,7 @@ test('should release to alternative host and proxy', async t => {
const options = {
github: {
tokenRef,
remoteUrl: `git://my-custom-host.org:user/repo`,
pushRepo: `git://my-custom-host.org:user/repo`,
host: 'my-custom-host.org',
proxy: 'http://proxy:8080'
}
Expand All @@ -107,8 +107,25 @@ test('should release to alternative host and proxy', async t => {
exec.restore();
});

test('should release to git.pushRepo', async t => {
const remote = { api: 'https://my-custom-host.org/api/v3', host: 'my-custom-host.org' };
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));
const options = { git: { pushRepo: 'upstream' }, github: { tokenRef, skipChecks: true } };
const github = factory(GitHub, { options });
const exec = sinon.stub(github.shell, 'exec').callThrough();
exec.withArgs('git describe --tags --abbrev=0').resolves('1.0.0');
exec.withArgs('git config --get remote.upstream.url').resolves('https://my-custom-host.org/user/repo');

await runTasks(github);

t.true(github.isReleased);
t.is(github.getReleaseUrl(), `https://my-custom-host.org/user/repo/releases/tag/1.0.1`);
exec.restore();
});

test('should throw for unauthenticated user', async t => {
const options = { github: { tokenRef, remoteUrl, host } };
const options = { github: { tokenRef, pushRepo, host } };
const github = factory(GitHub, { options });
const stub = sinon.stub(github.client.users, 'getAuthenticated');
stub.throws(new RequestError('Bad credentials', 401, { request: { url: '', headers: {} } }));
Expand All @@ -124,7 +141,7 @@ test('should throw for unauthenticated user', async t => {

test('should throw for non-collaborator', async t => {
interceptAuthentication({ username: 'john' });
const options = { github: { tokenRef, remoteUrl, host } };
const options = { github: { tokenRef, pushRepo, host } };
const github = factory(GitHub, { options });
const stub = sinon.stub(github.client.repos, 'checkCollaborator');
stub.throws(new RequestError('HttpError', 401, { request: { url: '', headers: {} } }));
Expand Down Expand Up @@ -159,7 +176,7 @@ test.serial('should skip authentication and collaborator checks when running on
});

test('should handle octokit client error (without retries)', async t => {
const github = factory(GitHub, { options: { github: { tokenRef, remoteUrl, host } } });
const github = factory(GitHub, { options: { github: { tokenRef, pushRepo, host } } });
const stub = sinon.stub(github.client.repos, 'createRelease');
stub.throws(new RequestError('Not found', 404, { request: { url: '', headers: {} } }));
interceptAuthentication();
Expand All @@ -172,7 +189,7 @@ test('should handle octokit client error (without retries)', async t => {
});

test('should handle octokit client error (with retries)', async t => {
const options = { github: { tokenRef, remoteUrl, host, retryMinTimeout: 0 } };
const options = { github: { tokenRef, pushRepo, host, retryMinTimeout: 0 } };
const github = factory(GitHub, { options });
const stub = sinon.stub(github.client.repos, 'createRelease');
stub.throws(new RequestError('Request failed', 500, { request: { url: '', headers: {} } }));
Expand All @@ -187,7 +204,7 @@ test('should handle octokit client error (with retries)', async t => {

test('should not call octokit client in dry run', async t => {
const options = {
github: { tokenRef, remoteUrl, releaseName: 'R ${version}', assets: ['*'] }
github: { tokenRef, pushRepo, releaseName: 'R ${version}', assets: ['*'] }
};
const github = factory(GitHub, { options, global: { isDryRun: true } });
const spy = sinon.spy(github, 'client', ['get']);
Expand Down
36 changes: 18 additions & 18 deletions test/gitlab.js
Expand Up @@ -9,8 +9,8 @@ const tokenRef = 'GITLAB_TOKEN';

test.serial('should validate token', async t => {
const tokenRef = 'MY_GITLAB_TOKEN';
const remoteUrl = 'https://gitlab.com/user/repo';
const options = { gitlab: { release: true, tokenRef, remoteUrl } };
const pushRepo = 'https://gitlab.com/user/repo';
const options = { gitlab: { release: true, tokenRef, pushRepo } };
const gitlab = factory(GitLab, { options });
delete process.env[tokenRef];

Expand All @@ -25,10 +25,10 @@ test.serial('should validate token', async t => {
});

test.serial('should upload assets and release', async t => {
const remoteUrl = 'https://gitlab.com/user/repo';
const pushRepo = 'https://gitlab.com/user/repo';
const asset = 'file1';
const options = {
git: { remoteUrl },
git: { pushRepo },
gitlab: {
tokenRef,
release: true,
Expand All @@ -52,7 +52,7 @@ test.serial('should upload assets and release', async t => {
links: [
{
name: asset,
url: `${remoteUrl}/uploads/7e8bec1fe27cc46a4bc6a91b9e82a07c/${asset}`
url: `${pushRepo}/uploads/7e8bec1fe27cc46a4bc6a91b9e82a07c/${asset}`
}
]
}
Expand All @@ -61,8 +61,8 @@ test.serial('should upload assets and release', async t => {

await runTasks(gitlab);

t.is(gitlab.assets[0].url, `${remoteUrl}/uploads/7e8bec1fe27cc46a4bc6a91b9e82a07c/${asset}`);
t.is(gitlab.getReleaseUrl(), `${remoteUrl}/releases`);
t.is(gitlab.assets[0].url, `${pushRepo}/uploads/7e8bec1fe27cc46a4bc6a91b9e82a07c/${asset}`);
t.is(gitlab.getReleaseUrl(), `${pushRepo}/releases`);
t.is(gitlab.isReleased, true);
});

Expand All @@ -71,7 +71,7 @@ test.serial('should release to self-managed host', async t => {
const scope = nock(host);
scope.post('/api/v4/projects/user%2Frepo/releases').reply(200, {});
const options = {
git: { remoteUrl: `${host}/user/repo`, tagName: '${version}' },
git: { pushRepo: `${host}/user/repo`, tagName: '${version}' },
gitlab: { releaseName: 'Release ${version}', releaseNotes: 'echo readme', tokenRef }
};
const gitlab = factory(GitLab, { options });
Expand All @@ -89,7 +89,7 @@ test.serial('should release to self-managed host', async t => {
test.serial('should release to sub-grouped repo', async t => {
const scope = nock('https://gitlab.com');
scope.post('/api/v4/projects/group%2Fsub-group%2Frepo/releases').reply(200, {});
const options = { gitlab: { tokenRef }, git: { remoteUrl: 'git@gitlab.com:group/sub-group/repo.git' } };
const options = { gitlab: { tokenRef }, git: { pushRepo: 'git@gitlab.com:group/sub-group/repo.git' } };
const gitlab = factory(GitLab, { options });

interceptUser({ owner: 'sub-group' });
Expand All @@ -103,8 +103,8 @@ test.serial('should release to sub-grouped repo', async t => {

test.serial('should throw for unauthenticated user', async t => {
const host = 'https://gitlab.com';
const remoteUrl = `${host}/user/repo`;
const options = { gitlab: { tokenRef, remoteUrl, host } };
const pushRepo = `${host}/user/repo`;
const options = { gitlab: { tokenRef, pushRepo, host } };
const gitlab = factory(GitLab, { options });
const scope = nock(host);
scope.get(`/api/v4/user`).reply(401);
Expand All @@ -117,8 +117,8 @@ test.serial('should throw for unauthenticated user', async t => {

test.serial('should throw for non-collaborator', async t => {
const host = 'https://gitlab.com';
const remoteUrl = `${host}/john/repo`;
const options = { gitlab: { tokenRef, remoteUrl, host } };
const pushRepo = `${host}/john/repo`;
const options = { gitlab: { tokenRef, pushRepo, host } };
const gitlab = factory(GitLab, { options });
const scope = nock(host);
scope.get(`/api/v4/projects/john%2Frepo/members/all/1`).reply(200, { username: 'emma' });
Expand All @@ -132,8 +132,8 @@ test.serial('should throw for non-collaborator', async t => {

test.serial('should throw for insufficient access level', async t => {
const host = 'https://gitlab.com';
const remoteUrl = `${host}/john/repo`;
const options = { gitlab: { tokenRef, remoteUrl, host } };
const pushRepo = `${host}/john/repo`;
const options = { gitlab: { tokenRef, pushRepo, host } };
const gitlab = factory(GitLab, { options });
const scope = nock(host);
scope.get(`/api/v4/projects/john%2Frepo/members/all/1`).reply(200, { username: 'john', access_level: 10 });
Expand All @@ -147,8 +147,8 @@ test.serial('should throw for insufficient access level', async t => {

test('should not make requests in dry run', async t => {
const [host, owner, repo] = ['https://gitlab.example.org', 'user', 'repo'];
const remoteUrl = `${host}/${owner}/${repo}`;
const options = { git: { remoteUrl }, gitlab: { releaseName: 'R', tokenRef } };
const pushRepo = `${host}/${owner}/${repo}`;
const options = { git: { pushRepo }, gitlab: { releaseName: 'R', tokenRef } };
const gitlab = factory(GitLab, { options, global: { isDryRun: true } });
sinon.stub(gitlab, 'getLatestVersion').resolves('1.0.0');
const spy = sinon.spy(gitlab, 'client', ['get']);
Expand All @@ -158,7 +158,7 @@ test('should not make requests in dry run', async t => {
t.is(spy.get.callCount, 0);
t.is(gitlab.log.exec.args[1][0], 'gitlab releases#uploadAssets');
t.is(gitlab.log.exec.args[2][0], 'gitlab releases#createRelease "R" (1.0.1)');
t.is(gitlab.getReleaseUrl(), `${remoteUrl}/releases`);
t.is(gitlab.getReleaseUrl(), `${pushRepo}/releases`);
t.is(gitlab.isReleased, true);
spy.restore();
});
Expand Down
6 changes: 3 additions & 3 deletions test/tasks.js
Expand Up @@ -167,7 +167,7 @@ test.serial('should release all the things (basic)', async t => {
interceptGitHubPublish({ owner, project, body: { draft: false, tag_name: '1.0.1' } });

const container = getContainer({
github: { release: true, remoteUrl: `https://github.com/${owner}/${project}` },
github: { release: true, pushRepo: `https://github.com/${owner}/${project}` },
npm: { name: pkgName }
});
const exec = sinon.spy(container.shell, 'exec');
Expand Down Expand Up @@ -244,13 +244,13 @@ test.serial('should release all the things (pre-release, github, gitlab)', async
git: { changelog: 'git log --pretty=format:%h ${latestTag}...HEAD' },
github: {
release: true,
remoteUrl: `https://github.com/${owner}/${project}`,
pushRepo: `https://github.com/${owner}/${project}`,
releaseNotes: 'echo Notes for ${name} [v${version}]: ${changelog}',
assets: ['file']
},
gitlab: {
release: true,
remoteUrl: `https://gitlab.com/${owner}/${project}`,
pushRepo: `https://gitlab.com/${owner}/${project}`,
releaseNotes: 'echo Notes for ${name}: ${changelog}',
assets: ['file']
},
Expand Down

0 comments on commit 2ab8aaf

Please sign in to comment.