cli/internal/skills/source/source.go
tommaso-moro 663df07fcf
cleanup frontmatter fields
remove git sha because we only need git tree sha

remove github-owner from frontmatter, and make github-repo support full url. Only support github.com as host, error out otherwise
2026-04-15 15:47:34 +02:00

66 lines
1.6 KiB
Go

package source
import (
"fmt"
"strings"
"github.com/cli/cli/v2/internal/ghrepo"
)
const SupportedHost = "github.com"
// BuildRepoURL returns the canonical repository URL stored in skill metadata.
func BuildRepoURL(host, owner, repo string) string {
return ghrepo.GenerateRepoURL(ghrepo.NewWithHost(owner, repo, host), "")
}
// ParseRepoURL parses a repository URL stored in skill metadata.
func ParseRepoURL(raw string) (ghrepo.Interface, error) {
raw = strings.TrimSpace(raw)
if raw == "" {
return nil, fmt.Errorf("repository URL is empty")
}
repo, err := ghrepo.FromFullName(raw)
if err != nil {
return nil, fmt.Errorf("invalid repository URL %q: %w", raw, err)
}
return repo, nil
}
// ParseMetadataRepo extracts repository information from skill metadata.
func ParseMetadataRepo(meta map[string]interface{}) (ghrepo.Interface, bool, error) {
if meta == nil {
return nil, false, nil
}
repoValue, _ := meta["github-repo"].(string)
if repoValue == "" {
return nil, false, nil
}
repo, err := ParseRepoURL(repoValue)
if err != nil {
return nil, true, err
}
return repo, true, nil
}
// ValidateSupportedHost rejects hosts that are not supported in public preview.
func ValidateSupportedHost(host string) error {
host = normalizeHost(host)
if host == "" {
return fmt.Errorf("could not determine repository host")
}
if host != SupportedHost {
return fmt.Errorf("GitHub Skills currently supports only %s as a host; got %s", SupportedHost, host)
}
return nil
}
func normalizeHost(host string) string {
host = strings.TrimSpace(strings.ToLower(host))
return strings.TrimPrefix(host, "www.")
}