| 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 |