Skip to content

Commit

Permalink
Provide an option that allows the user to turn off comments.
Browse files Browse the repository at this point in the history
  - Add an input variable, comment, to allow the user to stop the
    workflow from posting a comment.

  - The comment is still shown in the log.
  • Loading branch information
nwalfield committed Aug 24, 2023
1 parent 90c41a9 commit 114f79b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,31 @@ This workflow is only run when a comment that includes `/fast-forward`
is added to the pull request. The workflow is careful to check that
the user who triggered the workflow is actually authorized to push to
the repository.

## Disabling Comments

If you prefer to disable comments, you can set the `comment` input
variable to `false` like so:

```yaml
name: pull-request
on:
pull_request:
types: [opened, reopened, synchronize]
jobs:
check-fast-forward:
runs-on: ubuntu-latest
permissions:
contents: read
# We appear to need write permission for both pull-requests and
# issues in order to post a comment to a pull request.
pull-requests: write
issues: write
steps:
- name: Checking if fast forwarding is possible
uses: sequoia-pgp/fast-forward@main
with:
comment: false
```
16 changes: 15 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ inputs:
indicating whether it is possible to fast forward the target
branch.
default: false
comment:
description: >
Whether to post a comment.
If set to true, this posts a comment to the pull request
indicating whether it is possible to fast-forward the target
branch, and, if merge is true, whether fast forwarding
succeeded.
If false, the comment is still available via the comment output
variable.
default: true
debug:
description: >
Enables debugging output.
Expand All @@ -26,10 +38,12 @@ inputs:
runs:
using: "composite"
steps:
# github.action_path is set to $REPO.
- run: |
export GITHUB_TOKEN=${{ inputs.github_token }}
export DEBUG=${{ inputs.debug }}
export COMMENT=${{ inputs.comment }}
# github.action_path is set to $REPO.
if test "x${{ inputs.merge }}" = xtrue
then
${{ github.action_path }}/src/fast-forward.sh --merge
Expand Down
41 changes: 28 additions & 13 deletions src/fast-forward.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ case "${DEBUG:-0}" in
;;
esac

# Set to true to post a comment to the issue.
case "${COMMENT:-true}" in
0 | false | FALSE) COMMENT=0;;
1 | true | TRUE) COMMENT=1;;
*)
echo "Warning: Invalid value ('$COMMENT') for COMMENT." >&2;
COMMENT=1
;;
esac

if test "x$GITHUB_EVENT_PATH" = x
then
echo "GITHUB_EVENT_PATH environment variable must be set." >&2
Expand Down Expand Up @@ -292,22 +302,27 @@ LOG=$(mktemp)
fi
} 2>&1 | tee -a $GITHUB_STEP_SUMMARY "$LOG"

COMMENT=$(mktemp)
jq -n --rawfile log "$LOG" '{ "body": $log }' >"$COMMENT"
COMMENT_CONTENT=$(mktemp)
jq -n --rawfile log "$LOG" '{ "body": $log }' >"$COMMENT_CONTENT"

COMMENTS_URL="$(github_pull_request .comments_url)"
if test "x$COMMENTS_URL" != x
if test $COMMENT -gt 0
then
echo "Posting comment to $COMMENTS_URL."
curl --silent --show-error --location \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"$COMMENTS_URL" \
-d "@$COMMENT"
COMMENTS_URL="$(github_pull_request .comments_url)"
if test "x$COMMENTS_URL" != x
then
echo "Posting comment to $COMMENTS_URL."
curl --silent --show-error --location \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"$COMMENTS_URL" \
-d "@$COMMENT_CONTENT"
else
echo "Can't post a comment: github.event.pull_request.comments_url is not set." | tee -a $GITHUB_STEP_SUMMARY
fi
else
echo "Can't post a comment: github.event.pull_request.comments_url is not set." | tee -a $GITHUB_STEP_SUMMARY
echo "Not posting comment, disabled by user."
fi

exit $(cat $EXIT_CODE)

0 comments on commit 114f79b

Please sign in to comment.