gitlink-cli/shortcuts/branch/branch.go

158 lines
4.4 KiB
Go

package branch
import (
"fmt"
"net/url"
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
)
func Shortcuts() []*common.Shortcut {
return []*common.Shortcut{
{
Name: "list",
Description: "List branches",
Flags: []common.Flag{
{Name: "page", Short: "p", Usage: "Page number", Default: "1"},
{Name: "limit", Short: "l", Usage: "Items per page", Default: "20"},
},
Run: func(ctx *common.RuntimeContext) error {
if err := ctx.ResolveOwnerRepo(); err != nil {
return err
}
q := url.Values{}
q.Set("page", ctx.Arg("page"))
q.Set("limit", ctx.Arg("limit"))
env, err := ctx.CallAPIWithQuery("GET", "/v1"+ctx.RepoPath()+"/branches", q)
if err != nil {
return fmt.Errorf("获取分支列表失败: %w", err)
}
return ctx.Output(env)
},
},
{
Name: "create",
Description: "Create a branch",
DryRun: true,
DryRunHint: func(ctx *common.RuntimeContext) (string, error) {
name := ctx.Arg("name")
from := ctx.Arg("from")
if from == "" {
from = "master"
}
return fmt.Sprintf("Create branch: %s (from %s)", name, from), nil
},
Flags: []common.Flag{
{Name: "name", Short: "n", Usage: "Branch name", Required: true},
{Name: "from", Short: "f", Usage: "Source branch or commit", Default: "master"},
},
Run: func(ctx *common.RuntimeContext) error {
if err := ctx.ResolveOwnerRepo(); err != nil {
return err
}
name, err := ctx.RequireArg("name", "--name feature/new-thing")
if err != nil {
return err
}
from := ctx.Arg("from")
if from == "" {
from = "master"
}
payload := map[string]interface{}{
"new_branch_name": name,
"old_branch_name": from,
}
env, err := ctx.CallAPI("POST", "/v1"+ctx.RepoPath()+"/branches", payload)
if err != nil {
return fmt.Errorf("创建分支失败: %w", err)
}
return ctx.Output(env)
},
},
{
Name: "delete",
Description: "Delete a branch",
DryRun: true,
DryRunHint: func(ctx *common.RuntimeContext) (string, error) {
name := ctx.Arg("name")
return fmt.Sprintf("Delete branch: %s", name), nil
},
Flags: []common.Flag{
{Name: "name", Short: "n", Usage: "Branch name", Required: true},
},
Run: func(ctx *common.RuntimeContext) error {
if err := ctx.ResolveOwnerRepo(); err != nil {
return err
}
name, err := ctx.RequireArg("name", "--name feature/new-thing")
if err != nil {
return err
}
payload := map[string]interface{}{
"branch_name": name,
}
env, err := ctx.CallAPI("POST", "/v1"+ctx.RepoPath()+"/branches/delete", payload)
if err != nil {
return fmt.Errorf("删除分支失败: %w", err)
}
return ctx.Output(env)
},
},
{
Name: "protect",
Description: "Set branch protection",
DryRun: true,
DryRunHint: func(ctx *common.RuntimeContext) (string, error) {
name := ctx.Arg("name")
return fmt.Sprintf("Protect branch: %s", name), nil
},
Flags: []common.Flag{
{Name: "name", Short: "n", Usage: "Branch name", Required: true},
},
Run: func(ctx *common.RuntimeContext) error {
if err := ctx.ResolveOwnerRepo(); err != nil {
return err
}
name, err := ctx.RequireArg("name", "--name feature/new-thing")
if err != nil {
return err
}
payload := map[string]interface{}{
"branch_name": name,
}
env, err := ctx.CallAPI("POST", ctx.RepoPath()+"/protected_branches", payload)
if err != nil {
return fmt.Errorf("设置分支保护失败: %w", err)
}
return ctx.Output(env)
},
},
{
Name: "unprotect",
Description: "Remove branch protection",
DryRun: true,
DryRunHint: func(ctx *common.RuntimeContext) (string, error) {
name := ctx.Arg("name")
return fmt.Sprintf("Unprotect branch: %s", name), nil
},
Flags: []common.Flag{
{Name: "name", Short: "n", Usage: "Branch name", Required: true},
},
Run: func(ctx *common.RuntimeContext) error {
if err := ctx.ResolveOwnerRepo(); err != nil {
return err
}
name, err := ctx.RequireArg("name", "--name feature/new-thing")
if err != nil {
return err
}
env, err := ctx.CallAPI("DELETE", fmt.Sprintf("%s/protected_branches/%s", ctx.RepoPath(), url.PathEscape(name)), nil)
if err != nil {
return fmt.Errorf("取消分支保护失败: %w", err)
}
return ctx.Output(env)
},
},
}
}