From cd9f2bdd4428c0ac83f077b37bbe19fbda6bd773 Mon Sep 17 00:00:00 2001 From: pvdlg Date: Fri, 4 May 2018 11:38:11 -0400 Subject: [PATCH] feat: log `git` error message when authentication verification fails --- index.js | 5 ++++- lib/get-git-auth-url.js | 5 ++++- lib/git.js | 5 +++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 2abb32b6..ca618672 100644 --- a/index.js +++ b/index.js @@ -56,7 +56,10 @@ async function run(options, plugins) { return false; } - if (!await verifyAuth(options.repositoryUrl, options.branch)) { + try { + await verifyAuth(options.repositoryUrl, options.branch); + } catch (err) { + logger.error(`The command "${err.cmd}" failed with the error message %s.`, err.stderr); throw getError('EGITNOPERMISSION', {options}); } diff --git a/lib/get-git-auth-url.js b/lib/get-git-auth-url.js index 017dfd9e..a52b44ef 100644 --- a/lib/get-git-auth-url.js +++ b/lib/get-git-auth-url.js @@ -43,7 +43,9 @@ module.exports = async ({repositoryUrl, branch}) => { } // Test if push is allowed without transforming the URL (e.g. is ssh keys are set up) - if (!await verifyAuth(repositoryUrl, branch)) { + try { + await verifyAuth(repositoryUrl, branch); + } catch (err) { const envVar = Object.keys(GIT_TOKENS).find(envVar => !isUndefined(process.env[envVar])); const gitCredentials = `${GIT_TOKENS[envVar] || ''}${process.env[envVar] || ''}`; const {protocols, ...parsed} = gitUrlParse(repositoryUrl); @@ -52,5 +54,6 @@ module.exports = async ({repositoryUrl, branch}) => { // If credentials are set via anvironment variables, convert the URL to http/https and add basic auth, otherwise return `repositoryUrl` as is return gitCredentials ? {...parsed, protocols: [protocol], user: gitCredentials}.toString(protocol) : repositoryUrl; } + return repositoryUrl; }; diff --git a/lib/git.js b/lib/git.js index b59bef79..ad6b4b30 100644 --- a/lib/git.js +++ b/lib/git.js @@ -86,13 +86,14 @@ async function isGitRepo() { * @param {String} repositoryUrl The remote repository URL. * @param {String} branch The repositoru branch for which to verify write access. * - * @return {Boolean} `true` is authorized to push, falsy otherwise. + * @throws {Error} if not authorized to push. */ async function verifyAuth(repositoryUrl, branch) { try { - return (await execa('git', ['push', '--dry-run', repositoryUrl, `HEAD:${branch}`])).code === 0; + await execa('git', ['push', '--dry-run', repositoryUrl, `HEAD:${branch}`]); } catch (err) { debug(err); + throw err; } }