GitHub graphQL API multiple queries on organizations & repositories - github

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
}

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

Get status data for list of GitHub logins (users) using GraphQL in efficent way

What I would like to do is to get specific data from Status for a group of 10-40 GitHub users from the organization. I would like to get the data in one batch. Currently, my solution queries the data using for loop in the code and a query below:
{
user(login: "user1") {
status {
indicatesLimitedAvailability
updatedAt
createdAt
}
The disadvantage of this solution is I have to use sleep not to trigger API calls per minute limit in GitHub.
Would it be possible to transform the query to something like this (pseudocode):
{
user(login: "user1", "user2", "user3", ...) {
status {
indicatesLimitedAvailability
updatedAt
createdAt
}
And some kind of list in the response?
You can use the following query:
{
nodes(ids: ["node_id1", "node_id2"]) {
... on User {
status {
indicatesLimitedAvailability
updatedAt
createdAt
}
}
}
}
Where node_id is the id of the users you want to query for.
You can obtain these using the following query:
{
user(login: "username") {
id
}
}
Or you could get all members in an organisation with the following query
{
organization(login: "org_name") {
membersWithRole(first: 100) {
nodes {
login
id
}
}
}
}

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
}
}
}
}
}
}
}
}
}
}
}
}
}
}

Get last x commits from Github repo using Github Api V4

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
}
}
}
}