🐛 fix(spx): GITEA_TOKEN 只发给 tea 登录过的 host,origin 不在 tea 里时直接报错
This commit is contained in:
1 parent
e6d086d821
commit
9c7c9d8636
6 files changed
+43
-25
No files matched your search
+1
-1
@@ -60,7 +60,7 @@
|
|||||||
cd cli && make install # 编译到 bin/spx 并安装到 ~/.local/bin/(这个目录要在 PATH 里)
|
cd cli && make install # 编译到 bin/spx 并安装到 ~/.local/bin/(这个目录要在 PATH 里)
|
||||||
```
|
```
|
||||||
|
|
||||||
`spx` 按当前目录 git origin 的 host 选仓库:`github.com` 走 GitHub,凭据取 `gh auth token`;其余走 Gitea,凭据取 tea 配置里 host 匹配的 login,没有时回退环境变量 `GITEA_TOKEN`(tea 把 token 存在系统钥匙串里时就要设它)。
|
`spx` 按当前目录 git origin 的 host 选仓库:`github.com` 走 GitHub,凭据取 `gh auth token`;其余走 Gitea,凭据取 tea 配置里 host 匹配的 login,没有就报错,要先 `tea login add` 这个 host;login 里没存 token 时(tea 把 token 存在系统钥匙串里)用环境变量 `GITEA_TOKEN` 补上,`GITEA_TOKEN` 不会发给 tea 没登录过的 host。
|
||||||
|
|
||||||
| 命令 | 作用 |
|
| 命令 | 作用 |
|
||||||
|---|---|
|
|---|---|
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ spx 读 `git remote get-url origin`,按 host 决定连哪种仓库(`--host`
|
|||||||
| origin host | 仓库 | token 来源 |
|
| origin host | 仓库 | token 来源 |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| `github.com` | GitHub(API `https://api.github.com`) | `gh auth token --hostname github.com`;取不到就先运行 `gh auth login` |
|
| `github.com` | GitHub(API `https://api.github.com`) | `gh auth token --hostname github.com`;取不到就先运行 `gh auth login` |
|
||||||
| 其它任何 host | Gitea(API `<host>/api/v1`) | [`tea`](https://gitea.com/gitea/tea) 配置 `~/.config/tea/config.yml` 里 URL 与 origin 同 host 的 login;没有该 login 或 token 为空时回退环境变量 `GITEA_TOKEN` |
|
| 其它任何 host | Gitea(API `<host>/api/v1`) | [`tea`](https://gitea.com/gitea/tea) 配置 `~/.config/tea/config.yml` 里 URL 与 origin 同 host 的 login;该 login 的 token 为空时用环境变量 `GITEA_TOKEN` 补上;tea 里没有这个 host 的 login 就报错,要先运行 `tea login add` |
|
||||||
|
|
||||||
tea 用 keyring 存 token(v0.10+ 默认行为)时,可以设 `GITEA_TOKEN` 作为回退:
|
tea 用 keyring 存 token(v0.10+ 默认行为)时,配置文件里的 login 没有 token,可以设 `GITEA_TOKEN` 补上。`GITEA_TOKEN` 只会发给 tea 里登录过的 host,origin 指向别的 host 时 spx 直接报错,不会把它发出去:
|
||||||
|
|
||||||
```
|
```
|
||||||
export GITEA_TOKEN=...
|
export GITEA_TOKEN=...
|
||||||
|
|||||||
@@ -111,7 +111,8 @@ func buildRootCmd(open forgeOpener) *cobra.Command {
|
|||||||
Short: "superpowers-vscode 工作流 CLI(Gitea / GitHub 薄包装)",
|
Short: "superpowers-vscode 工作流 CLI(Gitea / GitHub 薄包装)",
|
||||||
Long: "spx 为 cc/codex agent 在 superpowers-vscode 工作流里提供确定性的工单 / PR 操作。\n" +
|
Long: "spx 为 cc/codex agent 在 superpowers-vscode 工作流里提供确定性的工单 / PR 操作。\n" +
|
||||||
"按 git origin 的 host 选仓库:github.com 走 GitHub(token 取自 `gh auth token`),\n" +
|
"按 git origin 的 host 选仓库:github.com 走 GitHub(token 取自 `gh auth token`),\n" +
|
||||||
"其它 host 走 Gitea(token 取自 ~/.config/tea/config.yml 里同 host 的 login,回退 GITEA_TOKEN)。",
|
"其它 host 走 Gitea(必须先 `tea login add` 过这个 host,token 取自 ~/.config/tea/config.yml 里同 host 的 login,\n" +
|
||||||
|
"该 login 没存 token 时才用 GITEA_TOKEN 补上;GITEA_TOKEN 不会发给 tea 没登录过的 host)。",
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
SilenceErrors: false,
|
SilenceErrors: false,
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
|||||||
@@ -51,8 +51,9 @@ func ghAuthToken(host string) (string, error) {
|
|||||||
|
|
||||||
// Open connects to the repo at `o` on the forge its host names, picking credentials the way that forge expects.
|
// Open connects to the repo at `o` on the forge its host names, picking credentials the way that forge expects.
|
||||||
//
|
//
|
||||||
// GitHub takes the token from `gh auth token`. Gitea takes the token of the tea login for the same host, and its
|
// GitHub takes the token from `gh auth token`. Gitea needs a [tea.TeaLogin] for the same host and uses its URL as the API
|
||||||
// URL as the API root; with no such login, or one without a token, it falls back to `GITEA_TOKEN` and the origin's web root.
|
// root and its token; `GITEA_TOKEN` only fills in when that login has no token, so the variable never reaches a host the
|
||||||
|
// user has not logged in to with tea.
|
||||||
func Open(o repo.Origin, env Env) (Forge, error) {
|
func Open(o repo.Origin, env Env) (Forge, error) {
|
||||||
if KindForHost(o.Host) == KindGitHub {
|
if KindForHost(o.Host) == KindGitHub {
|
||||||
token, err := env.GitHubToken(o.Host)
|
token, err := env.GitHubToken(o.Host)
|
||||||
@@ -69,15 +70,15 @@ func Open(o repo.Origin, env Env) (Forge, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
baseURL, token := o.WebURL, ""
|
if login == nil {
|
||||||
if login != nil {
|
return nil, fmt.Errorf("tea config 里没有 %s 的 login,spx 不会把 Gitea token 发给 tea 没登录过的 host;请先运行 `tea login add` 登录这个 host", o.Host)
|
||||||
baseURL, token = login.URL, login.Token
|
|
||||||
}
|
}
|
||||||
|
token := login.Token
|
||||||
if token == "" {
|
if token == "" {
|
||||||
token = env.Getenv("GITEA_TOKEN")
|
token = env.Getenv("GITEA_TOKEN")
|
||||||
}
|
}
|
||||||
if token == "" {
|
if token == "" {
|
||||||
return nil, fmt.Errorf("没有 %s 的 Gitea token:tea config 里没有该 host 的 login 或其 token 为空,GITEA_TOKEN 也没设置;请运行 `tea login add` 或设置 GITEA_TOKEN", o.Host)
|
return nil, fmt.Errorf("tea config 里 %s 的 login %q 没有 token,GITEA_TOKEN 也没设置;请重新运行 `tea login add` 或设置 GITEA_TOKEN", o.Host, login.Name)
|
||||||
}
|
}
|
||||||
return NewGitea(baseURL, token, o.Owner, o.Repo), nil
|
return NewGitea(login.URL, token, o.Owner, o.Repo), nil
|
||||||
}
|
}
|
||||||
@@ -17,6 +17,7 @@ type fakeEnv struct {
|
|||||||
env map[string]string
|
env map[string]string
|
||||||
ghAsked []string
|
ghAsked []string
|
||||||
teaAsked []string
|
teaAsked []string
|
||||||
|
envAsked []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeEnv) Env() Env {
|
func (f *fakeEnv) Env() Env {
|
||||||
@@ -29,7 +30,10 @@ func (f *fakeEnv) Env() Env {
|
|||||||
f.teaAsked = append(f.teaAsked, host)
|
f.teaAsked = append(f.teaAsked, host)
|
||||||
return f.logins[host], nil
|
return f.logins[host], nil
|
||||||
},
|
},
|
||||||
Getenv: func(key string) string { return f.env[key] },
|
Getenv: func(key string) string {
|
||||||
|
f.envAsked = append(f.envAsked, key)
|
||||||
|
return f.env[key]
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,22 +137,34 @@ func TestOpenGiteaFallsBackToEnvTokenWhenLoginHasNone(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOpenGiteaWithoutLoginUsesOriginWebURLAndEnvToken(t *testing.T) {
|
func TestOpenGiteaWithoutLoginForOriginHostNeverReadsEnvToken(t *testing.T) {
|
||||||
fe := &fakeEnv{env: map[string]string{"GITEA_TOKEN": "env-tok"}}
|
fe := &fakeEnv{
|
||||||
f, err := Open(giteaOrigin(), fe.Env())
|
logins: map[string]*tea.TeaLogin{
|
||||||
if err != nil {
|
"gitea.example.com": {Name: "work", URL: "https://gitea.example.com", Token: "tea-tok"},
|
||||||
t.Fatalf("Open 出错: %v", err)
|
},
|
||||||
|
env: map[string]string{"GITEA_TOKEN": "env-tok"},
|
||||||
}
|
}
|
||||||
rf := f.(*restForge)
|
o := repo.Origin{Host: "gitlab.com", WebURL: "https://gitlab.com", Owner: "acme", Repo: "widgets"}
|
||||||
if rf.apiBase != "https://gitea.example.com/api/v1" || rf.header.Get("Authorization") != "token env-tok" {
|
f, err := Open(o, fe.Env())
|
||||||
t.Fatalf("没有 tea login 时应用 origin 地址 + GITEA_TOKEN: base=%s auth=%q", rf.apiBase, rf.header.Get("Authorization"))
|
if err == nil || f != nil {
|
||||||
|
t.Fatalf("tea 里没有 origin host 的 login 时应拒绝连接,得到 forge=%v err=%v", f, err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "tea login add") || !strings.Contains(err.Error(), "gitlab.com") {
|
||||||
|
t.Fatalf("错误应点名 host 并提示先 tea login add,得到 %v", err)
|
||||||
|
}
|
||||||
|
if len(fe.envAsked) != 0 {
|
||||||
|
t.Fatalf("没有同 host 的 login 时不能读 GITEA_TOKEN,免得把它发给别的网站: %v", fe.envAsked)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOpenGiteaWithoutAnyTokenFails(t *testing.T) {
|
func TestOpenGiteaFailsWhenLoginAndEnvHaveNoToken(t *testing.T) {
|
||||||
fe := &fakeEnv{}
|
fe := &fakeEnv{
|
||||||
|
logins: map[string]*tea.TeaLogin{
|
||||||
|
"gitea.example.com": {Name: "work", URL: "https://gitea.example.com", Token: ""},
|
||||||
|
},
|
||||||
|
}
|
||||||
_, err := Open(giteaOrigin(), fe.Env())
|
_, err := Open(giteaOrigin(), fe.Env())
|
||||||
if err == nil || !strings.Contains(err.Error(), "GITEA_TOKEN") || !strings.Contains(err.Error(), "gitea.example.com") {
|
if err == nil || !strings.Contains(err.Error(), "GITEA_TOKEN") || !strings.Contains(err.Error(), "gitea.example.com") {
|
||||||
t.Fatalf("没有 token 时应报错并点名 host 与 GITEA_TOKEN,得到 %v", err)
|
t.Fatalf("login 和 GITEA_TOKEN 都没有 token 时应报错并点名 host 与 GITEA_TOKEN,得到 %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,8 +59,8 @@ func load(path string) (*TeaConfig, error) {
|
|||||||
//
|
//
|
||||||
// Hosts are compared by hostname only, ignoring case, scheme, port and trailing slashes, so the host read from a git
|
// 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. When several logins point at `host`, the one marked
|
// SSH remote still finds the login saved with the forge's web URL. When several logins point at `host`, the one marked
|
||||||
// `default` wins, because that is the identity the user picked; otherwise the first. A missing config file is not an
|
// `default` wins, because that is the identity the user picked; otherwise the first. A missing config file just means
|
||||||
// error either, because the caller can still fall back to `GITEA_TOKEN`.
|
// there is no login, so it returns `nil` rather than an error.
|
||||||
func LoginForHost(path, host string) (*TeaLogin, error) {
|
func LoginForHost(path, host string) (*TeaLogin, error) {
|
||||||
cfg, err := load(path)
|
cfg, err := load(path)
|
||||||
if errors.Is(err, fs.ErrNotExist) {
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
|
|||||||
Reference in new issue
Block a user