From 095299e1344bb7dd1235132397d14c27d560ed1f Mon Sep 17 00:00:00 2001 From: Rahul <42219389+rahulpatidar0191@users.noreply.github.com> Date: Tue, 9 Apr 2024 19:49:31 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Add=20action=20and=20shell=20scr?= =?UTF-8?q?ipts=20=20(#1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add scripts * add instructions * remove unwanted input; search term * add comment ID * debug id * add quotes * remove debug * modify if condition a bit * add array * add skipped * add or * add skipped * try with new if * debug * remove spacing * print author * add F * remove debug * improve steps --- README.md | 19 +++++++++ action.yml | 80 ++++++++++++++++++++++++++++++++++++++ script.sh | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 211 insertions(+) create mode 100644 action.yml create mode 100755 script.sh diff --git a/README.md b/README.md index 783c92a..299021a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,21 @@ # satel-pr-commenter This GitHub action posts comments on a PR when a workflow fails + +# Usage + +```yml +pr-commenter-action + runs-on: ubuntu-latest + if: always() + needs: [] + steps: + - name: pr-action + uses: SatelCreative/satel-pr-commenter@1.0.0 + if: ${{ github.ref != 'refs/heads/main' && !contains(github.ref, 'refs/tags/') }} + with: + token: ${{ secrets.GITHUB_TOKEN }} + job_status: ${{ needs.set-variables.result }} + body: "" + number: ${{ github.event.pull_request.number }} + author: ${{ github.event.pull_request.user.login }} +``` diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..078830e --- /dev/null +++ b/action.yml @@ -0,0 +1,80 @@ +name: 'Smart Comment Actions' +description: 'Create, update, delete or find a pull request or issue comment' +inputs: + token: + description: 'GitHub token' + required: true + job_status: + description: Status of previous jobs + required: true + default: '[]' + repository: + description: 'The GitHub repository' + default: ${{ github.repository }} + required: false + type: + description: create|update|delete|find + required: true + body: + description: 'The comment body' + required: false + number: + description: 'The number of the issue or pull request' + required: false + # Update/Append comment input + # Delete comment input + comment_id: + description: 'Comment ID. Required to update or delete the comment' + required: false + # Find comment input + author: + description: 'GitHub user name of the comment author to find a comment .' + required: false + +runs: + using: "composite" + steps: + - name: Create a comment + if: ${{ inputs.job_status == 'failure' || inputs.job_status == 'skipped' }} + id: create_comment + env: + GH_TOKEN: ${{ inputs.token }} + REPO: ${{ inputs.repository }} + ACTION_TYPE: "create" + BODY: ${{ inputs.body }} + ISSUE_NUMBER: ${{ inputs.number }} + AUTHOR: ${{ inputs.author }} + run: | + ${{ github.action_path }}/script.sh + shell: bash + + - name: Find a comment + if: ${{ steps.create_comment.outcome == 'skipped'}} #${{ inputs.job_status != 'failure' || inputs.job_status != 'skipped' }} + id: find_comment + env: + GH_TOKEN: ${{ inputs.token }} + REPO: ${{ inputs.repository }} + ACTION_TYPE: "find" + BODY: ${{ inputs.body }} + ISSUE_NUMBER: ${{ inputs.number }} + COMMENT_ID: ${{ inputs.comment_id }} + SEARCH_TERM: ${{ inputs.body }} + AUTHOR: ${{ inputs.author }} + run: | + ${{ github.action_path }}/script.sh + shell: bash + + - name: Delete a comment + if: ${{ steps.find_comment.outcome == 'success' && steps.find_comment.outputs.comment_id != 'null' }} + id: delete_comment + env: + GH_TOKEN: ${{ inputs.token }} + REPO: ${{ inputs.repository }} + ACTION_TYPE: "delete" + BODY: ${{ inputs.body }} + ISSUE_NUMBER: ${{ inputs.number }} + AUTHOR: ${{ inputs.author }} + COMMENT_ID: ${{ steps.find_comment.outputs.comment_id }} + run: | + ${{ github.action_path }}/script.sh + shell: bash diff --git a/script.sh b/script.sh new file mode 100755 index 0000000..b8f126f --- /dev/null +++ b/script.sh @@ -0,0 +1,112 @@ +#!/bin/bash +# https://docs.github.com/en/rest/pulls/comments?apiVersion=2022-11-28 + +comment_id= + +createComment() { +echo "======= Running create comment =======" + if [ -z "$ISSUE_NUMBER" ] || [ -z "$BODY" ]; then + echo "Issue number and comment body are required." + return + fi + + # Fetch existing comments for the given issue + existing_comments=$(gh pr view "$ISSUE_NUMBER" --json comments -q '.comments[].body') + + # Check if the new comment body already exists + if echo "$existing_comments" | grep -qF "@$AUTHOR $BODY"; then + echo "Comment already exists. Not creating again." + return + fi + + # Create a comment + gh pr comment "$ISSUE_NUMBER" --body "@$AUTHOR $BODY" + status=$? + + if [ "$status" -ne 0 ]; then + echo "Failed to create a comment. Exit code: $status" + return + fi + + echo "Created a comment on issue number: $ISSUE_NUMBER" +} + +# Function to find a comment +findComment() { +echo "======= Running find comment =======" + if [ -z "$ISSUE_NUMBER" ]; then + echo "Issue number is required." + return + fi + + if [ -z "$SEARCH_TERM" ] && [ -z "$AUTHOR" ]; then + echo "Either search term or comment author is required." + return + fi + + comments=$(gh api \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "/repos/$REPO/issues/$ISSUE_NUMBER/comments" + ) + + status=$? + + if [ "$status" -ne 0 ]; then + echo "Failed to retrieve comments. Exit code: $status" + return + fi + + comment_body=$(echo "$comments" | jq -r '.[0].body') + + echo "CommentBody: $comment_body" + + if [ -n "$comment_body" ]; then + comment_id=$(echo "$comments" | jq -r '.[0].id') + echo "Comment found for a search term: '$SEARCH_TERM'." + echo "Comment ID: '$comment_id'." + else + echo "No comment found for the given criteria." + fi +} + +# Function to delete a comment +deleteComment() { +echo "======= Running delete comment =======" + if [ -z "$COMMENT_ID" ]; then + echo "Comment ID is required." + return + fi + + # Delete the comment + gh api \ + --method DELETE \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "/repos/$REPO/issues/comments/$COMMENT_ID" + + STATUS=$? + + if [ "$STATUS" -ne 0 ]; then + echo "Failing deployment" + exit $STATUS + else + echo "Deleted a comment. Comment ID: $COMMENT_ID" + fi +} + +case $ACTION_TYPE in + "create") + createComment ;; + "update" | "append" | "prepend") + updateComment ;; + "find") + findComment ;; + "delete") + deleteComment ;; + *) + echo "Invalid action type: $ACTION_TYPE" ;; +esac + +# These outputs are used in other steps/jobs via action.yml +echo "comment_id=${comment_id}" >> "$GITHUB_OUTPUT"