feat(repo): add repo +edit to update repository settings
Adds a repo +edit shortcut that updates repository metadata via PATCH /:owner/:repo.json: --description, --website, --private, --default-branch, --category-id, --language-id. The server dispatches on which key is present (website, default_branch, or general metadata) and the metadata path validates name/identifier, so the command resolves them from the repository detail first and sends each field group as its own request. Requires at least one field and validates --private and integer IDs before any API call. Production-verified against gitlink.org.cn (description/website update and default-branch round-trip). Includes 6 unit tests and bilingual README examples plus en-US/zh-CN i18n keys. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
794210ec03
commit
1eb3b7c485
|
|
@ -253,6 +253,11 @@ gitlink-cli repo +unlike --owner Gitlink --repo forgeplus --project-id 123
|
|||
# Create a repository
|
||||
gitlink-cli repo +create -n my-project -d "Project description"
|
||||
|
||||
# Update repository settings (only the given fields change)
|
||||
gitlink-cli repo +edit --owner me --repo my-project -d "New description" --website "https://example.org"
|
||||
gitlink-cli repo +edit --owner me --repo my-project --private true
|
||||
gitlink-cli repo +edit --owner me --repo my-project --default-branch main
|
||||
|
||||
# Fork a repository
|
||||
gitlink-cli repo +fork --owner Gitlink --repo forgeplus
|
||||
```
|
||||
|
|
|
|||
|
|
@ -264,6 +264,11 @@ gitlink-cli repo +unlike --owner Gitlink --repo forgeplus --project-id 123
|
|||
# 创建仓库
|
||||
gitlink-cli repo +create -n my-project -d "项目描述"
|
||||
|
||||
# 更新仓库设置(只修改指定字段)
|
||||
gitlink-cli repo +edit --owner me --repo my-project -d "新描述" --website "https://example.org"
|
||||
gitlink-cli repo +edit --owner me --repo my-project --private true
|
||||
gitlink-cli repo +edit --owner me --repo my-project --default-branch main
|
||||
|
||||
# Fork 仓库
|
||||
gitlink-cli repo +fork --owner Gitlink --repo forgeplus
|
||||
```
|
||||
|
|
|
|||
|
|
@ -83,6 +83,7 @@
|
|||
"cmd.release.view.short": "View release details",
|
||||
"cmd.repo.create.short": "Create a new repository",
|
||||
"cmd.repo.delete.short": "Delete a repository",
|
||||
"cmd.repo.edit.short": "Update repository settings",
|
||||
"cmd.repo.fork.short": "Fork a repository",
|
||||
"cmd.repo.info.short": "Show repository details",
|
||||
"cmd.repo.list.short": "List repositories for a user or organization",
|
||||
|
|
@ -210,6 +211,10 @@
|
|||
"flag.repo": "Repository name (auto-detected from git remote)",
|
||||
"flag.repo.category": "Filter: manage/mirror/sync/fork/all (default: manage)",
|
||||
"flag.repo.description": "Repository description",
|
||||
"flag.repo.edit.category_id": "Project category ID",
|
||||
"flag.repo.edit.default_branch": "Default branch name",
|
||||
"flag.repo.edit.language_id": "Project language ID",
|
||||
"flag.repo.edit.website": "Repository website URL",
|
||||
"flag.repo.name": "Repository name",
|
||||
"flag.repo.private": "Make repository private (true/false)",
|
||||
"flag.repo.tree.path": "Directory path to list (default: repository root)",
|
||||
|
|
|
|||
|
|
@ -83,6 +83,7 @@
|
|||
"cmd.release.view.short": "查看发布详情",
|
||||
"cmd.repo.create.short": "创建新仓库",
|
||||
"cmd.repo.delete.short": "删除仓库",
|
||||
"cmd.repo.edit.short": "更新仓库设置",
|
||||
"cmd.repo.fork.short": "Fork 仓库",
|
||||
"cmd.repo.info.short": "显示仓库详情",
|
||||
"cmd.repo.list.short": "列出用户或组织的仓库",
|
||||
|
|
@ -210,6 +211,10 @@
|
|||
"flag.repo": "仓库名称(自动从 git remote 检测)",
|
||||
"flag.repo.category": "筛选:manage/mirror/sync/fork/all(默认:manage)",
|
||||
"flag.repo.description": "仓库描述",
|
||||
"flag.repo.edit.category_id": "项目分类 ID",
|
||||
"flag.repo.edit.default_branch": "默认分支名称",
|
||||
"flag.repo.edit.language_id": "项目语言 ID",
|
||||
"flag.repo.edit.website": "仓库网站 URL",
|
||||
"flag.repo.name": "仓库名称",
|
||||
"flag.repo.private": "设为私有仓库(true/false)",
|
||||
"flag.repo.tree.path": "要列出的目录路径(默认:仓库根目录)",
|
||||
|
|
|
|||
|
|
@ -56,6 +56,96 @@ func Shortcuts(translators ...*i18n.Translator) []*common.Shortcut {
|
|||
return ctx.Output(env)
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "edit",
|
||||
Description: tr.T("cmd.repo.edit.short"),
|
||||
Flags: []common.Flag{
|
||||
{Name: "description", Short: "d", Usage: tr.T("flag.repo.description")},
|
||||
{Name: "website", Usage: tr.T("flag.repo.edit.website")},
|
||||
{Name: "private", Usage: tr.T("flag.repo.private")},
|
||||
{Name: "default-branch", Usage: tr.T("flag.repo.edit.default_branch")},
|
||||
{Name: "category-id", Usage: tr.T("flag.repo.edit.category_id")},
|
||||
{Name: "language-id", Usage: tr.T("flag.repo.edit.language_id")},
|
||||
},
|
||||
Run: func(ctx *common.RuntimeContext) error {
|
||||
if err := ctx.ResolveOwnerRepo(); err != nil {
|
||||
return err
|
||||
}
|
||||
private := ctx.Arg("private")
|
||||
if private != "" && private != "true" && private != "false" {
|
||||
return fmt.Errorf("--private must be true or false, got %q", private)
|
||||
}
|
||||
ids := map[string]interface{}{}
|
||||
for flag, key := range map[string]string{"category-id": "project_category_id", "language-id": "project_language_id"} {
|
||||
if v := ctx.Arg(flag); v != "" {
|
||||
id, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("--%s must be an integer, got %q", flag, v)
|
||||
}
|
||||
ids[key] = id
|
||||
}
|
||||
}
|
||||
description := ctx.Arg("description")
|
||||
website := ctx.Arg("website")
|
||||
defaultBranch := ctx.Arg("default-branch")
|
||||
if description == "" && website == "" && defaultBranch == "" && private == "" && len(ids) == 0 {
|
||||
return fmt.Errorf("nothing to update: pass at least one of --description, --website, --private, --default-branch, --category-id, --language-id")
|
||||
}
|
||||
|
||||
detail, err := ctx.CallAPI("GET", ctx.RepoPath(), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, _ := detail.Data.(map[string]interface{})
|
||||
name, _ := data["name"].(string)
|
||||
identifier, _ := data["identifier"].(string)
|
||||
if name == "" || identifier == "" {
|
||||
return fmt.Errorf("cannot resolve repository name/identifier from %s", ctx.RepoPath())
|
||||
}
|
||||
base := func() map[string]interface{} {
|
||||
return map[string]interface{}{"name": name, "identifier": identifier}
|
||||
}
|
||||
|
||||
// The server dispatches on which key is present (website,
|
||||
// default_branch, or general metadata), so each group goes
|
||||
// out as its own request.
|
||||
if defaultBranch != "" {
|
||||
payload := base()
|
||||
payload["default_branch"] = defaultBranch
|
||||
if _, err := ctx.CallAPI("PATCH", ctx.RepoPath(), payload); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if website != "" {
|
||||
payload := base()
|
||||
payload["website"] = website
|
||||
if _, err := ctx.CallAPI("PATCH", ctx.RepoPath(), payload); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if description != "" || private != "" || len(ids) > 0 {
|
||||
payload := base()
|
||||
if description != "" {
|
||||
payload["description"] = description
|
||||
}
|
||||
if private != "" {
|
||||
payload["private"] = private == "true"
|
||||
}
|
||||
for k, v := range ids {
|
||||
payload[k] = v
|
||||
}
|
||||
if _, err := ctx.CallAPI("PATCH", ctx.RepoPath(), payload); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
env, err := ctx.CallAPI("GET", ctx.RepoPath(), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ctx.Output(env)
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "readme",
|
||||
Description: "Show repository README content",
|
||||
|
|
|
|||
|
|
@ -634,3 +634,133 @@ func assertEqual(t *testing.T, got interface{}, want interface{}) {
|
|||
t.Fatalf("got %v (%T), want %v (%T)", got, got, want, want)
|
||||
}
|
||||
}
|
||||
|
||||
// --- edit ---
|
||||
|
||||
func TestRepoEditRequiresAtLeastOneField(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
err := runShortcut(t, server, "edit", nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when no fields are given")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepoEditValidatesPrivate(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
err := runShortcut(t, server, "edit", map[string]string{"private": "yes"})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid --private value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepoEditValidatesCategoryID(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
err := runShortcut(t, server, "edit", map[string]string{"category-id": "abc"})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for non-integer --category-id")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepoEditSendsMetadataWithNameAndIdentifier(t *testing.T) {
|
||||
var patches []map[string]interface{}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/owner/repo.json" {
|
||||
t.Fatalf("unexpected path: %s", r.URL.Path)
|
||||
}
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
writeJSON(t, w, map[string]interface{}{"name": "repo", "identifier": "repo"})
|
||||
case "PATCH":
|
||||
var body map[string]interface{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatalf("decode body: %v", err)
|
||||
}
|
||||
patches = append(patches, body)
|
||||
writeJSON(t, w, map[string]interface{}{"id": 1})
|
||||
default:
|
||||
t.Fatalf("unexpected method: %s", r.Method)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
err := runShortcut(t, server, "edit", map[string]string{"description": "new desc", "private": "true"})
|
||||
if err != nil {
|
||||
t.Fatalf("edit failed: %v", err)
|
||||
}
|
||||
if len(patches) != 1 {
|
||||
t.Fatalf("expected 1 PATCH, got %d", len(patches))
|
||||
}
|
||||
body := patches[0]
|
||||
assertEqual(t, body["name"], "repo")
|
||||
assertEqual(t, body["identifier"], "repo")
|
||||
assertEqual(t, body["description"], "new desc")
|
||||
assertEqual(t, body["private"], true)
|
||||
}
|
||||
|
||||
func TestRepoEditSplitsWebsiteAndDefaultBranchRequests(t *testing.T) {
|
||||
var patches []map[string]interface{}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/owner/repo.json" {
|
||||
t.Fatalf("unexpected path: %s", r.URL.Path)
|
||||
}
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
writeJSON(t, w, map[string]interface{}{"name": "repo", "identifier": "repo"})
|
||||
case "PATCH":
|
||||
var body map[string]interface{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatalf("decode body: %v", err)
|
||||
}
|
||||
patches = append(patches, body)
|
||||
writeJSON(t, w, map[string]interface{}{"id": 1})
|
||||
default:
|
||||
t.Fatalf("unexpected method: %s", r.Method)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
err := runShortcut(t, server, "edit", map[string]string{
|
||||
"website": "https://example.org",
|
||||
"default-branch": "main",
|
||||
"description": "d",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("edit failed: %v", err)
|
||||
}
|
||||
if len(patches) != 3 {
|
||||
t.Fatalf("expected 3 PATCH requests (default-branch, website, metadata), got %d", len(patches))
|
||||
}
|
||||
assertEqual(t, patches[0]["default_branch"], "main")
|
||||
if _, ok := patches[0]["website"]; ok {
|
||||
t.Fatal("default-branch request must not carry website")
|
||||
}
|
||||
assertEqual(t, patches[1]["website"], "https://example.org")
|
||||
if _, ok := patches[1]["default_branch"]; ok {
|
||||
t.Fatal("website request must not carry default_branch")
|
||||
}
|
||||
assertEqual(t, patches[2]["description"], "d")
|
||||
}
|
||||
|
||||
func TestRepoEditFailsWhenIdentifierMissing(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(t, w, map[string]interface{}{"name": "repo"})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
err := runShortcut(t, server, "edit", map[string]string{"description": "x"})
|
||||
if err == nil {
|
||||
t.Fatal("expected error when identifier cannot be resolved")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue