feat: support multiple branches and distribution channels

- Allow to configure multiple branches to release from
- Allow to define a distribution channel associated with each branch
- Manage the availability on distribution channels based on git merges
- Support regular releases, maintenance releases and pre-releases
- Add the `addChannel` plugin step to make an existing release available on a different distribution channel

BREAKING CHANGE: the `branch` option has been removed in favor of `branches`

The new `branches` option expect either an Array or a single branch definition. To migrate your configuration:
- If you want to publish package from multiple branches, please the configuration documentation
- If you use the default configuration and want to publish only from `master`: nothing to change
- If you use the `branch` configuration and want to publish only from one branch: replace `branch` by `branches` (`"branch": "my-release-branch"` => `"branches": "my-release-branch"`)
This commit is contained in:
Pierre Vanduynslager
2018-11-29 14:13:03 -05:00
parent 7a9922a492
commit 7b4052470b
50 changed files with 4069 additions and 516 deletions
+100 -11
View File
@@ -1,13 +1,18 @@
const {trimStart, matches, pick, memoize} = require('lodash');
const gitLogParser = require('git-log-parser');
const getStream = require('get-stream');
const execa = require('execa');
const debug = require('debug')('semantic-release:git');
Object.assign(gitLogParser.fields, {hash: 'H', message: 'B', gitTags: 'd', committerDate: {key: 'ci', type: Date}});
/**
* Get the commit sha for a given tag.
*
* @param {String} tagName Tag name for which to retrieve the commit sha.
* @param {Object} [execaOpts] Options to pass to `execa`.
*
* @return {string} The commit sha of the tag in parameter or `null`.
* @return {String} The commit sha of the tag in parameter or `null`.
*/
async function getTagHead(tagName, execaOpts) {
try {
@@ -33,19 +38,67 @@ async function getTags(execaOpts) {
}
/**
* Verify if the `ref` is in the direct history of the current branch.
* Retrieve a range of commits.
*
* @param {String} from to includes all commits made after this sha (does not include this sha).
* @param {String} to to includes all commits made before this sha (also include this sha).
* @param {Object} [execaOpts] Options to pass to `execa`.
* @return {Promise<Array<Object>>} The list of commits between `from` and `to`.
*/
async function getCommits(from, to, execaOpts) {
return (await getStream.array(
gitLogParser.parse(
{_: `${from ? from + '..' : ''}${to}`},
{cwd: execaOpts.cwd, env: {...process.env, ...execaOpts.env}}
)
)).map(({message, gitTags, ...commit}) => ({...commit, message: message.trim(), gitTags: gitTags.trim()}));
}
/**
* Get all the repository branches.
*
* @param {Object} [execaOpts] Options to pass to `execa`.
*
* @return {Array<String>} List of git branches.
* @throws {Error} If the `git` command fails.
*/
async function getBranches(execaOpts) {
return (await execa.stdout('git', ['branch', '--list', '--no-color'], execaOpts))
.split('\n')
.map(branch => trimStart(branch, '*').trim())
.filter(Boolean);
}
const getBranchCommits = memoize((branch, execaOpts) =>
getStream.array(gitLogParser.parse({_: branch}, {cwd: execaOpts.cwd, env: {...process.env, ...execaOpts.env}}))
);
/**
* Verify if the `ref` is in the direct history of a given branch.
*
* @param {String} ref The reference to look for.
* @param {String} branch The branch for which to check if the `ref` is in history.
* @param {Boolean} findRebasedTags Weither consider in history tags associated with a commit that was rebased to another branch (i.e. GitHub Rebase and Merge feature).
* @param {Object} [execaOpts] Options to pass to `execa`.
*
* @return {Boolean} `true` if the reference is in the history of the current branch, falsy otherwise.
*/
async function isRefInHistory(ref, execaOpts) {
async function isRefInHistory(ref, branch, findRebasedTags, execaOpts) {
if (!(await isRefExists(branch, execaOpts))) {
return false;
}
try {
await execa('git', ['merge-base', '--is-ancestor', ref, 'HEAD'], execaOpts);
await execa('git', ['merge-base', '--is-ancestor', ref, branch], execaOpts);
return true;
} catch (error) {
if (error.code === 1) {
if (findRebasedTags) {
const [tagCommit] = await getStream.array(
gitLogParser.parse({_: ref, n: '1'}, {cwd: execaOpts.cwd, env: {...process.env, ...execaOpts.env}})
);
return (await getBranchCommits(branch, execaOpts)).some(matches(pick(tagCommit, ['message', 'author'])));
}
return false;
}
@@ -54,17 +107,32 @@ async function isRefInHistory(ref, execaOpts) {
}
}
/**
* Verify if the `ref` exits
*
* @param {String} ref The reference to verify.
* @param {Object} [execaOpts] Options to pass to `execa`.
*
* @return {Boolean} `true` if the reference exists, falsy otherwise.
*/
async function isRefExists(ref, execaOpts) {
try {
return (await execa('git', ['rev-parse', '--verify', ref], execaOpts)).code === 0;
} catch (error) {
debug(error);
}
}
/**
* Unshallow the git repository if necessary and fetch all the tags.
*
* @param {String} repositoryUrl The remote repository URL.
* @param {Object} [execaOpts] Options to pass to `execa`.
*/
async function fetch(repositoryUrl, execaOpts) {
async function fetch(execaOpts) {
try {
await execa('git', ['fetch', '--unshallow', '--tags', repositoryUrl], execaOpts);
await execa('git', ['fetch', '--unshallow', '--tags'], execaOpts);
} catch (error) {
await execa('git', ['fetch', '--tags', repositoryUrl], execaOpts);
await execa('git', ['fetch', '--tags'], execaOpts);
}
}
@@ -131,12 +199,13 @@ async function verifyAuth(repositoryUrl, branch, execaOpts) {
* Tag the commit head on the local repository.
*
* @param {String} tagName The name of the tag.
* @param {String} ref The Git reference to tag.
* @param {Object} [execaOpts] Options to pass to `execa`.
*
* @throws {Error} if the tag creation failed.
*/
async function tag(tagName, execaOpts) {
await execa('git', ['tag', tagName], execaOpts);
async function tag(tagName, ref, execaOpts) {
await execa('git', ['tag', tagName, ref], execaOpts);
}
/**
@@ -168,6 +237,22 @@ async function verifyTagName(tagName, execaOpts) {
}
}
/**
* Verify a branch name is a valid Git reference.
*
* @param {String} branch the branch name to verify.
* @param {Object} [execaOpts] Options to pass to `execa`.
*
* @return {Boolean} `true` if valid, falsy otherwise.
*/
async function verifyBranchName(branch, execaOpts) {
try {
return (await execa('git', ['check-ref-format', `refs/heads/${branch}`], execaOpts)).code === 0;
} catch (error) {
debug(error);
}
}
/**
* Verify the local branch is up to date with the remote one.
*
@@ -179,7 +264,7 @@ async function verifyTagName(tagName, execaOpts) {
async function isBranchUpToDate(branch, execaOpts) {
const remoteHead = await execa.stdout('git', ['ls-remote', '--heads', 'origin', branch], execaOpts);
try {
return await isRefInHistory(remoteHead.match(/^(\w+)?/)[1], execaOpts);
return await isRefInHistory(remoteHead.match(/^(\w+)?/)[1], branch, false, execaOpts);
} catch (error) {
debug(error);
}
@@ -188,7 +273,10 @@ async function isBranchUpToDate(branch, execaOpts) {
module.exports = {
getTagHead,
getTags,
getCommits,
getBranches,
isRefInHistory,
isRefExists,
fetch,
getGitHead,
repoUrl,
@@ -198,4 +286,5 @@ module.exports = {
push,
verifyTagName,
isBranchUpToDate,
verifyBranchName,
};