Skip to content

Commit

Permalink
fix: fetch all release branches on CI
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Nov 30, 2018
1 parent e63e753 commit b729183
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 28 deletions.
4 changes: 1 addition & 3 deletions index.js
Expand Up @@ -18,7 +18,7 @@ const {extractErrors, makeTag} = require('./lib/utils');
const getGitAuthUrl = require('./lib/get-git-auth-url');
const getBranches = require('./lib/branches');
const getLogger = require('./lib/get-logger');
const {fetch, verifyAuth, isBranchUpToDate, getGitHead, tag, push} = require('./lib/git');
const {verifyAuth, isBranchUpToDate, getGitHead, tag, push} = require('./lib/git');
const getError = require('./lib/get-error');
const {COMMIT_NAME, COMMIT_EMAIL} = require('./lib/definitions/constants');

Expand Down Expand Up @@ -53,8 +53,6 @@ async function run(context, plugins) {
// Verify config
await verify(context);

await fetch({cwd, env});

context.branches = await getBranches(context);
context.branch = context.branches.find(({name}) => name === ciBranch);

Expand Down
17 changes: 11 additions & 6 deletions lib/branches/index.js
Expand Up @@ -3,20 +3,25 @@ const AggregateError = require('aggregate-error');
const pEachSeries = require('p-each-series');
const DEFINITIONS = require('../definitions/branches');
const getError = require('../get-error');
const {verifyBranchName} = require('../git');
const {fetch, verifyBranchName} = require('../git');
const expand = require('./expand');
const getTags = require('./get-tags');
const normalize = require('./normalize');

module.exports = async context => {
const branches = await getTags(
const {cwd, env} = context;

const remoteBranches = await expand(
context,
await expand(
context,
context.options.branches.map(branch => (isString(branch) || isRegExp(branch) ? {name: branch} : branch))
)
context.options.branches.map(branch => (isString(branch) || isRegExp(branch) ? {name: branch} : branch))
);

await pEachSeries(remoteBranches, async ({name}) => {
await fetch(name, {cwd, env});
});

const branches = await getTags(context, remoteBranches);

const errors = [];
const branchesByType = Object.entries(DEFINITIONS).reduce(
(branchesByType, [type, {filter}]) => ({[type]: branches.filter(filter), ...branchesByType}),
Expand Down
31 changes: 24 additions & 7 deletions lib/git.js
@@ -1,4 +1,4 @@
const {trimStart, matches, pick, memoize} = require('lodash');
const {matches, pick, memoize} = require('lodash');
const gitLogParser = require('git-log-parser');
const getStream = require('get-stream');
const execa = require('execa');
Expand Down Expand Up @@ -63,9 +63,9 @@ async function getCommits(from, to, execaOpts) {
* @throws {Error} If the `git` command fails.
*/
async function getBranches(execaOpts) {
return (await execa.stdout('git', ['branch', '--list', '--no-color'], execaOpts))
return (await execa.stdout('git', ['ls-remote', '--heads', 'origin'], execaOpts))
.split('\n')
.map(branch => trimStart(branch, '*').trim())
.map(branch => branch.match(/^.+refs\/heads\/(.+)$/)[1])
.filter(Boolean);
}

Expand Down Expand Up @@ -126,13 +126,30 @@ async function isRefExists(ref, execaOpts) {
/**
* Unshallow the git repository if necessary and fetch all the tags.
*
* @param {String} branch The repository branch to fetch.
* @param {Object} [execaOpts] Options to pass to `execa`.
*/
async function fetch(execaOpts) {
async function fetch(branch, execaOpts) {
const isLocalExists =
(await execa('git', ['rev-parse', '--verify', branch], {...execaOpts, reject: false})).code === 0;

try {
await execa('git', ['fetch', '--unshallow', '--tags'], execaOpts);
await execa(
'git',
[
'fetch',
'--unshallow',
'--tags',
...(isLocalExists ? [] : ['origin', `+refs/heads/${branch}:refs/heads/${branch}`]),
],
execaOpts
);
} catch (error) {
await execa('git', ['fetch', '--tags'], execaOpts);
await execa(
'git',
['fetch', '--tags', ...(isLocalExists ? [] : ['origin', `+refs/heads/${branch}:refs/heads/${branch}`])],
execaOpts
);
}
}

Expand Down Expand Up @@ -181,7 +198,7 @@ async function isGitRepo(execaOpts) {
* Verify the write access authorization to remote repository with push dry-run.
*
* @param {String} repositoryUrl The remote repository URL.
* @param {String} branch The repositoru branch for which to verify write access.
* @param {String} branch The repository branch for which to verify write access.
* @param {Object} [execaOpts] Options to pass to `execa`.
*
* @throws {Error} if not authorized to push.
Expand Down
8 changes: 4 additions & 4 deletions test/branches/branches.test.js
Expand Up @@ -22,7 +22,7 @@ test('Enforce ranges with branching release workflow', async t => {
{name: 'beta', prerelease: true, tags: []},
{name: 'alpha', prerelease: true, tags: []},
];
const getBranches = proxyquire('../../lib/branches', {'./get-tags': () => branches});
const getBranches = proxyquire('../../lib/branches', {'./get-tags': () => branches, './expand': () => []});

let result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
t.is(getBranch(result, '1.0.x').range, '>=1.0.0 <1.0.0', 'Cannot release on 1.0.x before a releasing on master');
Expand Down Expand Up @@ -145,7 +145,7 @@ test('Throw SemanticReleaseError for invalid configurations', async t => {
{name: 'alpha', prerelease: 'alpha', tags: []},
{name: 'preview', prerelease: 'alpha', tags: []},
];
const getBranches = proxyquire('../../lib/branches', {'./get-tags': () => branches});
const getBranches = proxyquire('../../lib/branches', {'./get-tags': () => branches, './expand': () => []});
const errors = [...(await t.throws(getBranches({options: {branches}})))];

t.is(errors[0].name, 'SemanticReleaseError');
Expand All @@ -172,7 +172,7 @@ test('Throw SemanticReleaseError for invalid configurations', async t => {

test('Throw a SemanticReleaseError if there is duplicate branches', async t => {
const branches = [{name: 'master', tags: []}, {name: 'master', tags: []}];
const getBranches = proxyquire('../../lib/branches', {'./get-tags': () => branches});
const getBranches = proxyquire('../../lib/branches', {'./get-tags': () => branches, './expand': () => []});

const errors = [...(await t.throws(getBranches({options: {branches}})))];

Expand All @@ -184,7 +184,7 @@ test('Throw a SemanticReleaseError if there is duplicate branches', async t => {

test('Throw a SemanticReleaseError for each invalid branch name', async t => {
const branches = [{name: '~master', tags: []}, {name: '^master', tags: []}];
const getBranches = proxyquire('../../lib/branches', {'./get-tags': () => branches});
const getBranches = proxyquire('../../lib/branches', {'./get-tags': () => branches, './expand': () => []});

const errors = [...(await t.throws(getBranches({options: {branches}})))];

Expand Down
12 changes: 10 additions & 2 deletions test/branches/expand.test.js
@@ -1,24 +1,32 @@
import test from 'ava';
import expand from '../../lib/branches/expand';
import {gitRepo, gitCommits, gitCheckout} from '../helpers/git-utils';
import {gitRepo, gitCommits, gitCheckout, gitPush} from '../helpers/git-utils';

test('Expand branches defined with globs', async t => {
const {cwd} = await gitRepo();
const {cwd, repositoryUrl} = await gitRepo(true);
await gitCommits(['First'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
await gitCheckout('1.1.x', true, {cwd});
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, '1.1.x', {cwd});
await gitCheckout('1.x.x', true, {cwd});
await gitCommits(['Third'], {cwd});
await gitPush(repositoryUrl, '1.x.x', {cwd});
await gitCheckout('2.x', true, {cwd});
await gitCommits(['Fourth'], {cwd});
await gitPush(repositoryUrl, '2.x', {cwd});
await gitCheckout('next', true, {cwd});
await gitCommits(['Fifth'], {cwd});
await gitPush(repositoryUrl, 'next', {cwd});
await gitCheckout('pre/foo', true, {cwd});
await gitCommits(['Sixth'], {cwd});
await gitPush(repositoryUrl, 'pre/foo', {cwd});
await gitCheckout('pre/bar', true, {cwd});
await gitCommits(['Seventh'], {cwd});
await gitPush(repositoryUrl, 'pre/bar', {cwd});
await gitCheckout('beta', true, {cwd});
await gitCommits(['Eighth'], {cwd});
await gitPush(repositoryUrl, 'beta', {cwd});

const branches = [
// Should match all maintenance type branches
Expand Down
20 changes: 14 additions & 6 deletions test/git.test.js
Expand Up @@ -58,18 +58,23 @@ test('Unshallow and fetch repository', async t => {
// Verify the shallow clone contains only one commit
t.is((await gitGetCommits(undefined, {cwd})).length, 1);

await fetch({cwd});
await fetch('master', {cwd});

// Verify the shallow clone contains all the commits
t.is((await gitGetCommits(undefined, {cwd})).length, 2);
});

test('Do not throw error when unshallow a complete repository', async t => {
// Create a git repository, set the current working directory at the root of the repo
const {cwd} = await gitRepo(true);
// Add commits to the master branch
const {cwd, repositoryUrl} = await gitRepo(true);
await gitCommits(['First'], {cwd});
await t.notThrows(fetch({cwd}));
await gitPush(repositoryUrl, 'master', {cwd});
await gitCheckout('second-branch', true, {cwd});
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, 'second-branch', {cwd});

await t.notThrows(fetch('master', {cwd}));
await t.notThrows(fetch('second-branch', {cwd}));
});

test('Fetch all tags on a detached head repository', async t => {
Expand All @@ -84,7 +89,7 @@ test('Fetch all tags on a detached head repository', async t => {
await gitPush(repositoryUrl, 'master', {cwd});
cwd = await gitDetachedHead(repositoryUrl, commit.hash);

await fetch({cwd});
await fetch('master', {cwd});

t.deepEqual((await getTags({cwd})).sort(), ['v1.0.0', 'v1.0.1', 'v1.1.0'].sort());
});
Expand Down Expand Up @@ -122,12 +127,15 @@ test('Verify if a branch exists', async t => {
});

test('Get all branches', async t => {
const {cwd} = await gitRepo();
const {cwd, repositoryUrl} = await gitRepo(true);
await gitCommits(['First'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
await gitCheckout('second-branch', true, {cwd});
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, 'second-branch', {cwd});
await gitCheckout('third-branch', true, {cwd});
await gitCommits(['Third'], {cwd});
await gitPush(repositoryUrl, 'third-branch', {cwd});

t.deepEqual((await getBranches({cwd})).sort(), ['master', 'second-branch', 'third-branch'].sort());
});
Expand Down

0 comments on commit b729183

Please sign in to comment.