|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/shurcooL/githubv4" |
| 8 | +) |
| 9 | + |
| 10 | +// Pull request readiness is queried only for explicitly selected fields. REST |
| 11 | +// list/search responses carry node IDs, so one nodes query enriches the whole |
| 12 | +// page, including search pages spanning several repositories. |
| 13 | +type pullRequestReadinessQuery struct { |
| 14 | + Nodes []struct { |
| 15 | + PullRequest struct { |
| 16 | + ID githubv4.ID |
| 17 | + ReviewDecision githubv4.String `graphql:"reviewDecision @include(if: $includeReview)"` |
| 18 | + Commits struct { |
| 19 | + Nodes []struct { |
| 20 | + Commit struct { |
| 21 | + StatusCheckRollup struct { |
| 22 | + State githubv4.String |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + } `graphql:"commits(last: 1) @include(if: $includeChecks)"` |
| 27 | + } `graphql:"... on PullRequest"` |
| 28 | + } `graphql:"nodes(ids: $ids)"` |
| 29 | +} |
| 30 | + |
| 31 | +type pullRequestReadiness struct { |
| 32 | + ReviewDecision *string |
| 33 | + StatusCheckRollup *string |
| 34 | +} |
| 35 | + |
| 36 | +func requestedPullRequestReadiness(fields []string) bool { |
| 37 | + for _, field := range fields { |
| 38 | + if field == "review_decision" || field == "status_check_rollup" { |
| 39 | + return true |
| 40 | + } |
| 41 | + } |
| 42 | + return false |
| 43 | +} |
| 44 | + |
| 45 | +func fetchPullRequestReadiness(ctx context.Context, deps ToolDependencies, nodeIDs []string, fields []string) (map[string]pullRequestReadiness, error) { |
| 46 | + if len(nodeIDs) == 0 { |
| 47 | + return nil, nil |
| 48 | + } |
| 49 | + ids := make([]githubv4.ID, len(nodeIDs)) |
| 50 | + for i, id := range nodeIDs { |
| 51 | + if id == "" { |
| 52 | + return nil, fmt.Errorf("pull request readiness requires a node ID for item %d", i+1) |
| 53 | + } |
| 54 | + ids[i] = githubv4.ID(id) |
| 55 | + } |
| 56 | + client, err := deps.GetGQLClient(ctx) |
| 57 | + if err != nil { |
| 58 | + return nil, fmt.Errorf("get GraphQL client for pull request readiness: %w", err) |
| 59 | + } |
| 60 | + if client == nil { |
| 61 | + return nil, fmt.Errorf("GraphQL client for pull request readiness is unavailable") |
| 62 | + } |
| 63 | + var includeReview, includeChecks bool |
| 64 | + for _, field := range fields { |
| 65 | + switch field { |
| 66 | + case "review_decision": |
| 67 | + includeReview = true |
| 68 | + case "status_check_rollup": |
| 69 | + includeChecks = true |
| 70 | + } |
| 71 | + } |
| 72 | + var q pullRequestReadinessQuery |
| 73 | + if err := client.Query(ctx, &q, map[string]any{ |
| 74 | + "ids": ids, |
| 75 | + "includeReview": githubv4.Boolean(includeReview), |
| 76 | + "includeChecks": githubv4.Boolean(includeChecks), |
| 77 | + }); err != nil { |
| 78 | + return nil, fmt.Errorf("query pull request readiness: %w", err) |
| 79 | + } |
| 80 | + result := make(map[string]pullRequestReadiness, len(q.Nodes)) |
| 81 | + for _, node := range q.Nodes { |
| 82 | + id, ok := node.PullRequest.ID.(string) |
| 83 | + if !ok || id == "" { |
| 84 | + return nil, fmt.Errorf("pull request readiness response contained a missing or non-PR node") |
| 85 | + } |
| 86 | + var item pullRequestReadiness |
| 87 | + if value := string(node.PullRequest.ReviewDecision); value != "" { |
| 88 | + item.ReviewDecision = &value |
| 89 | + } |
| 90 | + if commits := node.PullRequest.Commits.Nodes; len(commits) > 0 { |
| 91 | + if value := string(commits[0].Commit.StatusCheckRollup.State); value != "" { |
| 92 | + item.StatusCheckRollup = &value |
| 93 | + } |
| 94 | + } |
| 95 | + result[id] = item |
| 96 | + } |
| 97 | + for _, id := range nodeIDs { |
| 98 | + if _, ok := result[id]; !ok { |
| 99 | + return nil, fmt.Errorf("pull request readiness response omitted node %s", id) |
| 100 | + } |
| 101 | + } |
| 102 | + return result, nil |
| 103 | +} |
| 104 | + |
| 105 | +func addPullRequestReadinessFields(items []map[string]any, nodeIDs []string, fields []string, readiness map[string]pullRequestReadiness) error { |
| 106 | + if len(items) != len(nodeIDs) { |
| 107 | + return fmt.Errorf("pull request readiness item count mismatch") |
| 108 | + } |
| 109 | + for i, id := range nodeIDs { |
| 110 | + value, ok := readiness[id] |
| 111 | + if !ok { |
| 112 | + return fmt.Errorf("pull request readiness missing for node %s", id) |
| 113 | + } |
| 114 | + for _, field := range fields { |
| 115 | + switch field { |
| 116 | + case "review_decision": |
| 117 | + items[i][field] = value.ReviewDecision |
| 118 | + case "status_check_rollup": |
| 119 | + items[i][field] = value.StatusCheckRollup |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + return nil |
| 124 | +} |
0 commit comments