Skip to content

Commit 6a3cf71

Browse files
authored
Merge pull request #9 from direct-actions/dev
update action
2 parents 39aed05 + 00f4ee1 commit 6a3cf71

2 files changed

Lines changed: 111 additions & 115 deletions

File tree

.github/workflows/tests.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ jobs:
6868
with:
6969
key: test1
7070
match: first
71-
ids: ${{ steps.t1get.outputs.ids }}
7271
comment: key=test1 v1 - This is updated dummy comment. (${{ github.run_id }})
7372

7473
- run: sleep 2
@@ -78,3 +77,9 @@ jobs:
7877
with:
7978
key: test1
8079
comment: key=test1 - This is final dummy comment. (${{ github.run_id }})
80+
81+
- name: Test 1 minimize
82+
uses: ./
83+
with:
84+
key: test1
85+
operation: minimize

action.yaml

Lines changed: 105 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ inputs:
1414
description: (Optional) specify author name to match existing comment(s).
1515
required: false
1616
type: string
17-
comment:
18-
default: ''
19-
description: Body of comment, used with `post` or `upsert` operation.
17+
classifier:
18+
default: resolved
19+
description: Override default classifier on `minimize` operation.
2020
required: false
2121
type: string
22-
ids:
22+
comment:
2323
default: ''
24-
description: JSON list of IDs to operate on, can be passed directly from previous call to this same action.
24+
description: Body of comment, used with `post` or `upsert` operation.
2525
required: false
2626
type: string
2727
key:
@@ -36,7 +36,7 @@ inputs:
3636
type: string
3737
operation:
3838
default: upsert
39-
description: Operation to execute - can be one of `delete`, `get`, `list-keys`, `post`, `upsert` (default).
39+
description: Operation to execute - can be one of `delete`, `get`, `list-keys`, `minimize`, `post`, `upsert` (default).
4040
required: false
4141
type: string
4242
pull-request:
@@ -70,76 +70,98 @@ inputs:
7070
required: false
7171
type: string
7272
outputs:
73-
ids:
74-
description: JSON list of matching comment IDs; can be used for chaining.
75-
value: ${{ steps.operation.outputs.ids }}
76-
key:
77-
description: Pass-through of inputs.key; can be used for chaining.
78-
value: ${{ inputs.key }}
7973
keys:
8074
description: JSON list of keys found on a `list-keys` operation.
8175
value: ${{ steps.operation.outputs.keys }}
8276
json-output:
8377
description: JSON output of last API operation.
8478
value: ${{ steps.operation.outputs.json-output }}
85-
pull-request:
86-
description: Pass-through of inputs.pull-request; can be used for chaining.
87-
value: ${{ inputs.pull-request }}
88-
repository:
89-
description: Pass-through of inputs.repository; can be used for chaining.
90-
value: ${{ inputs.repository }}
91-
token:
92-
description: Pass-through of inputs.token; can be used for chaining.
93-
value: ${{ inputs.token }}
9479

9580
runs:
9681
using: composite
9782
steps:
9883
- env:
84+
CLASSIFIER: ${{ inputs.classifier }}
85+
COMMENT: '<!--direct-actions/pr-comment:key:${{ inputs.key }}-->${{ inputs.comment }}'
9986
GITHUB_TOKEN: ${{ inputs.token }}
100-
IDS: ${{ inputs.ids }}
87+
OPERATION: ${{ inputs.operation }}
10188
PULL_REQUEST: ${{ inputs.pull-request }}
89+
QUIET: ${{ inputs.quiet }}
10290
REPOSITORY: ${{ inputs.repository }}
103-
id: setup
91+
id: operation
10492
if: ${{ inputs.operation != 'post' }}
10593
run: |
106-
# setup
107-
#
108-
#set -x
109-
OUTPUT=$(mktemp)
94+
# ${{ inputs.operation }}
95+
if [ "$ACTIONS_RUNNER_DEBUG" == 'true' ] ; then
96+
set -x
97+
fi
98+
99+
ALL_COMMENTS=$(mktemp)
100+
CLASSIFIER="$(tr '[a-z]' '[A-Z]' <<< "$CLASSIFIER")"
101+
COMMENTS=$(mktemp)
110102
ERROR=$(mktemp)
111-
if [ -n "$IDS" ] ; then
112-
jq -r '"ids=\(join(" "))"' <<< "$IDS" >> "$GITHUB_OUTPUT"
113-
else
114-
if [ -z "$PULL_REQUEST" ] ; then
115-
echo "::error title=direct-actions/pr-comment - Cannot determine" \
116-
"pull request::Cannot determine pull request number in this" \
117-
"sitation, pass explicitly via pull-request input."
118-
exit 1
119-
elif [ -z "$REPOSITORY" ] ; then
120-
echo "::error title=direct-actions/pr-comment - Cannot determine" \
121-
"repository::Cannot determine repository in this situation, pass" \
122-
"explicitly via repository input."
123-
exit 1
124-
elif ! gh api \
125-
--method GET \
103+
104+
gh_api() {
105+
local operation="$1"
106+
local method="$2"
107+
local url="$3"
108+
local comment="$4"
109+
local output=$(mktemp)
110+
111+
if ! gh api \
112+
--method "$method" \
126113
-H "Accept: application/vnd.github+json" \
127114
-H "X-GitHub-Api-Version: 2022-11-28" \
128-
"/repos/${REPOSITORY}/issues/${PULL_REQUEST}/comments" \
129-
> "$OUTPUT" 2> "$ERROR" ; then
130-
echo "::error title=direct-actions/pr-comment - Could not retrieve" \
131-
"comments::Error retrieving comments from repository (${REPOSITORY})" \
132-
"PR number (${PULL_REQUEST}) - \"$(cat "$ERROR")\"."
115+
"$url" \
116+
-f body="${comment}" \
117+
>"$output" 2>"$ERROR" ; then
118+
echo "::error title=direct-actions/pr-comment - API error::" \
119+
"::Operation (${operation}) method ${method} url ${url} failed -" \
120+
"\"$(cat "$ERROR")\"."
133121
exit 1
134122
fi
123+
if [ "$QUIET" != 'true' ] ; then
124+
echo "::group::operation=${operation} method=${method} url=${url}"
125+
jq -C . "$output"
126+
echo "::endgroup::"
127+
fi
128+
jq -r '"json-output<<_EOF_\n\(@json)\n_EOF_\n"' "$output" >> "$GITHUB_OUTPUT"
129+
}
130+
131+
if [ -z "$PULL_REQUEST" ] ; then
132+
echo "::error title=direct-actions/pr-comment - Cannot determine" \
133+
"pull request::Cannot determine pull request number in this" \
134+
"sitation, pass explicitly via pull-request input."
135+
exit 1
136+
elif [ -z "$REPOSITORY" ] ; then
137+
echo "::error title=direct-actions/pr-comment - Cannot determine" \
138+
"repository::Cannot determine repository in this situation, pass" \
139+
"explicitly via repository input."
140+
exit 1
141+
elif ! gh api \
142+
--method GET \
143+
-H "Accept: application/vnd.github+json" \
144+
-H "X-GitHub-Api-Version: 2022-11-28" \
145+
"/repos/${REPOSITORY}/issues/${PULL_REQUEST}/comments" \
146+
> "$ALL_COMMENTS" 2> "$ERROR" ; then
147+
echo "::error title=direct-actions/pr-comment - Could not retrieve" \
148+
"comments::Error retrieving comments from repository (${REPOSITORY})" \
149+
"PR number (${PULL_REQUEST}) - \"$(cat "$ERROR")\"."
150+
exit 1
151+
fi
135152
136-
jq \
137-
--arg author '${{ inputs.author }}' \
138-
--arg key '${{ inputs.key }}' \
139-
--arg match '${{ inputs.match }}' \
140-
--arg regex '${{ inputs.regex }}' \
141-
--arg regex_flags '${{ inputs.regex-flags }}' \
142-
-rC '
153+
if [ "$ACTIONS_RUNNER_DEBUG" == 'true' ] ; then echo ::group::all_comments.json
154+
jq -C . "$ALL_COMMENTS"
155+
echo ::endgroup::
156+
fi
157+
158+
jq \
159+
--arg author '${{ inputs.author }}' \
160+
--arg key '${{ inputs.key }}' \
161+
--arg match '${{ inputs.match }}' \
162+
--arg regex '${{ inputs.regex }}' \
163+
--arg regex_flags '${{ inputs.regex-flags }}' \
164+
-r '
143165
map(
144166
if $ARGS.named.author? != "" then
145167
select(.author.login == $ARGS.named.author)
@@ -157,9 +179,8 @@ runs:
157179
if $ARGS.named.regex? != "" then
158180
select(.body | test($ARGS.named.regex ; $ARGS.named.regex_flags))
159181
else
160-
.
161-
end |
162-
.id
182+
.
183+
end
163184
) |
164185
if length == 0 then
165186
[]
@@ -171,58 +192,19 @@ runs:
171192
[last]
172193
else
173194
error("Unknown value for argument \"match\": \($ARGS.named.match)")
174-
end |
175-
"ids=\(join(" "))"
176-
' "$OUTPUT" >> "$GITHUB_OUTPUT"
177-
fi
178-
echo "output-file=${OUTPUT}" >> "$GITHUB_OUTPUT"
179-
echo "pull-request=${PULL_REQUEST}" >> "$GITHUB_OUTPUT"
180-
shell: bash
181-
- env:
182-
COMMENT: '<!--direct-actions/pr-comment:key:${{ inputs.key }}-->${{ inputs.comment }}'
183-
GITHUB_TOKEN: ${{ inputs.token }}
184-
IDS: ${{ steps.setup.outputs.ids }}
185-
OUTPUT: ${{ steps.setup.outputs.output-file }}
186-
OPERATION: ${{ inputs.operation }}
187-
PULL_REQUEST: ${{ steps.setup.outputs.pull-request }}
188-
QUIET: ${{ inputs.quiet }}
189-
REPOSITORY: ${{ inputs.repository }}
190-
id: operation
191-
run: |
192-
# ${{ inputs.operation }}
193-
#
194-
#set -x
195-
ERROR=$(mktemp)
196-
OUTPUT_IDS=()
197-
198-
gh_api() {
199-
local operation="$1"
200-
local method="$2"
201-
local url="$3"
202-
local comment="$4"
195+
end
196+
' "$ALL_COMMENTS" > \
197+
"$COMMENTS"
198+
NUM_COMMENTS=$(jq -r length "$COMMENTS")
203199
204-
if ! gh api \
205-
--method "$method" \
206-
-H "Accept: application/vnd.github+json" \
207-
-H "X-GitHub-Api-Version: 2022-11-28" \
208-
"$url" \
209-
-f body="${comment}" \
210-
>"$OUTPUT" 2>"$ERROR" ; then
211-
echo "::error title=direct-actions/pr-comment - API error::" \
212-
"::Operation (${operation}) method ${method} url ${url} failed -" \
213-
"\"$(cat "$ERROR")\"."
214-
exit 1
215-
fi
216-
if [ "$QUIET" != 'true' ] ; then
217-
echo "::group::operation=${operation} method=${method} url=${url}"
218-
jq -C . "$OUTPUT"
219-
echo "::endgroup::"
220-
fi
221-
}
200+
if [ "$ACTIONS_RUNNER_DEBUG" == 'true' ] ; then
201+
echo "::group::Matching comments (${NUM_COMMENTS})::"
202+
jq -C . "$COMMENTS"
203+
echo '::endgroup::'
204+
fi
222205
223-
if [ "$OPERATION" == 'post' ] || [[ "$OPERATION" == 'upsert' && -z "$IDS" ]]; then
206+
if [ "$OPERATION" == 'post' ] || [[ "$OPERATION" == 'upsert' && "$NUM_COMMENTS" -eq 0 ]]; then
224207
gh_api "$OPERATION" POST "/repos/${REPOSITORY}/issues/${PULL_REQUEST}/comments" "$COMMENT"
225-
OUTPUT_IDS=("$(jq -r .id "$OUTPUT")")
226208
elif [ "$OPERATION" == 'list-keys' ] ; then
227209
jq '
228210
map(
@@ -233,15 +215,16 @@ runs:
233215
unique |
234216
"keys=\(@json)"
235217
' \
236-
"$OUTPUT" | \
218+
"$ALL_COMMENTS" | \
237219
tee -a "$GITHUB_OUTPUT"
238-
elif [[ "$OPERATION" =~ ^(delete|get|upsert)$ ]] ; then
239-
if [ -z "$IDS" ] && [ "$QUIET" != 'true' ] ; then
220+
elif [[ "$OPERATION" =~ ^(delete|get|minimize|upsert)$ ]] ; then
221+
if [ "$NUM_COMMENTS" -eq 0 ] && [ "$QUIET" != 'true' ] ; then
240222
echo "::warning title=direct-actions/pr-comment - Cannot perform" \
241223
"operation::Attempting operation ${OPERATION} but cannot find" \
242224
"matching comment(s)."
243225
else
244-
for ID in "$IDS" ; do
226+
for (( x=0 ; x < NUM_COMMENTS ; x++ )) ; do
227+
ID=$(jq --arg index "$x" -r '.[$index | tonumber].id' "$COMMENTS")
245228
URL="/repos/${REPOSITORY}/issues/comments/${ID}"
246229
case "$OPERATION" in
247230
delete)
@@ -250,19 +233,27 @@ runs:
250233
get)
251234
gh_api "$OPERATION" GET "$URL" "$COMMENT"
252235
;;
236+
minimize)
237+
echo "::group::operation=${OPERATION} graphql minimizeComment node_id=${NODE_ID} classifier=${CLASSIFIER}"
238+
NODE_ID=$(jq --arg index "$x" -r '.[$index | tonumber].node_id' "$COMMENTS")
239+
gh api graphql \
240+
-f query="
241+
mutation {
242+
minimizeComment(input: { subjectId: \"${NODE_ID}\", classifier: ${CLASSIFIER} }) {
243+
clientMutationId
244+
}
245+
}"
246+
echo '::endgroup::'
247+
;;
253248
upsert)
254249
gh_api "$OPERATION" PATCH "$URL" "$COMMENT"
255250
;;
256251
esac
257-
OUTPUT_IDS+=("$(jq -r .id "$OUTPUT")")
258252
done
259253
fi
260254
else
261255
echo "::warning title=direct-actions/pr-comment - Invalid" \
262256
"operation::Invalid operation (${OPERATION}) specified."
263257
exit 1
264258
fi
265-
266-
jq -rs '"ids=\(map(tostring))"' <<< "${OUTPUT_IDS[@]}" | tee -a "$GITHUB_OUTPUT"
267-
jq -r '"json-output<<_EOF_\n\(@json)\n_EOF_\n"' "$OUTPUT" >> "$GITHUB_OUTPUT"
268259
shell: bash

0 commit comments

Comments
 (0)