Skip to content

Commit

Permalink
feat: add support for merge_group trigger (#244)
Browse files Browse the repository at this point in the history
Signed-off-by: Rishav Dhar <19497993+RDhar@users.noreply.github.com>
  • Loading branch information
rdhar authored Jul 2, 2024
1 parent a28ac6b commit 0e5ba42
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
11 changes: 10 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 11 additions & 6 deletions scripts/comment_tf_output.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -84,7 +83,7 @@ ${changed_lines.join("\n").substring(0, 12000)}
const comment_output = `
<details><summary>${comment_summary}</br>
###### ${context.workflow} by @${context.actor} via [${context.eventName}](${check_url}) at ${context.payload.pull_request?.updated_at || context.payload.comment?.updated_at}.</summary>
###### ${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}.</summary>
\`\`\`hcl
${process.env.tf_output}
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down

0 comments on commit 0e5ba42

Please sign in to comment.