Get last x commits from Github repo using Github Api V4 - github

I'm trying to use the new Github GraphQL api (v4) and I don't seem to be able to figure out how to get the last x commits for master. I've used repository and ref but they still don't give me what I need.
The query below almost gives me what I need:
query{
repository(owner: "typelevel", name: "cats") {
refs(refPrefix:"refs/heads/", last: 5) {
edges{
node {
associatedPullRequests(states: MERGED, last: 5) {
edges{
node {
title
baseRef {
name
prefix
}
baseRefName
commits(last: 10) {
edges {
node {
commit {
abbreviatedOid
message
}
}
}
}
}
}
}
}
}
}
}
}
but:
doesn't seems to exactly match what is in the repo
limited to PRs
seems too unwieldy
I also tried using defaultBranchRef but that didn't work either:
query{
repository(owner: "typelevel", name: "cats") {
defaultBranchRef {
name
prefix
associatedPullRequests(states: [MERGED], last: 5) {
edges {
node {
title
}
}
}
}
}
}
I've been testing the queries using the explorer app on the Github api page.
Any ideas?

I was able to get this working with:
query {
repository(owner: "typelevel", name: "cats") {
ref(qualifiedName: "master") {
target {
... on Commit {
history(first: 10) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
oid
messageHeadline
}
}
}
}
}
}
}
}
by modifying this query linked to on the Github Platform Community.

Would using history be better in this case?
See this thread
A "ref" (short for reference) is anything that points to a git commit. This could be a local branch, a tag, a remote branch, etc. So master, for example, would be considered a ref.
In that vein, you can use the ref field on the Repository type to get a reference that targets a commit.
From that commit, you can get all of the commit's parents. If you target master, you can get the main history of the git repository.
query {
node(id: "MDEwOlJlcG9zaXRvcnk4NDM5MTQ3") {
... on Repository {
ref(qualifiedName: "master") {
target {
... on Commit {
id
history(first: 30) {
totalCount
pageInfo {
hasNextPage
}
edges {
node {
oid
message
author {
name
email
date
}
}
}
}
}
}
}
}
}
}

Here's the shortest way that works on any default branch even if it's not named master
Last 10 commits
{
repository(owner: "fregante", name: "webext-fun") {
defaultBranchRef {
target {
... on Commit {
history(first: 10) {
nodes {
oid
}
}
}
}
}
}
}
Last 1 commit (HEAD)
{
repository(owner: "fregante", name: "webext-fun") {
defaultBranchRef {
target {
oid
}
}
}
}

Related

How to write a GraphQL query to retrieve all the workflows/runs from github

How to write a GraphQL query to retrieve all the workflows/runs from github
I tried below query for to get node id
organization(login: "abc") {
repositories(first: 100) {
nodes {
id
name
}
}
}
and below query to get workflow.
nodes(ids: "sdefrgrt") {
... on Workflow {
id
createdAt
name
updatedAt
}
}
Yes, this should be available via GraphQL (you can try this query out with Github's GraphQL explorer):
{
repository(owner: "$ORG", name: "$REPO") {
pullRequest(number: $NUMBER) {
commits(first: 5) {
edges {
node {
commit {
checkSuites(first: 5) {
nodes {
workflowRun {
url
workflow {
name
}
}
}
}
}
}
}
}
}
}
}
You'd adjust the first parameters, and the client would need to do some grouping on the data that comes back from GraphQL.
You may get the workflow runs via:
{
node(id: "YOUR-WORKFLOW-NODE-ID") {
... on Workflow {
runs(first: 10) {
nodes {
runNumber
createdAt
checkSuite {
commit {
message
associatedPullRequests(first: 1) {
nodes {
number
}
}
history {
nodes {
author {
date
email
name
}
}
}
}
}
}
}
}
}
}
Workflow's node_id could be taken from https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow

How to get all repos that contain a certain branch on Github's GraphQL API

I have many repositories and some of them contain a branch that has the same name. I want to be able to fetch all the repositories that contain a specific branch name. This is what I have so far but I can't seem to figure out how to add the necessary query.
{
repositoryOwner(login: "dev") {
repositories(first: 1) {
nodes {
name
refs(first: 15, refPrefix: "refs/heads/") {
edges {
node {
name
}
}
}
}
}
}
}
Any help would be greatly appreciated.
One way is to request all repositories which have a ref with qualifiedName as <branch_name>. Then in your client side remove all null results :
{
repositoryOwner(login: "JakeWharton") {
repositories(first: 100) {
nodes {
ref(qualifiedName: "gh-pages") {
repository {
name
description
}
}
}
}
}
}
Try it in the explorer
With curl & jq to exclude the null results it would be :
curl -s -H "Authorization: token YOUR_TOKEN" \
-d '{
"query": "{ repositoryOwner(login: \"JakeWharton\") { repositories(first: 100) { nodes { ref(qualifiedName: \"gh-pages\") { repository { name } } } } } }"
}' https://api.github.com/graphql | \
jq -r '.data.repositoryOwner.repositories.nodes[] | select(.ref != null) | .ref.repository.name'
You will have to go through pagination if there are more than 100 repos
You can also use aliases in case you need to find a combination of branch name in a single repo (or one of them). For instance looking for branch gh-pages & 1.0 :
{
repositoryOwner(login: "JakeWharton") {
repositories(first: 100) {
nodes {
branch1: ref(qualifiedName: "1.0") {
repository {
name
description
}
}
branch2: ref(qualifiedName: "gh-pages") {
repository {
name
description
}
}
}
}
}
}
Try it in the explorer

GitHub graphQL API multiple queries on organizations & repositories

I am trying to get from GitHub using graphQL multiple organizations & repositories data. The code i wrote below get only 1 organization & repository.
I thought using variables of two Array [String!] one for Organizations & the second for Repositories.
So
organization(login:"javascript") {...}
should be
organization(login:$organization) {...}
and
repository(owner:"javascript", name:"sorted-array") {...}
should be
repository(owner:$organization, name:$repository) {...}
But i couldn't find how to implement the variables into the below code.
query {
rateLimit{
cost
remaining
resetAt
}
organization(login:"javascript") {
repositories{
totalCount
}
}
repository(owner:"javascript", name:"sorted-array") {
updatedAt
branches: refs(refPrefix:"refs/heads/") {
totalCount
}
tags: refs(refPrefix:"refs/tags/") {
totalCount
}
releases {
totalCount
}
object(expression:"master") {
... on Commit {
committedDate
history {
totalCount
}
}
}
}
}
Will appreciate the help.
Thanks
Here is the request updated to work with variables
query getOrg($owner: String!, $repo: String! ){
organization(login:$owner) {
repositories{
totalCount
}
}
repository(owner:$owner, name:$repo) {
updatedAt
branches: refs(refPrefix:"refs/heads/") {
totalCount
}
tags: refs(refPrefix:"refs/tags/") {
totalCount
}
releases {
totalCount
}
object(expression:"master") {
... on Commit {
committedDate
history {
totalCount
}
}
}
}
}
However, you may be better served by using GraphQL Node IDs as an array of inputs. With the Node ID of the Organization or Repository, something along the lines of...
query inputArray($id: [ID!]!){
nodes(ids: $id){
...on Repository{
id
}
}
}
Note that GitHub V3 of the API supports returning the GraphQL Node IDs to help transition to GraphQL queries.
Here are my solutions i hope it will be helpful to someone:)
First answer is using the Array variables with multiple id's of the organizations "Facebook","JavaScript" and
the repositories "360-Capture-SDK","sorted-array".
If you have 10 ,20 organizations/repositories or more , you will have a great time fetching the id's from the REST API:).
query inputArray($idOrg: [ID!]! $idRepo: [ID!]!){
orgNode:nodes(ids: $idOrg){
...on Organization{
name
}
}
repNode:nodes(ids: $idRepo){
...on Repository{
name
}
}
}
{
"idOrg": ["MDEyOk9yZ2FuaXphdGlvbjY5NjMx","MDEyOk9yZ2FuaXphdGlvbjE3ODIxODA="],
"idRepo":["MDEwOlJlcG9zaXRvcnk4Njg2MDg0Nw==","MDEwOlJlcG9zaXRvcnk5NzkxMTYy"]
}
Second answer is using a more readable approach though cumbersome.
query{
FacebookOrg: organization(login: "facebook") {
...OrgInfo
}
JavaScriptOrg: organization(login: "javaScript") {
...OrgInfo
}
FacebookRep: repository(owner: "facebook" name:"360-Capture-SDK"){
...RepInfo
}
JavaScriptRep: repository(owner: "javaScript" name:"sorted-array"){
...RepInfo
}
}
fragment OrgInfo on Organization {
name
}
fragment RepInfo on Repository {
name
}

How to query review requests by user, using Github's v4 GraphQL API?

Given a user's id, I want to get all pull requests where they are a requested reviewer.
The following won't work as it only allows me to get pull requests opened by that user:
query {
node(id: "$user") {
... on User {
pullRequests(first: 100) {
nodes {
reviewRequests(first: 100) {
nodes {
requestedReviewer {
... on User {
id
}
}
}
}
}
}
}
}
}
Is there a way to do this?
Thanks!
You can get username from the user id and perform a search with review-requested to match a user requested for review :
get username :
{
node(id: "MDQ6VXNlcjk2OTQ3") {
... on User {
login
}
}
}
get open PR where user is review requested :
{
search(query: "type:pr state:open review-requested:refack", type: ISSUE, first: 100) {
issueCount
pageInfo {
endCursor
startCursor
}
edges {
node {
... on PullRequest {
repository {
nameWithOwner
}
number
url
}
}
}
}
}
You can define the query string as a variable, try it in the explorer

Github GraphQL to recursively list all files in the directory

I want to use the GraphQL Github API to recursively list all files contained in the directory. Right now my query looks like this:
{
search(first:1, type: REPOSITORY, query: "language:C") {
edges {
node {
... on Repository {
name
descriptionHTML
stargazers {
totalCount
}
forks {
totalCount
}
object(expression: "master:") {
... on Tree {
entries {
name
type
}
}
}
}
}
}
}
}
However, this only gives me only the first level of directory contents, in particular some of the resulting objects are again trees. Is there a way to adjust the query, such that it recursively list the contents of tree again?
There is no way to recursively iterate in GraphQL. However, you can do so programmatically using a query variable:
query TestQuery($branch: GitObjectID) {
search(first: 1, type: REPOSITORY, query: "language:C") {
edges {
node {
... on Repository {
object(expression: "master:", oid: $branch) {
... on Tree {
entries {
oid
name
type
}
}
}
}
}
}
}
}
Start with a value of null and go from there.
working example
More info: https://docs.sourcegraph.com/api/graphql/examples
But probably this will change in the near feature. For example latest github version is v4 https://developer.github.com/v4/explorer/
{
search(first: 1, type: REPOSITORY, query: "language:C") {
edges {
node {
... on Repository {
name
descriptionHTML
stargazers {
totalCount
}
forks {
totalCount
}
object(expression: "master:") {
... on Tree {
entries {
name
object {
... on Tree {
entries {
name
object {
... on Tree {
entries {
name
}
}
}
}
}
}
}
}
}
}
}
}
}
}