forked from Gitlink/gitlink-cli
feat(repo): add mirror sync shortcut
This commit is contained in:
parent
71ca2bb683
commit
4a263c1ec0
|
|
@ -0,0 +1,13 @@
|
||||||
|
# Repo Mirror Sync Shortcut
|
||||||
|
|
||||||
|
Added `gitlink-cli repo-mirror +sync` for `POST /repositories/{id}/sync_mirror`.
|
||||||
|
|
||||||
|
Safety: supports `--dry-run`; validates `--id` as integer.
|
||||||
|
|
||||||
|
Validation:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
GOPROXY=https://goproxy.cn,direct go test ./shortcuts/repomirror ./shortcuts
|
||||||
|
go vet ./shortcuts/repomirror ./shortcuts
|
||||||
|
go run . repo-mirror --help
|
||||||
|
```
|
||||||
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"github.com/gitlink-org/gitlink-cli/shortcuts/profile"
|
"github.com/gitlink-org/gitlink-cli/shortcuts/profile"
|
||||||
"github.com/gitlink-org/gitlink-cli/shortcuts/release"
|
"github.com/gitlink-org/gitlink-cli/shortcuts/release"
|
||||||
"github.com/gitlink-org/gitlink-cli/shortcuts/repo"
|
"github.com/gitlink-org/gitlink-cli/shortcuts/repo"
|
||||||
|
"github.com/gitlink-org/gitlink-cli/shortcuts/repomirror"
|
||||||
"github.com/gitlink-org/gitlink-cli/shortcuts/search"
|
"github.com/gitlink-org/gitlink-cli/shortcuts/search"
|
||||||
"github.com/gitlink-org/gitlink-cli/shortcuts/user"
|
"github.com/gitlink-org/gitlink-cli/shortcuts/user"
|
||||||
"github.com/gitlink-org/gitlink-cli/shortcuts/webhook"
|
"github.com/gitlink-org/gitlink-cli/shortcuts/webhook"
|
||||||
|
|
@ -57,6 +58,7 @@ func RegisterAll(root *cobra.Command, translators ...*i18n.Translator) {
|
||||||
"wiki": wiki.Shortcuts(),
|
"wiki": wiki.Shortcuts(),
|
||||||
"health": health.Shortcuts(tr),
|
"health": health.Shortcuts(tr),
|
||||||
"ignore": ignore.Shortcuts(),
|
"ignore": ignore.Shortcuts(),
|
||||||
|
"repo-mirror": repomirror.Shortcuts(),
|
||||||
"workflow": workflow.Shortcuts(),
|
"workflow": workflow.Shortcuts(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -82,6 +84,7 @@ func RegisterAll(root *cobra.Command, translators ...*i18n.Translator) {
|
||||||
"wiki": "Wiki page management",
|
"wiki": "Wiki page management",
|
||||||
"health": "Project health data collection",
|
"health": "Project health data collection",
|
||||||
"ignore": tr.T("cmd.ignore.short"),
|
"ignore": tr.T("cmd.ignore.short"),
|
||||||
|
"repo-mirror": "Repository mirror operations",
|
||||||
"workflow": "AI agent workflow analysis",
|
"workflow": "AI agent workflow analysis",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ func TestRegisterAll(t *testing.T) {
|
||||||
"repo", "issue", "label", "license", "pr", "profile", "release", "branch",
|
"repo", "issue", "label", "license", "pr", "profile", "release", "branch",
|
||||||
"org", "user", "search", "ci", "workflow",
|
"org", "user", "search", "ci", "workflow",
|
||||||
"compare", "member", "milestone", "pipeline", "webhook",
|
"compare", "member", "milestone", "pipeline", "webhook",
|
||||||
"dataset", "health", "ignore", "wiki",
|
"dataset", "health", "ignore", "wiki", "repo-mirror",
|
||||||
}
|
}
|
||||||
|
|
||||||
groupSet := map[string]bool{}
|
groupSet := map[string]bool{}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
package repomirror
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Shortcuts returns mirror repository operations.
|
||||||
|
func Shortcuts() []*common.Shortcut {
|
||||||
|
return []*common.Shortcut{
|
||||||
|
{
|
||||||
|
Name: "sync",
|
||||||
|
Description: "Synchronize a mirror repository",
|
||||||
|
Flags: []common.Flag{
|
||||||
|
{Name: "id", Short: "i", Usage: "Repository/project ID", Required: true},
|
||||||
|
{Name: "dry-run", Usage: "Preview the request without syncing the mirror", Bool: true, Default: "false"},
|
||||||
|
},
|
||||||
|
Run: runSync,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runSync(ctx *common.RuntimeContext) error {
|
||||||
|
id, err := ctx.RequireArg("id")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := strconv.Atoi(id); err != nil {
|
||||||
|
return fmt.Errorf("invalid --id value %q: %w", id, err)
|
||||||
|
}
|
||||||
|
path := fmt.Sprintf("/repositories/%s/sync_mirror", url.PathEscape(id))
|
||||||
|
if ctx.Arg("dry-run") == "true" {
|
||||||
|
return ctx.OutputData(map[string]interface{}{"dry_run": true, "action": "sync_mirror", "method": "POST", "path": path})
|
||||||
|
}
|
||||||
|
env, err := ctx.CallAPI("POST", path, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return ctx.Output(env)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
package repomirror
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gitlink-org/gitlink-cli/internal/client"
|
||||||
|
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMirrorSync(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != "POST" || r.URL.Path != "/repositories/42/sync_mirror.json" {
|
||||||
|
t.Fatalf("got %s %s", r.Method, r.URL.Path)
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{"status": 0})
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
if err := runShortcut(server, map[string]string{"id": "42"}); err != nil {
|
||||||
|
t.Fatalf("sync failed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMirrorSyncDryRun(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
t.Fatalf("dry-run should not call API")
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
if err := runShortcut(server, map[string]string{"id": "42", "dry-run": "true"}); err != nil {
|
||||||
|
t.Fatalf("dry-run failed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMirrorSyncRejectsInvalidID(t *testing.T) {
|
||||||
|
if err := runShortcut(httptest.NewServer(http.NewServeMux()), map[string]string{"id": "abc"}); err == nil {
|
||||||
|
t.Fatal("expected invalid id error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runShortcut(server *httptest.Server, args map[string]string) error {
|
||||||
|
return Shortcuts()[0].Run(&common.RuntimeContext{Client: &client.Client{HTTP: server.Client(), BaseURL: server.URL}, Format: "json", Args: args})
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
---
|
||||||
|
name: gitlink-repo-mirror
|
||||||
|
version: 1.0.0
|
||||||
|
description: "GitLink 镜像仓库同步:触发镜像仓库 sync_mirror,支持 dry-run。"
|
||||||
|
metadata:
|
||||||
|
requires:
|
||||||
|
bins: ["gitlink-cli"]
|
||||||
|
cliHelp: "gitlink-cli repo-mirror --help"
|
||||||
|
---
|
||||||
|
|
||||||
|
# gitlink-repo-mirror
|
||||||
|
|
||||||
|
镜像同步是写操作,先 dry-run 并确认。
|
||||||
|
|
||||||
|
```bash
|
||||||
|
gitlink-cli repo-mirror +sync --id 42 --dry-run
|
||||||
|
gitlink-cli repo-mirror +sync --id 42
|
||||||
|
```
|
||||||
|
|
||||||
|
对应 OpenAPI:`POST /repositories/{id}/sync_mirror`。
|
||||||
Loading…
Reference in New Issue