From 0e5ba427f4ace54cf05c4268d524aed07f9ed399 Mon Sep 17 00:00:00 2001 From: Rishav Dhar <19497993+RDhar@users.noreply.github.com> Date: Tue, 2 Jul 2024 01:04:22 +0100 Subject: [PATCH] feat: add support for `merge_group` trigger (#244) Signed-off-by: Rishav Dhar <19497993+RDhar@users.noreply.github.com> --- action.yml | 11 ++++++++++- scripts/comment_tf_output.js | 17 +++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/action.yml b/action.yml index df4442bf..3310cac1 100644 --- a/action.yml +++ b/action.yml @@ -136,6 +136,15 @@ runs: retries: 3 script: await require(`${process.env.GITHUB_ACTION_PATH}/scripts/check_pr_approval.js`)({ github, context, core }); + # Get the PR number from non-`pull_request` or non-`issue_comment` events. + - name: Get PR number + if: github.event_name != 'pull_request' && github.event_name != 'issue_comment' + shell: bash + run: | + if [ "${{ github.event_name }}" == "merge_group" ]; then + echo "PR_NUMBER=$(echo $GITHUB_REF | grep -oP 'pr-\K\d+')" >> $GITHUB_ENV + fi + # For all supplied TF arguments, process each one in the format "-key=value". # E.g., "-chdir=path/to/dir", "-auto-approve", etc. - name: Process TF arguments @@ -211,7 +220,7 @@ runs: echo "tf_cwd=$arg_chdir" >> $GITHUB_OUTPUT # Store a combination of the PR number and TF command arguments # for use as a unique identifier to reference the TF plan file. - echo "tf_plan_id=$(echo ${{ github.event.number || github.event.issue.number }}$arg_backend_config$arg_chdir$arg_var_file$arg_workspace$arg_destroy$arg_tf_cli-tfplan | sed 's/[[:space:][:punct:]]/-/g')" >> $GITHUB_OUTPUT + echo "tf_plan_id=$(echo ${{ github.event.number || github.event.issue.number || env.PR_NUMBER }}$arg_backend_config$arg_chdir$arg_var_file$arg_workspace$arg_destroy$arg_tf_cli-tfplan | sed 's/[[:space:][:punct:]]/-/g')" >> $GITHUB_OUTPUT # If "-backend-config" argument is present, then include any prefix and suffix. if [ -n "$arg_backend_config" ]; then echo "arg_backend_config=-backend-config=${{ inputs.backend_config_prefix }}$arg_backend_config${{ inputs.backend_config_suffix }}" >> $GITHUB_OUTPUT; fi # If "-var-file" argument is present, then include any prefix and suffix. diff --git a/scripts/comment_tf_output.js b/scripts/comment_tf_output.js index 8de27b31..1abd1200 100644 --- a/scripts/comment_tf_output.js +++ b/scripts/comment_tf_output.js @@ -65,8 +65,7 @@ ${process.env.tf_fmt} if (diff_line.includes(" created")) return "+ " + diff_line; if (diff_line.includes(" destroyed")) return "- " + diff_line; if (diff_line.includes(" updated") || diff_line.includes(" replaced")) return "! " + diff_line; - if (diff_line.includes(" been")) return "# " + diff_line; - return diff_line; + return "# " + diff_line; }); // Create a collapsible summary of changes if any. @@ -84,7 +83,7 @@ ${changed_lines.join("\n").substring(0, 12000)} const comment_output = `
${comment_summary}
-###### ${context.workflow} by @${context.actor} via [${context.eventName}](${check_url}) at ${context.payload.pull_request?.updated_at || context.payload.comment?.updated_at}.
+###### ${context.workflow} by @${context.actor} via [${context.eventName}](${check_url}) at ${context.payload.pull_request?.updated_at || context.payload.comment?.updated_at || context.payload.merge_group?.head_commit.timestamp}. \`\`\`hcl ${process.env.tf_output} @@ -109,9 +108,15 @@ ${comment_output} core.summary.addRaw(comment_body); core.summary.write(); + // Determine PR number from non-'pull_request' or non-'issue_comment' events. + let pr_number = ''; + if (context.eventName === "merge_group") { + pr_number = parseInt(context.ref.split("/pr-")[1]); + } + // Check if the bot has commented on the PR using the TFPLAN identifier. const { data: list_comments } = await github.rest.issues.listComments({ - issue_number: context.issue.number, + issue_number: context.issue.number || pr_number, owner: context.repo.owner, per_page: 100, repo: context.repo.repo, @@ -139,7 +144,7 @@ ${comment_output} }); const { data: pr_comment } = await github.rest.issues.createComment({ ...comment_parameters, - issue_number: context.issue.number, + issue_number: context.issue.number || pr_number, }); core.setOutput("id", pr_comment.id); } else { @@ -152,7 +157,7 @@ ${comment_output} } else { const { data: pr_comment } = await github.rest.issues.createComment({ ...comment_parameters, - issue_number: context.issue.number, + issue_number: context.issue.number || pr_number, }); core.setOutput("id", pr_comment.id); }