-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathintake-common.sh
More file actions
executable file
·136 lines (114 loc) · 3.1 KB
/
intake-common.sh
File metadata and controls
executable file
·136 lines (114 loc) · 3.1 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
set -euo pipefail
require_env() {
local name="$1"
if [[ -z "${!name:-}" ]]; then
echo "Missing required environment variable: ${name}" >&2
exit 1
fi
}
urlencode() {
python3 - "$1" <<'PY'
import sys
import urllib.parse
print(urllib.parse.quote(sys.argv[1], safe=""))
PY
}
gitlab_project_api() {
local encoded
encoded="$(urlencode "${GITLAB_PROJECT_PATH}")"
printf '%s/api/v4/projects/%s' "${GITLAB_BASE_URL%/}" "${encoded}"
}
gitlab_auth_header() {
printf 'PRIVATE-TOKEN: %s' "${GITLAB_API_TOKEN}"
}
gitlab_find_issue_by_marker() {
local marker="$1"
local search_url
search_url="$(gitlab_project_api)/issues?search=$(urlencode "${marker}")&per_page=100"
curl -fsSL \
--header "$(gitlab_auth_header)" \
"${search_url}" \
| python3 - "${marker}" <<'PY'
import json
import sys
marker = sys.argv[1]
issues = json.load(sys.stdin)
for issue in issues:
description = issue.get("description") or ""
if marker in description:
print(json.dumps({
"iid": issue["iid"],
"web_url": issue["web_url"],
"title": issue["title"],
}))
raise SystemExit(0)
print("")
PY
}
gitlab_create_issue() {
local title="$1"
local description="$2"
local labels="$3"
curl -fsSL \
--request POST \
--header "$(gitlab_auth_header)" \
--data-urlencode "title=${title}" \
--data-urlencode "description=${description}" \
--data-urlencode "labels=${labels}" \
"$(gitlab_project_api)/issues"
}
github_comments_api() {
local issue_number="$1"
printf '%s/repos/%s/issues/%s/comments' "${GITHUB_API_URL%/}" "${GITHUB_REPOSITORY}" "${issue_number}"
}
github_comment_exists() {
local issue_number="$1"
local marker="$2"
curl -fsSL \
--header "Authorization: Bearer ${GITHUB_TOKEN}" \
--header "Accept: application/vnd.github+json" \
"$(github_comments_api "${issue_number}")" \
| python3 - "${marker}" <<'PY'
import json
import sys
marker = sys.argv[1]
comments = json.load(sys.stdin)
for comment in comments:
if marker in (comment.get("body") or ""):
raise SystemExit(0)
raise SystemExit(1)
PY
}
github_post_comment() {
local issue_number="$1"
local body="$2"
python3 - "${body}" <<'PY' >/tmp/github-intake-comment.json
import json
import sys
print(json.dumps({"body": sys.argv[1]}))
PY
curl -fsSL \
--request POST \
--header "Authorization: Bearer ${GITHUB_TOKEN}" \
--header "Accept: application/vnd.github+json" \
--header "Content-Type: application/json" \
--data @/tmp/github-intake-comment.json \
"$(github_comments_api "${issue_number}")" >/dev/null
}
maybe_post_backlink_comment() {
local issue_number="$1"
local marker="$2"
local body="$3"
local mode="${GITHUB_BACKLINK_MODE:-none}"
if [[ "${mode}" != "comment" ]]; then
echo "GitHub backlink comment skipped: mode=${mode}"
return 0
fi
if github_comment_exists "${issue_number}" "${marker}"; then
echo "GitHub backlink comment already present for ${marker}"
return 0
fi
github_post_comment "${issue_number}" "${body}"
echo "GitHub backlink comment posted for ${marker}"
}