-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sh
executable file
·112 lines (93 loc) · 3 KB
/
script.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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"
# Concatenate AUTHOR and SEARCH_TERM
search_string="@$AUTHOR $SEARCH_TERM"
if [ -n "$comment_body" ] && [ "$comment_body" = "$search_string" ]; then
comment_id=$(echo "$comments" | jq -r '.[0].id')
echo "Comment found for the search term '$SEARCH_TERM' and author '$AUTHOR'."
echo "Comment ID: '$comment_id'."
else
echo "No comment found matching the search term $search_string"
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"