feat: scripts/bg, een kleine CLI voor agents, met docs/agents.md
@@ -0,0 +1,90 @@
1
# BlueGit voor agents en scripts
2
3
`scripts/bg` is een kleine CLI op de BlueGit-API. Hij heeft alleen `bash`, `curl` en `jq` nodig.
4
5
```sh
6
export BLUEGIT_URL=https://git.timfalken.com # standaard
7
export BLUEGIT_TOKEN=... # optioneel, nooit printen
8
scripts/bg help
9
```
10
11
## Rechten
12
13
- **Publieke projecten** zijn zonder token leesbaar, net als in de UI. Dat geldt voor status, pipelines, jobs, logs, MR's, issues, bestanden en releases.
14
- **Privé projecten**, en projecten in een privé groep, geven `404` zonder token of als je geen lid bent. Je ziet dus ook niet of zo'n project bestaat.
15
- **Schrijven** (MR, notitie, issue, branch, release, retry) vraagt altijd een token, met de rol die de UI ook vraagt. Zonder token krijg je `401`, met te weinig rechten `403`.
16
- **Een token** werkt met de rechten van de eigenaar, beperkt door de scopes van het token (`read_api`, `api`, …).
17
18
`bg` zet fouten op stderr met de HTTP-code en eindigt met exit 1. Een 404 kan dus ook "geen toegang" betekenen.
19
20
## Pipelines
21
22
```sh
23
bg pipeline status timfa/bluegit --branch stage # nieuwste pipeline op een branch
24
bg pipeline status timfa/bluegit --sha 3e6b2f1 # op commit (prefix van 7+ tekens)
25
bg pipeline status timfa/bluegit --mr 40 # van MR 40 (incl. pushes naar de bronbranch)
26
bg pipeline status timfa/bluegit --id 158
27
28
bg pipeline wait timfa/bluegit --sha "$(git rev-parse HEAD)" --timeout 1200 --interval 10
29
bg pipeline logs timfa/bluegit 158 611
30
bg pipeline retry timfa/bluegit 158 # alleen mislukte jobs (+ wat daarop wachtte)
31
bg pipeline retry timfa/bluegit 158 --all # nieuwe pipeline
32
```
33
34
`wait` wacht eerst tot er voor de selectie een pipeline is (handig direct na een push) en daarna tot die klaar is. De exitcodes:
35
36
| exit | betekenis |
37
|---|---|
38
| 0 | success |
39
| 1 | failed of canceled; de laatste 80 regels van elke mislukte job staan op stderr (`BG_LOG_LINES` om aan te passen) |
40
| 2 | timeout |
41
42
Jobs met `allow_failure` die faalden geven een waarschuwing, maar het blijft exit 0.
43
44
Typisch voor een cloud-agent na een push:
45
46
```sh
47
git push bluegit HEAD:refs/heads/mijn-branch
48
sha=$(git rev-parse HEAD)
49
if scripts/bg pipeline wait timfa/bluegit --sha "$sha" --timeout 1800; then
50
echo groen
51
else
52
echo "rood of timeout ($?)"; exit 1
53
fi
54
```
55
56
## Merge requests
57
58
```sh
59
bg mr list timfa/bluegit # open
60
bg mr list timfa/bluegit --state all --source mijn-branch
61
bg mr create timfa/bluegit --source mijn-branch --target stage --title "fix: ..." --description "..."
62
bg mr show timfa/bluegit 40
63
bg mr comment timfa/bluegit 40 "Ziet er goed uit"
64
bg mr merge timfa/bluegit 40 # verwijdert de bronbranch; --keep-source om te houden
65
```
66
67
## Issues, branches, releases, bestanden
68
69
```sh
70
bg issue list timfa/bluegit [open|closed|all]
71
bg issue create timfa/bluegit "Titel" "Beschrijving"
72
bg issue comment timfa/bluegit 7 "Opgelost in !40"
73
bg issue close timfa/bluegit 7
74
75
bg branch create timfa/bluegit mijn-branch --from stage
76
bg release create timfa/bluegit v1.2.0 --commit <sha> --asset build:bluegit:bluegit-linux-amd64
77
bg file get timfa/bluegit README.md --ref stage
78
```
79
80
Een release-asset komt uit een artifact van een job in dezelfde repo (`JOB:PAD[:NAAM]`). Een los bestand uploaden kan de API nog niet.
81
82
## Ruwe API
83
84
De CLI is dun. Alles staat ook in `/api/v1/openapi.json`. Handige filters:
85
86
- `GET /api/v1/projects/{owner}/{repo}/pipelines?sha=…|ref=…|mr=…`
87
- `GET /api/v1/projects/{owner}/{repo}/merge_requests?state=open|merged|closed|all&source_branch=…&target_branch=…`
88
- `GET /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/log`
89
90
Owner en repo zijn twee padsegmenten (`timfa/bluegit`), niet urlencoded.
@@ -0,0 +1,90 @@1
# BlueGit voor agents en scripts2
3
`scripts/bg` is een kleine CLI op de BlueGit-API. Hij heeft alleen `bash`, `curl` en `jq` nodig.4
5
```sh6
export BLUEGIT_URL=https://git.timfalken.com # standaard7
export BLUEGIT_TOKEN=... # optioneel, nooit printen8
scripts/bg help9
```10
11
## Rechten12
13
- **Publieke projecten** zijn zonder token leesbaar, net als in de UI. Dat geldt voor status, pipelines, jobs, logs, MR's, issues, bestanden en releases.14
- **Privé projecten**, en projecten in een privé groep, geven `404` zonder token of als je geen lid bent. Je ziet dus ook niet of zo'n project bestaat.15
- **Schrijven** (MR, notitie, issue, branch, release, retry) vraagt altijd een token, met de rol die de UI ook vraagt. Zonder token krijg je `401`, met te weinig rechten `403`.16
- **Een token** werkt met de rechten van de eigenaar, beperkt door de scopes van het token (`read_api`, `api`, …).17
18
`bg` zet fouten op stderr met de HTTP-code en eindigt met exit 1. Een 404 kan dus ook "geen toegang" betekenen.19
20
## Pipelines21
22
```sh23
bg pipeline status timfa/bluegit --branch stage # nieuwste pipeline op een branch24
bg pipeline status timfa/bluegit --sha 3e6b2f1 # op commit (prefix van 7+ tekens)25
bg pipeline status timfa/bluegit --mr 40 # van MR 40 (incl. pushes naar de bronbranch)26
bg pipeline status timfa/bluegit --id 15827
28
bg pipeline wait timfa/bluegit --sha "$(git rev-parse HEAD)" --timeout 1200 --interval 1029
bg pipeline logs timfa/bluegit 158 61130
bg pipeline retry timfa/bluegit 158 # alleen mislukte jobs (+ wat daarop wachtte)31
bg pipeline retry timfa/bluegit 158 --all # nieuwe pipeline32
```33
34
`wait` wacht eerst tot er voor de selectie een pipeline is (handig direct na een push) en daarna tot die klaar is. De exitcodes:35
36
| exit | betekenis |37
|---|---|38
| 0 | success |39
| 1 | failed of canceled; de laatste 80 regels van elke mislukte job staan op stderr (`BG_LOG_LINES` om aan te passen) |40
| 2 | timeout |41
42
Jobs met `allow_failure` die faalden geven een waarschuwing, maar het blijft exit 0.43
44
Typisch voor een cloud-agent na een push:45
46
```sh47
git push bluegit HEAD:refs/heads/mijn-branch48
sha=$(git rev-parse HEAD)49
if scripts/bg pipeline wait timfa/bluegit --sha "$sha" --timeout 1800; then50
echo groen51
else52
echo "rood of timeout ($?)"; exit 153
fi54
```55
56
## Merge requests57
58
```sh59
bg mr list timfa/bluegit # open60
bg mr list timfa/bluegit --state all --source mijn-branch61
bg mr create timfa/bluegit --source mijn-branch --target stage --title "fix: ..." --description "..."62
bg mr show timfa/bluegit 4063
bg mr comment timfa/bluegit 40 "Ziet er goed uit"64
bg mr merge timfa/bluegit 40 # verwijdert de bronbranch; --keep-source om te houden65
```66
67
## Issues, branches, releases, bestanden68
69
```sh70
bg issue list timfa/bluegit [open|closed|all]71
bg issue create timfa/bluegit "Titel" "Beschrijving"72
bg issue comment timfa/bluegit 7 "Opgelost in !40"73
bg issue close timfa/bluegit 774
75
bg branch create timfa/bluegit mijn-branch --from stage76
bg release create timfa/bluegit v1.2.0 --commit <sha> --asset build:bluegit:bluegit-linux-amd6477
bg file get timfa/bluegit README.md --ref stage78
```79
80
Een release-asset komt uit een artifact van een job in dezelfde repo (`JOB:PAD[:NAAM]`). Een los bestand uploaden kan de API nog niet.81
82
## Ruwe API83
84
De CLI is dun. Alles staat ook in `/api/v1/openapi.json`. Handige filters:85
86
- `GET /api/v1/projects/{owner}/{repo}/pipelines?sha=…|ref=…|mr=…`87
- `GET /api/v1/projects/{owner}/{repo}/merge_requests?state=open|merged|closed|all&source_branch=…&target_branch=…`88
- `GET /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/log`89
90
Owner en repo zijn twee padsegmenten (`timfa/bluegit`), niet urlencoded.@@ -0,0 +1,208 @@
1
#!/usr/bin/env bash
2
# bg: kleine BlueGit-CLI voor mensen en agents. Alleen bash, curl en jq.
3
#
4
# BLUEGIT_URL standaard https://git.timfalken.com
5
# BLUEGIT_TOKEN optioneel; nodig voor schrijven en voor privé projecten.
6
# Wordt nooit geprint.
7
#
8
# Zie docs/agents.md of `bg help`.
9
set -euo pipefail
10
11
BASE="${BLUEGIT_URL:-https://git.timfalken.com}"
12
BASE="${BASE%/}"
13
API="$BASE/api/v1"
14
15
die() { echo "bg: $*" >&2; exit 64; }
16
command -v curl >/dev/null || die "curl ontbreekt"
17
command -v jq >/dev/null || die "jq ontbreekt"
18
19
# req METHOD PATH [JSON] -> body op stdout; bij HTTP >= 400 de fout op stderr en exit 1.
20
req() {
21
local method=$1 path=$2 data=${3:-} out code
22
local args=(-sS -X "$method" -w '\n%{http_code}' -H "Accept: application/json")
23
[[ -n "${BLUEGIT_TOKEN:-}" ]] && args+=(-H "Authorization: Bearer ${BLUEGIT_TOKEN}")
24
[[ -n "$data" ]] && args+=(-H "Content-Type: application/json" --data-binary "$data")
25
out=$(curl "${args[@]}" "$API$path") || { echo "bg: verbinding mislukt: $method $path" >&2; return 1; }
26
code=${out##*$'\n'}
27
out=${out%$'\n'*}
28
if (( code >= 400 )); then
29
local msg
30
msg=$(printf '%s' "$out" | jq -r '.message // .error // empty' 2>/dev/null || true)
31
echo "bg: HTTP $code bij $method $path${msg:+: $msg}" >&2
32
[[ $code == 404 ]] && echo "bg: (bestaat niet, of je hebt geen toegang)" >&2
33
return 1
34
fi
35
printf '%s' "$out"
36
}
37
get() { req GET "$1"; }
38
enc() { jq -rn --arg v "$1" '$v|@uri'; }
39
proj() { [[ "$1" == */* ]] || die "project moet owner/repo zijn: $1"; echo "/projects/$1"; }
40
issues() { echo "/repos/$1/issues"; }
41
json() { jq -cn "$@"; }
42
43
# ---------- pipelines ----------
44
45
# select_pipeline PROJECT ARGS... -> pipeline-JSON (of leeg als er nog geen is)
46
select_query() {
47
local q=""
48
while (($#)); do
49
case $1 in
50
--sha) q="sha=$2"; shift 2 ;;
51
--mr) q="mr=$2"; shift 2 ;;
52
--branch|--ref) q="ref=$(enc "$2")"; shift 2 ;;
53
--id) q="id=$2"; shift 2 ;;
54
--timeout|--interval) shift 2 ;;
55
*) die "onbekende optie: $1" ;;
56
esac
57
done
58
echo "$q"
59
}
60
find_pipeline() {
61
local p=$1 q=$2
62
if [[ $q == id=* ]]; then get "$(proj "$p")/pipelines/${q#id=}"; return; fi
63
get "$(proj "$p")/pipelines?per_page=1${q:+&$q}" | jq -c '.[0] // empty'
64
}
65
show_pipeline() {
66
local p=$1 pipe=$2
67
jq -r '"pipeline #\(.id) \(.status) \(.ref|sub("^refs/heads/";"")) \(.sha[0:8]) \(.web_url)"' <<<"$pipe"
68
get "$(proj "$p")/pipelines/$(jq -r .id <<<"$pipe")/jobs" |
69
jq -r '.[] | " \(.id)\t\(.status)\t\(.stage)/\(.name)\(if .allow_failure then " (allow_failure)" else "" end)"'
70
}
71
failed_logs() {
72
local p=$1 id=$2 jobs
73
jobs=$(get "$(proj "$p")/pipelines/$id/jobs")
74
jq -r '.[]|select(.status=="failed" and .allow_failure)|"bg: waarschuwing: \(.name) (#\(.id)) faalde (allow_failure)"' <<<"$jobs" >&2
75
for j in $(jq -r '.[]|select(.status=="failed" and (.allow_failure|not))|.id' <<<"$jobs"); do
76
echo "----- log job $j (laatste ${BG_LOG_LINES:-80} regels) -----" >&2
77
get "$(proj "$p")/pipelines/$id/jobs/$j/log" | tail -n "${BG_LOG_LINES:-80}" >&2 || true
78
done
79
}
80
cmd_pipeline() {
81
local sub=${1:-}; shift || true
82
case $sub in
83
status)
84
local p=${1:?project}; shift
85
local q pipe; q=$(select_query "$@")
86
pipe=$(find_pipeline "$p" "$q")
87
[[ -n "$pipe" ]] || { echo "geen pipeline gevonden" >&2; exit 3; }
88
show_pipeline "$p" "$pipe" ;;
89
wait)
90
local p=${1:?project}; shift
91
local timeout=1800 interval=10 a=("$@")
92
while (($#)); do case $1 in --timeout) timeout=$2; shift 2 ;; --interval) interval=$2; shift 2 ;; *) shift ;; esac; done
93
local q pipe status start=$SECONDS last=""
94
q=$(select_query "${a[@]}")
95
while :; do
96
pipe=$(find_pipeline "$p" "$q" || true)
97
status=$(jq -r '.status // empty' <<<"${pipe:-{\}}")
98
if [[ -n "$status" && "$status" != "$last" ]]; then
99
echo "pipeline #$(jq -r .id <<<"$pipe"): $status" >&2; last=$status
100
fi
101
case $status in
102
success) show_pipeline "$p" "$pipe"; failed_logs "$p" "$(jq -r .id <<<"$pipe")" 2>&1 | grep '^bg: waarschuwing' >&2 || true; exit 0 ;;
103
failed|canceled) show_pipeline "$p" "$pipe"; failed_logs "$p" "$(jq -r .id <<<"$pipe")"; exit 1 ;;
104
esac
105
(( SECONDS - start >= timeout )) && { echo "bg: timeout na ${timeout}s (status ${status:-geen pipeline})" >&2; exit 2; }
106
sleep "$interval"
107
done ;;
108
logs) get "$(proj "${1:?project}")/pipelines/${2:?pipeline-id}/jobs/${3:?job-id}/log" ;;
109
retry)
110
local mode=failed
111
[[ ${3:-} == --all ]] && mode=all
112
req POST "$(proj "${1:?project}")/pipelines/${2:?pipeline-id}/retry" "$(json --arg m "$mode" '{mode:$m}')" | jq -r '"pipeline #\(.id) \(.status) \(.web_url)"' ;;
113
list) get "$(proj "${1:?project}")/pipelines?per_page=${2:-10}" | jq -r '.[]|"\(.id)\t\(.status)\t\(.ref|sub("^refs/heads/";""))\t\(.sha[0:8])"' ;;
114
*) die "pipeline status|wait|logs|retry|list" ;;
115
esac
116
}
117
118
# ---------- merge requests ----------
119
cmd_mr() {
120
local sub=${1:-}; shift || true
121
case $sub in
122
list)
123
local p=${1:?project}; shift; local q="state=open"
124
while (($#)); do case $1 in --state) q="state=$2"; shift 2 ;; --source) q+="&source_branch=$(enc "$2")"; shift 2 ;; --target) q+="&target_branch=$(enc "$2")"; shift 2 ;; *) die "onbekende optie $1" ;; esac; done
125
get "$(proj "$p")/merge_requests?per_page=100&$q" | jq -r '.[]|"!\(.iid)\t\(.state)\t\(.source_branch) -> \(.target_branch)\t\(.title)"' ;;
126
show) get "$(proj "${1:?project}")/merge_requests/${2:?nummer}" | jq . ;;
127
create)
128
local p=${1:?project}; shift; local src="" tgt="" title="" desc="" remove=true
129
while (($#)); do case $1 in --source) src=$2; shift 2 ;; --target) tgt=$2; shift 2 ;; --title) title=$2; shift 2 ;; --description) desc=$2; shift 2 ;; --keep-source) remove=false; shift ;; *) die "onbekende optie $1" ;; esac; done
130
[[ -n $src && -n $tgt && -n $title ]] || die "mr create PROJECT --source B --target B --title T [--description D] [--keep-source]"
131
req POST "$(proj "$p")/merge_requests" "$(json --arg s "$src" --arg t "$tgt" --arg ti "$title" --arg d "$desc" --argjson r "$remove" '{source_branch:$s,target_branch:$t,title:$ti,description:$d,remove_source_branch:$r}')" | jq -r '"!\(.iid) \(.web_url)"' ;;
132
merge)
133
local remove=true; [[ ${3:-} == --keep-source ]] && remove=false
134
req POST "$(proj "${1:?project}")/merge_requests/${2:?nummer}/merge" "$(json --argjson r "$remove" '{remove_source_branch:$r}')" | jq -r '"\(.state) \(.web_url)"' ;;
135
comment) req POST "$(proj "${1:?project}")/merge_requests/${2:?nummer}/notes" "$(json --arg b "${3:?tekst}" '{body:$b}')" | jq -r '"notitie \(.id)"' ;;
136
*) die "mr list|show|create|merge|comment" ;;
137
esac
138
}
139
140
# ---------- issues ----------
141
cmd_issue() {
142
local sub=${1:-}; shift || true
143
case $sub in
144
list) get "$(issues "${1:?project}")?state=${2:-open}" | jq -r '.issues[]|"#\(.id)\t\(.status)\t\(.title)"' ;;
145
show) get "$(issues "${1:?project}")/${2:?nummer}" | jq . ;;
146
create) req POST "$(issues "${1:?project}")" "$(json --arg t "${2:?titel}" --arg d "${3:-}" '{title:$t,description:$d}')" | jq -r '"#\(.id) \(.web_url)"' ;;
147
comment) req POST "$(issues "${1:?project}")/${2:?nummer}/notes" "$(json --arg b "${3:?tekst}" '{body:$b}')" >/dev/null && echo "ok" ;;
148
close|reopen)
149
local st=closed; [[ $sub == reopen ]] && st=open
150
req PUT "$(issues "${1:?project}")/${2:?nummer}" "$(json --arg s "$st" '{state:$s}')" | jq -r '"#\(.id) \(.status)"' ;;
151
*) die "issue list|show|create|comment|close|reopen" ;;
152
esac
153
}
154
155
# ---------- overig ----------
156
cmd_branch() {
157
[[ ${1:-} == create ]] || die "branch create PROJECT NAAM [--from REF]"
158
local p=${2:?project} name=${3:?naam} from=""
159
[[ ${4:-} == --from ]] && from=${5:?ref}
160
req POST "$(proj "$p")/repository/branches" "$(json --arg n "$name" --arg f "$from" '{name:$n} + (if $f=="" then {} else {from:$f} end)')" | jq -r '.name // .'
161
}
162
cmd_release() {
163
[[ ${1:-} == create ]] || die "release create PROJECT TAG [--commit SHA] [--name N] [--body B] [--asset JOB:PAD[:NAAM]]..."
164
local p=${2:?project} tag=${3:?tag}; shift 3
165
local commit="" name="" body="" assets="[]"
166
while (($#)); do case $1 in
167
--commit) commit=$2; shift 2 ;; --name) name=$2; shift 2 ;; --body) body=$2; shift 2 ;;
168
--asset) IFS=: read -r j pth nm <<<"$2"; assets=$(jq -c --arg j "$j" --arg p "$pth" --arg n "${nm:-}" '. + [{job:$j,path:$p} + (if $n=="" then {} else {name:$n} end)]' <<<"$assets"); shift 2 ;;
169
*) die "onbekende optie $1" ;; esac; done
170
req POST "$(proj "$p")/releases" "$(json --arg t "$tag" --arg c "$commit" --arg n "$name" --arg b "$body" --argjson a "$assets" '{tag:$t,commit:$c,name:$n,body:$b,assets:$a}')" | jq -c .
171
}
172
cmd_file() {
173
[[ ${1:-} == get ]] || die "file get PROJECT PAD [--ref REF]"
174
local p=${2:?project} path=${3:?pad} ref=""
175
[[ ${4:-} == --ref ]] && ref="?ref=$(enc "${5:?ref}")"
176
local body; body=$(get "$(proj "$p")/repository/files/$(enc "$path")$ref")
177
if jq -e '.content and .encoding=="base64"' <<<"$body" >/dev/null 2>&1; then jq -r .content <<<"$body" | base64 -d
178
elif jq -e '.content' <<<"$body" >/dev/null 2>&1; then jq -r .content <<<"$body"
179
else printf '%s' "$body"; fi
180
}
181
182
usage() {
183
sed -n '2,8p' "$0" | sed 's/^# \{0,1\}//'
184
cat <<'U'
185
186
bg pipeline status PROJECT [--sha X | --mr N | --branch B | --id P]
187
bg pipeline wait PROJECT [selectie] [--timeout 1800] [--interval 10] exit 0 ok, 1 mislukt, 2 timeout
188
bg pipeline logs PROJECT PIPELINE JOB
189
bg pipeline retry PROJECT PIPELINE [--all]
190
bg pipeline list PROJECT [N]
191
bg mr list|show|create|merge|comment ...
192
bg issue list|show|create|comment|close|reopen ...
193
bg branch create PROJECT NAAM [--from REF]
194
bg release create PROJECT TAG [--commit SHA] [--asset JOB:PAD[:NAAM]]
195
bg file get PROJECT PAD [--ref REF]
196
U
197
}
198
199
case ${1:-help} in
200
pipeline|pl) shift; cmd_pipeline "$@" ;;
201
mr) shift; cmd_mr "$@" ;;
202
issue) shift; cmd_issue "$@" ;;
203
branch) shift; cmd_branch "$@" ;;
204
release) shift; cmd_release "$@" ;;
205
file) shift; cmd_file "$@" ;;
206
help|-h|--help) usage ;;
207
*) usage >&2; exit 64 ;;
208
esac
@@ -0,0 +1,208 @@1
#!/usr/bin/env bash2
# bg: kleine BlueGit-CLI voor mensen en agents. Alleen bash, curl en jq.3
#4
# BLUEGIT_URL standaard https://git.timfalken.com5
# BLUEGIT_TOKEN optioneel; nodig voor schrijven en voor privé projecten.6
# Wordt nooit geprint.7
#8
# Zie docs/agents.md of `bg help`.9
set -euo pipefail10
11
BASE="${BLUEGIT_URL:-https://git.timfalken.com}"12
BASE="${BASE%/}"13
API="$BASE/api/v1"14
15
die() { echo "bg: $*" >&2; exit 64; }16
command -v curl >/dev/null || die "curl ontbreekt"17
command -v jq >/dev/null || die "jq ontbreekt"18
19
# req METHOD PATH [JSON] -> body op stdout; bij HTTP >= 400 de fout op stderr en exit 1.20
req() {21
local method=$1 path=$2 data=${3:-} out code22
local args=(-sS -X "$method" -w '\n%{http_code}' -H "Accept: application/json")23
[[ -n "${BLUEGIT_TOKEN:-}" ]] && args+=(-H "Authorization: Bearer ${BLUEGIT_TOKEN}")24
[[ -n "$data" ]] && args+=(-H "Content-Type: application/json" --data-binary "$data")25
out=$(curl "${args[@]}" "$API$path") || { echo "bg: verbinding mislukt: $method $path" >&2; return 1; }26
code=${out##*$'\n'}27
out=${out%$'\n'*}28
if (( code >= 400 )); then29
local msg30
msg=$(printf '%s' "$out" | jq -r '.message // .error // empty' 2>/dev/null || true)31
echo "bg: HTTP $code bij $method $path${msg:+: $msg}" >&232
[[ $code == 404 ]] && echo "bg: (bestaat niet, of je hebt geen toegang)" >&233
return 134
fi35
printf '%s' "$out"36
}37
get() { req GET "$1"; }38
enc() { jq -rn --arg v "$1" '$v|@uri'; }39
proj() { [[ "$1" == */* ]] || die "project moet owner/repo zijn: $1"; echo "/projects/$1"; }40
issues() { echo "/repos/$1/issues"; }41
json() { jq -cn "$@"; }42
43
# ---------- pipelines ----------44
45
# select_pipeline PROJECT ARGS... -> pipeline-JSON (of leeg als er nog geen is)46
select_query() {47
local q=""48
while (($#)); do49
case $1 in50
--sha) q="sha=$2"; shift 2 ;;51
--mr) q="mr=$2"; shift 2 ;;52
--branch|--ref) q="ref=$(enc "$2")"; shift 2 ;;53
--id) q="id=$2"; shift 2 ;;54
--timeout|--interval) shift 2 ;;55
*) die "onbekende optie: $1" ;;56
esac57
done58
echo "$q"59
}60
find_pipeline() {61
local p=$1 q=$262
if [[ $q == id=* ]]; then get "$(proj "$p")/pipelines/${q#id=}"; return; fi63
get "$(proj "$p")/pipelines?per_page=1${q:+&$q}" | jq -c '.[0] // empty'64
}65
show_pipeline() {66
local p=$1 pipe=$267
jq -r '"pipeline #\(.id) \(.status) \(.ref|sub("^refs/heads/";"")) \(.sha[0:8]) \(.web_url)"' <<<"$pipe"68
get "$(proj "$p")/pipelines/$(jq -r .id <<<"$pipe")/jobs" |69
jq -r '.[] | " \(.id)\t\(.status)\t\(.stage)/\(.name)\(if .allow_failure then " (allow_failure)" else "" end)"'70
}71
failed_logs() {72
local p=$1 id=$2 jobs73
jobs=$(get "$(proj "$p")/pipelines/$id/jobs")74
jq -r '.[]|select(.status=="failed" and .allow_failure)|"bg: waarschuwing: \(.name) (#\(.id)) faalde (allow_failure)"' <<<"$jobs" >&275
for j in $(jq -r '.[]|select(.status=="failed" and (.allow_failure|not))|.id' <<<"$jobs"); do76
echo "----- log job $j (laatste ${BG_LOG_LINES:-80} regels) -----" >&277
get "$(proj "$p")/pipelines/$id/jobs/$j/log" | tail -n "${BG_LOG_LINES:-80}" >&2 || true78
done79
}80
cmd_pipeline() {81
local sub=${1:-}; shift || true82
case $sub in83
status)84
local p=${1:?project}; shift85
local q pipe; q=$(select_query "$@")86
pipe=$(find_pipeline "$p" "$q")87
[[ -n "$pipe" ]] || { echo "geen pipeline gevonden" >&2; exit 3; }88
show_pipeline "$p" "$pipe" ;;89
wait)90
local p=${1:?project}; shift91
local timeout=1800 interval=10 a=("$@")92
while (($#)); do case $1 in --timeout) timeout=$2; shift 2 ;; --interval) interval=$2; shift 2 ;; *) shift ;; esac; done93
local q pipe status start=$SECONDS last=""94
q=$(select_query "${a[@]}")95
while :; do96
pipe=$(find_pipeline "$p" "$q" || true)97
status=$(jq -r '.status // empty' <<<"${pipe:-{\}}")98
if [[ -n "$status" && "$status" != "$last" ]]; then99
echo "pipeline #$(jq -r .id <<<"$pipe"): $status" >&2; last=$status100
fi101
case $status in102
success) show_pipeline "$p" "$pipe"; failed_logs "$p" "$(jq -r .id <<<"$pipe")" 2>&1 | grep '^bg: waarschuwing' >&2 || true; exit 0 ;;103
failed|canceled) show_pipeline "$p" "$pipe"; failed_logs "$p" "$(jq -r .id <<<"$pipe")"; exit 1 ;;104
esac105
(( SECONDS - start >= timeout )) && { echo "bg: timeout na ${timeout}s (status ${status:-geen pipeline})" >&2; exit 2; }106
sleep "$interval"107
done ;;108
logs) get "$(proj "${1:?project}")/pipelines/${2:?pipeline-id}/jobs/${3:?job-id}/log" ;;109
retry)110
local mode=failed111
[[ ${3:-} == --all ]] && mode=all112
req POST "$(proj "${1:?project}")/pipelines/${2:?pipeline-id}/retry" "$(json --arg m "$mode" '{mode:$m}')" | jq -r '"pipeline #\(.id) \(.status) \(.web_url)"' ;;113
list) get "$(proj "${1:?project}")/pipelines?per_page=${2:-10}" | jq -r '.[]|"\(.id)\t\(.status)\t\(.ref|sub("^refs/heads/";""))\t\(.sha[0:8])"' ;;114
*) die "pipeline status|wait|logs|retry|list" ;;115
esac116
}117
118
# ---------- merge requests ----------119
cmd_mr() {120
local sub=${1:-}; shift || true121
case $sub in122
list)123
local p=${1:?project}; shift; local q="state=open"124
while (($#)); do case $1 in --state) q="state=$2"; shift 2 ;; --source) q+="&source_branch=$(enc "$2")"; shift 2 ;; --target) q+="&target_branch=$(enc "$2")"; shift 2 ;; *) die "onbekende optie $1" ;; esac; done125
get "$(proj "$p")/merge_requests?per_page=100&$q" | jq -r '.[]|"!\(.iid)\t\(.state)\t\(.source_branch) -> \(.target_branch)\t\(.title)"' ;;126
show) get "$(proj "${1:?project}")/merge_requests/${2:?nummer}" | jq . ;;127
create)128
local p=${1:?project}; shift; local src="" tgt="" title="" desc="" remove=true129
while (($#)); do case $1 in --source) src=$2; shift 2 ;; --target) tgt=$2; shift 2 ;; --title) title=$2; shift 2 ;; --description) desc=$2; shift 2 ;; --keep-source) remove=false; shift ;; *) die "onbekende optie $1" ;; esac; done130
[[ -n $src && -n $tgt && -n $title ]] || die "mr create PROJECT --source B --target B --title T [--description D] [--keep-source]"131
req POST "$(proj "$p")/merge_requests" "$(json --arg s "$src" --arg t "$tgt" --arg ti "$title" --arg d "$desc" --argjson r "$remove" '{source_branch:$s,target_branch:$t,title:$ti,description:$d,remove_source_branch:$r}')" | jq -r '"!\(.iid) \(.web_url)"' ;;132
merge)133
local remove=true; [[ ${3:-} == --keep-source ]] && remove=false134
req POST "$(proj "${1:?project}")/merge_requests/${2:?nummer}/merge" "$(json --argjson r "$remove" '{remove_source_branch:$r}')" | jq -r '"\(.state) \(.web_url)"' ;;135
comment) req POST "$(proj "${1:?project}")/merge_requests/${2:?nummer}/notes" "$(json --arg b "${3:?tekst}" '{body:$b}')" | jq -r '"notitie \(.id)"' ;;136
*) die "mr list|show|create|merge|comment" ;;137
esac138
}139
140
# ---------- issues ----------141
cmd_issue() {142
local sub=${1:-}; shift || true143
case $sub in144
list) get "$(issues "${1:?project}")?state=${2:-open}" | jq -r '.issues[]|"#\(.id)\t\(.status)\t\(.title)"' ;;145
show) get "$(issues "${1:?project}")/${2:?nummer}" | jq . ;;146
create) req POST "$(issues "${1:?project}")" "$(json --arg t "${2:?titel}" --arg d "${3:-}" '{title:$t,description:$d}')" | jq -r '"#\(.id) \(.web_url)"' ;;147
comment) req POST "$(issues "${1:?project}")/${2:?nummer}/notes" "$(json --arg b "${3:?tekst}" '{body:$b}')" >/dev/null && echo "ok" ;;148
close|reopen)149
local st=closed; [[ $sub == reopen ]] && st=open150
req PUT "$(issues "${1:?project}")/${2:?nummer}" "$(json --arg s "$st" '{state:$s}')" | jq -r '"#\(.id) \(.status)"' ;;151
*) die "issue list|show|create|comment|close|reopen" ;;152
esac153
}154
155
# ---------- overig ----------156
cmd_branch() {157
[[ ${1:-} == create ]] || die "branch create PROJECT NAAM [--from REF]"158
local p=${2:?project} name=${3:?naam} from=""159
[[ ${4:-} == --from ]] && from=${5:?ref}160
req POST "$(proj "$p")/repository/branches" "$(json --arg n "$name" --arg f "$from" '{name:$n} + (if $f=="" then {} else {from:$f} end)')" | jq -r '.name // .'161
}162
cmd_release() {163
[[ ${1:-} == create ]] || die "release create PROJECT TAG [--commit SHA] [--name N] [--body B] [--asset JOB:PAD[:NAAM]]..."164
local p=${2:?project} tag=${3:?tag}; shift 3165
local commit="" name="" body="" assets="[]"166
while (($#)); do case $1 in167
--commit) commit=$2; shift 2 ;; --name) name=$2; shift 2 ;; --body) body=$2; shift 2 ;;168
--asset) IFS=: read -r j pth nm <<<"$2"; assets=$(jq -c --arg j "$j" --arg p "$pth" --arg n "${nm:-}" '. + [{job:$j,path:$p} + (if $n=="" then {} else {name:$n} end)]' <<<"$assets"); shift 2 ;;169
*) die "onbekende optie $1" ;; esac; done170
req POST "$(proj "$p")/releases" "$(json --arg t "$tag" --arg c "$commit" --arg n "$name" --arg b "$body" --argjson a "$assets" '{tag:$t,commit:$c,name:$n,body:$b,assets:$a}')" | jq -c .171
}172
cmd_file() {173
[[ ${1:-} == get ]] || die "file get PROJECT PAD [--ref REF]"174
local p=${2:?project} path=${3:?pad} ref=""175
[[ ${4:-} == --ref ]] && ref="?ref=$(enc "${5:?ref}")"176
local body; body=$(get "$(proj "$p")/repository/files/$(enc "$path")$ref")177
if jq -e '.content and .encoding=="base64"' <<<"$body" >/dev/null 2>&1; then jq -r .content <<<"$body" | base64 -d178
elif jq -e '.content' <<<"$body" >/dev/null 2>&1; then jq -r .content <<<"$body"179
else printf '%s' "$body"; fi180
}181
182
usage() {183
sed -n '2,8p' "$0" | sed 's/^# \{0,1\}//'184
cat <<'U'185
186
bg pipeline status PROJECT [--sha X | --mr N | --branch B | --id P]187
bg pipeline wait PROJECT [selectie] [--timeout 1800] [--interval 10] exit 0 ok, 1 mislukt, 2 timeout188
bg pipeline logs PROJECT PIPELINE JOB189
bg pipeline retry PROJECT PIPELINE [--all]190
bg pipeline list PROJECT [N]191
bg mr list|show|create|merge|comment ...192
bg issue list|show|create|comment|close|reopen ...193
bg branch create PROJECT NAAM [--from REF]194
bg release create PROJECT TAG [--commit SHA] [--asset JOB:PAD[:NAAM]]195
bg file get PROJECT PAD [--ref REF]196
U197
}198
199
case ${1:-help} in200
pipeline|pl) shift; cmd_pipeline "$@" ;;201
mr) shift; cmd_mr "$@" ;;202
issue) shift; cmd_issue "$@" ;;203
branch) shift; cmd_branch "$@" ;;204
release) shift; cmd_release "$@" ;;205
file) shift; cmd_file "$@" ;;206
help|-h|--help) usage ;;207
*) usage >&2; exit 64 ;;208
esac@@ -0,0 +1,97 @@
1
package scripts
2
3
import (
4
"bytes"
5
"errors"
6
"fmt"
7
"net/http"
8
"net/http/httptest"
9
"os"
10
"os/exec"
11
"path/filepath"
12
"strings"
13
"sync/atomic"
14
"testing"
15
)
16
17
// fakeBlueGit geeft eerst geen pipeline, dan running, dan final.
18
func fakeBlueGit(t *testing.T, final string, token string) (*httptest.Server, *atomic.Int32) {
19
var calls atomic.Int32
20
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21
if token != "" && r.Header.Get("Authorization") != "Bearer "+token {
22
http.Error(w, `{"message":"no"}`, 401)
23
return
24
}
25
switch {
26
case r.URL.Path == "/api/v1/projects/tim/app/pipelines":
27
if r.URL.Query().Get("sha") != "abcdef1" {
28
t.Errorf("sha-filter ontbreekt: %s", r.URL.RawQuery)
29
}
30
n := calls.Add(1)
31
switch {
32
case n == 1:
33
fmt.Fprint(w, `[]`)
34
case n == 2:
35
fmt.Fprint(w, `[{"id":9,"status":"running","ref":"refs/heads/x","sha":"abcdef1000","web_url":"u"}]`)
36
default:
37
fmt.Fprintf(w, `[{"id":9,"status":%q,"ref":"refs/heads/x","sha":"abcdef1000","web_url":"u"}]`, final)
38
}
39
case r.URL.Path == "/api/v1/projects/tim/app/pipelines/9/jobs":
40
fmt.Fprint(w, `[{"id":1,"name":"test","stage":"test","status":"failed","allow_failure":false},{"id":2,"name":"aur","stage":"release","status":"failed","allow_failure":true}]`)
41
case r.URL.Path == "/api/v1/projects/tim/app/pipelines/9/jobs/1/log":
42
fmt.Fprint(w, "regel 1\nFOUT hier\n")
43
default:
44
http.NotFound(w, r)
45
}
46
}))
47
t.Cleanup(srv.Close)
48
return srv, &calls
49
}
50
51
func runBG(t *testing.T, url, token string, args ...string) (int, string, string) {
52
t.Helper()
53
for _, bin := range []string{"bash", "curl", "jq"} {
54
if _, err := exec.LookPath(bin); err != nil {
55
t.Skip(bin + " ontbreekt")
56
}
57
}
58
cmd := exec.Command(filepath.Join("..", "scripts", "bg"), args...)
59
cmd.Env = append(os.Environ(), "BLUEGIT_URL="+url, "BLUEGIT_TOKEN="+token)
60
var out, errb bytes.Buffer
61
cmd.Stdout, cmd.Stderr = &out, &errb
62
err := cmd.Run()
63
code := 0
64
var ee *exec.ExitError
65
if errors.As(err, &ee) {
66
code = ee.ExitCode()
67
} else if err != nil {
68
t.Fatal(err)
69
}
70
return code, out.String(), errb.String()
71
}
72
73
func TestBGWaitExitCodes(t *testing.T) {
74
const tok = "bg_geheimtoken123"
75
srv, _ := fakeBlueGit(t, "success", tok)
76
code, out, errs := runBG(t, srv.URL, tok, "pipeline", "wait", "tim/app", "--sha", "abcdef1", "--interval", "0")
77
if code != 0 || !strings.Contains(out, "pipeline #9 success") {
78
t.Fatalf("success: %d %s %s", code, out, errs)
79
}
80
if strings.Contains(out+errs, tok) {
81
t.Fatal("token geprint")
82
}
83
srv, _ = fakeBlueGit(t, "failed", "")
84
code, out, errs = runBG(t, srv.URL, "", "pipeline", "wait", "tim/app", "--sha", "abcdef1", "--interval", "0")
85
if code != 1 || !strings.Contains(errs, "FOUT hier") || !strings.Contains(errs, "waarschuwing: aur") {
86
t.Fatalf("failed: %d %s %s", code, out, errs)
87
}
88
srv, _ = fakeBlueGit(t, "running", "")
89
code, _, errs = runBG(t, srv.URL, "", "pipeline", "wait", "tim/app", "--sha", "abcdef1", "--interval", "1", "--timeout", "2")
90
if code != 2 || !strings.Contains(errs, "timeout") {
91
t.Fatalf("timeout: %d %s", code, errs)
92
}
93
code, _, errs = runBG(t, srv.URL, "", "mr", "show", "tim/app", "5")
94
if code != 1 || !strings.Contains(errs, "404") {
95
t.Fatalf("404: %d %s", code, errs)
96
}
97
}
@@ -0,0 +1,97 @@1
package scripts2
3
import (4
"bytes"5
"errors"6
"fmt"7
"net/http"8
"net/http/httptest"9
"os"10
"os/exec"11
"path/filepath"12
"strings"13
"sync/atomic"14
"testing"15
)16
17
// fakeBlueGit geeft eerst geen pipeline, dan running, dan final.18
func fakeBlueGit(t *testing.T, final string, token string) (*httptest.Server, *atomic.Int32) {19
var calls atomic.Int3220
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {21
if token != "" && r.Header.Get("Authorization") != "Bearer "+token {22
http.Error(w, `{"message":"no"}`, 401)23
return24
}25
switch {26
case r.URL.Path == "/api/v1/projects/tim/app/pipelines":27
if r.URL.Query().Get("sha") != "abcdef1" {28
t.Errorf("sha-filter ontbreekt: %s", r.URL.RawQuery)29
}30
n := calls.Add(1)31
switch {32
case n == 1:33
fmt.Fprint(w, `[]`)34
case n == 2:35
fmt.Fprint(w, `[{"id":9,"status":"running","ref":"refs/heads/x","sha":"abcdef1000","web_url":"u"}]`)36
default:37
fmt.Fprintf(w, `[{"id":9,"status":%q,"ref":"refs/heads/x","sha":"abcdef1000","web_url":"u"}]`, final)38
}39
case r.URL.Path == "/api/v1/projects/tim/app/pipelines/9/jobs":40
fmt.Fprint(w, `[{"id":1,"name":"test","stage":"test","status":"failed","allow_failure":false},{"id":2,"name":"aur","stage":"release","status":"failed","allow_failure":true}]`)41
case r.URL.Path == "/api/v1/projects/tim/app/pipelines/9/jobs/1/log":42
fmt.Fprint(w, "regel 1\nFOUT hier\n")43
default:44
http.NotFound(w, r)45
}46
}))47
t.Cleanup(srv.Close)48
return srv, &calls49
}50
51
func runBG(t *testing.T, url, token string, args ...string) (int, string, string) {52
t.Helper()53
for _, bin := range []string{"bash", "curl", "jq"} {54
if _, err := exec.LookPath(bin); err != nil {55
t.Skip(bin + " ontbreekt")56
}57
}58
cmd := exec.Command(filepath.Join("..", "scripts", "bg"), args...)59
cmd.Env = append(os.Environ(), "BLUEGIT_URL="+url, "BLUEGIT_TOKEN="+token)60
var out, errb bytes.Buffer61
cmd.Stdout, cmd.Stderr = &out, &errb62
err := cmd.Run()63
code := 064
var ee *exec.ExitError65
if errors.As(err, &ee) {66
code = ee.ExitCode()67
} else if err != nil {68
t.Fatal(err)69
}70
return code, out.String(), errb.String()71
}72
73
func TestBGWaitExitCodes(t *testing.T) {74
const tok = "bg_geheimtoken123"75
srv, _ := fakeBlueGit(t, "success", tok)76
code, out, errs := runBG(t, srv.URL, tok, "pipeline", "wait", "tim/app", "--sha", "abcdef1", "--interval", "0")77
if code != 0 || !strings.Contains(out, "pipeline #9 success") {78
t.Fatalf("success: %d %s %s", code, out, errs)79
}80
if strings.Contains(out+errs, tok) {81
t.Fatal("token geprint")82
}83
srv, _ = fakeBlueGit(t, "failed", "")84
code, out, errs = runBG(t, srv.URL, "", "pipeline", "wait", "tim/app", "--sha", "abcdef1", "--interval", "0")85
if code != 1 || !strings.Contains(errs, "FOUT hier") || !strings.Contains(errs, "waarschuwing: aur") {86
t.Fatalf("failed: %d %s %s", code, out, errs)87
}88
srv, _ = fakeBlueGit(t, "running", "")89
code, _, errs = runBG(t, srv.URL, "", "pipeline", "wait", "tim/app", "--sha", "abcdef1", "--interval", "1", "--timeout", "2")90
if code != 2 || !strings.Contains(errs, "timeout") {91
t.Fatalf("timeout: %d %s", code, errs)92
}93
code, _, errs = runBG(t, srv.URL, "", "mr", "show", "tim/app", "5")94
if code != 1 || !strings.Contains(errs, "404") {95
t.Fatalf("404: %d %s", code, errs)96
}97
}