-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sh
executable file
·73 lines (67 loc) · 1.9 KB
/
main.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
#!/usr/bin/env bash
#
# Required Environment variables:
# SEARCH_QUERY: the search query
# OUTPUT_FORMAT: the output format
# GITHUB_TOKEN: the token for gh
# GITHUB_REPO: the repo e.g. quotidian-ennui/find-matching-issues (added to the query) if required.
#
set -euo pipefail
export GH_PAGER="cat"
function query_issues() {
searchQuery=$(buildQueryString)
# shellcheck disable=SC2016
query='query($searchQuery: String!, $endCursor: String) {
search(first: 100, query: $searchQuery, type: ISSUE, after: $endCursor) {
pageInfo {
hasNextPage,
endCursor
}
nodes {
... on Issue {
title
url
}
}
}
}'
gh api graphql --paginate -F searchQuery="$searchQuery" --raw-field query="$(internal::compressQuery "$query")"
}
# This is clearly a bout of premature optimisation and saving
# a few newlines & spaces shouldn't be high on anyone's list
function internal::compressQuery() {
echo "$1" | tr -s ' ' | tr -d '\n'
}
function buildQueryString() {
if grep -q "repo:" <<< "$SEARCH_QUERY"; then
echo "$SEARCH_QUERY"
else
echo "repo:$GITHUB_REPO $SEARCH_QUERY"
fi
}
function executeQuery() {
case "$OUTPUT_FORMAT" in
simple|raw)
query_issues | jq -r ".data.search.nodes | .[] | .url"
;;
jsonl)
query_issues | jq -r -c ".data.search.nodes | .[]"
;;
json)
# Because pagination we turn it into json lines and
# reslurp it into a single json array. Probably
# not efficient.
query_issues | jq -r -c ".data.search.nodes | .[]" | jq --slurp "."
;;
markdown|list)
query_issues | jq -r ".data.search.nodes | .[] | \"- [\(.title)](\(.url))\""
;;
*)
echo "::error::invalid output format: $OUTPUT_FORMAT"
exit 1
;;
esac
}
OUTPUT_FILE=$(mktemp --tmpdir="${RUNNER_TEMP}" find-matching-issues.XXXXXXXXXX)
echo "path=$OUTPUT_FILE" >> "$GITHUB_OUTPUT"
executeQuery > "$OUTPUT_FILE"