✨ feat(spx): 按 git origin 支持 GitHub 仓库,state 评论改成隐藏注释格式,新增 issue view / pr view / pr create
This commit is contained in:
1 parent
64d5961b62
commit
c90d84d7f2
17 files changed
+2683
-400
No files matched your search
@@ -1,26 +1,33 @@
|
||||
package repo
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"os/exec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseRemoteURL(t *testing.T) {
|
||||
cases := []struct {
|
||||
raw, owner, repo string
|
||||
raw, host, webURL, owner, repo string
|
||||
}{
|
||||
{"https://gitea.example.com/foo/bar.git", "foo", "bar"},
|
||||
{"https://gitea.example.com/foo/bar", "foo", "bar"},
|
||||
{"http://localhost:3000/foo/bar.git", "foo", "bar"},
|
||||
{"git@gitea.example.com:foo/bar.git", "foo", "bar"},
|
||||
{"git@gitea.example.com:foo/bar", "foo", "bar"},
|
||||
{"ssh://git@gitea.example.com:22/foo/bar.git", "foo", "bar"},
|
||||
{"https://gitea.example.com/foo/bar.git", "gitea.example.com", "https://gitea.example.com", "foo", "bar"},
|
||||
{"https://gitea.example.com/foo/bar", "gitea.example.com", "https://gitea.example.com", "foo", "bar"},
|
||||
{"https://user:secret@gitea.example.com/foo/bar.git", "gitea.example.com", "https://gitea.example.com", "foo", "bar"},
|
||||
{"http://localhost:3000/foo/bar.git", "localhost", "http://localhost:3000", "foo", "bar"},
|
||||
{"https://GitHub.com/Foo/Bar.git", "github.com", "https://github.com", "Foo", "Bar"},
|
||||
{"git@gitea.example.com:foo/bar.git", "gitea.example.com", "https://gitea.example.com", "foo", "bar"},
|
||||
{"git@gitea.example.com:foo/bar", "gitea.example.com", "https://gitea.example.com", "foo", "bar"},
|
||||
{"git@github.com:foo/bar.git", "github.com", "https://github.com", "foo", "bar"},
|
||||
{"ssh://git@gitea.example.com:22/foo/bar.git", "gitea.example.com", "https://gitea.example.com", "foo", "bar"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.raw, func(t *testing.T) {
|
||||
owner, repo, err := parseRemoteURL(c.raw)
|
||||
o, err := parseRemoteURL(c.raw)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if owner != c.owner || repo != c.repo {
|
||||
t.Fatalf("got %s/%s want %s/%s", owner, repo, c.owner, c.repo)
|
||||
want := Origin{Host: c.host, WebURL: c.webURL, Owner: c.owner, Repo: c.repo}
|
||||
if o != want {
|
||||
t.Fatalf("got %+v want %+v", o, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -34,8 +41,68 @@ func TestParseRemoteURLRejectsBadInput(t *testing.T) {
|
||||
"https://gitea.example.com/onlyowner",
|
||||
}
|
||||
for _, raw := range bad {
|
||||
if _, _, err := parseRemoteURL(raw); err == nil {
|
||||
if _, err := parseRemoteURL(raw); err == nil {
|
||||
t.Fatalf("expected error for %q", raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseHost(t *testing.T) {
|
||||
cases := []struct {
|
||||
in, host, webURL string
|
||||
}{
|
||||
{"github.com", "github.com", "https://github.com"},
|
||||
{"GitHub.com", "github.com", "https://github.com"},
|
||||
{"gitea.example.com/", "gitea.example.com", "https://gitea.example.com"},
|
||||
{"https://gitea.example.com/", "gitea.example.com", "https://gitea.example.com"},
|
||||
{"http://localhost:3000", "localhost", "http://localhost:3000"},
|
||||
{"https://example.com/gitea/", "example.com", "https://example.com/gitea"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.in, func(t *testing.T) {
|
||||
host, webURL, err := ParseHost(c.in)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if host != c.host || webURL != c.webURL {
|
||||
t.Fatalf("got (%s, %s) want (%s, %s)", host, webURL, c.host, c.webURL)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, bad := range []string{"", " ", "https://", "https:///foo"} {
|
||||
if _, _, err := ParseHost(bad); err == nil {
|
||||
t.Fatalf("expected error for %q", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectOriginReadsGitRemote(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
for _, args := range [][]string{
|
||||
{"init", "-q"},
|
||||
{"remote", "add", "origin", "git@github.com:acme/widgets.git"},
|
||||
} {
|
||||
cmd := exec.Command("git", append([]string{"-C", dir}, args...)...)
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("git %v 失败: %v: %s", args, err, out)
|
||||
}
|
||||
}
|
||||
o, err := DetectOrigin(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("DetectOrigin 出错: %v", err)
|
||||
}
|
||||
want := Origin{Host: "github.com", WebURL: "https://github.com", Owner: "acme", Repo: "widgets"}
|
||||
if o != want {
|
||||
t.Fatalf("got %+v want %+v", o, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectOriginFailsWithoutRemote(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if out, err := exec.Command("git", "-C", dir, "init", "-q").CombinedOutput(); err != nil {
|
||||
t.Fatalf("git init 失败: %v: %s", err, out)
|
||||
}
|
||||
if _, err := DetectOrigin(dir); err == nil {
|
||||
t.Fatal("没有 origin 时应当报错")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user