Skip to content

Commit

Permalink
🚀 Add action and shell scripts (#1)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rahulpatidar0191 authored Apr 10, 2024
1 parent c6d63e4 commit 095299e
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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: [<PREVIOUS-JOBS>]
steps:
- name: pr-action
uses: SatelCreative/[email protected]
if: ${{ github.ref != 'refs/heads/main' && !contains(github.ref, 'refs/tags/') }}
with:
token: ${{ secrets.GITHUB_TOKEN }}
job_status: ${{ needs.set-variables.result }}
body: "<FAQ-COMMENT>"
number: ${{ github.event.pull_request.number }}
author: ${{ github.event.pull_request.user.login }}
```
80 changes: 80 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -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
112 changes: 112 additions & 0 deletions script.sh
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 095299e

Please sign in to comment.