Repository navigation
Expand file tree
/
Copy pathlinear-graphql-request.sh
More file actions
executable file
·71 lines (62 loc) · 2.31 KB
/
Copy pathlinear-graphql-request.sh
File metadata and controls
executable file
·71 lines (62 loc) · 2.31 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
#!/usr/bin/env bash
set -euo pipefail
# LINEAR_ACCESS_TOKEN is a Linear OAuth app token minted by linear-app-token.sh.
if [[ -z "${LINEAR_ACCESS_TOKEN:-}" ]]; then
echo "LINEAR_ACCESS_TOKEN is required" >&2
exit 1
fi
payload=$(cat)
configured_attempts="${LINEAR_GRAPHQL_ATTEMPTS:-4}"
retry_delay="${LINEAR_GRAPHQL_RETRY_DELAY_SECONDS:-2}"
endpoint="${LINEAR_GRAPHQL_ENDPOINT:-https://api.linear.app/graphql}"
is_mutation=$(jq -r '(.query // "") | test("^\\s*mutation(?:\\s|\\(|\\{)")' <<<"$payload")
# Retrying a mutation after an ambiguous transport failure can replay a write
# that Linear already committed. Queries are safe to retry; mutations fail
# visibly after one attempt and rely on the workflow's reconciliation pass.
if [[ "$is_mutation" == "true" ]]; then
attempts=1
else
attempts="$configured_attempts"
fi
for ((attempt = 1; attempt <= attempts; attempt++)); do
response_file=$(mktemp)
http_code=""
if http_code=$(curl \
--silent \
--show-error \
--output "$response_file" \
--write-out '%{http_code}' \
--connect-timeout 10 \
--max-time 45 \
-X POST "$endpoint" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LINEAR_ACCESS_TOKEN" \
-d "$payload"); then
response=$(<"$response_file")
rm -f "$response_file"
if [[ "$http_code" =~ ^2[0-9][0-9]$ ]] && jq -e . >/dev/null 2>&1 <<<"$response"; then
printf '%s' "$response"
exit 0
fi
if [[ "$http_code" =~ ^(408|429|5[0-9][0-9])$ ]]; then
echo "Linear GraphQL returned transient HTTP $http_code (attempt $attempt/$attempts)." >&2
elif jq -e . >/dev/null 2>&1 <<<"$response"; then
# Preserve structured non-retryable errors so the workflow can report
# the GraphQL response rather than replacing it with a transport error.
printf '%s' "$response"
exit 0
else
echo "Linear GraphQL returned a non-JSON response (HTTP $http_code, attempt $attempt/$attempts)." >&2
fi
else
curl_status=$?
response=$(<"$response_file")
rm -f "$response_file"
echo "Linear GraphQL request failed (curl $curl_status, HTTP ${http_code:-unknown}, attempt $attempt/$attempts)." >&2
fi
if ((attempt < attempts)); then
sleep "$retry_delay"
fi
done
echo "Linear GraphQL did not return a valid JSON response after $attempts attempts." >&2
exit 1