How to get the default merge method for a pull request from github graphql query? - github

Using a Github graphql query below I can see which merge options a repository has available:
{
node(id: "<id>") {
... on PullRequest {
number
repository {
mergeCommitAllowed
squashMergeAllowed
rebaseMergeAllowed
}
}
}
}
that returns:
{
"data": {
"node": {
"number": 666,
"repository": {
"mergeCommitAllowed": false,
"squashMergeAllowed": true,
"rebaseMergeAllowed": true
}
}
}
}
But, I don't see a way to know which is the default. When I look at the pull request on Github (see below), it knows that rebase is the preferred method for merging my pull request. Perhaps there is some kind of "sticky" data attached to my user? (I'm sure the last pull request I closed was using rebase)
Is it possible to know which merge method is the default with a graphql query?

Here's an example query which returns the default merge method for the authenticated user. replace owner and name as appropriate.
{
repository(owner: "TeamCodeStream", name: "codestream") {
viewerDefaultMergeMethod
}
}

I believe this is sticky to your user and possibly the repository. I've used a non-default merge method on a repository in the past, and when I came back, the merge button suggested the same value, which was definitely not the value I typically used.
The API wouldn't show you this, since that information is not a property of the repository, but both the repository and the user. API responses are not supposed to change based on the person who makes them unless there's an access control issue. Anyway, when performing a merge through the GraphQL API, the API defaults to the merge method anyway unless you specify something different.

Related

GitHub REST and GraphQL API are returning different data

I am scraping some data from GitHub. The RESTful URL to this particular PR shows that it has a merge_commit_sha value: https://api.github.com/repos/ansible/ansible/pulls/15088
However, when I try to get the same PR using GitHub GraphQL API, it shows it does not have any mergedCommit value.
resource(
url: "https://github.com/ansible/ansible/pull/15088"
) {
...on PullRequest {
id
number
title
merged
mergeCommit {
message
}
}
}
For context, the PR of interest is actually merged and should have a merged-commit value. I am looking for an explanation of the difference between these two APIs.
This link posted in the other answer contains the explanation:
As in, Git doesn’t have the originalCommit (which makes sense).
Presumably the original commit SHA is there, but the graphQL API actually checks to see if git has it, whereas the REST API doesn’t?
If you search for the commit SHA the API returns, you can't find it in the repo.
https://github.com/ansible/ansible/commit/d7b54c103050d9fc4965e57b7611a70cb964ab25
Since this is a very old pull request on an active repo, there's a good chance some old commits were cleaned up or other maintenance on the repo. It's hard to tell as that kind of maintenance obviously isn't version controlled.
Another option is the pull request was merged with fast-forward, which does not involve a merge commit. But that wouldn't explain the SHA on the REST API response.
So probably at some point they removed old merge commits to save some space, or something similar. Some objects still point to removed SHAs, but GraphQL API filters on existing objects.
Feel like it is a bug to me because if you query another PR such as 45454 , it can return the mergeCommit:
{
"data": {
"resource": {
"id": "MDExOlB1bGxSZXF1ZXN0MjE0NDYyOTY2",
"number": 45454,
"title": "win_say - fix up syntax and test issues (#45450)",
"merged": true,
"mergeCommit": {
"message": "win_say - fix up syntax and test issues (#45450)\n\n\n(cherry picked from commit c9c141fb6a51d6b77274958a2340fa54754db692)",
"oid": "f2d5954d11a1707cdb70b01dfb27c722b6416295"
}
}
}
}
Also find out other encountered the same problem at this and another similar issue at this. I suggest you can try to raise this issue to them at this.

How do I enable auto-merge on a github pull request via the Rest API

I have permission, the repo allows it. I create a bunch of PRs using a cli tool and then I have to go open N tabs and double click the Enable Auto-Merge -> Confirm process for each one.
Does the API offer an issue/pr modify method to set this attribute automatically without resorting to the UI?
It seems that at the moment it's not possible to do it via Rest API, but you can use GraphQL API mutation enablePullRequestAutoMerge instead.
I solved it like this:
Request POST to https://api.github.com/graphql to get pullRequestID value
body:
query MyQuery {
repository(name: "repo-example", owner: "org-example) {
pullRequest(number: 49) {
id
}
}
}
With pullRequest ID make a mutation:
Request POST to https://api.github.com/graphql
mutation MyMutation {
enablePullRequestAutoMerge(input: {pullRequestId: "$pullRequestID", mergeMethod: MERGE}) {
clientMutationId
}
}

How to order GitHub forks by stars, watchers, commits using the GitHub API?

For a given repository, we can view its forks by replacing 'username' and 'repo' in the following URL:
https://github.com/username/repo/network/members
Example: https://github.com/veracrypt/VeraCrypt/network/members
Question
Using the GitHub API, how can we order a repository's forks by
stars
watchers
commits (since forking)
Note: accessible wrappers are fine (e.g. using python library for GitHub API)
What I know so far
We could write a script to individually retrieve the metrics for each of the ~500 forks. This may be the best/simplest solution, although I do know GitHub has a lot of built in (hidden) features, so I do wonder if collecting the most active forks may have been considered and built into the API by GitHub. Iterating through hundreds/thousands of forks could also invoke the GitHub API's rate limiting, so please be mindful if using this approach.
Not a full answer, but it is possible to order by stargazers using GraphQL API (try GitHub GraphQL API Explorer for quick start!):
{
repository(owner: "vuejs", name: "vue") {
forks(orderBy: {field: STARGAZERS, direction: DESC}, first: 3) {
nodes {
nameWithOwner
stargazerCount
}
}
}
}
Result:
{
"data": {
"repository": {
"forks": {
"nodes": [
{
"nameWithOwner": "SmallComfort/react-vue",
"stargazerCount": 1198
},
{
"nameWithOwner": "supergaojian/vue",
"stargazerCount": 20
},
{
"nameWithOwner": "developedbyed/vue",
"stargazerCount": 13
}
]
}
}
}
}
Possible fields are:
enum RepositoryOrderField {
"""
Order repositories by creation time
"""
CREATED_AT
"""
Order repositories by name
"""
NAME
"""
Order repositories by push time
"""
PUSHED_AT
"""
Order repositories by number of stargazers
"""
STARGAZERS
"""
Order repositories by update time
"""
UPDATED_AT
}
I found a project for that: Useful Forks.
I have tried it and it works perfect as long as the fork didn't change its original name.

How to use Github GraphQL search for my own commits?

I'm trying to use the Github GraphQL to get a list of my organization's commits between this month and last month. I could just specify the first 100 commits and paginate backwards,
repository(owner: "myOrg", name: "myRepo") {
ref(qualifiedName: "master") {
target {
... on Commit {
history(first: 100) {
but it seems like the API should be set up to allow a more specific search so it doesn't have to do as much work. I know that you can query Repositories, Issues, and Users using a search query but this doesn't seem to do the same thing as just asking for the specific results I need.
What is best way to filter specific commits, or any information really, using GraphQL without making a ton of unnecessary requests?
Thanks!
Did you try since and until?
Like this:
repository(owner: "myOrg", name: "myRepo") {
ref(qualifiedName: "master") {
target {
... on Commit {
history(since: "2018-04-01T22:29:24+00:00", until: "2018-05-14T22:29:24+00:00") {

github graphql query for project contributors

I want to query using GitHub Graphql api for project contributors, can anyone give me any hints how to make it? Just been trying for some time, and I guess that I am missing some small element.
I'd like to get sth like https://api.github.com/repos/facebook/react/contributors?page=15 but only for amount of conttributions
Greetings!
updated: May, 2020.
Github GraphQL API currently don't support getting contributors of a repo.
You can get the collaborators though....
query {
repository(owner: "peek", name: "peek") {
id
name
collaborators(first: 10, affiliation: ALL) {
edges {
permission
node {
id
login
name
}
}
}
}
rateLimit {
cost
}
}
You need to have push rights to the repository to view the collaborators.
The Github graphql v4 API does not seem to support contributor nodes unless you have push access to a repo.
I get this error when i try to get a list of a repo's collaborators
"errors": [
{
"message": "Must have push access to view repository collaborators.",
"type": "FORBIDDEN",
The closest match to get the equivalent of v3 REST API url /repos/$USER/$REPO/contributors i could find is:
query {
repository(owner: "peek", name: "peek") {
id
name
mentionableUsers {
totalCount
}
}
}
It seems to count all contributors in a repository that could be matched to Github users (most probable through their emails).