Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly fetch Github PR status #160

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

benwaffle
Copy link

Fetch everything that makes a PR green, including the Status API and the Checks API.

Fixes #107

Fetch everything that makes a PR green, including the Status API and the Checks API.

Fixes Clever#107
@benwaffle benwaffle requested a review from a team as a code owner June 7, 2022 01:38
@benwaffle benwaffle requested review from rgarcia and removed request for a team June 7, 2022 01:38
@rgarcia
Copy link
Member

rgarcia commented Jun 22, 2022

@benwaffle thanks for the contribution! Didn't realize that there was a distinction between commit statuses and checks. The graphql addition is neat but I am hesitant to add it since we don't use graphql at Clever and so something like the query struct with all of it's graphql annotations would present a challenge for the team to understand and maintain. If there's a way to do this with the non-graphql API that would be preferred. Thanks again for this PR.

@rgarcia rgarcia removed their request for review June 22, 2022 13:26
@benwaffle
Copy link
Author

benwaffle commented Jun 22, 2022

@robherley there's a REST API for this too, right? but you said it was expensive to call?

@rgarcia Alternatively, we can still use the GraphQL API but avoid the Go annotations - just using JSON directly - see here

@robherley
Copy link

👋 @benwaffle @rgarcia Unfortunately there isn't an equivalent REST API for CombinedStatus (CheckRun states + Status states)

Alternatively if you wanted to use purely REST there's the List check suites for a Git reference API, which will get you all the check suites for a specific commit, and that covers checks (including actions). But, it's paginated and a bit slower than using the GraphQL query. You'd still need to call the Statuses API that you are doing today, in combination with the new Checks call.

My vote would be to just use the GraphQL call 👍

}
}
} `graphql:"commits(last: 1)"`
} `graphql:"pullRequest(number: $number)"`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benwaffle Is there a reason your query is going off of the Pull Request number?

The PR's latest head SHA is already available from here:

pr, _, err := client.PullRequests.Get(ctx, r.Owner, r.Name, po.PullRequestNumber)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no particular reason, using the PR number worked

TargetURL string
} `graphql:"... on StatusContext"`
}
} `graphql:"contexts(first: 10)"`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rgarcia @benwaffle I may be misunderstanding, but is the only reason why all the statuses are fetched in this function is to find the circle ci context?

microplane/push/push.go

Lines 146 to 160 in 724c03c

var circleCIBuildURL string
for _, status := range cs.Statuses {
if status.Context == "ci/circleci: build" && status.TargetURL != nil {
circleCIBuildURL = *status.TargetURL
// url has lots of ugly tracking query params, get rid of them
if parsedURL, err := url.Parse(circleCIBuildURL); err == nil {
query := parsedURL.Query()
query.Del("utm_campaign")
query.Del("utm_medium")
query.Del("utm_source")
parsedURL.RawQuery = query.Encode()
circleCIBuildURL = parsedURL.String()
}
}
}

If so, it may be better to just query for that context specifically using the GraphQL API: https://docs.github.com/en/graphql/reference/objects#status

query {
  repository(owner: "Clever", name: "microplane") {
    object(oid: "0e085b8bfa579162b9d3b8400cec7bfaaa42866a") {
      ... on Commit {
        status {
          context(name: "ci/circleci: build") {
            context
            state
            targetUrl
          }
        }
      }
    }
  }
}

I've seen repositories with hundreds of statuses before, there's no guarantee that the first 10 here will capture the CircleCI context for every commit.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, just to get the circle ci build. Not sure where that's used, but I suppose there would be value in displaying other CI URLs (travis, jenkins, gh actions, etc...)

@Enrico2
Copy link

Enrico2 commented Feb 15, 2023

Is this PR still needed? Discovering this repo now and looking into using it internally, we don't fear GraphQL and absolutely do need to check for all statuses and checks :)
I'm likely needing to fork this repo anyway to support forking a repo where a push fails due to a lack of write permissions.

Thanks!

@benwaffle
Copy link
Author

It's still relevant, but the maintainers would like it to be written using the REST API instead. Personally I've just been opening and merging all the PRs manually when green, and that's been good enough for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

If two workflows are triggered on a branch and one is successful and one is skipped, mp doesn't allow merge
4 participants