Merge pull request #386: feat(branch): add +all and +set-default

# Conflicts:
#	shortcuts/branch/branch_test.go
This commit is contained in:
wbtiger 2026-07-14 21:57:59 +08:00
commit 1e7b646587
5 changed files with 66 additions and 23 deletions

View File

@ -456,6 +456,10 @@ gitlink-cli branch +create --name feature/new-feature
# Delete a branch
gitlink-cli branch +delete --name feature/old-feature
# All branch names at once (no paging) and default-branch switch
gitlink-cli branch +all --owner myname --repo myrepo
gitlink-cli branch +set-default --owner myname --repo myrepo -n develop
# Protect a branch
gitlink-cli branch +protect --name main

View File

@ -5,10 +5,12 @@
"cmd.auth.logout.short": "Logout from GitLink",
"cmd.auth.short": "Authentication commands",
"cmd.auth.status.short": "Show authentication status",
"cmd.branch.all.short": "List all branch names (no paging)",
"cmd.branch.create.short": "Create a branch",
"cmd.branch.delete.short": "Delete a branch",
"cmd.branch.list.short": "List branches",
"cmd.branch.protect.short": "Set branch protection",
"cmd.branch.set_default.short": "Set the repository default branch",
"cmd.branch.short": "Branch operations",
"cmd.branch.unprotect.short": "Remove branch protection",
"cmd.ci.builds.short": "List CI builds",

View File

@ -5,10 +5,12 @@
"cmd.auth.logout.short": "退出 GitLink 登录",
"cmd.auth.short": "认证命令",
"cmd.auth.status.short": "显示认证状态",
"cmd.branch.all.short": "列出全部分支名(不分页)",
"cmd.branch.create.short": "创建分支",
"cmd.branch.delete.short": "删除分支",
"cmd.branch.list.short": "列出分支",
"cmd.branch.protect.short": "设置分支保护",
"cmd.branch.set_default.short": "设置仓库默认分支",
"cmd.branch.short": "分支操作",
"cmd.branch.unprotect.short": "移除分支保护",
"cmd.ci.builds.short": "列出 CI 构建",

View File

@ -99,6 +99,42 @@ func Shortcuts(translators ...*i18n.Translator) []*common.Shortcut {
return ctx.Output(env)
},
},
{
Name: "all",
Description: tr.T("cmd.branch.all.short"),
Flags: []common.Flag{},
Run: func(ctx *common.RuntimeContext) error {
if err := ctx.ResolveOwnerRepo(); err != nil {
return err
}
env, err := ctx.CallAPI("GET", "/v1"+ctx.RepoPath()+"/branches/all", nil)
if err != nil {
return err
}
return ctx.Output(env)
},
},
{
Name: "set-default",
Description: tr.T("cmd.branch.set_default.short"),
Flags: []common.Flag{
{Name: "name", Short: "n", Usage: tr.T("flag.branch.name"), Required: true},
},
Run: func(ctx *common.RuntimeContext) error {
if err := ctx.ResolveOwnerRepo(); err != nil {
return err
}
name, err := ctx.RequireArg("name")
if err != nil {
return err
}
env, err := ctx.CallAPI("PATCH", "/v1"+ctx.RepoPath()+"/branches/update_default_branch", map[string]interface{}{"name": name})
if err != nil {
return err
}
return ctx.Output(env)
},
},
{
Name: "protect",
Description: tr.T("cmd.branch.protect.short"),

View File

@ -4,7 +4,6 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gitlink-org/gitlink-cli/internal/client"
@ -218,26 +217,6 @@ func TestBranchUnprotectHTTPError(t *testing.T) {
}
}
func TestBranchListPassesKeyword(t *testing.T) {
var query string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/owner/repo/branches.json" {
t.Fatalf("unexpected path: %s", r.URL.Path)
}
query = r.URL.RawQuery
writeJSON(w, map[string]interface{}{"total_count": 1, "branches": []interface{}{}})
}))
defer server.Close()
err := runShortcut(t, server, "list", map[string]string{"page": "1", "limit": "20", "keyword": "feat"})
if err != nil {
t.Fatalf("list failed: %v", err)
}
if !strings.Contains(query, "keyword=feat") {
t.Fatalf("expected keyword in query, got %q", query)
}
}
func TestBranchAllUsesAllEndpoint(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" || r.URL.Path != "/v1/owner/repo/branches/all.json" {
@ -247,8 +226,28 @@ func TestBranchAllUsesAllEndpoint(t *testing.T) {
}))
defer server.Close()
err := runShortcut(t, server, "all", map[string]string{})
if err != nil {
if err := runShortcut(t, server, "all", nil); err != nil {
t.Fatalf("all failed: %v", err)
}
}
func TestBranchSetDefaultPatchesName(t *testing.T) {
var payload map[string]interface{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PATCH" || r.URL.Path != "/v1/owner/repo/branches/update_default_branch.json" {
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
}
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
t.Fatalf("decode payload: %v", err)
}
writeJSON(w, map[string]interface{}{"status": float64(0), "message": "success"})
}))
defer server.Close()
if err := runShortcut(t, server, "set-default", map[string]string{"name": "develop"}); err != nil {
t.Fatalf("set-default failed: %v", err)
}
if payload["name"] != "develop" {
t.Fatalf("expected name=develop, got %v", payload["name"])
}
}