Skip to content

Commit

Permalink
fix: use the branch.main flag to determine is the release need the …
Browse files Browse the repository at this point in the history
…`prerelease` flag
  • Loading branch information
pvdlg committed Nov 28, 2019
1 parent 5ffcf51 commit 7aaeb0f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 79 deletions.
22 changes: 6 additions & 16 deletions lib/add-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module.exports = async (pluginConfig, context) => {
const {
options: {repositoryUrl},
branch,
currentRelease: {gitTag: currentGitTag, channel: currentChannel},
nextRelease: {name, gitTag, notes},
logger,
} = context;
Expand All @@ -18,27 +17,21 @@ module.exports = async (pluginConfig, context) => {
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
let releaseId;

const release = {owner, repo, name, prerelease: isPrerelease(branch)};
const release = {owner, repo, name, prerelease: isPrerelease(branch), tag_name: gitTag};

debug('release owner: %o', release.owner);
debug('release repo: %o', release.repo);
debug('release tag_name: %o', release.tag_name);
debug('release name: %o', release.name);
debug('release prerelease: %o', release.prerelease);
debug('release object: %O', release);

try {
({
data: {id: releaseId},
} = await github.repos.getReleaseByTag({owner, repo, tag: currentGitTag}));
} = await github.repos.getReleaseByTag({owner, repo, tag: gitTag}));
} catch (error) {
if (error.status === 404) {
logger.log('There is no release for tag %s, creating a new one', currentGitTag);

debug('release tag_name: %o', gitTag);
logger.log('There is no release for tag %s, creating a new one', gitTag);

const {
data: {html_url: url},
} = await github.repos.createRelease({...release, body: notes, tag_name: gitTag});
} = await github.repos.createRelease({...release, body: notes});

logger.log('Published GitHub release: %s', url);
return {url, name: RELEASE_NAME};
Expand All @@ -47,14 +40,11 @@ module.exports = async (pluginConfig, context) => {
throw error;
}

const tagName = currentChannel ? gitTag : undefined;

debug('release release_id: %o', releaseId);
debug('release tag_name: %o', tagName);

const {
data: {html_url: url},
} = await github.repos.updateRelease({...release, release_id: releaseId, tag_name: tagName});
} = await github.repos.updateRelease({...release, release_id: releaseId});

logger.log('Updated GitHub release: %s', url);

Expand Down
2 changes: 1 addition & 1 deletion lib/is-prerelease.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = ({type, channel}) => type === 'prerelease' || (type === 'release' && Boolean(channel));
module.exports = ({type, main}) => type === 'prerelease' || (type === 'release' && !main);
68 changes: 23 additions & 45 deletions test/add-channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ test.serial('Update a release', async t => {
const repo = 'test_repo';
const env = {GITHUB_TOKEN: 'github_token'};
const pluginConfig = {};
const currentRelease = {gitTag: 'v1.0.0@next', channel: 'next', name: 'v1.0.0', notes: 'Test release note body'};
const nextRelease = {gitTag: 'v1.0.0', name: 'v1.0.0', notes: 'Test release note body'};
const options = {repositoryUrl: `https://github.com/${owner}/${repo}.git`};
const releaseUrl = `https://github.com/${owner}/${repo}/releases/${nextRelease.version}`;
const releaseId = 1;

const github = authenticate(env)
.get(`/repos/${owner}/${repo}/releases/tags/${currentRelease.gitTag}`)
.get(`/repos/${owner}/${repo}/releases/tags/${nextRelease.gitTag}`)
.reply(200, {id: releaseId})
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {
tag_name: nextRelease.gitTag,
Expand All @@ -48,8 +47,7 @@ test.serial('Update a release', async t => {
const result = await addChannel(pluginConfig, {
env,
options,
branch: {type: 'release'},
currentRelease,
branch: {type: 'release', main: true},
nextRelease,
logger: t.context.logger,
});
Expand All @@ -59,21 +57,21 @@ test.serial('Update a release', async t => {
t.true(github.isDone());
});

test.serial('Update a LTS release', async t => {
test.serial('Update a maintenance release', async t => {
const owner = 'test_user';
const repo = 'test_repo';
const env = {GITHUB_TOKEN: 'github_token'};
const pluginConfig = {};
const currentRelease = {gitTag: 'v1.0.0', name: 'v1.0.0', notes: 'Test release note body'};
const nextRelease = {gitTag: '[email protected]', channel: '1.x', name: 'v1.0.0', notes: 'Test release note body'};
const nextRelease = {gitTag: 'v1.0.0', channel: '1.x', name: 'v1.0.0', notes: 'Test release note body'};
const options = {repositoryUrl: `https://github.com/${owner}/${repo}.git`};
const releaseUrl = `https://github.com/${owner}/${repo}/releases/${nextRelease.version}`;
const releaseId = 1;

const github = authenticate(env)
.get(`/repos/${owner}/${repo}/releases/tags/${currentRelease.gitTag}`)
.get(`/repos/${owner}/${repo}/releases/tags/${nextRelease.gitTag}`)
.reply(200, {id: releaseId})
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {
tag_name: nextRelease.gitTag,
name: nextRelease.name,
prerelease: false,
})
Expand All @@ -82,8 +80,7 @@ test.serial('Update a LTS release', async t => {
const result = await addChannel(pluginConfig, {
env,
options,
branch: {type: 'lts', channel: '1.x'},
currentRelease,
branch: {type: 'maintenance', channel: '1.x', main: false},
nextRelease,
logger: t.context.logger,
});
Expand All @@ -98,19 +95,13 @@ test.serial('Update a prerelease', async t => {
const repo = 'test_repo';
const env = {GITHUB_TOKEN: 'github_token'};
const pluginConfig = {};
const currentRelease = {
gitTag: 'v1.0.0-beta.1',
channel: 'beta',
name: 'v1.0.0-beta.1',
notes: 'Test release note body',
};
const nextRelease = {gitTag: 'v1.0.0', name: 'v1.0.0', notes: 'Test release note body'};
const options = {repositoryUrl: `https://github.com/${owner}/${repo}.git`};
const releaseUrl = `https://github.com/${owner}/${repo}/releases/${nextRelease.version}`;
const releaseId = 1;

const github = authenticate(env)
.get(`/repos/${owner}/${repo}/releases/tags/${currentRelease.gitTag}`)
.get(`/repos/${owner}/${repo}/releases/tags/${nextRelease.gitTag}`)
.reply(200, {id: releaseId})
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {
tag_name: nextRelease.gitTag,
Expand All @@ -122,8 +113,7 @@ test.serial('Update a prerelease', async t => {
const result = await addChannel(pluginConfig, {
env,
options,
branch: {type: 'lts', channel: '1.x'},
currentRelease,
branch: {type: 'maintenance', channel: '1.x', main: false},
nextRelease,
logger: t.context.logger,
});
Expand All @@ -138,14 +128,13 @@ test.serial('Update a release with a custom github url', async t => {
const repo = 'test_repo';
const env = {GH_URL: 'https://othertesturl.com:443', GH_TOKEN: 'github_token', GH_PREFIX: 'prefix'};
const pluginConfig = {};
const currentRelease = {gitTag: 'v1.0.0@next', channel: 'next', name: 'v1.0.0', notes: 'Test release note body'};
const nextRelease = {gitTag: 'v1.0.0', name: 'v1.0.0', notes: 'Test release note body'};
const options = {repositoryUrl: `${env.GH_URL}/${owner}/${repo}.git`};
const releaseUrl = `${env.GH_URL}/${owner}/${repo}/releases/${nextRelease.version}`;
const releaseId = 1;

const github = authenticate(env)
.get(`/repos/${owner}/${repo}/releases/tags/${currentRelease.gitTag}`)
.get(`/repos/${owner}/${repo}/releases/tags/${nextRelease.gitTag}`)
.reply(200, {id: releaseId})
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {
tag_name: nextRelease.gitTag,
Expand All @@ -157,8 +146,7 @@ test.serial('Update a release with a custom github url', async t => {
const result = await addChannel(pluginConfig, {
env,
options,
branch: {type: 'release'},
currentRelease,
branch: {type: 'release', main: true},
nextRelease,
logger: t.context.logger,
});
Expand All @@ -173,17 +161,16 @@ test.serial('Update a release, retrying 4 times', async t => {
const repo = 'test_repo';
const env = {GITHUB_TOKEN: 'github_token'};
const pluginConfig = {};
const currentRelease = {gitTag: 'v1.0.0@next', channel: 'next', name: 'v1.0.0', notes: 'Test release note body'};
const nextRelease = {gitTag: 'v1.0.0', name: 'v1.0.0', notes: 'Test release note body'};
const options = {repositoryUrl: `https://github.com/${owner}/${repo}.git`};
const releaseUrl = `https://github.com/${owner}/${repo}/releases/${nextRelease.version}`;
const releaseId = 1;

const github = authenticate(env)
.get(`/repos/${owner}/${repo}/releases/tags/${currentRelease.gitTag}`)
.get(`/repos/${owner}/${repo}/releases/tags/${nextRelease.gitTag}`)
.times(3)
.reply(404)
.get(`/repos/${owner}/${repo}/releases/tags/${currentRelease.gitTag}`)
.get(`/repos/${owner}/${repo}/releases/tags/${nextRelease.gitTag}`)
.reply(200, {id: releaseId})
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {
tag_name: nextRelease.gitTag,
Expand All @@ -202,8 +189,7 @@ test.serial('Update a release, retrying 4 times', async t => {
const result = await addChannel(pluginConfig, {
env,
options,
branch: {type: 'release'},
currentRelease,
branch: {type: 'release', main: true},
nextRelease,
logger: t.context.logger,
});
Expand All @@ -218,13 +204,12 @@ test.serial('Create the new release if current one is missing', async t => {
const repo = 'test_repo';
const env = {GITHUB_TOKEN: 'github_token'};
const pluginConfig = {};
const currentRelease = {gitTag: 'v1.0.0@next', channel: 'next', name: 'v1.0.0', notes: 'Test release note body'};
const nextRelease = {gitTag: 'v1.0.0', name: 'v1.0.0', notes: 'Test release note body'};
const options = {repositoryUrl: `https://github.com/${owner}/${repo}.git`};
const releaseUrl = `https://github.com/${owner}/${repo}/releases/${nextRelease.version}`;

const github = authenticate(env)
.get(`/repos/${owner}/${repo}/releases/tags/${currentRelease.gitTag}`)
.get(`/repos/${owner}/${repo}/releases/tags/${nextRelease.gitTag}`)
.times(4)
.reply(404)
.post(`/repos/${owner}/${repo}/releases`, {
Expand All @@ -238,14 +223,13 @@ test.serial('Create the new release if current one is missing', async t => {
const result = await addChannel(pluginConfig, {
env,
options,
branch: {type: 'release'},
currentRelease,
branch: {type: 'release', main: true},
nextRelease,
logger: t.context.logger,
});

t.is(result.url, releaseUrl);
t.deepEqual(t.context.log.args[0], ['There is no release for tag %s, creating a new one', currentRelease.gitTag]);
t.deepEqual(t.context.log.args[0], ['There is no release for tag %s, creating a new one', nextRelease.gitTag]);
t.deepEqual(t.context.log.args[1], ['Published GitHub release: %s', releaseUrl]);
t.true(github.isDone());
});
Expand All @@ -255,21 +239,19 @@ test.serial('Throw error if cannot read current release', async t => {
const repo = 'test_repo';
const env = {GITHUB_TOKEN: 'github_token'};
const pluginConfig = {};
const currentRelease = {gitTag: 'v1.0.0@next', channel: 'next', name: 'v1.0.0', notes: 'Test release note body'};
const nextRelease = {gitTag: 'v1.0.0', name: 'v1.0.0', notes: 'Test release note body'};
const options = {repositoryUrl: `https://github.com/${owner}/${repo}.git`};

const github = authenticate(env)
.get(`/repos/${owner}/${repo}/releases/tags/${currentRelease.gitTag}`)
.get(`/repos/${owner}/${repo}/releases/tags/${nextRelease.gitTag}`)
.times(4)
.reply(500);

const error = await t.throwsAsync(
addChannel(pluginConfig, {
env,
options,
branch: {type: 'release'},
currentRelease,
branch: {type: 'release', main: true},
nextRelease,
logger: t.context.logger,
})
Expand All @@ -284,12 +266,11 @@ test.serial('Throw error if cannot create missing current release', async t => {
const repo = 'test_repo';
const env = {GITHUB_TOKEN: 'github_token'};
const pluginConfig = {};
const currentRelease = {gitTag: 'v1.0.0@next', channel: 'next', name: 'v1.0.0', notes: 'Test release note body'};
const nextRelease = {gitTag: 'v1.0.0', name: 'v1.0.0', notes: 'Test release note body'};
const options = {repositoryUrl: `https://github.com/${owner}/${repo}.git`};

const github = authenticate(env)
.get(`/repos/${owner}/${repo}/releases/tags/${currentRelease.gitTag}`)
.get(`/repos/${owner}/${repo}/releases/tags/${nextRelease.gitTag}`)
.times(4)
.reply(404)
.post(`/repos/${owner}/${repo}/releases`, {
Expand All @@ -305,8 +286,7 @@ test.serial('Throw error if cannot create missing current release', async t => {
addChannel(pluginConfig, {
env,
options,
branch: {type: 'release'},
currentRelease,
branch: {type: 'release', main: true},
nextRelease,
logger: t.context.logger,
})
Expand All @@ -321,13 +301,12 @@ test.serial('Throw error if cannot update release', async t => {
const repo = 'test_repo';
const env = {GITHUB_TOKEN: 'github_token'};
const pluginConfig = {};
const currentRelease = {gitTag: 'v1.0.0@next', channel: 'next', name: 'v1.0.0', notes: 'Test release note body'};
const nextRelease = {gitTag: 'v1.0.0', name: 'v1.0.0', notes: 'Test release note body'};
const options = {repositoryUrl: `https://github.com/${owner}/${repo}.git`};
const releaseId = 1;

const github = authenticate(env)
.get(`/repos/${owner}/${repo}/releases/tags/${currentRelease.gitTag}`)
.get(`/repos/${owner}/${repo}/releases/tags/${nextRelease.gitTag}`)
.reply(200, {id: releaseId})
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {
tag_name: nextRelease.gitTag,
Expand All @@ -341,8 +320,7 @@ test.serial('Throw error if cannot update release', async t => {
addChannel(pluginConfig, {
env,
options,
branch: {type: 'release'},
currentRelease,
branch: {type: 'release', main: true},
nextRelease,
logger: t.context.logger,
})
Expand Down
14 changes: 6 additions & 8 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ test.serial('Publish a release with an array of assets', async t => {

const result = await t.context.m.publish(
{assets},
{cwd, env, options, branch: {type: 'release'}, nextRelease, logger: t.context.logger}
{cwd, env, options, branch: {type: 'release', main: true}, nextRelease, logger: t.context.logger}
);

t.is(result.url, releaseUrl);
Expand Down Expand Up @@ -244,7 +244,6 @@ test.serial('Update a release', async t => {
const owner = 'test_user';
const repo = 'test_repo';
const env = {GITHUB_TOKEN: 'github_token'};
const currentRelease = {gitTag: 'v1.0.0@next', channel: 'next', name: 'v1.0.0', notes: 'Test release note body'};
const nextRelease = {gitTag: 'v1.0.0', name: 'v1.0.0', notes: 'Test release note body'};
const options = {repositoryUrl: `https://github.com/${owner}/${repo}.git`};
const releaseUrl = `https://github.com/${owner}/${repo}/releases/${nextRelease.version}`;
Expand All @@ -253,7 +252,7 @@ test.serial('Update a release', async t => {
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {permissions: {push: true}})
.get(`/repos/${owner}/${repo}/releases/tags/${currentRelease.gitTag}`)
.get(`/repos/${owner}/${repo}/releases/tags/${nextRelease.gitTag}`)
.reply(200, {id: releaseId})
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {
tag_name: nextRelease.gitTag,
Expand All @@ -264,7 +263,7 @@ test.serial('Update a release', async t => {

const result = await t.context.m.addChannel(
{},
{cwd, env, options, branch: {type: 'release'}, currentRelease, nextRelease, logger: t.context.logger}
{cwd, env, options, branch: {type: 'release', main: true}, nextRelease, logger: t.context.logger}
);

t.is(result.url, releaseUrl);
Expand Down Expand Up @@ -419,7 +418,7 @@ test.serial('Verify, release and notify success', async t => {
await t.notThrowsAsync(t.context.m.verifyConditions({}, {cwd, env, options, logger: t.context.logger}));
await t.context.m.publish(
{assets},
{cwd, env, options, branch: {type: 'release'}, nextRelease, logger: t.context.logger}
{cwd, env, options, branch: {type: 'release', main: true}, nextRelease, logger: t.context.logger}
);
await t.context.m.success(
{assets, failTitle},
Expand All @@ -444,7 +443,6 @@ test.serial('Verify, update release and notify success', async t => {
publish: [{path: '@semantic-release/npm'}, {path: '@semantic-release/github'}],
repositoryUrl: `https://github.com/${owner}/${repo}.git`,
};
const currentRelease = {gitTag: 'v1.0.0@next', channel: 'next', name: 'v1.0.0', notes: 'Test release note body'};
const nextRelease = {gitTag: 'v1.0.0', name: 'v1.0.0', notes: 'Test release note body'};
const releaseUrl = `https://github.com/${owner}/${repo}/releases/${nextRelease.version}`;
const releaseId = 1;
Expand All @@ -453,7 +451,7 @@ test.serial('Verify, update release and notify success', async t => {
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {permissions: {push: true}})
.get(`/repos/${owner}/${repo}/releases/tags/${currentRelease.gitTag}`)
.get(`/repos/${owner}/${repo}/releases/tags/${nextRelease.gitTag}`)
.reply(200, {id: releaseId})
.patch(`/repos/${owner}/${repo}/releases/${releaseId}`, {
tag_name: nextRelease.gitTag,
Expand Down Expand Up @@ -485,7 +483,7 @@ test.serial('Verify, update release and notify success', async t => {
await t.notThrowsAsync(t.context.m.verifyConditions({}, {cwd, env, options, logger: t.context.logger}));
await t.context.m.addChannel(
{},
{cwd, env, branch: {type: 'release'}, currentRelease, nextRelease, options, logger: t.context.logger}
{cwd, env, branch: {type: 'release', main: true}, nextRelease, options, logger: t.context.logger}
);
await t.context.m.success(
{failTitle},
Expand Down
Loading

0 comments on commit 7aaeb0f

Please sign in to comment.