RepoNetwork inspects the relationship between multiple GitHub repositories
(client *Client, repos []ghrepo.Interface)
| 471 | |
| 472 | // RepoNetwork inspects the relationship between multiple GitHub repositories |
| 473 | func 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 |
no test coverage detected