From 506aec6a176d71b6c5c3cbfdf03432e8a49e1cf4 Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 4 Dec 2024 23:02:34 +1100 Subject: [PATCH] fix: Autodetect PR Triggering Context Only attempt commenting if in a pr context. Otherwise don't --- action.yml | 4 --- dist/index.js | 66 +++++++++++++++++++++++++------------------------- src/comment.ts | 1 - src/index.ts | 7 +++--- 4 files changed, 37 insertions(+), 41 deletions(-) diff --git a/action.yml b/action.yml index 18875c7..7f21770 100644 --- a/action.yml +++ b/action.yml @@ -27,10 +27,6 @@ inputs: description: Whether to skip posting a pull request comment when no changes need to be performed. required: true default: "false" - should-comment: - description: Whether to attempt outputting the terraform plan as a PR comment - required: false - default: "true" outputs: plan-markdown: description: The raw markdown output of the terraform plan diff --git a/dist/index.js b/dist/index.js index 559a52b..5c9ddd8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4281,18 +4281,18 @@ var require_webidl = __commonJS({ webidl.errors.exception = function(message) { return new TypeError(`${message.header}: ${message.message}`); }; - webidl.errors.conversionFailed = function(context2) { - const plural = context2.types.length === 1 ? "" : " one of"; - const message = `${context2.argument} could not be converted to${plural}: ${context2.types.join(", ")}.`; + webidl.errors.conversionFailed = function(context3) { + const plural = context3.types.length === 1 ? "" : " one of"; + const message = `${context3.argument} could not be converted to${plural}: ${context3.types.join(", ")}.`; return webidl.errors.exception({ - header: context2.prefix, + header: context3.prefix, message }); }; - webidl.errors.invalidArgument = function(context2) { + webidl.errors.invalidArgument = function(context3) { return webidl.errors.exception({ - header: context2.prefix, - message: `"${context2.value}" is an invalid ${context2.type}.` + header: context3.prefix, + message: `"${context3.value}" is an invalid ${context3.type}.` }); }; webidl.brandCheck = function(V, I, opts = void 0) { @@ -9609,15 +9609,15 @@ var require_api_request = __commonJS({ } addSignal(this, signal); } - onConnect(abort, context2) { + onConnect(abort, context3) { if (!this.callback) { throw new RequestAbortedError(); } this.abort = abort; - this.context = context2; + this.context = context3; } onHeaders(statusCode, rawHeaders, resume, statusMessage) { - const { callback, opaque, abort, context: context2, responseHeaders, highWaterMark } = this; + const { callback, opaque, abort, context: context3, responseHeaders, highWaterMark } = this; const headers = responseHeaders === "raw" ? util2.parseRawHeaders(rawHeaders) : util2.parseHeaders(rawHeaders); if (statusCode < 200) { if (this.onInfo) { @@ -9644,7 +9644,7 @@ var require_api_request = __commonJS({ trailers: this.trailers, opaque, body, - context: context2 + context: context3 }); } } @@ -9764,15 +9764,15 @@ var require_api_stream = __commonJS({ } addSignal(this, signal); } - onConnect(abort, context2) { + onConnect(abort, context3) { if (!this.callback) { throw new RequestAbortedError(); } this.abort = abort; - this.context = context2; + this.context = context3; } onHeaders(statusCode, rawHeaders, resume, statusMessage) { - const { factory, opaque, context: context2, callback, responseHeaders } = this; + const { factory, opaque, context: context3, callback, responseHeaders } = this; const headers = responseHeaders === "raw" ? util2.parseRawHeaders(rawHeaders) : util2.parseHeaders(rawHeaders); if (statusCode < 200) { if (this.onInfo) { @@ -9800,7 +9800,7 @@ var require_api_stream = __commonJS({ statusCode, headers, opaque, - context: context2 + context: context3 }); if (!res || typeof res.write !== "function" || typeof res.end !== "function" || typeof res.on !== "function") { throw new InvalidReturnValueError("expected Writable"); @@ -9992,17 +9992,17 @@ var require_api_pipeline = __commonJS({ this.res = null; addSignal(this, signal); } - onConnect(abort, context2) { + onConnect(abort, context3) { const { ret, res } = this; assert(!res, "pipeline cannot be retried"); if (ret.destroyed) { throw new RequestAbortedError(); } this.abort = abort; - this.context = context2; + this.context = context3; } onHeaders(statusCode, rawHeaders, resume) { - const { opaque, handler: handler2, context: context2 } = this; + const { opaque, handler: handler2, context: context3 } = this; if (statusCode < 200) { if (this.onInfo) { const headers = this.responseHeaders === "raw" ? util2.parseRawHeaders(rawHeaders) : util2.parseHeaders(rawHeaders); @@ -10020,7 +10020,7 @@ var require_api_pipeline = __commonJS({ headers, opaque, body: this.res, - context: context2 + context: context3 }); } catch (err) { this.res.on("error", util2.nop); @@ -10104,7 +10104,7 @@ var require_api_upgrade = __commonJS({ this.context = null; addSignal(this, signal); } - onConnect(abort, context2) { + onConnect(abort, context3) { if (!this.callback) { throw new RequestAbortedError(); } @@ -10115,7 +10115,7 @@ var require_api_upgrade = __commonJS({ throw new SocketError("bad upgrade", null); } onUpgrade(statusCode, rawHeaders, socket) { - const { callback, opaque, context: context2 } = this; + const { callback, opaque, context: context3 } = this; assert.strictEqual(statusCode, 101); removeSignal(this); this.callback = null; @@ -10124,7 +10124,7 @@ var require_api_upgrade = __commonJS({ headers, socket, opaque, - context: context2 + context: context3 }); } onError(err) { @@ -10192,18 +10192,18 @@ var require_api_connect = __commonJS({ this.abort = null; addSignal(this, signal); } - onConnect(abort, context2) { + onConnect(abort, context3) { if (!this.callback) { throw new RequestAbortedError(); } this.abort = abort; - this.context = context2; + this.context = context3; } onHeaders() { throw new SocketError("bad connect", null); } onUpgrade(statusCode, rawHeaders, socket) { - const { callback, opaque, context: context2 } = this; + const { callback, opaque, context: context3 } = this; removeSignal(this); this.callback = null; let headers = rawHeaders; @@ -10215,7 +10215,7 @@ var require_api_connect = __commonJS({ headers, socket, opaque, - context: context2 + context: context3 }); } onError(err) { @@ -21083,8 +21083,8 @@ function isDefined(value) { function isKeyOperator(operator) { return operator === ";" || operator === "&" || operator === "?"; } -function getValues(context2, operator, key, modifier) { - var value = context2[key], result = []; +function getValues(context3, operator, key, modifier) { + var value = context3[key], result = []; if (isDefined(value) && value !== "") { if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") { value = value.toString(); @@ -21148,7 +21148,7 @@ function parseUrl(template) { expand: expand.bind(null, template) }; } -function expand(template, context2) { +function expand(template, context3) { var operators = ["+", "#", ".", "/", ";", "?", "&"]; template = template.replace( /\{([^\{\}]+)\}|([^\{\}]+)/g, @@ -21162,7 +21162,7 @@ function expand(template, context2) { } expression.split(/,/g).forEach(function(variable) { var tmp = /([^:\*]*)(?::(\d+)|(\*))?/.exec(variable); - values.push(getValues(context2, operator, tmp[1], tmp[2] || tmp[3])); + values.push(getValues(context3, operator, tmp[1], tmp[2] || tmp[3])); }); if (operator && operator !== "+") { var separator = ","; @@ -30686,8 +30686,7 @@ async function run() { terraformCmd: core.getInput("terraform-cmd", { required: true }), workingDirectory: core.getInput("working-directory", { required: true }), header: core.getInput("header", { required: true }), - skipEmpty: core.getBooleanInput("skip-empty", { required: true }), - shouldComment: core.getInput("should-comment", { required: false }) + skipEmpty: core.getBooleanInput("skip-empty", { required: true }) }; const octokit = github2.getOctokit(inputs.token); const plan = await core.group( @@ -30699,12 +30698,13 @@ async function run() { }) ); if (!inputs.skipEmpty || !planIsEmpty(plan)) { + const prContext = "pull_request" in github2.context.payload; const planMarkdown = await core.group("Render plan diff markdown", () => { const markdown = renderMarkdown({ plan, header: inputs.header }); core.setOutput("plan-markdown", markdown); return Promise.resolve(markdown); }); - if (inputs.shouldComment === "true") { + if (prContext === true) { await core.group("Render comment", () => { return createOrUpdateComment({ octokit, content: planMarkdown }); }); diff --git a/src/comment.ts b/src/comment.ts index 0f673d6..3da29aa 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -1,4 +1,3 @@ -import * as core from '@actions/core' import type { GitHub } from '@actions/github/lib/utils' import * as github from '@actions/github' import type { PullRequestEvent } from '@octokit/webhooks-types' diff --git a/src/index.ts b/src/index.ts index 4431e1c..c8134d2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,8 +11,7 @@ async function run() { terraformCmd: core.getInput('terraform-cmd', { required: true }), workingDirectory: core.getInput('working-directory', { required: true }), header: core.getInput('header', { required: true }), - skipEmpty: core.getBooleanInput('skip-empty', { required: true }), - shouldComment: core.getInput('should-comment', { required: false }) + skipEmpty: core.getBooleanInput('skip-empty', { required: true }) } const octokit = github.getOctokit(inputs.token) @@ -26,6 +25,8 @@ async function run() { ) if (!inputs.skipEmpty || !planIsEmpty(plan)) { + const prContext = 'pull_request' in github.context.payload + // 3) Render the plan diff markdown and set it as output const planMarkdown = await core.group('Render plan diff markdown', () => { const markdown = renderMarkdown({ plan, header: inputs.header }) @@ -34,7 +35,7 @@ async function run() { }) // 4) Post comment with markdown (if applicable) - if (inputs.shouldComment === 'true') { + if (prContext === true) { await core.group('Render comment', () => { return createOrUpdateComment({ octokit, content: planMarkdown }) })