-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforgejo
More file actions
executable file
·6325 lines (5508 loc) · 200 KB
/
Copy pathforgejo
File metadata and controls
executable file
·6325 lines (5508 loc) · 200 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -euo pipefail
CONFIG_FILE="$HOME/.config/forgejo-cli/config"
JSON_OUTPUT=0
FORGEJO_URL=""
FORGEJO_TOKEN=""
FORGEJO_LOG_PATH=""
FORGEJO_PASSWORD=""
FORGEJO_REVIEW_TOKEN="" # optional: submit `pr review` as a separate identity
check_deps() {
local missing=()
command -v jq &>/dev/null || missing+=("jq")
command -v curl &>/dev/null || missing+=("curl")
if [[ ${#missing[@]} -gt 0 ]]; then
echo "Missing dependencies: ${missing[*]}" >&2
echo "Install with: sudo apt install ${missing[*]}" >&2
exit 1
fi
}
load_config() {
if [[ ! -f "$CONFIG_FILE" ]]; then
echo "Config file not found: $CONFIG_FILE" >&2
echo "" >&2
echo "Create it with:" >&2
echo " mkdir -p ~/.config/forgejo-cli" >&2
echo " cat > ~/.config/forgejo-cli/config << 'EOF'" >&2
echo " FORGEJO_URL=https://forgejo.example.com" >&2
echo " FORGEJO_TOKEN=your-token-here" >&2
echo " EOF" >&2
echo " chmod 600 ~/.config/forgejo-cli/config" >&2
exit 1
fi
local perms
perms=$(stat -c '%a' "$CONFIG_FILE")
if [[ "$perms" != "600" ]]; then
echo "Config file has insecure permissions: $perms (expected 600)" >&2
echo "Fix with: chmod 600 $CONFIG_FILE" >&2
exit 1
fi
# shellcheck source=/dev/null
source "$CONFIG_FILE"
if [[ -z "${FORGEJO_URL:-}" || -z "${FORGEJO_TOKEN:-}" ]]; then
echo "Config file must set FORGEJO_URL and FORGEJO_TOKEN" >&2
exit 1
fi
}
api_call() {
local method="$1"
local endpoint="$2"
local body="${3:-}"
local url="${FORGEJO_URL}/api/v1/${endpoint}"
local tmp
tmp=$(mktemp)
local curl_args=(
-s
-X "$method"
-H "Authorization: token $FORGEJO_TOKEN"
-H "Accept: application/json"
-w '%{http_code}'
-o "$tmp"
)
if [[ -n "$body" ]]; then
curl_args+=(-H "Content-Type: application/json" -d "$body")
fi
local status rc=0
status=$(curl "${curl_args[@]}" "$url") || rc=$?
if [[ "$rc" -ne 0 ]]; then
rm -f "$tmp"
echo "forgejo: network error (curl exit $rc)" >&2
exit 1
fi
if [[ "$status" -lt 200 || "$status" -ge 300 ]]; then
echo "API error ($status):" >&2
jq -r '.message // .error // .' "$tmp" >&2 2>/dev/null || cat "$tmp" >&2
rm -f "$tmp"
exit 1
fi
cat "$tmp"
rm -f "$tmp"
}
# Like api_call but does NOT exit on non-2xx. Returns the HTTP status on the
# first line of stdout and the response body on subsequent lines. Caller is
# responsible for splitting and acting. Used by branch_protect's idempotent
# existence check; not used by cmd_api (which calls curl directly).
#
# Usage:
# out=$(api_call_status GET "repos/foo/bar/branch_protections/main")
# status=$(printf '%s' "$out" | head -1)
# body=$(printf '%s' "$out" | tail -n +2)
api_call_status() {
local method="$1"
local endpoint="$2"
local body="${3:-}"
local url="${FORGEJO_URL}/api/v1/${endpoint}"
local tmp
tmp=$(mktemp)
local curl_args=(
-s
-X "$method"
-H "Authorization: token $FORGEJO_TOKEN"
-H "Accept: application/json"
-w '%{http_code}'
-o "$tmp"
)
if [[ -n "$body" ]]; then
curl_args+=(-H "Content-Type: application/json" -d "$body")
fi
local status rc=0
status=$(curl "${curl_args[@]}" "$url") || rc=$?
if [[ "$rc" -ne 0 ]]; then
rm -f "$tmp"
echo "forgejo: network error (curl exit $rc)" >&2
exit 1
fi
printf '%s\n' "$status"
cat "$tmp"
rm -f "$tmp"
}
# Like api_call but with Accept: */* and no jq parsing. Streams response body
# to stdout verbatim. On non-2xx, prints raw body to stderr and exits 1.
# Used by pr_diff to surface raw .diff/.patch output.
api_call_raw() {
local method="$1"
local endpoint="$2"
local url="${FORGEJO_URL}/api/v1/${endpoint}"
local tmp
tmp=$(mktemp)
local status rc=0
status=$(curl -s -X "$method" \
-H "Authorization: token $FORGEJO_TOKEN" \
-H "Accept: */*" \
-w '%{http_code}' \
-o "$tmp" \
"$url") || rc=$?
if [[ "$rc" -ne 0 ]]; then
rm -f "$tmp"
echo "forgejo: network error (curl exit $rc)" >&2
exit 1
fi
if [[ "$status" -lt 200 || "$status" -ge 300 ]]; then
echo "API error ($status):" >&2
cat "$tmp" >&2
rm -f "$tmp"
exit 1
fi
cat "$tmp"
rm -f "$tmp"
}
# Paginated GET for list endpoints. A single api_call may return only the
# first page. This fetches pages (limit=50, page=1,2,…), merges them, and
# prints one JSON array. Termination is by NEW items contributed, deduped by
# .id: this is robust both when the server paginates correctly (a trailing
# short/empty page adds nothing) AND when the server ignores ?page= and
# re-returns the same set (page 2 adds nothing new → stop). List items on the
# endpoints this serves (reviews, comments, review comments) all carry a
# unique .id. Caps at 200 pages so a misbehaving server can't spin forever.
api_call_paged() {
local method="$1" endpoint="$2"
local sep='?'
[[ "$endpoint" == *\?* ]] && sep='&'
local page=1 acc='[]' pagejson merged prevlen newlen pagelen
while :; do
pagejson=$(api_call "$method" "${endpoint}${sep}limit=50&page=${page}")
pagelen=$(echo "$pagejson" | jq 'length')
prevlen=$(echo "$acc" | jq 'length')
merged=$(jq -n --argjson a "$acc" --argjson b "$pagejson" \
'($a + $b) | unique_by(.id)')
newlen=$(echo "$merged" | jq 'length')
acc="$merged"
# No new items this page (empty tail page, or server ignored ?page=).
[[ "$newlen" -eq "$prevlen" ]] && break
# Normal last page: fewer than a full page returned.
[[ "$pagelen" -lt 50 ]] && break
page=$((page+1))
if [[ "$page" -gt 200 ]]; then
echo "forgejo: warning: pagination cap (200 pages) hit at $endpoint" >&2
break
fi
done
echo "$acc"
}
# Like api_call but authenticates with HTTP Basic (username + FORGEJO_PASSWORD)
# instead of bearer token. Required for /users/{u}/tokens endpoints, which
# Forgejo does not allow over bearer auth. Exits with a setup hint if
# FORGEJO_PASSWORD is unset.
api_call_basic() {
local method="$1"
local username="$2"
local endpoint="$3"
local body="${4:-}"
if [[ -z "$FORGEJO_PASSWORD" ]]; then
echo "FORGEJO_PASSWORD is not set in $CONFIG_FILE." >&2
echo "Forgejo's token endpoints require basic auth, not bearer." >&2
echo "Add to your config: FORGEJO_PASSWORD=your-account-password" >&2
exit 1
fi
local url="${FORGEJO_URL}/api/v1/${endpoint}"
local tmp
tmp=$(mktemp)
local curl_args=(
-s
-X "$method"
-u "${username}:${FORGEJO_PASSWORD}"
-H "Accept: application/json"
-w '%{http_code}'
-o "$tmp"
)
if [[ -n "$body" ]]; then
curl_args+=(-H "Content-Type: application/json" -d "$body")
fi
local status rc=0
status=$(curl "${curl_args[@]}" "$url") || rc=$?
if [[ "$rc" -ne 0 ]]; then
rm -f "$tmp"
echo "forgejo: network error (curl exit $rc)" >&2
exit 1
fi
if [[ "$status" -lt 200 || "$status" -ge 300 ]]; then
echo "API error ($status):" >&2
jq -r '.message // .error // .' "$tmp" >&2 2>/dev/null || cat "$tmp" >&2
rm -f "$tmp"
exit 1
fi
cat "$tmp"
rm -f "$tmp"
}
confirm_delete() {
local resource_type="$1"
local resource_name="$2"
shift 2
if has_flag "--yes" "$@"; then
return 0
fi
echo "Are you sure you want to delete $resource_type \"$resource_name\"?"
read -rp "Type the name to confirm: " confirm
if [[ "$confirm" != "$resource_name" ]]; then
echo "Aborted." >&2
exit 1
fi
}
get_flag() {
local flag="$1"
shift
while [[ $# -gt 0 ]]; do
case "$1" in
"$flag"=*) echo "${1#*=}"; return 0 ;;
"$flag") echo "${2:-}"; return 0 ;;
esac
shift
done
return 1
}
has_flag() {
local flag="$1"
shift
while [[ $# -gt 0 ]]; do
[[ "$1" == "$flag" ]] && return 0
shift
done
return 1
}
# Resolves the SSH key string for `repo key add` from either --key= or
# --key-file=. Errors if neither or both supplied. Prints the key to stdout.
read_key_input() {
local key
local key_file
key=$(get_flag "--key" "$@" 2>/dev/null) || key=""
key_file=$(get_flag "--key-file" "$@" 2>/dev/null) || key_file=""
if [[ -n "$key" && -n "$key_file" ]]; then
echo "Provide --key OR --key-file, not both" >&2; exit 1
fi
if [[ -z "$key" && -z "$key_file" ]]; then
echo "Missing --key or --key-file" >&2; exit 1
fi
if [[ -n "$key_file" ]]; then
[[ -r "$key_file" ]] || { echo "Cannot read key file: $key_file" >&2; exit 1; }
cat "$key_file"
else
printf '%s\n' "$key"
fi
}
show_usage() {
cat << 'USAGE'
Usage: forgejo <resource> <action> [args] [--json]
=== QUICK START ===
# Issues — create, comment, label, close
forgejo issue create OWNER/REPO --title="Bug: login broken" --body="Steps to reproduce..."
forgejo issue comment OWNER/REPO 42 --body="I can reproduce this."
forgejo issue view OWNER/REPO 42 # shows issue + all comments
forgejo issue comment delete OWNER/REPO 123 # delete comment #123
forgejo issue close OWNER/REPO 42
# PRs — create, comment, review, merge
forgejo pr create OWNER/REPO --title="Fix login" --head=feature/login --base=main
forgejo pr comment OWNER/REPO 17 --body="LGTM, ship it!"
forgejo pr comment list OWNER/REPO 17 # list all comments on PR
forgejo pr comment view OWNER/REPO 456 # view a single comment
forgejo pr comment delete OWNER/REPO 456 # delete a comment
forgejo pr review OWNER/REPO 17 --approve
forgejo pr merge OWNER/REPO 17 --method=squash
# Repos & branches
forgejo repo list OWNER
forgejo repo view OWNER/REPO
forgejo branch protect OWNER/REPO main --no-push --merge-whitelist=you
Run `forgejo <resource> --help` for detailed help on a specific resource.
Run `forgejo <resource> <subcommand> --help` for subcommand-specific help (e.g. comments).
Global flags:
--json Output raw JSON instead of formatted columns
--help Show help (works at any level: resource, subcommand)
=== FULL COMMAND LIST ===
Resources:
api Generic API passthrough (escape hatch)
branch Manage branches and branch protection
repo Manage repositories
issue Manage issues
actions CI/Actions workflow runs
pr Pull requests
release Releases and tags
wiki Wiki pages
user Manage users (admin)
org Manage organizations
admin Server administration (cron, config)
auth Log in and check authentication status
notification Notification inbox
search Search across the instance
API commands:
api [METHOD] <path> [-f key=val]... [-F key=val]... [--input -]
Generic API passthrough.
METHOD defaults to GET.
-f: string field; -F: typed
(bool/int/@file). Paths
starting with / auto-prefix
with /api/v1. Body to
stdout; exit 22 on HTTP error.
Repo commands:
repo list [org] List repos
repo create <name> [--org=X] [--private] [--desc=X] Create repo
repo view <owner/repo> View repo details
repo edit <owner/repo> [--name=X] [--desc=X] [--private|--public] Edit/rename repo
repo delete <owner/repo> Delete repo
repo fork <owner/repo> [--org=X] Fork repo (idempotent on 409)
repo transfer <owner/repo> --new-owner=X Transfer repo ownership (confirms)
repo archive <owner/repo> Archive repo (idempotent)
repo unarchive <owner/repo> Unarchive repo (idempotent)
repo tags list <owner/repo> List tags
repo tags create <owner/repo> --tag=X [--message=X] [--target=<sha|branch>] Create tag
repo tags delete <owner/repo> <tag> Delete tag
repo collaborator list <owner/repo> List collaborators
repo collaborator add <owner/repo> --user=X [--permission=read|write|admin] Add collaborator (default write)
repo collaborator remove <owner/repo> --user=X Remove collaborator
repo webhook list <owner/repo> List webhooks
repo webhook view <owner/repo> <id> View webhook detail
repo webhook create <owner/repo> --url=X --events=a,b [--secret=X] [--content-type=json|form] [--inactive] Create forgejo-type webhook
repo webhook edit <owner/repo> <id> [--url=X] [--events=a,b] [--secret=X] [--content-type=X] [--active|--inactive] Edit webhook (partial)
repo webhook delete <owner/repo> <id> Delete webhook
repo key list <owner/repo> List deploy keys
repo key add <owner/repo> --title=X (--key=X | --key-file=path) [--read-only] Add deploy key
repo key delete <owner/repo> <id> Delete deploy key
repo topic list <owner/repo> List topics
repo topic add <owner/repo> --topics=a,b Add topics (idempotent per topic)
repo topic remove <owner/repo> --topics=a,b Remove topics
repo mirror sync <owner/repo> Trigger push-mirror sync
Issue commands:
issue list <owner/repo> [--state=open|closed] List issues
issue create <owner/repo> --title=X [--body=X] Create issue
issue view <owner/repo> <number> View issue + comments
issue edit <owner/repo> <number> [--title=X] [--state=X] [--body=X] [--labels=X,Y] Edit issue
issue comment <owner/repo> <number> --body=X Add comment
issue comment delete <owner/repo> <comment_id> Delete comment
issue label <owner/repo> list [--scope=org|repo] List labels (default: org)
issue label <owner/repo> create --name=X [--color=X] [--desc=X] [--scope=org|repo]
issue label <owner/repo> add <number> --labels=X,Y Add labels to issue
issue label <owner/repo> remove <number> --label=X Remove label from issue
issue close <owner/repo> <number> Close issue
issue reopen <owner/repo> <number> Reopen issue
issue assign <owner/repo> <number> --users=u1,u2 Add assignees (union)
issue unassign <owner/repo> <number> --users=u1,u2 Remove assignees
issue search [--owner=X] [--state=open|closed|all] [--labels=a,b] [--query=text] [--limit=N] Cross-repo search
issue milestone <list|create|edit|delete|set> [args] Milestone CRUD + set on issue
Actions commands:
actions list <owner/repo> List recent workflow runs
actions view <owner/repo> <run_id> View run details + jobs
actions watch <owner/repo> <run_id> Poll until run completes
actions logs <owner/repo> <run_id> View run logs (browser link;
reads from disk if
FORGEJO_LOG_PATH is set)
actions runner list <owner/repo|org|--admin> List Actions runners (scope)
actions runner register <owner/repo|org|--admin> Print runner registration token
actions runner delete <owner/repo|org|--admin> <id> [--yes] Remove runner
actions workflow list <owner/repo> List workflow files in repo
actions workflow dispatch <owner/repo> --workflow=X --ref=Y [--input=k=v]... Trigger workflow
actions secret list <owner/repo|org> List secrets (names only)
actions secret set <owner/repo|org> --name=X --value=X Create or update secret
actions secret delete <owner/repo|org> <name> [--yes] Delete secret
actions variable list <owner/repo|org> List variables
actions variable set <owner/repo|org> --name=X --value=X Create or update variable (idempotent)
actions variable delete <owner/repo|org> <name> [--yes] Delete variable
PR commands:
pr list <owner/repo> [--state=open|closed|all] List pull requests
pr create <owner/repo> --title=X --head=X [--base=main] [--body=X] Create PR
pr view <owner/repo> <number> View PR details
pr merge <owner/repo> <number> [--method=merge|rebase|squash] Merge PR
pr close <owner/repo> <number> Close PR (no merge)
pr edit <owner/repo> <number> [--title=X] [--body=X] [--base=X] Edit PR
pr ready <owner/repo> <number> Mark draft PR ready
pr comment <owner/repo> <number> --body=X Comment on PR
pr comment list <owner/repo> <number> List PR comments
pr comment view <owner/repo> <comment_id> View a single comment
pr comment delete <owner/repo> <comment_id> Delete a comment
pr diff <owner/repo> <number> Raw unified diff
pr files <owner/repo> <number> Changed file list
pr checks <owner/repo> <number> Commit statuses + Actions runs
pr review <owner/repo> <number> --approve|--request-changes|--comment [--body=X] Submit review
pr review list <owner/repo> <number> List reviews
pr review lookup <owner/repo> <number> Unified reviews + comments (JSON)
pr review dismiss <owner/repo> <number> <review_id> --message=X Dismiss a review
pr requested-reviewers add <owner/repo> <number> --users=u1,u2 Request reviewers
pr requested-reviewers remove <owner/repo> <number> --users=u1,u2 Withdraw reviewer requests
Release commands:
release list <owner/repo> List releases
release create <owner/repo> --tag=X --title=X [--body=X] [--draft] [--prerelease] Create release
release view <owner/repo> <tag> View release detail
release edit <owner/repo> <tag> [--title=X] [--body=X] [--draft|--no-draft] [--prerelease|--no-prerelease] Edit release
release delete <owner/repo> <tag> [--yes] Delete release (confirms by tag)
release upload-asset <owner/repo> <tag> <file>... Upload one or more asset files (fail-fast)
release asset list <owner/repo> <tag> List release assets
release asset delete <owner/repo> <tag> <asset_id> [--yes] Delete asset (confirms by id)
Wiki commands:
wiki list <owner/repo> List wiki pages
wiki view <owner/repo> <page> View a page (decoded content)
wiki create <owner/repo> --title=X [--content=X|--file=path|--file=-] [--message=X] Create a page
wiki edit <owner/repo> <page> [--title=X] [--content=X|--file=path|--file=-] [--message=X] Edit/rename a page
wiki delete <owner/repo> <page> [--yes] Delete a page (confirms by title)
wiki revisions <owner/repo> <page> Show a page's commit history
User commands (admin):
user list List users
user create --login=X --email=X --password=X [--admin] [--fullname=X] Create user
user view <username> View user
user edit <username> [--email=X] [--admin=true|false] [--active=true|false] Edit user
user delete <username> Delete user
user key list <user|--self>
user key add <user|--self> --title=T --key=<contents-or-@file>
user key delete <user|--self> <id> [--yes]
user gpg list <user|--self>
user gpg add --self --armored=<contents-or-@file>
user gpg delete --self <id> [--yes]
user token list <user>
user token create <user> --name=N [--scopes=S1,S2,...]
user token delete <user> <name> [--yes]
Org commands:
org list List orgs
org create <name> [--desc=X] [--visibility=X] Create org
org view <name> View org + members
org edit <name> [--desc=X] [--visibility=X] Edit org
org delete <name> Delete org
org member list <org>
org member remove <org> --user=<user>
org team list <org>
org team view <org> <team-name-or-id>
org team create <org> --name=N [--description=D] [--permission=read|write|admin]
org team edit <org> <team> [--name=N] [--description=D] [--permission=P]
org team delete <org> <team> [--yes]
org team member list|add|remove <org> <team> [--user=U]
org team repo list|add|remove <org> <team> [--repo=owner/repo]
Branch commands:
branch list <owner/repo> List branches
branch view <owner/repo> <branch> View branch + protection
branch delete <owner/repo> <branch> Delete branch
branch protect <owner/repo> <branch> [flags] Apply/update protection (idempotent)
Flags: --no-push, --push-whitelist=u1,u2,
--merge-whitelist=u1,u2, --required-approvals=N,
--dismiss-stale-approvals, --require-signed,
--block-on-outdated
branch unprotect <owner/repo> <branch> Remove protection
Admin commands:
admin cron list List cron tasks (name/schedule/next/prev/exec_times)
admin cron run <name> Trigger a cron task
admin config view Show server settings (api/attachment/repository/ui)
admin queue list|view|pause|resume Not exposed by Forgejo API - use web UI
admin stats Not exposed by Forgejo API - use web UI
admin notice list|delete Not exposed by Forgejo API - use web UI
Auth commands:
auth login Interactive setup: prompt, mint a token, write config
auth status Print URL/login/email + scopes-not-exposed note
Notification commands:
notification list [--all] [--status=unread|read|pinned] [--limit=N] List inbox
notification read <id> Mark a thread as read
notification read --all Mark all unread as read
Search commands:
search repos --query=X [--owner=X] [--limit=N] Search repos
search users --query=X [--limit=N] Search users
search issues --query=X [--type=issue|pr] [--state=open|closed|all] [--owner=X] [--limit=N] Search issues+PRs
USAGE
}
# Show the most specific help available for the given args, then return.
# Called from MAIN whenever --help/-h appears anywhere in the argument list,
# so it must never run a command — only print help. Falls back to the
# top-level usage for resources that have no dedicated help function.
route_help() {
case "${1:-}" in
issue)
case "${2:-}" in
comment) show_issue_comment_help ;;
*) show_issue_help ;;
esac
;;
pr)
case "${2:-}" in
comment) show_pr_comment_help ;;
*) show_pr_help ;;
esac
;;
wiki) show_wiki_help ;;
*) show_usage ;;
esac
}
# --- RESOURCE-SPECIFIC HELP FUNCTIONS ---
show_issue_help() {
cat << 'ISSUE_HELP'
Issue commands:
issue list <owner/repo> [--state=open|closed]
List issues in a repository.
issue create <owner/repo> --title=TEXT [--body=TEXT]
Create a new issue.
issue view <owner/repo> <number>
Show issue details and all comments inline.
issue edit <owner/repo> <number> [--title=TEXT] [--state=open|closed] [--body=TEXT] [--labels=X,Y]
Edit an issue's title, state, body, or labels.
issue comment <owner/repo> <number> --body=TEXT
Add a comment to an issue.
Comment IDs are shown in `issue view` and in the API response (--json).
issue comment delete <owner/repo> <comment_id>
Delete a specific comment by its numeric ID.
There is NO separate `issue comment list` or `issue comment view` —
use `forgejo issue view <owner/repo> <number>` which shows all comments inline.
issue close <owner/repo> <number>
Close an issue.
issue reopen <owner/repo> <number>
Reopen a closed issue.
issue assign <owner/repo> <number> --users=u1,u2
Add assignees to an issue (union with existing).
issue unassign <owner/repo> <number> --users=u1,u2
Remove assignees from an issue.
issue search [--owner=ORG] [--state=open|closed|all] [--labels=a,b] [--query=TEXT] [--limit=N]
Search issues across repositories.
issue milestone list <owner/repo> [--state=open|closed|all]
issue milestone create <owner/repo> --title=TEXT [--description=TEXT] [--due=YYYY-MM-DD]
issue milestone edit <owner/repo> <id> [--title=TEXT] [--description=TEXT] [--due=YYYY-MM-DD] [--state=open|closed]
issue milestone delete <owner/repo> <id>
issue milestone set <owner/repo> <number> --milestone=<id|title>
Manage milestones. Pass --milestone=0 to clear.
issue label <owner/repo> list [--scope=org|repo]
List labels (scope defaults to org).
issue label <owner/repo> create --name=TEXT [--color=HEX] [--desc=TEXT] [--scope=org|repo]
Create a label.
issue label <owner/repo> add <number> --labels=X,Y
Add labels to an issue.
issue label <owner/repo> remove <number> --label=TEXT
Remove a single label from an issue.
issue images <owner/repo> <number> [--output=DIR]
Download all image attachments (body + comments) to DIR.
DIR defaults to ./issue-<number>-images/. Non-image attachments
(extension not in .png .jpg .jpeg .gif .webp .svg .bmp) are skipped.
EXAMPLES:
forgejo issue create myorg/myrepo --title="Fix auth" --body="The OAuth flow is broken."
forgejo issue comment myorg/myrepo 42 --body="I can reproduce this on staging."
forgejo issue view myorg/myrepo 42 # shows issue + all comments
forgejo issue comment delete myorg/myrepo 123
forgejo issue label myorg/myrepo add 42 --labels=bug,high-priority
forgejo issue assign myorg/myrepo 42 --users=alice,bob
forgejo issue close myorg/myrepo 42
Run `forgejo issue comment --help` for comment-specific help.
ISSUE_HELP
}
show_issue_comment_help() {
cat << 'CMT_HELP'
Issue comment commands:
forgejo issue comment <owner/repo> <number> --body=TEXT
Add a comment to an issue or PR.
The <number> is the issue number (e.g., #42).
forgejo issue comment delete <owner/repo> <comment_id>
Delete a comment by its numeric comment ID.
Find the comment ID from `forgejo issue view <owner/repo> <number>`
(shown as `[comment #123]`) or use --json for raw API output.
IMPORTANT NOTES:
- There is NO `issue comment list` or `issue comment view` subcommand.
To see all comments on an issue, use: forgejo issue view <owner/repo> <number>
- Issue and PR comments share the same API backend; comment IDs are globally
unique within a repo across both issues and PRs.
- `issue comment` on a PR number also works (comments on the PR thread).
EXAMPLES:
forgejo issue comment myorg/myrepo 42 --body="I can reproduce this."
forgejo issue view myorg/myrepo 42 # view issue with all comments
forgejo issue comment delete myorg/myrepo 789 # delete comment #789
CMT_HELP
}
show_pr_help() {
cat << 'PR_HELP'
Pull request commands:
pr list <owner/repo> [--state=open|closed|all]
List pull requests.
pr create <owner/repo> --title=TEXT --head=BRANCH [--base=main] [--body=TEXT]
Create a new PR.
pr view <owner/repo> <number>
View a PR with full conversation: details, all reviews, inline
review-thread comments, and general comments. Use --json for the
complete machine-readable object (pull fields plus .reviews[] with
nested .comments[] and top-level .issue_comments[]).
pr merge <owner/repo> <number> [--method=merge|rebase|squash]
Merge a PR.
pr close <owner/repo> <number>
Close a PR without merging.
pr edit <owner/repo> <number> [--title=TEXT] [--body=TEXT] [--base=BRANCH]
Edit a PR's title, body, or base branch.
pr ready <owner/repo> <number>
Mark a draft PR as ready for review.
pr comment <owner/repo> <number> --body=TEXT
Add a comment to a PR thread.
pr comment list <owner/repo> <number>
List all comments on a PR. Shows ID, author, date, and body preview.
pr comment view <owner/repo> <comment_id>
View a single comment by its numeric ID.
pr comment delete <owner/repo> <comment_id>
Delete a comment by its numeric ID.
pr diff <owner/repo> <number>
Show the raw unified diff for a PR.
pr files <owner/repo> <number>
List changed files with additions/deletions.
pr checks <owner/repo> <number>
Show commit statuses and Actions CI runs for the PR's head commit.
pr review <owner/repo> <number> --approve|--request-changes|--comment [--body=TEXT] [--comments=FILE|-]
Submit a PR review. Exactly one of --approve, --request-changes, --comment
is required. --body is required with --request-changes and --comment.
--comments attaches inline line-level review comments.
pr review list <owner/repo> <number>
List all reviews on a PR.
pr review lookup <owner/repo> <number>
Unified JSON output: all reviews, review comments, and PR comments with
a "type" field ("review", "review_comment", "comment").
pr review dismiss <owner/repo> <number> <review_id> --message=TEXT
Dismiss a previous review by its ID.
pr requested-reviewers add <owner/repo> <number> --users=u1,u2
Request specific users to review the PR.
pr requested-reviewers remove <owner/repo> <number> --users=u1,u2
Withdraw reviewer requests.
EXAMPLES:
forgejo pr create myorg/myrepo --title="Fix login" --head=feature/login
forgejo pr comment myorg/myrepo 17 --body="LGTM, ship it!"
forgejo pr comment list myorg/myrepo 17
forgejo pr comment view myorg/myrepo 456
forgejo pr comment delete myorg/myrepo 456
forgejo pr review myorg/myrepo 17 --approve
forgejo pr review myorg/myrepo 17 --request-changes --body="Please add tests."
forgejo pr merge myorg/myrepo 17 --method=squash
Run `forgejo pr comment --help` for comment-specific help.
PR_HELP
}
show_pr_comment_help() {
cat << 'PCMT_HELP'
PR comment commands:
forgejo pr comment <owner/repo> <number> --body=TEXT
Add a comment to a PR thread.
The <number> is the PR number (e.g., #17).
forgejo pr comment list <owner/repo> <number>
List all comments on a PR with ID, author, date, and body preview.
forgejo pr comment view <owner/repo> <comment_id>
View a single comment's full body by its numeric comment ID.
forgejo pr comment delete <owner/repo> <comment_id>
Delete a comment by its numeric comment ID.
IMPORTANT NOTES:
- For issues, comments are shown inline via `forgejo issue view <owner/repo> <number>`.
There is no separate `issue comment list` subcommand.
- PR comments have full list/view/delete support (unlike issue comments).
- Comment IDs are numeric and globally unique within a repo across both
issues and PRs.
EXAMPLES:
forgejo pr comment myorg/myrepo 17 --body="LGTM!"
forgejo pr comment list myorg/myrepo 17 # list all comments on PR #17
forgejo pr comment view myorg/myrepo 456 # view comment #456 in full
forgejo pr comment delete myorg/myrepo 456 # delete comment #456
PCMT_HELP
}
# --- RESOURCE COMMANDS (stubs for now) ---
cmd_repo() {
if [[ $# -eq 0 ]] || [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
echo "Usage: forgejo repo <list|create|view|edit|delete|fork|transfer|archive|unarchive|tags|collaborator|webhook|key|topic|mirror> [args]" >&2
echo "Run 'forgejo --help' for the full command reference." >&2
exit 1
fi
local action="$1"
shift
case "$action" in
list) repo_list "$@" ;;
create) repo_create "$@" ;;
view) repo_view "$@" ;;
edit) repo_edit "$@" ;;
delete) repo_delete "$@" ;;
fork) repo_fork "$@" ;;
transfer) repo_transfer "$@" ;;
archive) repo_archive "$@" ;;
unarchive) repo_unarchive "$@" ;;
tags) cmd_repo_tags "$@" ;;
collaborator) cmd_repo_collaborator "$@" ;;
webhook) cmd_repo_webhook "$@" ;;
key) cmd_repo_key "$@" ;;
topic) cmd_repo_topic "$@" ;;
mirror) cmd_repo_mirror "$@" ;;
*) echo "Unknown repo action: $action" >&2; exit 1 ;;
esac
}
repo_list() {
local endpoint="user/repos?limit=50"
local org
if org=$(get_flag "--org" "$@" 2>/dev/null) || [[ $# -gt 0 && "$1" != --* ]]; then
org="${org:-$1}"
endpoint="orgs/$org/repos?limit=50"
fi
local result
result=$(api_call GET "$endpoint")
if [[ "$JSON_OUTPUT" -eq 1 ]]; then
echo "$result" | jq .
else
echo "$result" | jq -r '
["NAME","PRIVATE","STARS","UPDATED"],
(.[] | [.full_name, (if .private then "yes" else "no" end), (.stars_count|tostring), .updated_at[:10]])
| @tsv
' | column -t -s $'\t'
fi
}
repo_create() {
if [[ $# -eq 0 ]]; then
echo "Usage: forgejo repo create <name> [--org=X] [--private] [--desc=X]" >&2
exit 1
fi
local name="$1"
shift
local org=""
local private="false"
local desc=""
org=$(get_flag "--org" "$@" 2>/dev/null) || true
desc=$(get_flag "--desc" "$@" 2>/dev/null) || true
has_flag "--private" "$@" && private="true"
local body
body=$(jq -n \
--arg name "$name" \
--arg desc "$desc" \
--argjson private "$private" \
'{name: $name, description: $desc, private: $private}')
local endpoint="user/repos"
if [[ -n "$org" ]]; then
endpoint="orgs/$org/repos"
fi
local result
result=$(api_call POST "$endpoint" "$body")
if [[ "$JSON_OUTPUT" -eq 1 ]]; then
echo "$result" | jq .
else
echo "$result" | jq -r '"Created: \(.full_name)\nURL: \(.html_url)\nSSH: \(.ssh_url)"'
fi
}
repo_view() {
if [[ $# -eq 0 ]]; then
echo "Usage: forgejo repo view <owner/repo>" >&2
exit 1
fi
local result
result=$(api_call GET "repos/$1")
if [[ "$JSON_OUTPUT" -eq 1 ]]; then
echo "$result" | jq .
else
echo "$result" | jq -r '
"Name: \(.full_name)",
"Description: \(.description // "-")",
"Private: \(if .private then "yes" else "no" end)",
"Stars: \(.stars_count)",
"Forks: \(.forks_count)",
"HTTP URL: \(.clone_url)",
"SSH URL: \(.ssh_url)",
"Created: \(.created_at[:10])",
"Updated: \(.updated_at[:10])"
'
fi
}
repo_edit() {
if [[ $# -eq 0 ]]; then
echo "Usage: forgejo repo edit <owner/repo> [--name=X] [--desc=X] [--private] [--public]" >&2
exit 1
fi
local repo="$1"
shift
local body="{}"
local val
if val=$(get_flag "--name" "$@" 2>/dev/null); then
body=$(echo "$body" | jq --arg n "$val" '. + {name: $n}')
fi
local desc
if desc=$(get_flag "--desc" "$@" 2>/dev/null); then
body=$(echo "$body" | jq --arg d "$desc" '. + {description: $d}')
fi
if has_flag "--private" "$@"; then
body=$(echo "$body" | jq '. + {private: true}')
fi
if has_flag "--public" "$@"; then
body=$(echo "$body" | jq '. + {private: false}')
fi
if [[ "$body" == "{}" ]]; then
echo "No fields to update. Provide --name, --desc, --private, or --public." >&2
exit 1
fi
local result
result=$(api_call PATCH "repos/$repo" "$body")
if [[ "$JSON_OUTPUT" -eq 1 ]]; then
echo "$result" | jq .
else
echo "Updated: $(echo "$result" | jq -r '.full_name')"
fi
}
repo_delete() {
if [[ $# -eq 0 ]]; then
echo "Usage: forgejo repo delete <owner/repo> [--yes]" >&2
exit 1
fi
local repo="$1"
shift
confirm_delete "repo" "$repo" "$@"
api_call DELETE "repos/$repo" > /dev/null
echo "Deleted: $repo"
}
repo_fork() {
if [[ $# -eq 0 ]]; then
echo "Usage: forgejo repo fork <owner/repo> [--org=X]" >&2
exit 1
fi
local repo="$1"
shift
local org=""
org=$(get_flag "--org" "$@" 2>/dev/null) || true
local body="{}"
if [[ -n "$org" ]]; then
body=$(jq -n --arg o "$org" '{organization: $o}')
fi
local out
local status
local response
out=$(api_call_status POST "repos/$repo/forks" "$body")
status=$(printf '%s' "$out" | head -1)
response=$(printf '%s' "$out" | tail -n +2)
if [[ "$status" -ge 200 && "$status" -lt 300 ]]; then
if [[ "$JSON_OUTPUT" -eq 1 ]]; then
echo "$response" | jq .
else
echo "$response" | jq -r '"Forked: \(.full_name)\nURL: \(.html_url)"'