Skip to content

Commit

Permalink
Merge pull request #9 from direct-actions/dev
Browse files Browse the repository at this point in the history
update action
  • Loading branch information
robzr authored Mar 13, 2024
2 parents 39aed05 + 00f4ee1 commit 6a3cf71
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 115 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ jobs:
with:
key: test1
match: first
ids: ${{ steps.t1get.outputs.ids }}
comment: key=test1 v1 - This is updated dummy comment. (${{ github.run_id }})

- run: sleep 2
Expand All @@ -78,3 +77,9 @@ jobs:
with:
key: test1
comment: key=test1 - This is final dummy comment. (${{ github.run_id }})

- name: Test 1 minimize
uses: ./
with:
key: test1
operation: minimize
219 changes: 105 additions & 114 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ inputs:
description: (Optional) specify author name to match existing comment(s).
required: false
type: string
comment:
default: ''
description: Body of comment, used with `post` or `upsert` operation.
classifier:
default: resolved
description: Override default classifier on `minimize` operation.
required: false
type: string
ids:
comment:
default: ''
description: JSON list of IDs to operate on, can be passed directly from previous call to this same action.
description: Body of comment, used with `post` or `upsert` operation.
required: false
type: string
key:
Expand All @@ -36,7 +36,7 @@ inputs:
type: string
operation:
default: upsert
description: Operation to execute - can be one of `delete`, `get`, `list-keys`, `post`, `upsert` (default).
description: Operation to execute - can be one of `delete`, `get`, `list-keys`, `minimize`, `post`, `upsert` (default).
required: false
type: string
pull-request:
Expand Down Expand Up @@ -70,76 +70,98 @@ inputs:
required: false
type: string
outputs:
ids:
description: JSON list of matching comment IDs; can be used for chaining.
value: ${{ steps.operation.outputs.ids }}
key:
description: Pass-through of inputs.key; can be used for chaining.
value: ${{ inputs.key }}
keys:
description: JSON list of keys found on a `list-keys` operation.
value: ${{ steps.operation.outputs.keys }}
json-output:
description: JSON output of last API operation.
value: ${{ steps.operation.outputs.json-output }}
pull-request:
description: Pass-through of inputs.pull-request; can be used for chaining.
value: ${{ inputs.pull-request }}
repository:
description: Pass-through of inputs.repository; can be used for chaining.
value: ${{ inputs.repository }}
token:
description: Pass-through of inputs.token; can be used for chaining.
value: ${{ inputs.token }}

runs:
using: composite
steps:
- env:
CLASSIFIER: ${{ inputs.classifier }}
COMMENT: '<!--direct-actions/pr-comment:key:${{ inputs.key }}-->${{ inputs.comment }}'
GITHUB_TOKEN: ${{ inputs.token }}
IDS: ${{ inputs.ids }}
OPERATION: ${{ inputs.operation }}
PULL_REQUEST: ${{ inputs.pull-request }}
QUIET: ${{ inputs.quiet }}
REPOSITORY: ${{ inputs.repository }}
id: setup
id: operation
if: ${{ inputs.operation != 'post' }}
run: |
# setup
#
#set -x
OUTPUT=$(mktemp)
# ${{ inputs.operation }}
if [ "$ACTIONS_RUNNER_DEBUG" == 'true' ] ; then
set -x
fi
ALL_COMMENTS=$(mktemp)
CLASSIFIER="$(tr '[a-z]' '[A-Z]' <<< "$CLASSIFIER")"
COMMENTS=$(mktemp)
ERROR=$(mktemp)
if [ -n "$IDS" ] ; then
jq -r '"ids=\(join(" "))"' <<< "$IDS" >> "$GITHUB_OUTPUT"
else
if [ -z "$PULL_REQUEST" ] ; then
echo "::error title=direct-actions/pr-comment - Cannot determine" \
"pull request::Cannot determine pull request number in this" \
"sitation, pass explicitly via pull-request input."
exit 1
elif [ -z "$REPOSITORY" ] ; then
echo "::error title=direct-actions/pr-comment - Cannot determine" \
"repository::Cannot determine repository in this situation, pass" \
"explicitly via repository input."
exit 1
elif ! gh api \
--method GET \
gh_api() {
local operation="$1"
local method="$2"
local url="$3"
local comment="$4"
local output=$(mktemp)
if ! gh api \
--method "$method" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${REPOSITORY}/issues/${PULL_REQUEST}/comments" \
> "$OUTPUT" 2> "$ERROR" ; then
echo "::error title=direct-actions/pr-comment - Could not retrieve" \
"comments::Error retrieving comments from repository (${REPOSITORY})" \
"PR number (${PULL_REQUEST}) - \"$(cat "$ERROR")\"."
"$url" \
-f body="${comment}" \
>"$output" 2>"$ERROR" ; then
echo "::error title=direct-actions/pr-comment - API error::" \
"::Operation (${operation}) method ${method} url ${url} failed -" \
"\"$(cat "$ERROR")\"."
exit 1
fi
if [ "$QUIET" != 'true' ] ; then
echo "::group::operation=${operation} method=${method} url=${url}"
jq -C . "$output"
echo "::endgroup::"
fi
jq -r '"json-output<<_EOF_\n\(@json)\n_EOF_\n"' "$output" >> "$GITHUB_OUTPUT"
}
if [ -z "$PULL_REQUEST" ] ; then
echo "::error title=direct-actions/pr-comment - Cannot determine" \
"pull request::Cannot determine pull request number in this" \
"sitation, pass explicitly via pull-request input."
exit 1
elif [ -z "$REPOSITORY" ] ; then
echo "::error title=direct-actions/pr-comment - Cannot determine" \
"repository::Cannot determine repository in this situation, pass" \
"explicitly via repository input."
exit 1
elif ! gh api \
--method GET \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${REPOSITORY}/issues/${PULL_REQUEST}/comments" \
> "$ALL_COMMENTS" 2> "$ERROR" ; then
echo "::error title=direct-actions/pr-comment - Could not retrieve" \
"comments::Error retrieving comments from repository (${REPOSITORY})" \
"PR number (${PULL_REQUEST}) - \"$(cat "$ERROR")\"."
exit 1
fi
jq \
--arg author '${{ inputs.author }}' \
--arg key '${{ inputs.key }}' \
--arg match '${{ inputs.match }}' \
--arg regex '${{ inputs.regex }}' \
--arg regex_flags '${{ inputs.regex-flags }}' \
-rC '
if [ "$ACTIONS_RUNNER_DEBUG" == 'true' ] ; then echo ::group::all_comments.json
jq -C . "$ALL_COMMENTS"
echo ::endgroup::
fi
jq \
--arg author '${{ inputs.author }}' \
--arg key '${{ inputs.key }}' \
--arg match '${{ inputs.match }}' \
--arg regex '${{ inputs.regex }}' \
--arg regex_flags '${{ inputs.regex-flags }}' \
-r '
map(
if $ARGS.named.author? != "" then
select(.author.login == $ARGS.named.author)
Expand All @@ -157,9 +179,8 @@ runs:
if $ARGS.named.regex? != "" then
select(.body | test($ARGS.named.regex ; $ARGS.named.regex_flags))
else
.
end |
.id
.
end
) |
if length == 0 then
[]
Expand All @@ -171,58 +192,19 @@ runs:
[last]
else
error("Unknown value for argument \"match\": \($ARGS.named.match)")
end |
"ids=\(join(" "))"
' "$OUTPUT" >> "$GITHUB_OUTPUT"
fi
echo "output-file=${OUTPUT}" >> "$GITHUB_OUTPUT"
echo "pull-request=${PULL_REQUEST}" >> "$GITHUB_OUTPUT"
shell: bash
- env:
COMMENT: '<!--direct-actions/pr-comment:key:${{ inputs.key }}-->${{ inputs.comment }}'
GITHUB_TOKEN: ${{ inputs.token }}
IDS: ${{ steps.setup.outputs.ids }}
OUTPUT: ${{ steps.setup.outputs.output-file }}
OPERATION: ${{ inputs.operation }}
PULL_REQUEST: ${{ steps.setup.outputs.pull-request }}
QUIET: ${{ inputs.quiet }}
REPOSITORY: ${{ inputs.repository }}
id: operation
run: |
# ${{ inputs.operation }}
#
#set -x
ERROR=$(mktemp)
OUTPUT_IDS=()
gh_api() {
local operation="$1"
local method="$2"
local url="$3"
local comment="$4"
end
' "$ALL_COMMENTS" > \
"$COMMENTS"
NUM_COMMENTS=$(jq -r length "$COMMENTS")
if ! gh api \
--method "$method" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"$url" \
-f body="${comment}" \
>"$OUTPUT" 2>"$ERROR" ; then
echo "::error title=direct-actions/pr-comment - API error::" \
"::Operation (${operation}) method ${method} url ${url} failed -" \
"\"$(cat "$ERROR")\"."
exit 1
fi
if [ "$QUIET" != 'true' ] ; then
echo "::group::operation=${operation} method=${method} url=${url}"
jq -C . "$OUTPUT"
echo "::endgroup::"
fi
}
if [ "$ACTIONS_RUNNER_DEBUG" == 'true' ] ; then
echo "::group::Matching comments (${NUM_COMMENTS})::"
jq -C . "$COMMENTS"
echo '::endgroup::'
fi
if [ "$OPERATION" == 'post' ] || [[ "$OPERATION" == 'upsert' && -z "$IDS" ]]; then
if [ "$OPERATION" == 'post' ] || [[ "$OPERATION" == 'upsert' && "$NUM_COMMENTS" -eq 0 ]]; then
gh_api "$OPERATION" POST "/repos/${REPOSITORY}/issues/${PULL_REQUEST}/comments" "$COMMENT"
OUTPUT_IDS=("$(jq -r .id "$OUTPUT")")
elif [ "$OPERATION" == 'list-keys' ] ; then
jq '
map(
Expand All @@ -233,15 +215,16 @@ runs:
unique |
"keys=\(@json)"
' \
"$OUTPUT" | \
"$ALL_COMMENTS" | \
tee -a "$GITHUB_OUTPUT"
elif [[ "$OPERATION" =~ ^(delete|get|upsert)$ ]] ; then
if [ -z "$IDS" ] && [ "$QUIET" != 'true' ] ; then
elif [[ "$OPERATION" =~ ^(delete|get|minimize|upsert)$ ]] ; then
if [ "$NUM_COMMENTS" -eq 0 ] && [ "$QUIET" != 'true' ] ; then
echo "::warning title=direct-actions/pr-comment - Cannot perform" \
"operation::Attempting operation ${OPERATION} but cannot find" \
"matching comment(s)."
else
for ID in "$IDS" ; do
for (( x=0 ; x < NUM_COMMENTS ; x++ )) ; do
ID=$(jq --arg index "$x" -r '.[$index | tonumber].id' "$COMMENTS")
URL="/repos/${REPOSITORY}/issues/comments/${ID}"
case "$OPERATION" in
delete)
Expand All @@ -250,19 +233,27 @@ runs:
get)
gh_api "$OPERATION" GET "$URL" "$COMMENT"
;;
minimize)
echo "::group::operation=${OPERATION} graphql minimizeComment node_id=${NODE_ID} classifier=${CLASSIFIER}"
NODE_ID=$(jq --arg index "$x" -r '.[$index | tonumber].node_id' "$COMMENTS")
gh api graphql \
-f query="
mutation {
minimizeComment(input: { subjectId: \"${NODE_ID}\", classifier: ${CLASSIFIER} }) {
clientMutationId
}
}"
echo '::endgroup::'
;;
upsert)
gh_api "$OPERATION" PATCH "$URL" "$COMMENT"
;;
esac
OUTPUT_IDS+=("$(jq -r .id "$OUTPUT")")
done
fi
else
echo "::warning title=direct-actions/pr-comment - Invalid" \
"operation::Invalid operation (${OPERATION}) specified."
exit 1
fi
jq -rs '"ids=\(map(tostring))"' <<< "${OUTPUT_IDS[@]}" | tee -a "$GITHUB_OUTPUT"
jq -r '"json-output<<_EOF_\n\(@json)\n_EOF_\n"' "$OUTPUT" >> "$GITHUB_OUTPUT"
shell: bash

0 comments on commit 6a3cf71

Please sign in to comment.