✨ 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,16 +1,18 @@
|
||||
// Package tea reads the tea CLI config to extract Gitea host + token.
|
||||
// Package tea reads the tea CLI config to find the Gitea login, and its token, for a given host.
|
||||
//
|
||||
// tea config typically lives at $HOME/.config/tea/config.yml. Older versions
|
||||
// of tea (<v0.10) stored the token inline in the YAML; newer versions move
|
||||
// tokens into a system keyring and only keep metadata in the file.
|
||||
//
|
||||
// We support the inline form here (matches the spx task contract). If the
|
||||
// token field is missing, callers should consider falling back to a
|
||||
// GITEA_TOKEN environment variable and emit a clear error otherwise.
|
||||
// We support the inline form here. If the token field is missing, callers
|
||||
// fall back to the GITEA_TOKEN environment variable.
|
||||
package tea
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -53,52 +55,25 @@ func load(path string) (*TeaConfig, error) {
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
// LoadDefault returns host and token from the default login entry.
|
||||
// LoginForHost returns the login in the tea config at `path` whose URL points at `host`, or `nil` when none does.
|
||||
//
|
||||
// If multiple logins have default=true, the first wins. If no entry is marked
|
||||
// default, the first entry is used.
|
||||
func LoadDefault() (host, token string, err error) {
|
||||
path, err := DefaultConfigPath()
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
// Hosts are compared by hostname only, ignoring case, scheme, port and trailing slashes, so the host read from a git
|
||||
// SSH remote still finds the login saved with the forge's web URL. A missing config file is not an error either,
|
||||
// because the caller can still fall back to `GITEA_TOKEN`.
|
||||
func LoginForHost(path, host string) (*TeaLogin, error) {
|
||||
cfg, err := load(path)
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
return nil, err
|
||||
}
|
||||
if len(cfg.Logins) == 0 {
|
||||
return "", "", fmt.Errorf("tea config %s 里没有 logins 条目,请先运行 `tea login add`", path)
|
||||
}
|
||||
var chosen *TeaLogin
|
||||
want := strings.ToLower(host)
|
||||
for i := range cfg.Logins {
|
||||
if cfg.Logins[i].Default {
|
||||
chosen = &cfg.Logins[i]
|
||||
break
|
||||
u, perr := url.Parse(strings.TrimSpace(cfg.Logins[i].URL))
|
||||
if perr == nil && strings.ToLower(u.Hostname()) == want {
|
||||
return &cfg.Logins[i], nil
|
||||
}
|
||||
}
|
||||
if chosen == nil {
|
||||
chosen = &cfg.Logins[0]
|
||||
}
|
||||
return chosen.URL, chosen.Token, nil
|
||||
}
|
||||
|
||||
// LoadByHost returns the token for the login whose URL matches host.
|
||||
//
|
||||
// Host matching is case-insensitive and ignores trailing slashes.
|
||||
func LoadByHost(host string) (token string, err error) {
|
||||
path, err := DefaultConfigPath()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
cfg, err := load(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
want := strings.TrimRight(strings.ToLower(host), "/")
|
||||
for _, l := range cfg.Logins {
|
||||
if strings.TrimRight(strings.ToLower(l.URL), "/") == want {
|
||||
return l.Token, nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("tea config 里没找到 host=%s 的 login", host)
|
||||
return nil, nil
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package tea
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func writeConfig(t *testing.T, content string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "config.yml")
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatalf("写 tea config 失败: %v", err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
const sampleConfig = `logins:
|
||||
- name: local
|
||||
url: http://localhost:3000
|
||||
token: tok-local
|
||||
default: true
|
||||
- name: work
|
||||
url: https://Gitea.Example.com/
|
||||
token: tok-work
|
||||
`
|
||||
|
||||
func TestLoginForHostMatchesByHostname(t *testing.T) {
|
||||
path := writeConfig(t, sampleConfig)
|
||||
cases := map[string]string{
|
||||
"gitea.example.com": "tok-work",
|
||||
"GITEA.example.com": "tok-work",
|
||||
"localhost": "tok-local",
|
||||
}
|
||||
for host, wantToken := range cases {
|
||||
l, err := LoginForHost(path, host)
|
||||
if err != nil {
|
||||
t.Fatalf("LoginForHost(%s) 出错: %v", host, err)
|
||||
}
|
||||
if l == nil || l.Token != wantToken {
|
||||
t.Fatalf("LoginForHost(%s) = %+v, want token %s", host, l, wantToken)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginForHostIgnoresDefaultLoginOfOtherHost(t *testing.T) {
|
||||
path := writeConfig(t, sampleConfig)
|
||||
l, err := LoginForHost(path, "git.other.org")
|
||||
if err != nil {
|
||||
t.Fatalf("没匹配上不应报错: %v", err)
|
||||
}
|
||||
if l != nil {
|
||||
t.Fatalf("host 不匹配时不应回退到默认 login,得到 %+v", l)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginForHostMissingConfigIsNotAnError(t *testing.T) {
|
||||
l, err := LoginForHost(filepath.Join(t.TempDir(), "absent.yml"), "gitea.example.com")
|
||||
if err != nil || l != nil {
|
||||
t.Fatalf("没有 tea config 时应返回 (nil, nil),得到 (%+v, %v)", l, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginForHostBadYAMLIsAnError(t *testing.T) {
|
||||
path := writeConfig(t, "logins: [this is: not valid")
|
||||
if _, err := LoginForHost(path, "gitea.example.com"); err == nil {
|
||||
t.Fatal("tea config 解析失败应当报错")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user