-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtrigger-dispatch-common.sh
More file actions
136 lines (129 loc) · 3.39 KB
/
Copy pathtrigger-dispatch-common.sh
File metadata and controls
136 lines (129 loc) · 3.39 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env bash
# Shared helpers for repository_dispatch trigger scripts.
# Safe to source: does not enable set -euo (callers own shell options).
# shellcheck shell=bash
# shellcheck disable=SC2034
DEFAULT_REPO="cppalliance/boost-docs-translation"
DEFAULT_VERSION="boost-1.90.0"
infer_repo_from_git() {
local url root o r
root="$(git rev-parse --show-toplevel 2>/dev/null)" || return 1
url="$(git -C "$root" remote get-url origin 2>/dev/null)" || return 1
if [[ "$url" =~ github\.com[:/]([^/]+)/([^[:space:]]+) ]]; then
o="${BASH_REMATCH[1]}"
r="${BASH_REMATCH[2]}"
r="${r%.git}"
r="${r%/}"
echo "${o}/${r}"
return 0
fi
return 1
}
resolve_trigger_repo() {
local explicit="${1:-}"
if [[ -n "$explicit" ]]; then
echo "$explicit"
return 0
fi
if [[ -n "${GITHUB_REPOSITORY:-}" ]]; then
echo "$GITHUB_REPOSITORY"
return 0
fi
local inferred
inferred="$(infer_repo_from_git)" && {
echo "$inferred"
return 0
}
if [[ -n "${DEFAULT_REPO:-}" ]]; then
echo "$DEFAULT_REPO"
return 0
fi
return 1
}
resolve_trigger_token() {
local explicit="${1:-}"
local token="${explicit:-${GH_TOKEN:-${GITHUB_TOKEN:-}}}"
if [[ -z "$token" ]]; then
echo "error: set GH_TOKEN (e.g. in repo-root .env), GITHUB_TOKEN, or pass --token" >&2
return 1
fi
echo "$token"
}
require_curl() {
if ! command -v curl >/dev/null 2>&1; then
echo "error: curl is required" >&2
return 1
fi
}
# Build repository_dispatch JSON. Remaining args are alternating KEY VALUE pairs;
# keys with empty values are omitted from client_payload.
build_dispatch_json() {
local event_type="$1"
shift
if command -v jq >/dev/null 2>&1; then
local json_pairs='[]' key val
while [[ $# -gt 0 ]]; do
key="$1"
val="$2"
shift 2
json_pairs="$(jq -n --argjson arr "$json_pairs" --arg k "$key" --arg v "$val" \
'$arr + [{key: $k, value: $v}]')"
done
jq -n --arg event_type "$event_type" --argjson pairs "$json_pairs" \
'{
event_type: $event_type,
client_payload: (
{}
| reduce pairs[] as $p (
.;
if ($p.value | length) > 0 then . + {($p.key): $p.value} else . end
)
)
}'
return 0
fi
local py=""
command -v python3 >/dev/null 2>&1 && py="python3"
[[ -z "$py" ]] && command -v python >/dev/null 2>&1 && py="python"
if [[ -n "$py" ]]; then
"$py" -c 'import json,sys
et=sys.argv[1]
pairs=sys.argv[2:]
d={}
for i in range(0,len(pairs),2):
k,v=pairs[i],pairs[i+1]
if v:
d[k]=v
print(json.dumps({"event_type":et,"client_payload":d}))' \
"$event_type" "$@"
return 0
fi
return 1
}
post_repository_dispatch() {
local repo="$1" token="$2" body="$3" label="$4"
local resp url code
resp="$(mktemp)"
# shellcheck disable=SC2064
trap 'rm -f "$resp"' RETURN
url="https://api.github.com/repos/${repo}/dispatches"
code="$(
curl -sS -o "$resp" -w '%{http_code}' \
-X POST \
-H "Authorization: Bearer ${token}" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Content-Type: application/json" \
-d "$body" \
"$url"
)"
if [[ "$code" == "204" ]]; then
echo "Dispatched ${label} to ${repo} (HTTP ${code})."
return 0
fi
echo "GitHub API error: HTTP ${code}" >&2
if [[ -s "$resp" ]]; then
cat "$resp" >&2
fi
return 1
}