@@ -13,6 +13,7 @@ import (
1313 "github.com/cli/cli/internal/ghinstance"
1414 "github.com/cli/cli/internal/ghrepo"
1515 "github.com/shurcooL/githubv4"
16+ "golang.org/x/sync/errgroup"
1617)
1718
1819type PullRequestsPayload struct {
@@ -232,14 +233,16 @@ func (c Client) PullRequestDiff(baseRepo ghrepo.Interface, prNumber int) (io.Rea
232233}
233234
234235type pullRequestFeature struct {
235- HasReviewDecision bool
236- HasStatusCheckRollup bool
236+ HasReviewDecision bool
237+ HasStatusCheckRollup bool
238+ HasBranchProtectionRule bool
237239}
238240
239241func determinePullRequestFeatures (httpClient * http.Client , hostname string ) (prFeatures pullRequestFeature , err error ) {
240242 if ! ghinstance .IsEnterprise (hostname ) {
241243 prFeatures .HasReviewDecision = true
242244 prFeatures .HasStatusCheckRollup = true
245+ prFeatures .HasBranchProtectionRule = true
243246 return
244247 }
245248
@@ -256,8 +259,26 @@ func determinePullRequestFeatures(httpClient *http.Client, hostname string) (prF
256259 } `graphql:"Commit: __type(name: \"Commit\")"`
257260 }
258261
262+ // needs to be a separate query because the backend only supports 2 `__type` expressions in one query
263+ var featureDetection2 struct {
264+ Ref struct {
265+ Fields []struct {
266+ Name string
267+ } `graphql:"fields(includeDeprecated: true)"`
268+ } `graphql:"Ref: __type(name: \"Ref\")"`
269+ }
270+
259271 v4 := graphQLClient (httpClient , hostname )
260- err = v4 .QueryNamed (context .Background (), "PullRequest_fields" , & featureDetection , nil )
272+
273+ g := new (errgroup.Group )
274+ g .Go (func () error {
275+ return v4 .QueryNamed (context .Background (), "PullRequest_fields" , & featureDetection , nil )
276+ })
277+ g .Go (func () error {
278+ return v4 .QueryNamed (context .Background (), "PullRequest_fields2" , & featureDetection2 , nil )
279+ })
280+
281+ err = g .Wait ()
261282 if err != nil {
262283 return
263284 }
@@ -274,6 +295,12 @@ func determinePullRequestFeatures(httpClient *http.Client, hostname string) (prF
274295 prFeatures .HasStatusCheckRollup = true
275296 }
276297 }
298+ for _ , field := range featureDetection2 .Ref .Fields {
299+ switch field .Name {
300+ case "branchProtectionRule" :
301+ prFeatures .HasBranchProtectionRule = true
302+ }
303+ }
277304 return
278305}
279306
@@ -333,6 +360,16 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
333360 `
334361 }
335362
363+ var requiresStrictStatusChecks string
364+ if prFeatures .HasBranchProtectionRule {
365+ requiresStrictStatusChecks = `
366+ baseRef {
367+ branchProtectionRule {
368+ requiresStrictStatusChecks
369+ }
370+ }`
371+ }
372+
336373 fragments := fmt .Sprintf (`
337374 fragment pr on PullRequest {
338375 number
@@ -344,11 +381,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
344381 headRepositoryOwner {
345382 login
346383 }
347- baseRef {
348- branchProtectionRule {
349- requiresStrictStatusChecks
350- }
351- }
384+ %s
352385 isCrossRepository
353386 isDraft
354387 %s
@@ -357,16 +390,13 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
357390 ...pr
358391 %s
359392 }
360- ` , statusesFragment , reviewsFragment )
393+ ` , requiresStrictStatusChecks , statusesFragment , reviewsFragment )
361394
362395 queryPrefix := `
363396 query PullRequestStatus($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
364397 repository(owner: $owner, name: $repo) {
365398 defaultBranchRef {
366399 name
367- branchProtectionRule {
368- requiresStrictStatusChecks
369- }
370400 }
371401 pullRequests(headRefName: $headRefName, first: $per_page, orderBy: { field: CREATED_AT, direction: DESC }) {
372402 totalCount
@@ -382,12 +412,9 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
382412 queryPrefix = `
383413 query PullRequestStatus($owner: String!, $repo: String!, $number: Int!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
384414 repository(owner: $owner, name: $repo) {
385- defaultBranchRef {
386- name
387- branchProtectionRule {
388- requiresStrictStatusChecks
415+ defaultBranchRef {
416+ name
389417 }
390- }
391418 pullRequest(number: $number) {
392419 ...prWithReviews
393420 }
0 commit comments