Merge pull request 'feat(branch): add +all and keyword filtering for +list' (#377) from Taoyouce/gitlink-cli:feat/branch-all-and-filter into master
This commit is contained in:
commit
df36a9aa40
|
|
@ -441,6 +441,12 @@ gitlink-cli pr +review --owner Gitlink --repo forgeplus -i 42 --status approved
|
|||
# List branches
|
||||
gitlink-cli branch +list --owner Gitlink --repo forgeplus
|
||||
|
||||
# Filter branches by name keyword
|
||||
gitlink-cli branch +list --owner Gitlink --repo forgeplus -k feature
|
||||
|
||||
# List all branch names without pagination
|
||||
gitlink-cli branch +all --owner Gitlink --repo forgeplus
|
||||
|
||||
# Create a branch
|
||||
gitlink-cli branch +create --name feature/new-feature
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
"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 pagination)",
|
||||
"cmd.branch.create.short": "Create a branch",
|
||||
"cmd.branch.delete.short": "Delete a branch",
|
||||
"cmd.branch.list.short": "List branches",
|
||||
|
|
@ -127,6 +128,7 @@
|
|||
"flag.api.query": "Query parameters (key=val&key2=val2)",
|
||||
"flag.auth.token": "Login by pasting an existing token",
|
||||
"flag.branch.from": "Source branch or commit",
|
||||
"flag.branch.keyword": "Filter branches by name keyword",
|
||||
"flag.branch.name": "Branch name",
|
||||
"flag.ci.build": "Build number",
|
||||
"flag.ci.stage": "Stage number",
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
"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": "列出分支",
|
||||
|
|
@ -127,6 +128,7 @@
|
|||
"flag.api.query": "查询参数(key=val&key2=val2)",
|
||||
"flag.auth.token": "通过粘贴已有 Token 登录",
|
||||
"flag.branch.from": "源分支或 Commit",
|
||||
"flag.branch.keyword": "按名称关键字过滤分支",
|
||||
"flag.branch.name": "分支名称",
|
||||
"flag.ci.build": "构建编号",
|
||||
"flag.ci.stage": "阶段编号",
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ func Shortcuts(translators ...*i18n.Translator) []*common.Shortcut {
|
|||
Name: "list",
|
||||
Description: tr.T("cmd.branch.list.short"),
|
||||
Flags: []common.Flag{
|
||||
{Name: "keyword", Short: "k", Usage: tr.T("flag.branch.keyword")},
|
||||
{Name: "page", Short: "p", Usage: tr.T("flag.page"), Default: "1"},
|
||||
{Name: "limit", Short: "l", Usage: tr.T("flag.limit"), Default: "20"},
|
||||
},
|
||||
|
|
@ -25,6 +26,9 @@ func Shortcuts(translators ...*i18n.Translator) []*common.Shortcut {
|
|||
q := url.Values{}
|
||||
q.Set("page", ctx.Arg("page"))
|
||||
q.Set("limit", ctx.Arg("limit"))
|
||||
if keyword := ctx.Arg("keyword"); keyword != "" {
|
||||
q.Set("keyword", keyword)
|
||||
}
|
||||
env, err := ctx.CallAPIWithQuery("GET", "/v1"+ctx.RepoPath()+"/branches", q)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -32,6 +36,21 @@ 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: "create",
|
||||
Description: tr.T("cmd.branch.create.short"),
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gitlink-org/gitlink-cli/internal/client"
|
||||
|
|
@ -216,3 +217,38 @@ func TestBranchUnprotectHTTPError(t *testing.T) {
|
|||
t.Fatal("expected error for HTTP 500")
|
||||
}
|
||||
}
|
||||
|
||||
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" {
|
||||
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
||||
}
|
||||
writeJSON(w, []interface{}{map[string]interface{}{"name": "master"}})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
err := runShortcut(t, server, "all", map[string]string{})
|
||||
if err != nil {
|
||||
t.Fatalf("all failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue