From 36104229593c167e9086bc5fd8a533117ee3b579 Mon Sep 17 00:00:00 2001 From: jmodjeski75 <13580368+jmodjeski75@users.noreply.github.com> Date: Wed, 5 Jun 2019 13:33:54 -0500 Subject: [PATCH] fix(repositoryUrl): on beta repositoryUrl needs auth for pre-release flows (#1186) --- index.js | 7 +++---- lib/branches/expand.js | 4 ++-- lib/branches/index.js | 5 +++-- lib/git.js | 21 +++++++++++++------- test/branches/branches.test.js | 36 +++++++++++++++++----------------- test/branches/expand.test.js | 2 +- test/git.test.js | 18 ++++++++--------- test/index.test.js | 2 +- 8 files changed, 51 insertions(+), 44 deletions(-) diff --git a/index.js b/index.js index 309741539c..9bad0aca62 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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 diff --git a/lib/branches/expand.js b/lib/branches/expand.js index c40632acb2..bf0d1da12a 100644 --- a/lib/branches/expand.js +++ b/lib/branches/expand.js @@ -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) => [ diff --git a/lib/branches/index.js b/lib/branches/index.js index 8f586f692b..044289e821 100644 --- a/lib/branches/index.js +++ b/lib/branches/index.js @@ -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); diff --git a/lib/git.js b/lib/git.js index 896a5daf76..905f953fca 100644 --- a/lib/git.js +++ b/lib/git.js @@ -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} 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); @@ -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; @@ -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 ); } @@ -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) { diff --git a/test/branches/branches.test.js b/test/branches/branches.test.js index 0bddf703d3..339099ef91 100644 --- a/test/branches/branches.test.js +++ b/test/branches/branches.test.js @@ -24,7 +24,7 @@ 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'); @@ -32,43 +32,43 @@ test('Enforce ranges with branching release workflow', async t => { 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, @@ -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, @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); diff --git a/test/branches/expand.test.js b/test/branches/expand.test.js index 285aaac061..a912e193da 100644 --- a/test/branches/expand.test.js +++ b/test/branches/expand.test.js @@ -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'}, diff --git a/test/git.test.js b/test/git.test.js index 37e48b8023..d804966d81 100644 --- a/test/git.test.js +++ b/test/git.test.js @@ -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); @@ -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 => { @@ -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()); }); @@ -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 => { @@ -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 => { @@ -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 => { @@ -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})); }); diff --git a/test/index.test.js b/test/index.test.js index 3144446198..92b8e677ef 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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, });