From 739831dbb3c4c1bfac726aa0944bb8c4d376d2c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Louv-Jansen?= Date: Sun, 9 Oct 2022 21:47:11 +0200 Subject: [PATCH] Add better logging (#95) --- action.yml | 1 + dist/index.js | 37 +++++++++++++++++++++++++++++++------ dist/run.js | 35 +++++++++++++++++++++++++++++++---- dist/run.test.js | 3 +++ src/index.ts | 4 ++-- src/run.test.ts | 4 ++++ src/run.ts | 26 ++++++++++++++------------ 7 files changed, 86 insertions(+), 24 deletions(-) diff --git a/action.yml b/action.yml index fcfc2b0..b6ec00f 100644 --- a/action.yml +++ b/action.yml @@ -13,6 +13,7 @@ inputs: add_original_reviewers: description: Boolean, if set to true the reviewers of the original PR will also be added to the backport PRs required: false + default: false runs: using: 'node16' main: 'dist/index.js' diff --git a/dist/index.js b/dist/index.js index a8d6669..71d8bd6 100644 --- a/dist/index.js +++ b/dist/index.js @@ -176261,7 +176261,6 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -/* eslint-disable no-console */ const core = __importStar(__nccwpck_require__(42186)); const github_1 = __nccwpck_require__(95438); const run_1 = __nccwpck_require__(7764); @@ -176283,6 +176282,7 @@ const run_1 = __nccwpck_require__(7764); }, }) .then((res) => { + core.info(`Backport success: ${res.status}`); core.setOutput('Result', res); const failureMessage = (0, run_1.getFailureMessage)(res); if (failureMessage) { @@ -176290,7 +176290,7 @@ const run_1 = __nccwpck_require__(7764); } }) .catch((error) => { - console.error('An error occurred while backporting', error); + core.error(`Backport failure: ${error.message}`); core.setFailed(error.message); }); @@ -176298,14 +176298,39 @@ const run_1 = __nccwpck_require__(7764); /***/ }), /***/ 7764: -/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => { +/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; Object.defineProperty(exports, "__esModule", ({ value: true })); exports.getFailureMessage = exports.run = void 0; +const core = __importStar(__nccwpck_require__(42186)); const backport_1 = __nccwpck_require__(76253); async function run({ context, inputs, }) { + core.info('Initiate backport'); const { payload, repo, runId } = context; const pullRequest = payload.pull_request; if (!pullRequest) { @@ -176322,14 +176347,14 @@ async function run({ context, inputs, }) { const reviewers = inputs.addOriginalReviewers && Array.isArray(requestedReviewers) ? requestedReviewers.map((reviewer) => reviewer.login) : []; - console.log({ + core.info(JSON.stringify({ assignees, branchLabelMapping, pullNumber, repo, repoForkOwner, reviewers, - }); + })); // support for Github enterprise const gitHostname = context.serverUrl.replace(/^https{0,1}:\/\//, ''); const result = await (0, backport_1.backportRun)({ @@ -176351,7 +176376,7 @@ async function run({ context, inputs, }) { }, exitCodeOnFailure: false, }); - console.log('Result', JSON.stringify(result, null, 2)); + core.info(`Result ${JSON.stringify(result, null, 2)}`); return result; } exports.run = run; diff --git a/dist/run.js b/dist/run.js index 8bb7ef2..adf3082 100644 --- a/dist/run.js +++ b/dist/run.js @@ -1,8 +1,33 @@ "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; Object.defineProperty(exports, "__esModule", { value: true }); exports.getFailureMessage = exports.run = void 0; +const core = __importStar(require("@actions/core")); const backport_1 = require("backport"); async function run({ context, inputs, }) { + core.info('Initiate backport'); const { payload, repo, runId } = context; const pullRequest = payload.pull_request; if (!pullRequest) { @@ -19,17 +44,19 @@ async function run({ context, inputs, }) { const reviewers = inputs.addOriginalReviewers && Array.isArray(requestedReviewers) ? requestedReviewers.map((reviewer) => reviewer.login) : []; - console.log({ + core.info(JSON.stringify({ assignees, branchLabelMapping, pullNumber, repo, repoForkOwner, reviewers, - }); + })); + // support for Github enterprise + const gitHostname = context.serverUrl.replace(/^https{0,1}:\/\//, ''); const result = await (0, backport_1.backportRun)({ options: { - gitHostname: context.serverUrl.replace(/^https{0,1}:\/\//, ''), + gitHostname, accessToken: inputs.accessToken, assignees, branchLabelMapping, @@ -46,7 +73,7 @@ async function run({ context, inputs, }) { }, exitCodeOnFailure: false, }); - console.log('Result', JSON.stringify(result, null, 2)); + core.info(`Result ${JSON.stringify(result, null, 2)}`); return result; } exports.run = run; diff --git a/dist/run.test.js b/dist/run.test.js index 06383f9..4edc0b0 100644 --- a/dist/run.test.js +++ b/dist/run.test.js @@ -23,6 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", { value: true }); +const core = __importStar(require("@actions/core")); const backport = __importStar(require("backport")); const run_1 = require("./run"); describe('run', () => { @@ -31,6 +32,8 @@ describe('run', () => { .spyOn(backport, 'backportRun') // @ts-expect-error .mockResolvedValue('backport return value'); + // disable logs + jest.spyOn(core, 'info').mockReturnValue(); await (0, run_1.run)({ inputs: { accessToken: 'very-secret', diff --git a/src/index.ts b/src/index.ts index d4607e1..0a3c30b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,3 @@ -/* eslint-disable no-console */ import * as core from '@actions/core'; import { context } from '@actions/github'; import { getFailureMessage, run } from './run'; @@ -21,6 +20,7 @@ run({ }, }) .then((res) => { + core.info(`Backport success: ${res.status}`); core.setOutput('Result', res); const failureMessage = getFailureMessage(res); if (failureMessage) { @@ -28,6 +28,6 @@ run({ } }) .catch((error) => { - console.error('An error occurred while backporting', error); + core.error(`Backport failure: ${error.message}`); core.setFailed(error.message); }); diff --git a/src/run.test.ts b/src/run.test.ts index 2f1e6a2..beaad83 100644 --- a/src/run.test.ts +++ b/src/run.test.ts @@ -1,3 +1,4 @@ +import * as core from '@actions/core'; import { Context } from '@actions/github/lib/context'; import * as backport from 'backport'; import { getFailureMessage, run } from './run'; @@ -9,6 +10,9 @@ describe('run', () => { // @ts-expect-error .mockResolvedValue('backport return value'); + // disable logs + jest.spyOn(core, 'info').mockReturnValue(); + await run({ inputs: { accessToken: 'very-secret', diff --git a/src/run.ts b/src/run.ts index 704cc75..a29d5ad 100644 --- a/src/run.ts +++ b/src/run.ts @@ -1,4 +1,4 @@ -/* eslint-disable no-console */ +import * as core from '@actions/core'; import { Context } from '@actions/github/lib/context'; import { BackportResponse, backportRun, UnhandledErrorResult } from 'backport'; @@ -14,6 +14,8 @@ export async function run({ addOriginalReviewers: boolean; }; }) { + core.info('Initiate backport'); + const { payload, repo, runId } = context; const pullRequest = payload.pull_request; @@ -41,18 +43,19 @@ export async function run({ ? requestedReviewers.map((reviewer) => reviewer.login) : []; - console.log({ - assignees, - branchLabelMapping, - pullNumber, - repo, - repoForkOwner, - reviewers, - }); + core.info( + JSON.stringify({ + assignees, + branchLabelMapping, + pullNumber, + repo, + repoForkOwner, + reviewers, + }) + ); // support for Github enterprise const gitHostname = context.serverUrl.replace(/^https{0,1}:\/\//, ''); - const result = await backportRun({ options: { gitHostname, @@ -73,8 +76,7 @@ export async function run({ exitCodeOnFailure: false, }); - console.log('Result', JSON.stringify(result, null, 2)); - + core.info(`Result ${JSON.stringify(result, null, 2)}`); return result; }