diff --git a/README.md b/README.md index fd0cf8e..e7ea1cf 100644 --- a/README.md +++ b/README.md @@ -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 +``` diff --git a/action.yml b/action.yml index 6f48f50..1685a35 100644 --- a/action.yml +++ b/action.yml @@ -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. @@ -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 diff --git a/src/fast-forward.sh b/src/fast-forward.sh index 1fcf5bd..58a99e5 100755 --- a/src/fast-forward.sh +++ b/src/fast-forward.sh @@ -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 @@ -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)