MCPcopy Create free account
hub / github.com/cli/cli / checkUpstreamProvenance

Function checkUpstreamProvenance

pkg/cmd/skills/install/install.go:1293–1368  ·  view source on GitHub ↗

checkUpstreamProvenance fetches the skill's SKILL.md via the contents API to check if it contains github-repo metadata pointing to a different repository, indicating the skill was re-published from an upstream source. In interactive mode, the user is asked whether to install from the re-publisher or

(opts *InstallOptions, client *api.Client, hostname string, skill discovery.Skill, commitSHA string)

Source from the content-addressed store, hash-verified

1291// installs from the re-publisher.
1292// Returns (repo to redirect to, whether upstream was detected, error).
1293func checkUpstreamProvenance(opts *InstallOptions, client *api.Client, hostname string, skill discovery.Skill, commitSHA string) (ghrepo.Interface, bool, error) {
1294 u, err := safeurl.JoinPath("repos", opts.repo.RepoOwner(), opts.repo.RepoName(), "contents", skill.Path+"/SKILL.md")
1295 if err != nil {
1296 return nil, false, err
1297 }
1298 u.SetQuery("ref", commitSHA)
1299 var fileResp struct {
1300 Content string `json:"content"`
1301 Encoding string `json:"encoding"`
1302 }
1303 if err := client.REST(hostname, "GET", u.String(), nil, &fileResp); err != nil {
1304 return nil, false, nil //nolint:nilerr // best-effort check; failing to fetch is not fatal
1305 }
1306 if fileResp.Encoding != "base64" {
1307 return nil, false, nil
1308 }
1309 decoded, decodeErr := io.ReadAll(base64.NewDecoder(base64.StdEncoding, strings.NewReader(fileResp.Content)))
1310 if decodeErr != nil {
1311 return nil, false, nil //nolint:nilerr // best-effort; decode failure is not fatal
1312 }
1313 content := string(decoded)
1314
1315 result, parseErr := frontmatter.Parse(content)
1316 if parseErr != nil || result.Metadata.Meta == nil {
1317 //nolint:nilerr // unparsable frontmatter means no upstream to detect
1318 return nil, false, nil
1319 }
1320
1321 existingRepo, _ := result.Metadata.Meta["github-repo"].(string)
1322 if existingRepo == "" {
1323 return nil, false, nil
1324 }
1325
1326 currentRepoURL := source.BuildRepoURL(hostname, opts.repo.RepoOwner(), opts.repo.RepoName())
1327 if existingRepo == currentRepoURL {
1328 return nil, false, nil
1329 }
1330
1331 upstreamRepo, parseErr := source.ParseRepoURL(existingRepo)
1332 if parseErr != nil {
1333 //nolint:nilerr // invalid repo URL means we can't redirect; install normally
1334 return nil, false, nil
1335 }
1336
1337 cs := opts.IO.ColorScheme()
1338 upstreamLabel := ghrepo.FullName(upstreamRepo)
1339 repoSource := ghrepo.FullName(opts.repo)
1340
1341 fmt.Fprintf(opts.IO.ErrOut, "%s This skill was originally published in %s\n", cs.WarningIcon(), upstreamLabel)
1342
1343 if opts.Upstream {
1344 fmt.Fprintf(opts.IO.ErrOut, "Redirecting install to %s...\n", upstreamLabel)
1345 return upstreamRepo, true, nil
1346 }
1347
1348 if !opts.IO.CanPrompt() {
1349 fmt.Fprintf(opts.IO.ErrOut, " Installing from %s (use --upstream or interactive mode to choose upstream)\n", repoSource)
1350 return nil, true, nil

Callers 1

installRunFunction · 0.85

Calls 14

JoinPathFunction · 0.92
ParseFunction · 0.92
BuildRepoURLFunction · 0.92
ParseRepoURLFunction · 0.92
FullNameFunction · 0.92
SetQueryMethod · 0.80
ColorSchemeMethod · 0.80
WarningIconMethod · 0.80
CanPromptMethod · 0.80
RepoOwnerMethod · 0.65
RepoNameMethod · 0.65
RESTMethod · 0.65

Tested by

no test coverage detected