-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathgh.sh
More file actions
executable file
·114 lines (100 loc) · 2.55 KB
/
gh.sh
File metadata and controls
executable file
·114 lines (100 loc) · 2.55 KB
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
113
114
#!/bin/bash
source "$(dirname "$0")/setup.sh"
query=$1
item=$(
cat <<'EOF'
{
uid: .id,
title: .full_name,
subtitle: "Open \(.html_url)",
arg: .html_url,
autocomplete: .full_name,
mods: {
alt: {
arg: .ssh_url,
subtitle: "Copy clone command with \(.ssh_url)"
},
"shift+alt": {
arg: .clone_url,
subtitle: "Copy clone command with \(.clone_url)"
},
ctrl: {
arg: "\(.html_url)/actions",
subtitle: "Open \(.html_url)/actions"
},
cmd: {
arg: .full_name,
subtitle: "List open pull requests"
}
},
text: {
copy: .html_url
}
}
EOF
)
empty_result='{"items":[]}'
err=$(mktemp) || {
printf '%s' "$empty_result"
exit 0
}
repos=$(gh api /user/repos --method GET \
-f sort=pushed \
-F per_page=100 \
--hostname "$API_HOST" \
--cache "$CACHE_USER_REPOS" \
--paginate \
--jq "[.[] | select(.archived == false) | $item]" 2>"$err")
gh_exit=$?
err_msg=$(<"$err")
rm -f "$err"
if [[ $gh_exit -ne 0 ]]; then
if [[ -n "$err_msg" ]]; then
printf '%s\n' "$err_msg" >&2
fi
printf '%s' "$empty_result"
exit 0
fi
# --paginate outputs one JSON array per page; merge them and optionally filter
if [[ -n "$query" ]]; then
# Use --arg to safely pass query into jq (no injection possible)
repos=$(printf '%s\n' "$repos" | jq -s --arg q "$query" \
'[add // [] | .[] | select(.title | ascii_downcase | contains($q | ascii_downcase))]') \
|| repos="[]"
else
repos=$(printf '%s\n' "$repos" | jq -s 'add // []') || repos="[]"
fi
# Check if we got results
has_results=$(printf '%s' "$repos" | jq 'length > 0' 2>/dev/null)
# Fall back to search if no local match and query is present
if [[ "$has_results" != "true" && -n "$query" ]]; then
# Sanitize query: allowlist of safe characters only
safe_query=$(printf '%s' "$query" | tr -cd 'a-zA-Z0-9 ._-')
if [[ -n "$safe_query" ]]; then
err=$(mktemp) || {
printf '%s' "$empty_result"
exit 0
}
repos=$(gh api /search/repositories --method GET \
--hostname "$API_HOST" \
-f q="$safe_query in:name archived:false" \
-F per_page=9 \
-f sort=pushed \
--cache "$CACHE_SEARCH_REPOS" \
--jq "[.items[] | $item]" 2>"$err")
gh_exit=$?
err_msg=$(<"$err")
rm -f "$err"
if [[ $gh_exit -ne 0 ]]; then
if [[ -n "$err_msg" ]]; then
printf '%s\n' "$err_msg" >&2
fi
repos="[]"
fi
fi
if [[ -z "$repos" ]]; then
repos="[]"
fi
fi
# Final output with jq fallback
printf '%s' "$repos" | jq -c '{items: .}' 2>/dev/null || printf '%s' "$empty_result"