Skip to content

Commit

Permalink
fix(repositoryUrl): on beta repositoryUrl needs auth for pre-release …
Browse files Browse the repository at this point in the history
…flows (#1186)
  • Loading branch information
jmodjeski75 authored and gr2m committed Jun 5, 2019
1 parent 37bcc9e commit 3610422
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 44 deletions.
7 changes: 3 additions & 4 deletions index.js
Expand Up @@ -53,7 +53,8 @@ async function run(context, plugins) {
// Verify config
await verify(context);

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

if (!context.branch) {
Expand All @@ -69,13 +70,11 @@ async function run(context, plugins) {
`Run automated release from branch ${ciBranch}${options.dryRun ? ' in dry-run mode' : ''}`
);

options.repositoryUrl = await getGitAuthUrl(context);

try {
try {
await verifyAuth(options.repositoryUrl, context.branch.name, {cwd, env});
} catch (error) {
if (!(await isBranchUpToDate(context.branch.name, {cwd, env}))) {
if (!(await isBranchUpToDate(options.repositoryUrl, context.branch.name, {cwd, env}))) {
logger.log(
`The local branch ${
context.branch.name
Expand Down
4 changes: 2 additions & 2 deletions lib/branches/expand.js
Expand Up @@ -2,8 +2,8 @@ const {isString, remove, omit, mapValues, template} = require('lodash');
const micromatch = require('micromatch');
const {getBranches} = require('../git');

module.exports = async ({cwd}, branches) => {
const gitBranches = await getBranches({cwd});
module.exports = async (repositoryUrl, {cwd}, branches) => {
const gitBranches = await getBranches(repositoryUrl, {cwd});

return branches.reduce(
(branches, branch) => [
Expand Down
5 changes: 3 additions & 2 deletions lib/branches/index.js
Expand Up @@ -8,16 +8,17 @@ const expand = require('./expand');
const getTags = require('./get-tags');
const normalize = require('./normalize');

module.exports = async context => {
module.exports = async (repositoryUrl, context) => {
const {cwd, env} = context;

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

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

const branches = await getTags(context, remoteBranches);
Expand Down
21 changes: 14 additions & 7 deletions lib/git.js
Expand Up @@ -57,13 +57,14 @@ async function getCommits(from, to, execaOpts) {
/**
* Get all the repository branches.
*
* @param {String} repositoryUrl The remote repository URL.
* @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', ['ls-remote', '--heads', 'origin'], execaOpts))
async function getBranches(repositoryUrl, execaOpts) {
return (await execa.stdout('git', ['ls-remote', '--heads', repositoryUrl], execaOpts))
.split('\n')
.map(branch => branch.match(/^.+refs\/heads\/(.+)$/)[1])
.filter(Boolean);
Expand Down Expand Up @@ -127,10 +128,11 @@ async function isRefExists(ref, execaOpts) {
/**
* Unshallow the git repository if necessary and fetch all the tags.
*
* @param {String} repositoryUrl The remote repository URL.
* @param {String} branch The repository branch to fetch.
* @param {Object} [execaOpts] Options to pass to `execa`.
*/
async function fetch(branch, execaOpts) {
async function fetch(repositoryUrl, branch, execaOpts) {
const isLocalExists =
(await execa('git', ['rev-parse', '--verify', branch], {...execaOpts, reject: false})).code === 0;

Expand All @@ -141,14 +143,18 @@ async function fetch(branch, execaOpts) {
'fetch',
'--unshallow',
'--tags',
...(isLocalExists ? [] : ['origin', `+refs/heads/${branch}:refs/heads/${branch}`]),
...(isLocalExists ? [repositoryUrl] : [repositoryUrl, `+refs/heads/${branch}:refs/remotes/upstream/${branch}`]),
],
execaOpts
);
} catch (error) {
await execa(
'git',
['fetch', '--tags', ...(isLocalExists ? [] : ['origin', `+refs/heads/${branch}:refs/heads/${branch}`])],
[
'fetch',
'--tags',
...(isLocalExists ? [repositoryUrl] : [repositoryUrl, `+refs/heads/${branch}:refs/remotes/upstream/${branch}`]),
],
execaOpts
);
}
Expand Down Expand Up @@ -273,13 +279,14 @@ async function verifyBranchName(branch, execaOpts) {
/**
* Verify the local branch is up to date with the remote one.
*
* @param {String} repositoryUrl The remote repository URL.
* @param {String} branch The repository branch for which to verify status.
* @param {Object} [execaOpts] Options to pass to `execa`.
*
* @return {Boolean} `true` is the HEAD of the current local branch is the same as the HEAD of the remote branch, falsy otherwise.
*/
async function isBranchUpToDate(branch, execaOpts) {
const remoteHead = await execa.stdout('git', ['ls-remote', '--heads', 'origin', branch], execaOpts);
async function isBranchUpToDate(repositoryUrl, branch, execaOpts) {
const remoteHead = await execa.stdout('git', ['ls-remote', '--heads', repositoryUrl, branch], execaOpts);
try {
return await isRefInHistory(remoteHead.match(/^(\w+)?/)[1], branch, false, execaOpts);
} catch (error) {
Expand Down
36 changes: 18 additions & 18 deletions test/branches/branches.test.js
Expand Up @@ -24,51 +24,51 @@ test('Enforce ranges with branching release workflow', async t => {
];
const getBranches = proxyquire('../../lib/branches', {'./get-tags': () => branches, './expand': () => []});

let result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
let result = (await getBranches('repositoryUrl', {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');
t.is(getBranch(result, '1.x').range, '>=1.1.0 <1.0.0', 'Cannot release on 1.x before a releasing on master');
t.is(getBranch(result, 'master').range, '>=1.0.0 <1.1.0', 'Can release only patch on master');
t.is(getBranch(result, 'next').range, '>=1.1.0 <2.0.0', 'Can release only minor on next');
t.is(getBranch(result, 'next-major').range, '>=2.0.0', 'Can release only major on next-major');

release(branches, 'master', '1.0.0');
result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
result = (await getBranches('repositoryUrl', {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');
t.is(getBranch(result, '1.x').range, '>=1.1.0 <1.0.0', 'Cannot release on 1.x before a releasing on master');
t.is(getBranch(result, 'master').range, '>=1.0.0 <1.1.0', 'Can release only patch on master');

release(branches, 'master', '1.0.1');
result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
result = (await getBranches('repositoryUrl', {options: {branches}})).map(({name, range}) => ({name, range}));
t.is(getBranch(result, 'master').range, '>=1.0.1 <1.1.0', 'Can release only patch, > than 1.0.1 on master');

merge(branches, 'master', 'next');
merge(branches, 'master', 'next-major');
result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
result = (await getBranches('repositoryUrl', {options: {branches}})).map(({name, range}) => ({name, range}));
t.is(getBranch(result, 'master').range, '>=1.0.1 <1.1.0', 'Can release only patch, > than 1.0.1 on master');
t.is(getBranch(result, 'next').range, '>=1.1.0 <2.0.0', 'Can release only minor on next');
t.is(getBranch(result, 'next-major').range, '>=2.0.0', 'Can release only major on next-major');

release(branches, 'next', '1.1.0');
release(branches, 'next', '1.1.1');
result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
result = (await getBranches('repositoryUrl', {options: {branches}})).map(({name, range}) => ({name, range}));
t.is(getBranch(result, 'next').range, '>=1.1.1 <2.0.0', 'Can release only patch or minor, > than 1.1.0 on next');

release(branches, 'next-major', '2.0.0');
release(branches, 'next-major', '2.0.1');
result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
result = (await getBranches('repositoryUrl', {options: {branches}})).map(({name, range}) => ({name, range}));
t.is(getBranch(result, 'next-major').range, '>=2.0.1', 'Can release any version, > than 2.0.1 on next-major');

merge(branches, 'next-major', 'beta');
release(branches, 'beta', '3.0.0-beta.1');
merge(branches, 'beta', 'alpha');
release(branches, 'alpha', '4.0.0-alpha.1');
result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
result = (await getBranches('repositoryUrl', {options: {branches}})).map(({name, range}) => ({name, range}));
t.is(getBranch(result, 'next-major').range, '>=2.0.1', 'Can release any version, > than 2.0.1 on next-major');

merge(branches, 'master', '1.0.x');
merge(branches, 'master', '1.x');
release(branches, 'master', '1.0.1');
result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
result = (await getBranches('repositoryUrl', {options: {branches}})).map(({name, range}) => ({name, range}));
t.is(getBranch(result, 'master').range, '>=1.0.1 <1.1.0', 'Can release only patch, > than 1.0.1 on master');
t.is(
getBranch(result, '1.0.x').range,
Expand All @@ -80,7 +80,7 @@ test('Enforce ranges with branching release workflow', async t => {
release(branches, 'master', '1.0.2');
release(branches, 'master', '1.0.3');
release(branches, 'master', '1.0.4');
result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
result = (await getBranches('repositoryUrl', {options: {branches}})).map(({name, range}) => ({name, range}));
t.is(getBranch(result, 'master').range, '>=1.0.4 <1.1.0', 'Can release only patch, > than 1.0.4 on master');
t.is(
getBranch(result, '1.0.x').range,
Expand All @@ -90,7 +90,7 @@ test('Enforce ranges with branching release workflow', async t => {
t.is(getBranch(result, '1.x').range, '>=1.1.0 <1.0.2', 'Cannot release on 1.x before >= 1.2.0 is released on master');

merge(branches, 'next', 'master');
result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
result = (await getBranches('repositoryUrl', {options: {branches}})).map(({name, range}) => ({name, range}));
t.is(getBranch(result, 'master').range, '>=1.1.1 <1.2.0', 'Can release only patch, > than 1.1.1 on master');
t.is(getBranch(result, 'next').range, '>=1.2.0 <2.0.0', 'Can release only patch or minor, > than 1.2.0 on next');
t.is(getBranch(result, 'next-major').range, '>=2.0.1', 'Can release any version, > than 2.0.1 on next-major');
Expand All @@ -102,34 +102,34 @@ test('Enforce ranges with branching release workflow', async t => {
t.is(getBranch(result, '1.x').range, '>=1.1.0 <1.0.2', 'Cannot release on 1.x before >= 2.0.0 is released on master');

merge(branches, 'master', '1.0.x', '1.0.4');
result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
result = (await getBranches('repositoryUrl', {options: {branches}})).map(({name, range}) => ({name, range}));
t.is(getBranch(result, 'master').range, '>=1.1.1 <1.2.0', 'Can release only patch, > than 1.1.1 on master');
t.is(getBranch(result, '1.0.x').range, '>=1.0.4 <1.1.0', 'Can release on 1.0.x only within range');
t.is(getBranch(result, '1.x').range, '>=1.1.0 <1.1.0', 'Cannot release on 1.x before >= 2.0.0 is released on master');

merge(branches, 'master', '1.x');
result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
result = (await getBranches('repositoryUrl', {options: {branches}})).map(({name, range}) => ({name, range}));
t.is(getBranch(result, 'master').range, '>=1.1.1 <1.2.0', 'Can release only patch, > than 1.1.1 on master');
t.is(getBranch(result, '1.0.x').range, '>=1.0.4 <1.1.0', 'Can release on 1.0.x only within range');
t.is(getBranch(result, '1.x').range, '>=1.1.1 <1.1.1', 'Cannot release on 1.x before >= 2.0.0 is released on master');

merge(branches, 'next-major', 'next');
merge(branches, 'next', 'master');
result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
result = (await getBranches('repositoryUrl', {options: {branches}})).map(({name, range}) => ({name, range}));
t.is(getBranch(result, 'master').range, '>=2.0.1 <2.1.0', 'Can release only patch, > than 2.0.1 on master');
t.is(getBranch(result, 'next').range, '>=2.1.0 <3.0.0', 'Can release only minor on next');
t.is(getBranch(result, 'next-major').range, '>=3.0.0', 'Can release only major on next-major');
t.is(getBranch(result, '1.x').range, '>=1.1.1 <2.0.0', 'Can release on 1.x only within range');

merge(branches, 'beta', 'master');
result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
result = (await getBranches('repositoryUrl', {options: {branches}})).map(({name, range}) => ({name, range}));
t.is(getBranch(result, 'master').range, '>=2.0.1 <2.1.0', 'Can release only patch, > than 2.0.1 on master');
t.is(getBranch(result, 'next').range, '>=2.1.0 <3.0.0', 'Can release only minor on next');
t.is(getBranch(result, 'next-major').range, '>=3.0.0', 'Can release only major on next-major');

branches.push({name: '1.1.x', tags: []});
merge(branches, '1.x', '1.1.x');
result = (await getBranches({options: {branches}})).map(({name, range}) => ({name, range}));
result = (await getBranches('repositoryUrl', {options: {branches}})).map(({name, range}) => ({name, range}));
t.is(getBranch(result, '1.0.x').range, '>=1.0.4 <1.1.0', 'Can release on 1.0.x only within range');
t.is(getBranch(result, '1.1.x').range, '>=1.1.1 <1.2.0', 'Can release on 1.1.x only within range');
t.is(getBranch(result, '1.x').range, '>=1.2.0 <2.0.0', 'Can release on 1.x only within range');
Expand All @@ -146,7 +146,7 @@ test('Throw SemanticReleaseError for invalid configurations', async t => {
{name: 'preview', prerelease: 'alpha', tags: []},
];
const getBranches = proxyquire('../../lib/branches', {'./get-tags': () => branches, './expand': () => []});
const errors = [...(await t.throwsAsync(getBranches({options: {branches}})))];
const errors = [...(await t.throwsAsync(getBranches('repositoryUrl', {options: {branches}})))];

t.is(errors[0].name, 'SemanticReleaseError');
t.is(errors[0].code, 'EMAINTENANCEBRANCH');
Expand Down Expand Up @@ -174,7 +174,7 @@ 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, './expand': () => []});

const errors = [...(await t.throwsAsync(getBranches({options: {branches}})))];
const errors = [...(await t.throwsAsync(getBranches('repositoryUrl', {options: {branches}})))];

t.is(errors[0].name, 'SemanticReleaseError');
t.is(errors[0].code, 'EDUPLICATEBRANCHES');
Expand All @@ -186,7 +186,7 @@ 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, './expand': () => []});

const errors = [...(await t.throwsAsync(getBranches({options: {branches}})))];
const errors = [...(await t.throwsAsync(getBranches('repositoryUrl', {options: {branches}})))];

t.is(errors[0].name, 'SemanticReleaseError');
t.is(errors[0].code, 'EINVALIDBRANCHNAME');
Expand Down
2 changes: 1 addition & 1 deletion test/branches/expand.test.js
Expand Up @@ -41,7 +41,7 @@ test('Expand branches defined with globs', async t => {
{name: 'beta', channel: `channel-\${name}`, prerelease: true},
];

t.deepEqual(await expand({cwd}, branches), [
t.deepEqual(await expand(repositoryUrl, {cwd}, branches), [
{name: '1.0.x'},
{name: '1.x.x'},
{name: '2.x'},
Expand Down
18 changes: 9 additions & 9 deletions test/git.test.js
Expand Up @@ -58,7 +58,7 @@ 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('master', {cwd});
await fetch(repositoryUrl, 'master', {cwd});

// Verify the shallow clone contains all the commits
t.is((await gitGetCommits(undefined, {cwd})).length, 2);
Expand All @@ -73,8 +73,8 @@ test('Do not throw error when unshallow a complete repository', async t => {
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, 'second-branch', {cwd});

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

test('Fetch all tags on a detached head repository', async t => {
Expand All @@ -89,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('master', {cwd});
await fetch(repositoryUrl, 'master', {cwd});

t.deepEqual((await getTags({cwd})).sort(), ['v1.0.0', 'v1.0.1', 'v1.1.0'].sort());
});
Expand Down Expand Up @@ -137,7 +137,7 @@ test('Get all branches', async t => {
await gitCommits(['Third'], {cwd});
await gitPush(repositoryUrl, 'third-branch', {cwd});

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

test('Get the commit sha for a given tag or falsy if the tag does not exists', async t => {
Expand Down Expand Up @@ -251,7 +251,7 @@ test('Return "true" if repository is up to date', async t => {
await gitCommits(['First'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});

t.true(await isBranchUpToDate('master', {cwd}));
t.true(await isBranchUpToDate(repositoryUrl, 'master', {cwd}));
});

test('Return falsy if repository is not up to date', async t => {
Expand All @@ -260,13 +260,13 @@ test('Return falsy if repository is not up to date', async t => {
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});

t.true(await isBranchUpToDate('master', {cwd}));
t.true(await isBranchUpToDate(repositoryUrl, 'master', {cwd}));

const tmpRepo = await gitShallowClone(repositoryUrl);
await gitCommits(['Third'], {cwd: tmpRepo});
await gitPush('origin', 'master', {cwd: tmpRepo});

t.falsy(await isBranchUpToDate('master', {cwd}));
t.falsy(await isBranchUpToDate(repositoryUrl, 'master', {cwd}));
});

test('Return "true" if local repository is ahead', async t => {
Expand All @@ -275,5 +275,5 @@ test('Return "true" if local repository is ahead', async t => {
await gitPush(repositoryUrl, 'master', {cwd});
await gitCommits(['Second'], {cwd});

t.true(await isBranchUpToDate('master', {cwd}));
t.true(await isBranchUpToDate(repositoryUrl, 'master', {cwd}));
});
2 changes: 1 addition & 1 deletion test/index.test.js
Expand Up @@ -353,7 +353,7 @@ test('Plugins are called with expected values', async t => {

t.deepEqual(result, {
lastRelease,
commits: [{...commits[0], gitTags: '(HEAD -> master, origin/master, origin/HEAD, next)'}],
commits: [{...commits[0], gitTags: '(HEAD -> master, next)'}],
nextRelease: {...nextRelease, notes: `${notes1}\n\n${notes2}\n\n${notes3}`},
releases,
});
Expand Down

0 comments on commit 3610422

Please sign in to comment.