-
Notifications
You must be signed in to change notification settings - Fork 0
/
in
executable file
·211 lines (194 loc) · 6.58 KB
/
in
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
clearup ()
{
set +x
rm -f "$apijson"
rm -f "$tmpjson"
}
trap clearup EXIT INT TERM
apijson=$(mktemp -u /tmp/json-XXXXXX)
tmpjson=$(mktemp -u /tmp/json-XXXXXX)
exec 3>&1
exec 1>&2
payload="$(cat <&0)"
if [ "$(jq -r '.source | has("url")' <<< "$payload")" = 'true' ]; then
url="$(jq -r .source.url <<< "$payload")"
else
echo 'You need to provide the url for Jira REST APIs.'
exit 1
fi
if [ "$(jq -r '.source | has("user")' <<< "$payload")" = 'true' ]; then
user="$(jq -r .source.user <<< "$payload")"
else
echo 'You need to provide the user for Jira REST APIs.'
exit 1
fi
if [ "$(jq -r '.source | has("token")' <<< "$payload")" = 'true' ]; then
token="$(jq -r .source.token <<< "$payload")"
else
echo 'You need to provide the token for Jira REST APIs.'
exit 1
fi
if [ "$(jq -r '.source | has("resource")' <<< "$payload")" = 'true' ]; then
resource="$(jq -r .source.resource <<< "$payload")"
else
echo 'You need to provide the resource path for Jira REST APIs.'
exit 1
fi
fetch="$(jq -r '.source.fetch // ""' <<< "$payload")"
if [ "$(jq -r '.source | has("debug")' <<< "$payload")" = 'true' ]; then
debug="$(jq -r .source.debug <<< "$payload")"
else
debug=
fi
if [ -n "$debug" ] && [ "$debug" = "verbose" ]; then
set -x
fi
if [ -n "$debug" ]; then
printenv
[ "$(jq -r 'has("version")' <<< "$payload")" = 'true' ] && jq -r -S -C '.version' <<< "$payload"
fi
CURL=(curl --retry 3 --max-time 10 -fSL --user "$user:$token")
URL="$url$resource"
case "$0" in
('/opt/resource/check')
[ -z "$debug" ] && echo "curl -fSL ${URL}"
"${CURL[@]}" "$URL" > "$apijson"
if [[ "$resource" =~ ^search.* ]] && [ "$(jq -r .total < "$apijson")" = "0" ]; then
jq -S -M < "$apijson"
exit 1
fi
if [ -n "$fetch" ] && [ "$(jq -r 'has("startAt")' < "$apijson")" = "true" ] && [ "$(jq -r 'has("total")' < "$apijson")" = "true" ] && [ "$(jq -r "has(\"$fetch\")" < "$apijson")" = "true" ]; then
startAt="$(jq -r '.startAt' < "$apijson")"
maxResults="$(jq -r '.maxResults // 50' < "$apijson")"
total="$(jq -r '.total' < "$apijson")"
next=$((startAt+maxResults))
while [ "$next" -lt "$total" ]; do
mv "$apijson" "$tmpjson"
"${CURL[@]}" "$URL&startAt=$next" > "$apijson"
jq --slurpfile prev "$tmpjson" ".$fetch += \$prev[].$fetch" "$apijson" > tmp.json
mv tmp.json "$apijson"
startAt="$(jq -r '.startAt' < "$apijson")"
maxResults="$(jq -r '.maxResults // 50' < "$apijson")"
total="$(jq -r '.total' < "$apijson")"
next=$((startAt+maxResults))
done
fi
digest=$(jq -S -M < "$apijson" | sha256sum | awk '{print $1}')
json=$(cat <<ENDLINE
[
{
"digest": "sha256:${digest}"
}
]
ENDLINE
)
;;
('/opt/resource/in')
[ -z "$debug" ] && echo "curl -fSL ${URL}"
"${CURL[@]}" "$URL" > "$apijson"
if [[ "$resource" =~ ^search.* ]] && [ "$(jq -r .total < "$apijson")" = "0" ]; then
jq -S -M < "$apijson"
exit 1
fi
if [ -n "$fetch" ] && [ "$(jq -r 'has("startAt")' < "$apijson")" = "true" ] && [ "$(jq -r 'has("total")' < "$apijson")" = "true" ] && [ "$(jq -r "has(\"$fetch\")" < "$apijson")" = "true" ]; then
startAt="$(jq -r '.startAt' < "$apijson")"
maxResults="$(jq -r '.maxResults // 50' < "$apijson")"
total="$(jq -r '.total' < "$apijson")"
next=$((startAt+maxResults))
while [ "$next" -lt "$total" ]; do
mv "$apijson" "$tmpjson"
"${CURL[@]}" "$URL&startAt=$next" > "$apijson"
jq --slurpfile prev "$tmpjson" ".$fetch += \$prev[].$fetch" "$apijson" > tmp.json
mv tmp.json "$apijson"
startAt="$(jq -r '.startAt' < "$apijson")"
maxResults="$(jq -r '.maxResults // 50' < "$apijson")"
total="$(jq -r '.total' < "$apijson")"
next=$((startAt+maxResults))
done
fi
cp "$apijson" "$1"/payload.json
digest=$(jq -S -M < "$apijson" | sha256sum | awk '{print $1}')
json=$(cat <<ENDLINE
{
"version": {
"digest": "sha256:${digest}"
},
"metadata": [
{
"name": "resource",
"value": "$(echo "$resource" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g')"
}
]
}
ENDLINE
)
;;
('/opt/resource/out')
if [ "$(jq -r '.params | has("json")' <<< "$payload")" = 'false' ]; then
echo 'You need to provide the json param in the put step.'
exit 1
fi
cd "$1"
DATA=$(jq -r '.params.json' <<< "$payload")
if [ -f "$DATA" ]; then
if ! jq -r -S -C < "$DATA"; then
echo "The json param doesn't contain the valid json content or the existing file path."
exit 1
fi
DATA="@$DATA"
else
if ! jq -r -S -C <<< "$DATA"; then
echo "The json param doesn't contain the valid json content or the existing file path."
if [ -n "$debug" ]; then
echo "$DATA"
fi
exit 1
fi
fi
if [[ "$resource" =~ [0-9]+$ ]]; then
METHOD=(-X PUT)
else
METHOD=(-X POST)
fi
[ -z "$debug" ] && echo "curl -fSL ${METHOD[*]} --data \"$DATA\" -H \"Content-Type: application/json\" ${URL}"
"${CURL[@]}" "${METHOD[@]}" --data "$DATA" -H "Content-Type: application/json" "$URL" > "$apijson"
if [ -s "$apijson" ] && [ -n "$debug" ] && [ "$debug" = "verbose" ]; then
jq -r -S -C < "$apijson"
fi
if [[ "$resource" =~ [0-9]+$ ]]; then
[ -z "$debug" ] && echo "curl -fSL ${URL}"
"${CURL[@]}" "$URL" > "$apijson"
if [ -n "$debug" ] && [ "$debug" = "verbose" ]; then
jq -r -S -C < "$apijson"
fi
fi
digest=$(jq -S -M < "$apijson" | sha256sum | awk '{print $1}')
json=$(cat <<ENDLINE
{
"version": {
"digest": "sha256:${digest}"
},
"metadata": [
{
"name": "resource",
"value": "$(echo "$resource" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g')"
}
]
}
ENDLINE
)
;;
esac
echo "$json" > "$tmpjson"
if [ -n "$debug" ]; then
if jq -S -M < "$tmpjson" > /dev/null; then
jq -r -S -C < "$tmpjson"
else
cat "$tmpjson"
fi
fi
sed -i 's/\t/ /g' "$tmpjson"
jq -n --slurpfile all "$tmpjson" '$all[0]' >&3