MCPcopy
hub / github.com/cli/cli / RepoNetwork

Function RepoNetwork

api/queries_repo.go:473–567  ·  view source on GitHub ↗

RepoNetwork inspects the relationship between multiple GitHub repositories

(client *Client, repos []ghrepo.Interface)

Source from the content-addressed store, hash-verified

471
472// RepoNetwork inspects the relationship between multiple GitHub repositories
473func RepoNetwork(client *Client, repos []ghrepo.Interface) (RepoNetworkResult, error) {
474 var hostname string
475 if len(repos) > 0 {
476 hostname = repos[0].RepoHost()
477 }
478
479 queries := make([]string, 0, len(repos))
480 for i, repo := range repos {
481 queries = append(queries, fmt.Sprintf(`
482 repo_%03d: repository(owner: %q, name: %q) {
483 ...repo
484 parent {
485 ...repo
486 }
487 }
488 `, i, repo.RepoOwner(), repo.RepoName()))
489 }
490
491 // Since the query is constructed dynamically, we can't parse a response
492 // format using a static struct. Instead, hold the raw JSON data until we
493 // decide how to parse it manually.
494 graphqlResult := make(map[string]*json.RawMessage)
495 var result RepoNetworkResult
496
497 err := client.GraphQL(hostname, fmt.Sprintf(`
498 fragment repo on Repository {
499 id
500 name
501 owner { login }
502 viewerPermission
503 defaultBranchRef {
504 name
505 }
506 isPrivate
507 }
508 query RepositoryNetwork {
509 viewer { login }
510 %s
511 }
512 `, strings.Join(queries, "")), nil, &graphqlResult)
513 var graphqlError GraphQLError
514 if errors.As(err, &graphqlError) {
515 // If the only errors are that certain repositories are not found,
516 // continue processing this response instead of returning an error
517 tolerated := true
518 for _, ge := range graphqlError.Errors {
519 if ge.Type != "NOT_FOUND" {
520 tolerated = false
521 }
522 }
523 if tolerated {
524 err = nil
525 }
526 }
527 if err != nil {
528 return result, err
529 }
530

Callers 1

resolveNetworkFunction · 0.92

Calls 7

RepoOwnerMethod · 0.95
RepoNameMethod · 0.95
InitRepoHostnameFunction · 0.85
GraphQLMethod · 0.80
JoinMethod · 0.80
RepoHostMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected