From 595b523194b98a03da1f521d9524266e6cdf7cea Mon Sep 17 00:00:00 2001 From: Anders Matheson <67680649+anders-cognite@users.noreply.github.com> Date: Fri, 19 Mar 2021 10:05:28 +0100 Subject: [PATCH] Don't catch errors in inner functions (#10) --- dist/index.js | 266 +++++++++++++++++++++++++++++++++++--------------- src/main.ts | 92 ++++++++--------- 2 files changed, 229 insertions(+), 129 deletions(-) diff --git a/dist/index.js b/dist/index.js index 59ee085..2727651 100644 --- a/dist/index.js +++ b/dist/index.js @@ -466,6 +466,32 @@ function onceStrict (fn) { } +/***/ }), + +/***/ 82: +/***/ (function(__unusedmodule, exports) { + +"use strict"; + +// We use any as a valid input type +/* eslint-disable @typescript-eslint/no-explicit-any */ +Object.defineProperty(exports, "__esModule", { value: true }); +/** + * Sanitizes an input into a string so it can be passed into issueCommand safely + * @param input input to sanitize into a string + */ +function toCommandValue(input) { + if (input === null || input === undefined) { + return ''; + } + else if (typeof input === 'string' || input instanceof String) { + return input; + } + return JSON.stringify(input); +} +exports.toCommandValue = toCommandValue; +//# sourceMappingURL=utils.js.map + /***/ }), /***/ 87: @@ -475,6 +501,42 @@ module.exports = require("os"); /***/ }), +/***/ 102: +/***/ (function(__unusedmodule, exports, __webpack_require__) { + +"use strict"; + +// For internal use, subject to change. +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +// We use any as a valid input type +/* eslint-disable @typescript-eslint/no-explicit-any */ +const fs = __importStar(__webpack_require__(747)); +const os = __importStar(__webpack_require__(87)); +const utils_1 = __webpack_require__(82); +function issueCommand(command, message) { + const filePath = process.env[`GITHUB_${command}`]; + if (!filePath) { + throw new Error(`Unable to find environment variable for file command ${command}`); + } + if (!fs.existsSync(filePath)) { + throw new Error(`Missing file at path: ${filePath}`); + } + fs.appendFileSync(filePath, `${utils_1.toCommandValue(message)}${os.EOL}`, { + encoding: 'utf8' + }); +} +exports.issueCommand = issueCommand; +//# sourceMappingURL=file-command.js.map + +/***/ }), + /***/ 118: /***/ (function(module, __unusedexports, __webpack_require__) { @@ -1456,65 +1518,55 @@ const ALLOWED_COMMITTERS = [ const ALLOWED_REVIEWERS = ['github-actions[bot]'].reduce((acc, name) => (Object.assign(Object.assign({}, acc), { [name]: true })), {}); function all_committers_allowed(client, pr) { return __awaiter(this, void 0, void 0, function* () { - try { - // Get a pull request - const { data: pullRequest } = yield client.pulls.get({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - pull_number: pr.number, - }); - // Get creator of PR - const pr_user = pullRequest.user.login; - core.info(`PR #${pr.number} opened from ${pr_user}`); - // Get list of commits on a PR - const { data: listCommits } = yield client.pulls.listCommits({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - pull_number: pr.number, - }); - // Get all committers on a PR - for (let commit of listCommits) { - // Check if there are committers other than ALLOWED_COMMITTERS - if (!ALLOWED_COMMITTERS[commit.author.login]) { - core.info(`Commit ${commit.sha} is not from an approved source (${commit.author.login})`); - // Remove approvals by dependabot if any - yield remove_dependabot_approvals(client, pr); - return false; - } + // Get a pull request + const { data: pullRequest } = yield client.pulls.get({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + pull_number: pr.number, + }); + // Get creator of PR + const pr_user = pullRequest.user.login; + core.info(`PR #${pr.number} opened from ${pr_user}`); + // Get list of commits on a PR + const { data: listCommits } = yield client.pulls.listCommits({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + pull_number: pr.number, + }); + // Get all committers on a PR + for (let commit of listCommits) { + // Check if there are committers other than ALLOWED_COMMITTERS + if (!ALLOWED_COMMITTERS[commit.author.login]) { + core.info(`Commit ${commit.sha} is not from an approved source (${commit.author.login})`); + // Remove approvals by dependabot if any + yield remove_dependabot_approvals(client, pr); + return false; } } - catch (error) { - core.setFailed(error.message); - } return true; }); } function remove_dependabot_approvals(client, pr) { return __awaiter(this, void 0, void 0, function* () { - try { - // Get list of all reviews on a PR - const { data: listReviews } = yield client.pulls.listReviews({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - pull_number: pr.number, - }); - // Check if there is an approval by ALLOWED_REVIEWERS - for (let review of listReviews) { - if (ALLOWED_REVIEWERS[review.user.login] && review.state === `APPROVED`) { - core.info(`Removing an approval from ${review.user.login}`); - yield client.pulls.dismissReview({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - pull_number: pr.number, - review_id: review.id, - message: `A commit was added after a dependabot approval`, - }); - } + // Get list of all reviews on a PR + const { data: listReviews } = yield client.pulls.listReviews({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + pull_number: pr.number, + }); + // Check if there is an approval by ALLOWED_REVIEWERS + for (let review of listReviews) { + if (ALLOWED_REVIEWERS[review.user.login] && review.state === `APPROVED`) { + core.info(`Removing an approval from ${review.user.login}`); + yield client.pulls.dismissReview({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + pull_number: pr.number, + review_id: review.id, + message: `A commit was added after a dependabot approval`, + }); } } - catch (error) { - core.setFailed(error.message); - } }); } function run() { @@ -3706,17 +3758,25 @@ function octokitValidate(octokit) { "use strict"; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; Object.defineProperty(exports, "__esModule", { value: true }); -const os = __webpack_require__(87); +const os = __importStar(__webpack_require__(87)); +const utils_1 = __webpack_require__(82); /** * Commands * * Command Format: - * ##[name key=value;key=value]message + * ::name key=value,key=value::message * * Examples: - * ##[warning]This is the user warning message - * ##[set-secret name=mypassword]definitelyNotAPassword! + * ::warning::This is the message + * ::set-env name=MY_VAR::some value */ function issueCommand(command, properties, message) { const cmd = new Command(command, properties, message); @@ -3741,34 +3801,39 @@ class Command { let cmdStr = CMD_STRING + this.command; if (this.properties && Object.keys(this.properties).length > 0) { cmdStr += ' '; + let first = true; for (const key in this.properties) { if (this.properties.hasOwnProperty(key)) { const val = this.properties[key]; if (val) { - // safely append the val - avoid blowing up when attempting to - // call .replace() if message is not a string for some reason - cmdStr += `${key}=${escape(`${val || ''}`)},`; + if (first) { + first = false; + } + else { + cmdStr += ','; + } + cmdStr += `${key}=${escapeProperty(val)}`; } } } } - cmdStr += CMD_STRING; - // safely append the message - avoid blowing up when attempting to - // call .replace() if message is not a string for some reason - const message = `${this.message || ''}`; - cmdStr += escapeData(message); + cmdStr += `${CMD_STRING}${escapeData(this.message)}`; return cmdStr; } } function escapeData(s) { - return s.replace(/\r/g, '%0D').replace(/\n/g, '%0A'); + return utils_1.toCommandValue(s) + .replace(/%/g, '%25') + .replace(/\r/g, '%0D') + .replace(/\n/g, '%0A'); } -function escape(s) { - return s +function escapeProperty(s) { + return utils_1.toCommandValue(s) + .replace(/%/g, '%25') .replace(/\r/g, '%0D') .replace(/\n/g, '%0A') - .replace(/]/g, '%5D') - .replace(/;/g, '%3B'); + .replace(/:/g, '%3A') + .replace(/,/g, '%2C'); } //# sourceMappingURL=command.js.map @@ -5791,10 +5856,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; Object.defineProperty(exports, "__esModule", { value: true }); const command_1 = __webpack_require__(431); -const os = __webpack_require__(87); -const path = __webpack_require__(622); +const file_command_1 = __webpack_require__(102); +const utils_1 = __webpack_require__(82); +const os = __importStar(__webpack_require__(87)); +const path = __importStar(__webpack_require__(622)); /** * The code to exit an action */ @@ -5815,11 +5889,21 @@ var ExitCode; /** * Sets env variable for this action and future actions in the job * @param name the name of the variable to set - * @param val the value of the variable + * @param val the value of the variable. Non-string values will be converted to a string via JSON.stringify */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any function exportVariable(name, val) { - process.env[name] = val; - command_1.issueCommand('set-env', { name }, val); + const convertedVal = utils_1.toCommandValue(val); + process.env[name] = convertedVal; + const filePath = process.env['GITHUB_ENV'] || ''; + if (filePath) { + const delimiter = '_GitHubActionsFileCommandDelimeter_'; + const commandValue = `${name}<<${delimiter}${os.EOL}${convertedVal}${os.EOL}${delimiter}`; + file_command_1.issueCommand('ENV', commandValue); + } + else { + command_1.issueCommand('set-env', { name }, convertedVal); + } } exports.exportVariable = exportVariable; /** @@ -5835,7 +5919,13 @@ exports.setSecret = setSecret; * @param inputPath */ function addPath(inputPath) { - command_1.issueCommand('add-path', {}, inputPath); + const filePath = process.env['GITHUB_PATH'] || ''; + if (filePath) { + file_command_1.issueCommand('PATH', inputPath); + } + else { + command_1.issueCommand('add-path', {}, inputPath); + } process.env['PATH'] = `${inputPath}${path.delimiter}${process.env['PATH']}`; } exports.addPath = addPath; @@ -5858,12 +5948,22 @@ exports.getInput = getInput; * Sets the value of an output. * * @param name name of the output to set - * @param value value to store + * @param value value to store. Non-string values will be converted to a string via JSON.stringify */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any function setOutput(name, value) { command_1.issueCommand('set-output', { name }, value); } exports.setOutput = setOutput; +/** + * Enables or disables the echoing of commands into stdout for the rest of the step. + * Echoing is disabled by default if ACTIONS_STEP_DEBUG is not set. + * + */ +function setCommandEcho(enabled) { + command_1.issue('echo', enabled ? 'on' : 'off'); +} +exports.setCommandEcho = setCommandEcho; //----------------------------------------------------------------------- // Results //----------------------------------------------------------------------- @@ -5880,6 +5980,13 @@ exports.setFailed = setFailed; //----------------------------------------------------------------------- // Logging Commands //----------------------------------------------------------------------- +/** + * Gets whether Actions Step Debug is on or not + */ +function isDebug() { + return process.env['RUNNER_DEBUG'] === '1'; +} +exports.isDebug = isDebug; /** * Writes debug message to user log * @param message debug message @@ -5890,18 +5997,18 @@ function debug(message) { exports.debug = debug; /** * Adds an error issue - * @param message error issue message + * @param message error issue message. Errors will be converted to string via toString() */ function error(message) { - command_1.issue('error', message); + command_1.issue('error', message instanceof Error ? message.toString() : message); } exports.error = error; /** * Adds an warning issue - * @param message warning issue message + * @param message warning issue message. Errors will be converted to string via toString() */ function warning(message) { - command_1.issue('warning', message); + command_1.issue('warning', message instanceof Error ? message.toString() : message); } exports.warning = warning; /** @@ -5959,8 +6066,9 @@ exports.group = group; * Saves state for current action, the state can only be retrieved by this action's post job execution. * * @param name name of the state to store - * @param value value to store + * @param value value to store. Non-string values will be converted to a string via JSON.stringify */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any function saveState(name, value) { command_1.issueCommand('save-state', { name }, value); } diff --git a/src/main.ts b/src/main.ts index 645bb51..9b4242e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -12,68 +12,60 @@ const ALLOWED_REVIEWERS = ['github-actions[bot]'].reduce( ); async function all_committers_allowed(client: any, pr: any) { - try { - // Get a pull request - const { data: pullRequest } = await client.pulls.get({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - pull_number: pr.number, - }); + // Get a pull request + const { data: pullRequest } = await client.pulls.get({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + pull_number: pr.number, + }); - // Get creator of PR - const pr_user = pullRequest.user.login; + // Get creator of PR + const pr_user = pullRequest.user.login; - core.info(`PR #${pr.number} opened from ${pr_user}`); + core.info(`PR #${pr.number} opened from ${pr_user}`); - // Get list of commits on a PR - const { data: listCommits } = await client.pulls.listCommits({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - pull_number: pr.number, - }); + // Get list of commits on a PR + const { data: listCommits } = await client.pulls.listCommits({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + pull_number: pr.number, + }); - // Get all committers on a PR - for (let commit of listCommits) { - // Check if there are committers other than ALLOWED_COMMITTERS - if (!ALLOWED_COMMITTERS[commit.author.login]) { - core.info( - `Commit ${commit.sha} is not from an approved source (${commit.author.login})` - ); - // Remove approvals by dependabot if any - await remove_dependabot_approvals(client, pr); - return false; - } + // Get all committers on a PR + for (let commit of listCommits) { + // Check if there are committers other than ALLOWED_COMMITTERS + if (!ALLOWED_COMMITTERS[commit.author.login]) { + core.info( + `Commit ${commit.sha} is not from an approved source (${commit.author.login})` + ); + // Remove approvals by dependabot if any + await remove_dependabot_approvals(client, pr); + return false; } - } catch (error) { - core.setFailed(error.message); } return true; } async function remove_dependabot_approvals(client: any, pr: any) { - try { - // Get list of all reviews on a PR - const { data: listReviews } = await client.pulls.listReviews({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - pull_number: pr.number, - }); + // Get list of all reviews on a PR + const { data: listReviews } = await client.pulls.listReviews({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + pull_number: pr.number, + }); - // Check if there is an approval by ALLOWED_REVIEWERS - for (let review of listReviews) { - if (ALLOWED_REVIEWERS[review.user.login] && review.state === `APPROVED`) { - core.info(`Removing an approval from ${review.user.login}`); - await client.pulls.dismissReview({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - pull_number: pr.number, - review_id: review.id, - message: `A commit was added after a dependabot approval`, - }); - } + // Check if there is an approval by ALLOWED_REVIEWERS + for (let review of listReviews) { + if (ALLOWED_REVIEWERS[review.user.login] && review.state === `APPROVED`) { + core.info(`Removing an approval from ${review.user.login}`); + await client.pulls.dismissReview({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + pull_number: pr.number, + review_id: review.id, + message: `A commit was added after a dependabot approval`, + }); } - } catch (error) { - core.setFailed(error.message); } }