Skip to content

Commit 1bea40f

Browse files
committed
only make the graphql call once
1 parent 3f0df3d commit 1bea40f

File tree

1 file changed

+42
-83
lines changed

1 file changed

+42
-83
lines changed

internal/cmd/match_criteria.go

Lines changed: 42 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -210,114 +210,73 @@ func GetPRStatusInfo(ctx context.Context, graphQlClient *api.GraphQLClient, owne
210210
return response, nil
211211
}
212212

213-
// HasPassingCI checks if a pull request has passing CI
214-
func HasPassingCI(ctx context.Context, graphQlClient *api.GraphQLClient, owner, repo string, prNumber int) (bool, error) {
215-
// Check for context cancellation
216-
select {
217-
case <-ctx.Done():
218-
return false, ctx.Err()
219-
default:
220-
// Continue processing
213+
// PrMeetsRequirements checks if a PR meets additional requirements beyond basic criteria
214+
func PrMeetsRequirements(ctx context.Context, graphQlClient *api.GraphQLClient, owner, repo string, prNumber int) (bool, error) {
215+
// If no additional requirements are specified, the PR meets requirements
216+
if !requireCI && !mustBeApproved {
217+
return true, nil
221218
}
222219

223-
// Get PR status info using GraphQL
220+
// Fetch PR status info once
224221
response, err := GetPRStatusInfo(ctx, graphQlClient, owner, repo, prNumber)
225222
if err != nil {
226223
return false, err
227224
}
228225

229-
// Get the commit status check info
226+
// Check CI status if required
227+
if requireCI {
228+
passing := isCIPassing(response)
229+
if !passing {
230+
return false, nil
231+
}
232+
}
233+
234+
// Check approval status if required
235+
if mustBeApproved {
236+
approved := isPRApproved(response)
237+
if !approved {
238+
return false, nil
239+
}
240+
}
241+
242+
return true, nil
243+
}
244+
245+
// isCIPassing checks if the CI status is passing based on the response
246+
func isCIPassing(response *prStatusResponse) bool {
230247
commits := response.Data.Repository.PullRequest.Commits.Nodes
231248
if len(commits) == 0 {
232-
Logger.Debug("No commits found for PR", "repo", fmt.Sprintf("%s/%s", owner, repo), "pr", prNumber)
233-
return false, nil
249+
Logger.Debug("No commits found for PR")
250+
return false
234251
}
235252

236-
// Get status check info
237253
statusCheckRollup := commits[0].Commit.StatusCheckRollup
238254
if statusCheckRollup == nil {
239-
Logger.Debug("No status checks found for PR", "repo", fmt.Sprintf("%s/%s", owner, repo), "pr", prNumber)
240-
return true, nil // If no checks defined, consider it passing
255+
Logger.Debug("No status checks found for PR")
256+
return true // If no checks defined, consider it passing
241257
}
242258

243-
// Check if status is SUCCESS
244259
if statusCheckRollup.State != "SUCCESS" {
245-
Logger.Debug("PR failed CI check", "repo", fmt.Sprintf("%s/%s", owner, repo), "pr", prNumber, "status", statusCheckRollup.State)
246-
return false, nil
260+
Logger.Debug("PR failed CI check", "status", statusCheckRollup.State)
261+
return false
247262
}
248263

249-
return true, nil
264+
return true
250265
}
251266

252-
// HasApproval checks if a pull request has been approved
253-
func HasApproval(ctx context.Context, graphQlClient *api.GraphQLClient, owner, repo string, prNumber int) (bool, error) {
254-
// Check for context cancellation
255-
select {
256-
case <-ctx.Done():
257-
return false, ctx.Err()
258-
default:
259-
// Continue processing
260-
}
261-
262-
// Get PR status info using GraphQL
263-
response, err := GetPRStatusInfo(ctx, graphQlClient, owner, repo, prNumber)
264-
if err != nil {
265-
return false, err
266-
}
267-
267+
// isPRApproved checks if the PR is approved based on the response
268+
func isPRApproved(response *prStatusResponse) bool {
268269
reviewDecision := response.Data.Repository.PullRequest.ReviewDecision
269-
Logger.Debug("PR review decision", "repo", fmt.Sprintf("%s/%s", owner, repo), "pr", prNumber, "decision", reviewDecision)
270+
Logger.Debug("PR review decision", "decision", reviewDecision)
270271

271-
// Check the review decision
272272
switch reviewDecision {
273273
case "APPROVED":
274-
return true, nil
274+
return true
275275
case "": // When no reviews are required
276-
Logger.Debug("PR has no required reviewers", "repo", fmt.Sprintf("%s/%s", owner, repo), "pr", prNumber)
277-
return true, nil // If no reviews required, consider it approved
278-
default:
279-
// Any other decision (REVIEW_REQUIRED, CHANGES_REQUESTED, etc.)
280-
Logger.Debug("PR not approved", "repo", fmt.Sprintf("%s/%s", owner, repo), "pr", prNumber, "decision", reviewDecision)
281-
return false, nil
282-
}
283-
}
284-
285-
// PrMeetsRequirements checks if a PR meets additional requirements beyond basic criteria
286-
func PrMeetsRequirements(ctx context.Context, graphQlClient *api.GraphQLClient, owner, repo string, prNumber int) (bool, error) {
287-
// If no additional requirements are specified, the PR meets requirements
288-
if !requireCI && !mustBeApproved {
289-
return true, nil
290-
}
291-
292-
// Check for context cancellation
293-
select {
294-
case <-ctx.Done():
295-
return false, ctx.Err()
276+
Logger.Debug("PR has no required reviewers")
277+
return true // If no reviews required, consider it approved
296278
default:
297-
// Continue processing
298-
}
299-
300-
// Check CI status if required
301-
if requireCI {
302-
passing, err := HasPassingCI(ctx, graphQlClient, owner, repo, prNumber)
303-
if err != nil {
304-
return false, err
305-
}
306-
if !passing {
307-
return false, nil
308-
}
309-
}
310-
311-
// Check approval status if required
312-
if mustBeApproved {
313-
approved, err := HasApproval(ctx, graphQlClient, owner, repo, prNumber)
314-
if err != nil {
315-
return false, err
316-
}
317-
if !approved {
318-
return false, nil
319-
}
279+
Logger.Debug("PR not approved", "decision", reviewDecision)
280+
return false
320281
}
321-
322-
return true, nil
323282
}

0 commit comments

Comments
 (0)