diff --git a/README-zh.md b/README-zh.md new file mode 100644 index 00000000..676efada --- /dev/null +++ b/README-zh.md @@ -0,0 +1,306 @@ +

+ skillshare +

+ +

skillshare

+ +

+ 网站 + License: MIT + 发布版本 + 平台 + Go 质量报告 + DeepWiki 提问 +

+ +

+ 在 GitHub 上点亮 Star +

+ +

+ runkids%2Fskillshare | Trendshift +

+ +

+ AI CLI 技能(Skills)、智能体(Agents)、规则(Rules)、命令(Commands)等资源的唯一事实来源。
+ 一键同步到所有平台——从个人到组织级全覆盖。
+ 支持 Codex、Claude Code、OpenClaw、OpenCode 及 60+ 更多工具。 +

+ +

+ skillshare 演示 +

+ +

+ 官网 • + 安装 • + 快速开始 • + 亮点功能 • + 截图预览 • + 文档 +

+ +> [!NOTE] +> **最新版本**: [v0.20.0](https://github.com/runkids/skillshare/releases/tag/v0.20.0) — 通过 git_root scope 选择 commit/push/pull 的版本范围(skills、agents、extras,或全部合并在一个仓库中);extras 扩展转换在同步时将 Markdown 转换为原生格式(Gemini TOML 命令、Codex TOML 智能体)。[查看全部版本 →](https://github.com/runkids/skillshare/releases) + +## 为什么选择 skillshare + +每个 AI CLI 都有自己的技能目录。 +你在一个工具里编辑了技能,却忘了复制到另一个,最后记不清哪个在哪里。 + +skillshare 解决了这个问题: + +- **单一来源,覆盖所有智能体** — 一条 `skillshare sync` 命令同步到 Claude、Cursor、Codex 及 60+ 工具 +- **智能体管理** — 将自定义智能体与技能一起同步到支持智能体的目标端 +- **不止于技能** — 使用 [extras](https://skillshare.runkids.cc/docs/reference/targets/configuration#extras) 管理规则、命令、提示词及任何基于文件的资源 +- **从任何地方安装** — GitHub、GitLab、Bitbucket、Azure DevOps 或任何自托管的 Git 仓库 +- **内置安全** — 在使用前审计技能是否存在提示注入和数据泄露风险 +- **团队就绪** — 项目中通过 `.skillshare/` 管理技能,组织级技能通过代码仓库同步 +- **本地轻量** — 单一二进制文件,无需注册中心,无遥测,完全支持离线使用 +- **细粒度过滤** — 通过 [`.skillignore`](https://skillshare.runkids.cc/docs/how-to/daily-tasks/filtering-skills)、SKILL.md 中的 `targets` 字段以及按目标端的 include/exclude 配置,精确控制哪些技能同步到哪些目标端 + +> 从其他工具迁移?[迁移指南](https://skillshare.runkids.cc/docs/how-to/advanced/migration) · [功能对比](https://skillshare.runkids.cc/docs/understand/philosophy/comparison) + +## 工作原理 + +- macOS / Linux:`~/.config/skillshare/` +- Windows:`%AppData%\skillshare\` + +``` +┌─────────────────────────────────────────────────────────────┐ +│ 源目录 │ +│ ~/.config/skillshare/skills/ ← 技能(SKILL.md) │ +│ ~/.config/skillshare/agents/ ← 智能体 │ +│ ~/.config/skillshare/extras/ ← 规则、命令等 │ +└─────────────────────────────────────────────────────────────┘ + │ sync + ┌───────────────┼───────────────┐ + ▼ ▼ ▼ + ┌───────────┐ ┌───────────┐ ┌───────────┐ + │ Claude │ │ OpenCode │ │ OpenClaw │ ... + └───────────┘ └───────────┘ └───────────┘ +``` + +| 平台 | 技能源目录 | 智能体源目录 | 扩展资源源目录 | 链接方式 | +|----------|---------------|---------------|---------------|-----------| +| macOS/Linux | `~/.config/skillshare/skills/` | `~/.config/skillshare/agents/` | `~/.config/skillshare/extras/` | 符号链接 | +| Windows | `%AppData%\skillshare\skills\` | `%AppData%\skillshare\agents\` | `%AppData%\skillshare\extras\` | NTFS 交接点(无需管理员权限) | + +| | 命令式(逐命令安装) | 声明式(skillshare) | +|---|---|---| +| **事实来源** | 技能各自独立复制 | 单一来源 → 符号链接(或复制) | +| **新机器配置** | 重新手动执行每次安装 | `git clone` 配置 + `sync` | +| **安全审计** | 无 | 内置 `audit` + 安装/更新时自动扫描 | +| **Web 仪表盘** | 无 | `skillshare ui` | +| **运行时依赖** | Node.js + npm | 无(单一 Go 二进制文件) | + +> [完整对比 →](https://skillshare.runkids.cc/docs/understand/philosophy/comparison) + +## CLI 和 UI 预览 + +| 技能详细页 | 安全审计 | +|---|---| +| CLI 同步输出 | CLI 安装附带安全审计 | + +| UI 仪表盘 | UI 技能列表 | +|---|---| +| Web 仪表盘概览 | Web UI 技能页面 | + +## 安装 + +### macOS / Linux + +```bash +curl -fsSL https://raw.githubusercontent.com/runkids/skillshare/main/install.sh | sh +``` + +### Windows PowerShell + +```powershell +irm https://raw.githubusercontent.com/runkids/skillshare/main/install.ps1 | iex +``` + +### Homebrew + +```bash +brew install skillshare +``` + +> **提示:** 运行 `skillshare upgrade` 即可更新到最新版本。它会自动检测你的安装方式并完成后续操作。 + +### GitHub Actions + +```yaml +- uses: runkids/setup-skillshare@v1 + with: + source: ./skills +- run: skillshare sync +``` + +查看 [`setup-skillshare`](https://github.com/marketplace/actions/setup-skillshare) 获取所有选项(审计、项目模式、版本锁定等)。 + +### 缩写别名(可选) + +在 shell 配置(`~/.zshrc` 或 `~/.bashrc`)中添加别名: + +```bash +alias ss='skillshare' +``` + +## 快速开始 + +```bash +skillshare init # 创建配置、源目录并检测目标端 +skillshare sync # 将技能同步到所有目标端 +``` + +## 亮点功能 + +**安装和更新技能** — 从 GitHub、GitLab 或任何 Git 仓库 + +```bash +skillshare install github.com/reponame/skills +skillshare update --all +skillshare target claude --mode copy # 如果符号链接不适用 +``` + +**符号链接有问题?** — 为每个目标端切换到复制模式 + +```bash +skillshare target <名称> --mode copy +skillshare sync +``` + +**安全审计** — 在技能到达智能体之前进行扫描 + +```bash +skillshare audit +``` + +**项目级技能** — 按仓库管理,随代码一起提交 + +```bash +skillshare init -p && skillshare sync +``` + +**智能体** — 将自定义智能体同步到支持智能体的目标端 + +```bash +skillshare sync agents # 仅同步智能体 +skillshare sync --all # 同步技能 + 智能体 + 扩展资源 +``` + +**扩展资源** — 管理规则、命令、提示词等 + +```bash +skillshare extras init rules # 创建一个 "rules" 扩展 +skillshare sync --all # 同步技能 + 扩展资源 +skillshare extras collect rules # 将本地文件收集回源目录 +``` + +**Shell 自动补全** — Tab 键补全命令、标志和子命令 + +```bash +skillshare completion bash --install # 也支持:zsh、fish、powershell、nushell +``` + +**本地检查点** — 提交源目录变更而不推送 + +```bash +skillshare commit -m "更新审查技能" +skillshare commit --dry-run +``` + +**Web 仪表盘** — 可视化控制面板 + +```bash +skillshare ui +``` + +[所有命令和指南 →](https://skillshare.runkids.cc/docs/reference/commands) + +## 参与贡献 + +欢迎贡献!请先提交 issue,然后提交带测试的草稿 PR。 +查看 [CONTRIBUTING.md](CONTRIBUTING.md) 了解开发环境设置。 + +```bash +git clone https://github.com/runkids/skillshare.git && cd skillshare +make check # 格式化 + 代码检查 + 测试 +``` + +> [!TIP] +> 不知道从哪里开始?浏览 [open issues](https://github.com/runkids/skillshare/issues) 或尝试 [Playground](https://skillshare.runkids.cc/docs/learn/with-playground) 获取零配置开发环境。 + +## 贡献者 + +感谢所有帮助 skillshare 变得更好的人。 + +leeeezx +Vergil333 +romanr +xocasdashdash +philippe-granet +terranc +benrfairless +nerveband +EarthChen +gdm257 +skovtunenko +TyceHerrman +1am2syman +thealokkr +JasonLandbridge +masonc15 +richardwhatever +reneleonhardt +ndeybach +hhh2210 +leoarry +salmonumbrella +daylamtayari +dstotijn +ipruning +massukio +kevincobain2000 +StephenPAdams +mk-imagine +Curtion +amdoi7 +jessica-engel +AlimuratYusup +thor-shuang +bishopmatthew +chaosky +iFwu +ildunari +aestilog +xarthurx +m0cun +bit3125 +eekryuos +Bongseop-Kim +sophodex +PeterTianbuhan +dotned +ismferd +jblackburn21 +jnhu76 +jacobleft +rhysmcneill +druellan +12britz + +--- + +如果 skillshare 对你有帮助,不妨点个 ⭐ 支持一下 + +## Star 历史 + +[![Star 历史图](https://api.star-history.com/svg?repos=runkids/skillshare&type=date&legend=top-left)](https://www.star-history.com/#runkids/skillshare&type=date&legend=top-left) + +--- + +## 许可证 + +MIT diff --git a/cmd/skillshare/install.go b/cmd/skillshare/install.go index 88f6a044..33840aae 100644 --- a/cmd/skillshare/install.go +++ b/cmd/skillshare/install.go @@ -252,6 +252,8 @@ func parseOptsFromConfig(cfg *config.Config) install.ParseOptions { return install.ParseOptions{ GitLabHosts: cfg.EffectiveGitLabHosts(), AzureHosts: cfg.EffectiveAzureHosts(), + CNBHosts: cfg.EffectiveCNBHosts(), + GiteaHosts: cfg.EffectiveGiteaHosts(), } } @@ -260,6 +262,8 @@ func parseOptsFromProjectConfig(cfg *config.ProjectConfig) install.ParseOptions return install.ParseOptions{ GitLabHosts: cfg.EffectiveGitLabHosts(), AzureHosts: cfg.EffectiveAzureHosts(), + CNBHosts: cfg.EffectiveCNBHosts(), + GiteaHosts: cfg.EffectiveGiteaHosts(), } } diff --git a/cmd/skillshare/install_context.go b/cmd/skillshare/install_context.go index 54399489..2c794e8e 100644 --- a/cmd/skillshare/install_context.go +++ b/cmd/skillshare/install_context.go @@ -65,6 +65,8 @@ func (g *globalInstallContext) PostInstallSkill(string) error { return nil } func (g *globalInstallContext) Mode() string { return "global" } func (g *globalInstallContext) GitLabHosts() []string { return g.cfg.EffectiveGitLabHosts() } func (g *globalInstallContext) AzureHosts() []string { return g.cfg.EffectiveAzureHosts() } +func (g *globalInstallContext) CNBHosts() []string { return g.cfg.EffectiveCNBHosts() } +func (g *globalInstallContext) GiteaHosts() []string { return g.cfg.EffectiveGiteaHosts() } // --------------------------------------------------------------------------- // projectInstallContext @@ -107,3 +109,9 @@ func (p *projectInstallContext) GitLabHosts() []string { func (p *projectInstallContext) AzureHosts() []string { return p.runtime.config.EffectiveAzureHosts() } +func (p *projectInstallContext) CNBHosts() []string { + return p.runtime.config.EffectiveCNBHosts() +} +func (p *projectInstallContext) GiteaHosts() []string { + return p.runtime.config.EffectiveGiteaHosts() +} diff --git a/internal/config/config.go b/internal/config/config.go index cda2cb23..f8dae204 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -288,6 +288,8 @@ type Config struct { TUI *bool `yaml:"tui,omitempty"` // nil = default true GitLabHosts []string `yaml:"gitlab_hosts,omitempty"` AzureHosts []string `yaml:"azure_hosts,omitempty"` + CNBHosts []string `yaml:"cnb_hosts,omitempty"` + GiteaHosts []string `yaml:"gitea_hosts,omitempty"` // PreserveTildeOnSave folds $HOME prefixes back to ~ when serializing the // config to YAML. Useful when the config is shared via dotfiles across @@ -430,6 +432,16 @@ func (c *Config) EffectiveAzureHosts() []string { return mergeAzureHostsFromEnv(c.AzureHosts) } +// EffectiveCNBHosts returns CNBHosts merged with SKILLSHARE_CNB_HOSTS env var. +func (c *Config) EffectiveCNBHosts() []string { + return mergeCNBHostsFromEnv(c.CNBHosts) +} + +// EffectiveGiteaHosts returns GiteaHosts merged with SKILLSHARE_GITEA_HOSTS env var. +func (c *Config) EffectiveGiteaHosts() []string { + return mergeGiteaHostsFromEnv(c.GiteaHosts) +} + // IsTUIEnabled reports whether interactive TUI is enabled. // nil (absent from config) is treated as true for backward compatibility. func (c *Config) IsTUIEnabled() bool { @@ -571,6 +583,20 @@ func Load() (*Config, error) { } cfg.AzureHosts = azureHosts + // Validate and normalize cnb_hosts + cnbHosts, err := normalizeCNBHosts(cfg.CNBHosts) + if err != nil { + return nil, err + } + cfg.CNBHosts = cnbHosts + + // Validate and normalize gitea_hosts + giteaHosts, err := normalizeGiteaHosts(cfg.GiteaHosts) + if err != nil { + return nil, err + } + cfg.GiteaHosts = giteaHosts + // Migrate legacy flat target fields to skills: sub-key (one-time, persisted immediately) if migrateTargetConfigs(cfg.Targets) { if data, err := marshalYAML(&cfg); err == nil { @@ -830,6 +856,14 @@ func normalizeAzureHosts(hosts []string) ([]string, error) { return normalizeHostList(hosts, "azure_hosts") } +func normalizeCNBHosts(hosts []string) ([]string, error) { + return normalizeHostList(hosts, "cnb_hosts") +} + +func normalizeGiteaHosts(hosts []string) ([]string, error) { + return normalizeHostList(hosts, "gitea_hosts") +} + // mergeHostsFromEnv merges comma-separated env var entries with config file hosts. // Invalid entries in the env var are silently skipped. func mergeHostsFromEnv(configHosts []string, envKey string) []string { @@ -863,6 +897,14 @@ func mergeAzureHostsFromEnv(configHosts []string) []string { return mergeHostsFromEnv(configHosts, "SKILLSHARE_AZURE_HOSTS") } +func mergeCNBHostsFromEnv(configHosts []string) []string { + return mergeHostsFromEnv(configHosts, "SKILLSHARE_CNB_HOSTS") +} + +func mergeGiteaHostsFromEnv(configHosts []string) []string { + return mergeHostsFromEnv(configHosts, "SKILLSHARE_GITEA_HOSTS") +} + func normalizeAuditBlockThreshold(v string) (string, error) { threshold := strings.ToUpper(strings.TrimSpace(v)) if threshold == "" { diff --git a/internal/config/project.go b/internal/config/project.go index 926bc180..ef32dc79 100644 --- a/internal/config/project.go +++ b/internal/config/project.go @@ -273,6 +273,8 @@ type ProjectConfig struct { Hub HubConfig `yaml:"hub,omitempty"` GitLabHosts []string `yaml:"gitlab_hosts,omitempty"` AzureHosts []string `yaml:"azure_hosts,omitempty"` + CNBHosts []string `yaml:"cnb_hosts,omitempty"` + GiteaHosts []string `yaml:"gitea_hosts,omitempty"` } // EffectiveSkillsSource returns the resolved skills source directory. @@ -339,6 +341,16 @@ func (c *ProjectConfig) EffectiveAzureHosts() []string { return mergeAzureHostsFromEnv(c.AzureHosts) } +// EffectiveCNBHosts returns CNBHosts merged with SKILLSHARE_CNB_HOSTS env var. +func (c *ProjectConfig) EffectiveCNBHosts() []string { + return mergeCNBHostsFromEnv(c.CNBHosts) +} + +// EffectiveGiteaHosts returns GiteaHosts merged with SKILLSHARE_GITEA_HOSTS env var. +func (c *ProjectConfig) EffectiveGiteaHosts() []string { + return mergeGiteaHostsFromEnv(c.GiteaHosts) +} + // ProjectConfigPath returns the project config path for the given root. func ProjectConfigPath(projectRoot string) string { return filepath.Join(projectRoot, ".skillshare", "config.yaml") @@ -381,6 +393,20 @@ func LoadProject(projectRoot string) (*ProjectConfig, error) { } cfg.AzureHosts = azureHosts + // Validate and normalize cnb_hosts + cnbHosts, err := normalizeCNBHosts(cfg.CNBHosts) + if err != nil { + return nil, fmt.Errorf("project config: %w", err) + } + cfg.CNBHosts = cnbHosts + + // Validate and normalize gitea_hosts + giteaHosts, err := normalizeGiteaHosts(cfg.GiteaHosts) + if err != nil { + return nil, fmt.Errorf("project config: %w", err) + } + cfg.GiteaHosts = giteaHosts + for _, target := range cfg.Targets { if strings.TrimSpace(target.Name) == "" { return nil, fmt.Errorf("project config has target with empty name") diff --git a/internal/install/auth.go b/internal/install/auth.go index a699f493..809167ed 100644 --- a/internal/install/auth.go +++ b/internal/install/auth.go @@ -17,6 +17,8 @@ const ( PlatformGitLab // gitlab.com and self-hosted GitLab PlatformBitbucket // bitbucket.org PlatformAzureDevOps // dev.azure.com and visualstudio.com + PlatformCNB // cnb.cool and self-hosted CNB instances + PlatformGitea // gitea.com and self-hosted Gitea instances ) // extractHost returns the hostname from a clone URL. @@ -62,6 +64,12 @@ func detectPlatform(cloneURL string) Platform { if host == "dev.azure.com" || host == "ssh.dev.azure.com" || strings.HasSuffix(host, ".visualstudio.com") { return PlatformAzureDevOps } + if strings.Contains(host, "cnb.cool") { + return PlatformCNB + } + if strings.Contains(host, "gitea") { + return PlatformGitea + } return PlatformUnknown } @@ -70,11 +78,15 @@ func detectPlatform(cloneURL string) Platform { // SKILLSHARE_GIT_TOKEN. Returns empty strings if no token is available or // the URL is not HTTPS. func resolveToken(cloneURL string) (token, username string) { + return resolveTokenWithOptions(cloneURL, nil, nil) +} + +func resolveTokenWithOptions(cloneURL string, cnbHosts, giteaHosts []string) (token, username string) { if !isHTTPS(cloneURL) { return "", "" } - platform := detectPlatform(cloneURL) + platform := detectPlatformWithOptions(cloneURL, cnbHosts, giteaHosts) switch platform { case PlatformGitHub: if t := os.Getenv("GITHUB_TOKEN"); t != "" { @@ -98,6 +110,14 @@ func resolveToken(cloneURL string) (token, username string) { if t := os.Getenv("AZURE_DEVOPS_TOKEN"); t != "" { return t, "x-access-token" } + case PlatformCNB: + if t := os.Getenv("CNB_TOKEN"); t != "" { + return t, "cnb" + } + case PlatformGitea: + if t := os.Getenv("GITEA_TOKEN"); t != "" { + return t, "x-access-token" + } } // Generic fallback — use platform-appropriate username, or preserve @@ -126,7 +146,11 @@ func resolveToken(cloneURL string) (token, username string) { // git config entries (e.g. from CI pipelines). // Returns nil for SSH/file URLs or when no token is available. func authEnv(cloneURL string) []string { - token, username := resolveToken(cloneURL) + return authEnvWithOptions(cloneURL, nil, nil) +} + +func authEnvWithOptions(cloneURL string, cnbHosts, giteaHosts []string) []string { + token, username := resolveTokenWithOptions(cloneURL, cnbHosts, giteaHosts) if token == "" { return nil } @@ -163,6 +187,28 @@ func DetectPlatformForURL(cloneURL string) Platform { return detectPlatform(cloneURL) } +func detectPlatformWithOptions(cloneURL string, cnbHosts, giteaHosts []string) Platform { + host := strings.ToLower(extractHost(cloneURL)) + if host == "" { + return PlatformUnknown + } + return detectPlatformFromHost(host, cnbHosts, giteaHosts) +} + +func (s *Source) authEnv() []string { + if s == nil { + return nil + } + return authEnvWithOptions(s.CloneURL, s.CNBHosts, s.GiteaHosts) +} + +func (s *Source) platform() Platform { + if s == nil { + return PlatformUnknown + } + return detectPlatformWithOptions(s.CloneURL, s.CNBHosts, s.GiteaHosts) +} + // existingConfigCount returns the current GIT_CONFIG_COUNT from the // environment, or 0 if unset/invalid. func existingConfigCount() int { @@ -195,6 +241,7 @@ func sanitizeTokens(text string) string { vars := []string{ "GITHUB_TOKEN", "GH_TOKEN", "GITLAB_TOKEN", "BITBUCKET_TOKEN", "AZURE_DEVOPS_TOKEN", "SKILLSHARE_GIT_TOKEN", "BITBUCKET_USERNAME", + "CNB_TOKEN", "GITEA_TOKEN", } for _, v := range vars { if t := os.Getenv(v); t != "" { diff --git a/internal/install/auth_test.go b/internal/install/auth_test.go index f23ea766..dfe93d26 100644 --- a/internal/install/auth_test.go +++ b/internal/install/auth_test.go @@ -11,6 +11,8 @@ var authTestEnvKeys = []string{ "BITBUCKET_TOKEN", "BITBUCKET_USERNAME", "AZURE_DEVOPS_TOKEN", + "CNB_TOKEN", + "GITEA_TOKEN", "SKILLSHARE_GIT_TOKEN", "GIT_CONFIG_COUNT", } @@ -164,6 +166,21 @@ func TestResolveToken(t *testing.T) { wantTok: "tok", wantUser: "oauth2", }, + + { + name: "cnb token", + url: "https://cnb.cool/org/repo.git", + envVars: map[string]string{"CNB_TOKEN": "cnb_pat"}, + wantTok: "cnb_pat", + wantUser: "cnb", + }, + { + name: "gitea token", + url: "https://gitea.example.com/org/repo.git", + envVars: map[string]string{"GITEA_TOKEN": "gitea_pat"}, + wantTok: "gitea_pat", + wantUser: "x-access-token", + }, { name: "azure devops token", url: "https://dev.azure.com/org/proj/_git/repo", @@ -231,6 +248,61 @@ func TestResolveToken(t *testing.T) { } } +func TestSourceAuthEnv_ConfiguredCNBGiteaHosts(t *testing.T) { + tests := []struct { + name string + source *Source + envVars map[string]string + wantKey string + }{ + { + name: "configured CNB host uses CNB_TOKEN", + source: &Source{ + CloneURL: "https://git.corp.example/org/repo.git", + CNBHosts: []string{"git.corp.example"}, + }, + envVars: map[string]string{"CNB_TOKEN": "cnb_private"}, + wantKey: "url.https://cnb:cnb_private@git.corp.example/.insteadOf", + }, + { + name: "configured Gitea host uses GITEA_TOKEN", + source: &Source{ + CloneURL: "https://git.example.com/org/repo.git", + GiteaHosts: []string{"git.example.com"}, + }, + envVars: map[string]string{"GITEA_TOKEN": "gitea_private"}, + wantKey: "url.https://x-access-token:gitea_private@git.example.com/.insteadOf", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + resetAuthTestEnv(t) + for k, v := range tt.envVars { + t.Setenv(k, v) + } + got := tt.source.authEnv() + if len(got) < 2 { + t.Fatalf("authEnv() = %v, want git config env", got) + } + if got[1] != "GIT_CONFIG_KEY_0="+tt.wantKey { + t.Errorf("auth key = %q, want %q", got[1], "GIT_CONFIG_KEY_0="+tt.wantKey) + } + }) + } +} + +func TestAPISourceDetection_UsesConfiguredHosts(t *testing.T) { + cnb := &Source{CloneURL: "https://git.corp.example/org/repo.git", CNBHosts: []string{"git.corp.example"}} + if !isCNBAPISource(cnb) { + t.Fatalf("configured CNB host was not treated as CNB API source") + } + gitea := &Source{CloneURL: "https://git.example.com/org/repo.git", GiteaHosts: []string{"git.example.com"}} + if !isGiteaAPISource(gitea) { + t.Fatalf("configured Gitea host was not treated as Gitea API source") + } +} + func TestAuthEnv(t *testing.T) { tests := []struct { name string diff --git a/internal/install/context.go b/internal/install/context.go index 1d699bb3..cf1f28fe 100644 --- a/internal/install/context.go +++ b/internal/install/context.go @@ -62,6 +62,12 @@ type InstallContext interface { // AzureHosts returns extra hostnames to treat as Azure DevOps on-premises instances. AzureHosts() []string + + // CNBHosts returns extra hostnames to treat as CNB instances. + CNBHosts() []string + + // GiteaHosts returns extra hostnames to treat as Gitea instances. + GiteaHosts() []string } // ConfigInstallResult summarises the outcome of InstallFromConfig. diff --git a/internal/install/gitea_download.go b/internal/install/gitea_download.go new file mode 100644 index 00000000..f3d8c107 --- /dev/null +++ b/internal/install/gitea_download.go @@ -0,0 +1,536 @@ +package install + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "os" + "path/filepath" + "strings" + "time" +) + +type giteaContentItem struct { + Type string `json:"type"` + Name string `json:"name"` + Path string `json:"path"` + DownloadURL string `json:"download_url"` +} + +type cnbContentItem struct { + Type string `json:"type"` + Name string `json:"name"` + Path string `json:"path"` + Content string `json:"content"` + Entries []cnbContentItem `json:"entries"` +} + +type giteaCommit struct { + SHA string `json:"sha"` +} + +// isGiteaAPISource reports whether the source is a Gitea instance that supports +// the Contents API for direct file downloads. +func isGiteaAPISource(source *Source) bool { + if source == nil { + return false + } + host := strings.ToLower(extractHost(source.CloneURL)) + return isGiteaHost(host, source.GiteaHosts) +} + +// isCNBAPISource reports whether the source is a CNB instance that supports +// the git contents API documented in bin/cnb-swagger.json. +func isCNBAPISource(source *Source) bool { + if source == nil { + return false + } + host := strings.ToLower(extractHost(source.CloneURL)) + return isCNBHost(host, source.CNBHosts) +} + +// downloadGiteaDir downloads a repository subdirectory via the Gitea Contents API. +func downloadGiteaDir(owner, repo, path, destDir string, source *Source, onProgress ProgressCallback) (string, error) { + if owner == "" || repo == "" { + return "", fmt.Errorf("gitea download requires owner and repo") + } + + apiBase := giteaAPIBase(source) + if err := os.MkdirAll(destDir, 0755); err != nil { + return "", err + } + + if onProgress != nil { + onProgress("Downloading via Gitea API...") + } + + client := &http.Client{Timeout: 30 * time.Second} + if err := giteaDownloadDirRecursive(client, apiBase, owner, repo, strings.Trim(path, "/"), destDir, onProgress); err != nil { + return "", err + } + + commitHash, err := giteaFetchLatestCommitHash(apiBase, owner, repo, source) + if err != nil { + return "", nil + } + return shortHash(commitHash), nil +} + +// giteaAPIBase returns the base API URL for a Gitea instance. +func giteaAPIBase(source *Source) string { + host := strings.ToLower(extractHost(source.CloneURL)) + scheme := "https" + // For standard gitea.com, use api.gitea.com convention + // For self-hosted, use https://{host}/api/v1 + if host == "gitea.com" { + return "https://gitea.com/api/v1" + } + return fmt.Sprintf("%s://%s/api/v1", scheme, host) +} + +// giteaDownloadDirRecursive recursively downloads a directory via the Gitea Contents API. +func giteaDownloadDirRecursive(client *http.Client, apiBase, owner, repo, path, destDir string, onProgress ProgressCallback) error { + contentsURL := buildGiteaContentsURL(apiBase, owner, repo, path) + + req, err := giteaNewRequest(contentsURL) + if err != nil { + return err + } + + resp, err := client.Do(req) + if err != nil { + return fmt.Errorf("Gitea API request failed: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("Gitea contents API returned %d for %s", resp.StatusCode, path) + } + + var raw json.RawMessage + if err := json.NewDecoder(resp.Body).Decode(&raw); err != nil { + return fmt.Errorf("failed to parse Gitea contents response: %w", err) + } + + trimmed := bytes.TrimSpace(raw) + if len(trimmed) == 0 { + return fmt.Errorf("empty Gitea contents response for %q", path) + } + + // Single file response: {type, name, path, download_url, ...} + if trimmed[0] == '{' { + var item giteaContentItem + if err := json.Unmarshal(trimmed, &item); err != nil { + return err + } + if item.Type != "file" { + return fmt.Errorf("unsupported Gitea content type %q", item.Type) + } + fileName, err := giteaSanitizeName(item.Name) + if err != nil { + return err + } + target := filepath.Join(destDir, fileName) + if onProgress != nil { + onProgress(fmt.Sprintf("Downloading %s", item.Path)) + } + return giteaDownloadFile(client, item.DownloadURL, target) + } + + // Directory listing response: [{type, name, path, download_url, ...}] + if trimmed[0] == '[' { + var items []giteaContentItem + if err := json.Unmarshal(trimmed, &items); err != nil { + return err + } + for _, item := range items { + name, err := giteaSanitizeName(item.Name) + if err != nil { + return err + } + switch item.Type { + case "dir": + childDir := filepath.Join(destDir, name) + if err := os.MkdirAll(childDir, 0755); err != nil { + return err + } + if err := giteaDownloadDirRecursive(client, apiBase, owner, repo, item.Path, childDir, onProgress); err != nil { + return err + } + case "file": + target := filepath.Join(destDir, name) + if onProgress != nil { + onProgress(fmt.Sprintf("Downloading %s", item.Path)) + } + if err := giteaDownloadFile(client, item.DownloadURL, target); err != nil { + return err + } + } + } + return nil + } + + return fmt.Errorf("unexpected Gitea contents payload for %q", path) +} + +// giteaDownloadFile downloads a single file from a URL. +func giteaDownloadFile(client *http.Client, fileURL, destPath string) error { + req, err := giteaNewRequest(fileURL) + if err != nil { + return err + } + + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("download returned %d", resp.StatusCode) + } + + if err := os.MkdirAll(filepath.Dir(destPath), 0755); err != nil { + return err + } + + f, err := os.OpenFile(destPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) + if err != nil { + return err + } + defer f.Close() + + _, err = io.Copy(f, resp.Body) + return err +} + +// giteaNewRequest creates a GET request with Gitea API headers and optional +// token authentication (GITEA_TOKEN or platform-resolved token). +func giteaNewRequest(reqURL string) (*http.Request, error) { + req, err := http.NewRequest(http.MethodGet, reqURL, nil) + if err != nil { + return nil, err + } + + req.Header.Set("Accept", "application/json") + + // Try GITEA_TOKEN first, then SKILLSHARE_GIT_TOKEN + token := os.Getenv("GITEA_TOKEN") + if token == "" { + token = os.Getenv("SKILLSHARE_GIT_TOKEN") + } + if token != "" { + req.Header.Set("Authorization", "token "+token) + } + + return req, nil +} + +// giteaFetchLatestCommitHash retrieves the latest commit SHA from a Gitea repo. +func giteaFetchLatestCommitHash(apiBase, owner, repo string, source *Source) (string, error) { + commitsURL := fmt.Sprintf("%s/repos/%s/%s/commits?per_page=1", + strings.TrimRight(apiBase, "/"), url.PathEscape(owner), url.PathEscape(repo)) + + req, err := giteaNewRequest(commitsURL) + if err != nil { + return "", err + } + + client := &http.Client{Timeout: 15 * time.Second} + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + + if resp.StatusCode == http.StatusOK { + var commits []giteaCommit + if decodeErr := json.NewDecoder(resp.Body).Decode(&commits); decodeErr == nil && len(commits) > 0 { + return commits[0].SHA, nil + } + } + + // Fallback: use git ls-remote + if source != nil && source.CloneURL != "" { + return getRemoteHeadCommit(source.CloneURL) + } + + return "", fmt.Errorf("failed to fetch latest commit hash") +} + +// giteaSanitizeName validates a Gitea file/directory name. +func giteaSanitizeName(name string) (string, error) { + name = strings.TrimSpace(name) + if name == "" || name == "." || name == ".." || strings.Contains(name, "/") || strings.Contains(name, "\\") { + return "", fmt.Errorf("invalid Gitea item name %q", name) + } + return name, nil +} + +// giteaOwnerRepo extracts the owner and repo name from a Gitea clone URL. +// Clone URLs are in the format: https://host/owner/repo.git or git@host:owner/repo.git +func giteaOwnerRepo(cloneURL string) (owner, repo string) { + u := strings.TrimSpace(cloneURL) + u = strings.TrimSuffix(u, ".git") + u = strings.TrimSuffix(u, "/") + + // SSH: git@host:owner/repo + if strings.HasPrefix(u, "git@") { + colon := strings.LastIndex(u, ":") + if colon != -1 { + segments := strings.Split(strings.Trim(u[colon+1:], "/"), "/") + if len(segments) >= 2 { + return segments[0], strings.TrimSuffix(segments[1], ".git") + } + } + return "", "" + } + + // HTTPS: https://host/owner/repo + parsed, err := url.Parse(u) + if err != nil { + return "", "" + } + path := strings.Trim(parsed.Path, "/") + segments := strings.Split(path, "/") + if len(segments) >= 2 { + return segments[0], strings.TrimSuffix(segments[1], ".git") + } + return "", "" +} + +// escapeGiteaPath escapes each path segment individually for the Gitea Contents API. +// This preserves directory separators while encoding special characters in each segment. +func escapeGiteaPath(path string) string { + parts := strings.Split(path, "/") + for i := range parts { + parts[i] = url.PathEscape(parts[i]) + } + return strings.Join(parts, "/") +} + +// downloadCNBDir downloads a repository subdirectory via the CNB git contents API. +func downloadCNBDir(repo, path, destDir string, source *Source, onProgress ProgressCallback) (string, error) { + if repo == "" { + return "", fmt.Errorf("cnb download requires repo") + } + apiBase := cnbAPIBase(source) + if err := os.MkdirAll(destDir, 0755); err != nil { + return "", err + } + if onProgress != nil { + onProgress("Downloading via CNB API...") + } + client := &http.Client{Timeout: 30 * time.Second} + if err := cnbDownloadDirRecursive(client, apiBase, repo, strings.Trim(path, "/"), destDir, onProgress); err != nil { + return "", err + } + commitHash, err := cnbFetchLatestCommitHash(apiBase, repo, source) + if err != nil { + return "", nil + } + return shortCommitHash(commitHash), nil +} + +func cnbAPIBase(source *Source) string { + host := strings.ToLower(extractHost(source.CloneURL)) + return fmt.Sprintf("https://%s", host) +} + +func cnbDownloadDirRecursive(client *http.Client, apiBase, repo, path, destDir string, onProgress ProgressCallback) error { + contentsURL := buildCNBContentsURL(apiBase, repo, path) + req, err := cnbNewRequest(contentsURL) + if err != nil { + return err + } + resp, err := client.Do(req) + if err != nil { + return fmt.Errorf("CNB API request failed: %w", err) + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("CNB contents API returned %d for %s", resp.StatusCode, path) + } + var item cnbContentItem + if err := json.NewDecoder(resp.Body).Decode(&item); err != nil { + return fmt.Errorf("failed to parse CNB contents response: %w", err) + } + return cnbWriteContentItem(client, apiBase, repo, item, destDir, onProgress) +} + +func cnbWriteContentItem(client *http.Client, apiBase, repo string, item cnbContentItem, destDir string, onProgress ProgressCallback) error { + switch item.Type { + case "tree": + entries := item.Entries + for _, entry := range entries { + name, err := giteaSanitizeName(entry.Name) + if err != nil { + return err + } + target := filepath.Join(destDir, name) + switch entry.Type { + case "tree": + if err := os.MkdirAll(target, 0755); err != nil { + return err + } + if err := cnbDownloadDirRecursive(client, apiBase, repo, entry.Path, target, onProgress); err != nil { + return err + } + case "blob": + if onProgress != nil { + onProgress(fmt.Sprintf("Downloading %s", entry.Path)) + } + if err := cnbDownloadFile(client, apiBase, repo, entry.Path, target); err != nil { + return err + } + case "empty": + if err := os.MkdirAll(target, 0755); err != nil { + return err + } + case "link", "submodule": + // Ignore links and submodules; they are not portable skill content. + default: + return fmt.Errorf("unsupported CNB content type %q", entry.Type) + } + } + return nil + case "blob": + name, err := giteaSanitizeName(item.Name) + if err != nil { + return err + } + if onProgress != nil { + onProgress(fmt.Sprintf("Downloading %s", item.Path)) + } + return cnbWriteBlob(item, filepath.Join(destDir, name)) + case "empty": + return nil + } + return fmt.Errorf("unsupported CNB content type %q", item.Type) +} + +func cnbDownloadFile(client *http.Client, apiBase, repo, filePath, destPath string) error { + req, err := cnbNewRequest(buildCNBContentsURL(apiBase, repo, filePath)) + if err != nil { + return err + } + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("CNB file download returned %d", resp.StatusCode) + } + var item cnbContentItem + if err := json.NewDecoder(resp.Body).Decode(&item); err != nil { + return fmt.Errorf("failed to parse CNB file response: %w", err) + } + return cnbWriteBlob(item, destPath) +} + +func cnbWriteBlob(item cnbContentItem, destPath string) error { + if item.Type != "blob" { + return fmt.Errorf("unsupported CNB file type %q", item.Type) + } + data := []byte{} + if item.Content != "" { + decoded, decodeErr := base64.StdEncoding.DecodeString(item.Content) + if decodeErr != nil { + return fmt.Errorf("failed to decode CNB file content: %w", decodeErr) + } + data = decoded + } + if err := os.MkdirAll(filepath.Dir(destPath), 0755); err != nil { + return err + } + return os.WriteFile(destPath, data, 0644) +} + +func cnbNewRequest(reqURL string) (*http.Request, error) { + req, err := http.NewRequest(http.MethodGet, reqURL, nil) + if err != nil { + return nil, err + } + req.Header.Set("Accept", "application/vnd.cnb.api+json") + token := os.Getenv("CNB_TOKEN") + if token == "" { + token = os.Getenv("SKILLSHARE_GIT_TOKEN") + } + if token != "" { + req.Header.Set("Authorization", "Bearer "+token) + } + return req, nil +} + +func cnbFetchLatestCommitHash(apiBase, repo string, source *Source) (string, error) { + commitsURL := fmt.Sprintf("%s/%s/-/git/commits?page=1&page_size=1", strings.TrimRight(apiBase, "/"), escapeRepoPath(repo)) + req, err := cnbNewRequest(commitsURL) + if err != nil { + return "", err + } + client := &http.Client{Timeout: 15 * time.Second} + resp, err := client.Do(req) + if err == nil { + defer resp.Body.Close() + if resp.StatusCode == http.StatusOK { + var commits []giteaCommit + if decodeErr := json.NewDecoder(resp.Body).Decode(&commits); decodeErr == nil && len(commits) > 0 { + return commits[0].SHA, nil + } + } + } + if source != nil && source.CloneURL != "" { + return getRemoteHeadCommit(source.CloneURL) + } + return "", fmt.Errorf("failed to fetch latest commit hash") +} + +func cnbRepoPath(cloneURL string) string { + u := strings.TrimSpace(cloneURL) + u = strings.TrimSuffix(u, ".git") + u = strings.TrimSuffix(u, "/") + if strings.Contains(u, "://") { + parsed, err := url.Parse(u) + if err != nil { + return "" + } + return strings.Trim(parsed.Path, "/") + } + if strings.Contains(u, "@") { + if colon := strings.LastIndex(u, ":"); colon != -1 { + return strings.Trim(u[colon+1:], "/") + } + } + return "" +} + +func buildGiteaContentsURL(apiBase, owner, repo, path string) string { + base := fmt.Sprintf("%s/repos/%s/%s/contents", strings.TrimRight(apiBase, "/"), url.PathEscape(owner), url.PathEscape(repo)) + path = strings.Trim(strings.TrimSpace(path), "/") + if path == "" { + return base + } + return base + "/" + escapeGiteaPath(path) +} + +func buildCNBContentsURL(apiBase, repo, path string) string { + base := fmt.Sprintf("%s/%s/-/git/contents", strings.TrimRight(apiBase, "/"), escapeRepoPath(repo)) + path = strings.Trim(strings.TrimSpace(path), "/") + if path == "" { + return base + } + return base + "/" + escapeGiteaPath(path) +} + +func escapeRepoPath(repo string) string { + parts := strings.Split(strings.Trim(repo, "/"), "/") + for i := range parts { + parts[i] = url.PathEscape(parts[i]) + } + return strings.Join(parts, "/") +} diff --git a/internal/install/install_apply.go b/internal/install/install_apply.go index e3a047f9..8c9c1df4 100644 --- a/internal/install/install_apply.go +++ b/internal/install/install_apply.go @@ -460,7 +460,7 @@ func installFromGitSubdir(source *Source, destPath string, result *InstallResult // Works for GitHub and non-GitHub hosts. if gitSupportsSparseCheckout() { resolved = source.Subdir - if err := sparseCloneSubdir(source.CloneURL, resolved, tempRepoPath, source.Branch, authEnv(source.CloneURL), opts.OnProgress); err == nil { + if err := sparseCloneSubdir(source.CloneURL, resolved, tempRepoPath, source.Branch, source.authEnv(), opts.OnProgress); err == nil { subdirPath = filepath.Join(tempRepoPath, resolved) if info, statErr := os.Stat(subdirPath); statErr != nil || !info.IsDir() { subdirPath = "" @@ -492,6 +492,36 @@ func installFromGitSubdir(source *Source, destPath string, result *InstallResult } } + // Fast path 2b: CNB Contents API + if subdirPath == "" && isCNBAPISource(source) { + repo := cnbRepoPath(source.CloneURL) + resolved = source.Subdir + subdirPath = filepath.Join(tempRepoPath, resolved) + hash, dlErr := downloadCNBDir(repo, source.Subdir, subdirPath, source, opts.OnProgress) + if dlErr == nil { + commitHash = hash + } else { + result.Warnings = append(result.Warnings, fmt.Sprintf("CNB API install fallback: %v", dlErr)) + subdirPath = "" + _ = os.RemoveAll(tempRepoPath) + } + } + + // Fast path 2b: Gitea Contents API + if subdirPath == "" && isGiteaAPISource(source) { + owner, repo := giteaOwnerRepo(source.CloneURL) + resolved = source.Subdir + subdirPath = filepath.Join(tempRepoPath, resolved) + hash, dlErr := downloadGiteaDir(owner, repo, source.Subdir, subdirPath, source, opts.OnProgress) + if dlErr == nil { + commitHash = hash + } else { + result.Warnings = append(result.Warnings, fmt.Sprintf("Gitea API install fallback: %v", dlErr)) + subdirPath = "" + _ = os.RemoveAll(tempRepoPath) + } + } + // Fallback: full clone + fuzzy subdir resolution if subdirPath == "" { cleanupTempRepo(tempRepoPath) diff --git a/internal/install/install_config.go b/internal/install/install_config.go index 69138123..a1589c22 100644 --- a/internal/install/install_config.go +++ b/internal/install/install_config.go @@ -108,6 +108,8 @@ func InstallFromConfig(ctx InstallContext, opts InstallOptions) (ConfigInstallRe parseOpts := ParseOptions{ GitLabHosts: ctx.GitLabHosts(), AzureHosts: ctx.AzureHosts(), + CNBHosts: ctx.CNBHosts(), + GiteaHosts: ctx.GiteaHosts(), } // ── Classify skills: tracked / plain (groupable vs singles) ── diff --git a/internal/install/install_discovery.go b/internal/install/install_discovery.go index 3700d3a7..a74e4805 100644 --- a/internal/install/install_discovery.go +++ b/internal/install/install_discovery.go @@ -329,7 +329,7 @@ func discoverFromGitSubdirWithProgressImpl(source *Source, onProgress ProgressCa // Fast path 1: sparse checkout (preferred for speed if git is modern) // Works for GitHub and non-GitHub hosts. if gitSupportsSparseCheckout() { - if err := sparseCloneSubdir(source.CloneURL, source.Subdir, repoPath, source.Branch, authEnv(source.CloneURL), onProgress); err == nil { + if err := sparseCloneSubdir(source.CloneURL, source.Subdir, repoPath, source.Branch, source.authEnv(), onProgress); err == nil { subdirPath = filepath.Join(repoPath, source.Subdir) if info, statErr := os.Stat(subdirPath); statErr == nil && info.IsDir() { if hash, hashErr := getGitCommit(repoPath); hashErr == nil { @@ -389,6 +389,62 @@ func discoverFromGitSubdirWithProgressImpl(source *Source, onProgress ProgressCa subdirPath = "" } + // Fast path 2b: CNB Contents API + if subdirPath == "" && isCNBAPISource(source) { + repo := cnbRepoPath(source.CloneURL) + subdirPath = filepath.Join(repoPath, source.Subdir) + hash, dlErr := downloadCNBDir(repo, source.Subdir, subdirPath, source, onProgress) + if dlErr == nil { + commitHash = hash + skills := discoverSkills(subdirPath, true) + agents := discoverAgents(subdirPath, len(skills) > 0) + skills, agents, err = constrainDiscoveryToExplicitSkill(source, skills, agents) + if err != nil { + _ = os.RemoveAll(tempDir) + return nil, err + } + return &DiscoveryResult{ + RepoPath: tempDir, + Skills: skills, + Agents: agents, + Source: source, + CommitHash: commitHash, + Warnings: warnings, + }, nil + } + warnings = append(warnings, fmt.Sprintf("CNB API discovery fallback: %v", dlErr)) + _ = os.RemoveAll(repoPath) + subdirPath = "" + } + + // Fast path 2b: Gitea Contents API + if subdirPath == "" && isGiteaAPISource(source) { + owner, repo := giteaOwnerRepo(source.CloneURL) + subdirPath = filepath.Join(repoPath, source.Subdir) + hash, dlErr := downloadGiteaDir(owner, repo, source.Subdir, subdirPath, source, onProgress) + if dlErr == nil { + commitHash = hash + skills := discoverSkills(subdirPath, true) + agents := discoverAgents(subdirPath, len(skills) > 0) + skills, agents, err = constrainDiscoveryToExplicitSkill(source, skills, agents) + if err != nil { + _ = os.RemoveAll(tempDir) + return nil, err + } + return &DiscoveryResult{ + RepoPath: tempDir, + Skills: skills, + Agents: agents, + Source: source, + CommitHash: commitHash, + Warnings: warnings, + }, nil + } + warnings = append(warnings, fmt.Sprintf("Gitea API discovery fallback: %v", dlErr)) + _ = os.RemoveAll(repoPath) + subdirPath = "" + } + // Fallback: full clone + fuzzy subdir resolution cleanupTempRepo(repoPath) if onProgress != nil { diff --git a/internal/install/install_git.go b/internal/install/install_git.go index 1fff8128..544489fd 100644 --- a/internal/install/install_git.go +++ b/internal/install/install_git.go @@ -75,7 +75,7 @@ func WrapGitError(stderr string, err error, tokenAuthAttempted bool) error { } return fmt.Errorf("authentication required — options:\n"+ " 1. SSH URL: git@:/.git\n"+ - " 2. Token env var: GITHUB_TOKEN, GITLAB_TOKEN, BITBUCKET_TOKEN, AZURE_DEVOPS_TOKEN, or SKILLSHARE_GIT_TOKEN\n"+ + " 2. Token env var: GITHUB_TOKEN, GITLAB_TOKEN, BITBUCKET_TOKEN, AZURE_DEVOPS_TOKEN, CNB_TOKEN, GITEA_TOKEN, or SKILLSHARE_GIT_TOKEN\n"+ " 3. Git credential helper: gh auth login\n %s", s) } if s != "" { @@ -139,6 +139,10 @@ func extractGitFatal(stderr string) string { // GIT_CONFIG env vars without modifying the stored remote URL. // branch specifies the branch to clone; empty string uses the remote default. func cloneRepo(url, destPath, branch string, shallow bool, onProgress ProgressCallback) error { + return cloneRepoWithEnv(url, destPath, branch, shallow, authEnv(url), onProgress) +} + +func cloneRepoWithEnv(url, destPath, branch string, shallow bool, extraEnv []string, onProgress ProgressCallback) error { args := []string{"clone"} if onProgress != nil { args = append(args, "--progress") @@ -152,11 +156,11 @@ func cloneRepo(url, destPath, branch string, shallow bool, onProgress ProgressCa args = append(args, "--branch", branch) } args = append(args, url, destPath) - return runGitCommandWithProgress(args, "", authEnv(url), onProgress) + return runGitCommandWithProgress(args, "", extraEnv, onProgress) } func cloneRepoForSource(source *Source, destPath, branch string, shallow bool, onProgress ProgressCallback) error { - err := cloneRepo(source.CloneURL, destPath, branch, shallow, onProgress) + err := cloneRepoWithEnv(source.CloneURL, destPath, branch, shallow, source.authEnv(), onProgress) if err == nil { return nil } @@ -172,7 +176,7 @@ func cloneRepoForSource(source *Source, destPath, branch string, shallow bool, o if onProgress != nil { onProgress("Clone failed; retrying as a nested GitLab repository...") } - fallbackErr := cloneRepo(fallback.CloneURL, destPath, branch, shallow, onProgress) + fallbackErr := cloneRepoWithEnv(fallback.CloneURL, destPath, branch, shallow, fallback.authEnv(), onProgress) if fallbackErr == nil { *source = fallback return nil diff --git a/internal/install/install_tracked.go b/internal/install/install_tracked.go index 04acf0e6..ddeecbf0 100644 --- a/internal/install/install_tracked.go +++ b/internal/install/install_tracked.go @@ -197,7 +197,7 @@ func cloneRepoFull(url, destPath, branch string, onProgress ProgressCallback) er } func cloneTrackedRepoForSource(source *Source, destPath, branch string, onProgress ProgressCallback) error { - err := cloneTrackedRepo(source.CloneURL, source.Subdir, destPath, branch, onProgress) + err := cloneTrackedRepoForParsedSource(source, destPath, branch, onProgress) if err == nil { return nil } @@ -213,7 +213,7 @@ func cloneTrackedRepoForSource(source *Source, destPath, branch string, onProgre if onProgress != nil { onProgress("Clone failed; retrying as a nested GitLab repository...") } - fallbackErr := cloneTrackedRepo(fallback.CloneURL, fallback.Subdir, destPath, branch, onProgress) + fallbackErr := cloneTrackedRepoForParsedSource(&fallback, destPath, branch, onProgress) if fallbackErr == nil { *source = fallback return nil @@ -234,13 +234,36 @@ func cloneTrackedRepoForSource(source *Source, destPath, branch string, onProgre // When subdir is provided, sparse checkout is attempted first to reduce payload // while preserving .git for future tracked updates. func cloneTrackedRepo(url, subdir, destPath, branch string, onProgress ProgressCallback) error { + return cloneTrackedRepoWithEnv(url, subdir, destPath, branch, authEnv(url), onProgress) +} + +func cloneTrackedRepoForParsedSource(source *Source, destPath, branch string, onProgress ProgressCallback) error { + if source == nil { + return fmt.Errorf("nil source") + } + return cloneTrackedRepoWithEnv(source.CloneURL, source.Subdir, destPath, branch, source.authEnv(), onProgress) +} + +func cloneTrackedRepoWithEnv(url, subdir, destPath, branch string, extraEnv []string, onProgress ProgressCallback) error { subdir = strings.TrimSpace(subdir) if subdir != "" && gitSupportsSparseCheckout() { if onProgress != nil { onProgress("Preparing sparse checkout...") } - if err := sparseCloneSubdir(url, subdir, destPath, branch, authEnv(url), onProgress); err == nil { - return nil + if err := sparseCloneSubdir(url, subdir, destPath, branch, extraEnv, onProgress); err == nil { + checkedSubdir := strings.TrimLeft(subdir, "/") + if checkedSubdir == "" { + return nil + } + if info, statErr := os.Stat(filepath.Join(destPath, checkedSubdir)); statErr == nil && info.IsDir() { + return nil + } + if cleanupErr := removeAll(destPath); cleanupErr != nil { + return fmt.Errorf("sparse checkout produced no subdirectory %q, and cleanup failed: %w", subdir, cleanupErr) + } + if onProgress != nil { + onProgress("Sparse checkout path missing; retrying standard clone...") + } } else if shouldFallbackSparseTrackedClone(err) { // sparseCloneSubdir may have already created destPath. Clean it before // falling back to a standard clone strategy. @@ -271,7 +294,7 @@ func cloneTrackedRepo(url, subdir, destPath, branch string, onProgress ProgressC } args = append(args, url, destPath) - err := runGitCommandWithProgress(args, "", authEnv(url), onProgress) + err := runGitCommandWithProgress(args, "", extraEnv, onProgress) if err == nil { return nil } @@ -281,7 +304,7 @@ func cloneTrackedRepo(url, subdir, destPath, branch string, onProgress ProgressC if onProgress != nil { onProgress("Remote lacks partial clone support; retrying standard clone...") } - return cloneRepoFull(url, destPath, branch, onProgress) + return cloneRepoWithEnv(url, destPath, branch, false, extraEnv, onProgress) } // isAuthOrAccessError returns true for auth failures and access denials that diff --git a/internal/install/source.go b/internal/install/source.go index 7e4b667e..1895ea16 100644 --- a/internal/install/source.go +++ b/internal/install/source.go @@ -126,6 +126,10 @@ type Source struct { Path string // Local path (empty for git) Name string // Derived skill name Branch string // Git branch to clone from (empty = remote default) + // CNBHosts contains configured CNB hostnames for platform-specific auth/API behavior. + CNBHosts []string + // GiteaHosts contains configured Gitea hostnames for platform-specific auth/API behavior. + GiteaHosts []string // ExplicitSkill is true when the user pointed directly at a SKILL.md file. // That intent should resolve to exactly one skill, not a pack/discovery view. ExplicitSkill bool @@ -166,6 +170,8 @@ var azureOnPremPattern = regexp.MustCompile( type ParseOptions struct { GitLabHosts []string // extra hostnames to treat as GitLab (nested subgroup support) AzureHosts []string // extra hostnames to treat as Azure DevOps on-premises + CNBHosts []string // extra hostnames to treat as CNB instances + GiteaHosts []string // extra hostnames to treat as Gitea instances } // IsSSHURL reports whether input is an SSH URL — either scp-style @@ -211,7 +217,7 @@ func ParseSourceWithOptions(input string, opts ParseOptions) (*Source, error) { // Expand GitHub shorthand: owner/repo -> github.com/owner/repo input = expandGitHubShorthand(input) - source := &Source{Raw: input} + source := &Source{Raw: input, CNBHosts: opts.CNBHosts, GiteaHosts: opts.GiteaHosts} // Check for file:// URL (for testing with local git repos) if matches := fileURLPattern.FindStringSubmatch(input); matches != nil { @@ -668,6 +674,19 @@ func parseGitHTTPS(matches []string, source *Source, opts ParseOptions) (*Source // may have nested subgroups up to 20 levels deep. // Without .git, treat entire path as repo. repoPath = path + } else if isCNBHost(host, opts.CNBHosts) || isGiteaHost(host, opts.GiteaHosts) { + // CNB and Gitea Contents APIs address repositories as {owner}/{repo}; + // keep the existing owner/repo + subdir split but make configured hosts + // explicit so platform detection and future URL handling stay aligned. + parts := strings.SplitN(path, "/", 3) + if len(parts) >= 2 { + repoPath = parts[0] + "/" + parts[1] + if len(parts) == 3 { + subdir = parts[2] + } + } else { + repoPath = path + } } else { // Default for GHE, Gitea, Gogs, and other platforms: // assume owner/repo (2 segments), remainder is subdir. @@ -739,6 +758,44 @@ func isGitLabHost(host string, extraHosts []string) bool { hostMatchesAny(host, extraHosts) } +// isCNBHost returns true if the host should be treated as a CNB instance. +func isCNBHost(host string, extraHosts []string) bool { + return strings.Contains(host, "cnb.cool") || hostMatchesAny(host, extraHosts) +} + +// isGiteaHost returns true if the host should be treated as a Gitea instance. +func isGiteaHost(host string, extraHosts []string) bool { + return strings.Contains(host, "gitea") || hostMatchesAny(host, extraHosts) +} + +// detectPlatformFromHost returns the platform for a given hostname, using +// configured extra hosts for CNB and Gitea self-hosted instances. +func detectPlatformFromHost(host string, cnbHosts, giteaHosts []string) Platform { + host = strings.ToLower(host) + if host == "" { + return PlatformUnknown + } + if strings.Contains(host, "github") { + return PlatformGitHub + } + if strings.Contains(host, "gitlab") { + return PlatformGitLab + } + if strings.Contains(host, "bitbucket") { + return PlatformBitbucket + } + if host == "dev.azure.com" || host == "ssh.dev.azure.com" || strings.HasSuffix(host, ".visualstudio.com") { + return PlatformAzureDevOps + } + if isCNBHost(host, cnbHosts) { + return PlatformCNB + } + if isGiteaHost(host, giteaHosts) { + return PlatformGitea + } + return PlatformUnknown +} + // stripGitBranchPrefix removes platform-specific branch path segments from web URLs. // Bitbucket: src/{branch}/path → path // GitLab: -/tree/{branch}/path → path, -/blob/{branch}/path → path diff --git a/internal/install/source_test.go b/internal/install/source_test.go index 1c5a0e24..ef0e61d5 100644 --- a/internal/install/source_test.go +++ b/internal/install/source_test.go @@ -1520,6 +1520,65 @@ func TestParseSourceWithOptions_GitLabHosts(t *testing.T) { } } +func TestParseSourceWithOptions_CNBGiteaHosts(t *testing.T) { + tests := []struct { + name string + input string + opts ParseOptions + wantCloneURL string + wantSubdir string + wantName string + }{ + { + name: "cnb root repo keeps two segment repo", + input: "https://cnb.cool/org/repo", + wantCloneURL: "https://cnb.cool/org/repo.git", + wantName: "repo", + }, + { + name: "cnb subdir uses owner repo split", + input: "https://cnb.cool/org/repo/skills/foo", + wantCloneURL: "https://cnb.cool/org/repo.git", + wantSubdir: "skills/foo", + wantName: "foo", + }, + { + name: "custom cnb host uses configured host", + input: "https://git.corp.example/org/repo/skills/foo", + opts: ParseOptions{CNBHosts: []string{"git.corp.example"}}, + wantCloneURL: "https://git.corp.example/org/repo.git", + wantSubdir: "skills/foo", + wantName: "foo", + }, + { + name: "custom gitea host uses configured host", + input: "https://git.example.com/org/repo/skills/foo", + opts: ParseOptions{GiteaHosts: []string{"git.example.com"}}, + wantCloneURL: "https://git.example.com/org/repo.git", + wantSubdir: "skills/foo", + wantName: "foo", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + source, err := ParseSourceWithOptions(tt.input, tt.opts) + if err != nil { + t.Fatalf("ParseSourceWithOptions(%q) error = %v", tt.input, err) + } + if source.CloneURL != tt.wantCloneURL { + t.Errorf("CloneURL = %q, want %q", source.CloneURL, tt.wantCloneURL) + } + if source.Subdir != tt.wantSubdir { + t.Errorf("Subdir = %q, want %q", source.Subdir, tt.wantSubdir) + } + if source.Name != tt.wantName { + t.Errorf("Name = %q, want %q", source.Name, tt.wantName) + } + }) + } +} + func TestParseSourceWithOptions_AzureHosts(t *testing.T) { tests := []struct { name string diff --git a/internal/server/server.go b/internal/server/server.go index 8488da80..49056989 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -176,11 +176,15 @@ func (s *Server) parseOpts() install.ParseOptions { return install.ParseOptions{ GitLabHosts: s.projectCfg.EffectiveGitLabHosts(), AzureHosts: s.projectCfg.EffectiveAzureHosts(), + CNBHosts: s.projectCfg.EffectiveCNBHosts(), + GiteaHosts: s.projectCfg.EffectiveGiteaHosts(), } } return install.ParseOptions{ GitLabHosts: s.cfg.EffectiveGitLabHosts(), AzureHosts: s.cfg.EffectiveAzureHosts(), + CNBHosts: s.cfg.EffectiveCNBHosts(), + GiteaHosts: s.cfg.EffectiveGiteaHosts(), } }