Get info about several repositories from Github Graphql API in a single call - github

I am trying to create a query to Github GraphQL API that receive a list of repos as a parameter, and returns the info of those repositories in a single API call, does anyone know how to do that?
Something like this (I know this doesn't work)
query myOrgRepos($repos: [String!]!) {
search(query:"""
repo in $repos
""", type: REPOSITORY, first: 100) {
repo: nodes {
... on Repository{
name
description
updatedAt
}
}
}

It took me some time to understand your comment so I will post the answer in full. The key is to use Github's advanced search “syntax” in the query string, see here for examples.
The following query will find all repositories of user “github” as well as the graphql-js repository of user “graphql”:
query {
search(query: "user:github repo:graphql/graphql-js" type: REPOSITORY first: 10) {
nodes {
... on Repository {
name
description
updatedAt
}
}
}
}

My understanding of your question is that you want to input a list of "known" repos and output information about them. In this case you can form a query of the following type.
{
nodes(ids: ["node_id_1", "node_id_2"]) {
... on Repository {
nameWithOwner
createdAt
}
}
}
Here the ids are the node_ids of the repositories you are interested in. You can obtain these by individual GraphQL calls as follows
{
repository(owner: "owner", name: "name") {
id
}
}

Related

How to search branch_count and repository list by name in single network call of Github Api?

I am trying to use Github Search Api to show a list of repositories by name. My requirement is to show a list of all repositories matching name or description with string provided by user in search bar.
I was sucessfully able to do so by using this call `
https://api.github.com/search/repositories?q=swiftui&page=1&per_page=10
`
But my problem is it does not returns branch count, which is an essential to show. How can I do this without making multiple network calls.
I have tried some different methods using Rest API, but couldn't find any solution for branch count. So In the end Graphql was the solution for my problem.
let getReposQuery: String =
"""
query getRepos($searchText: String!) {
search(query: $searchText type: REPOSITORY first: 10) {
nodes {
... on Repository {
name,
description,
createdAt,
refs(first: 100, refPrefix:"refs/heads/") {
totalCount
nodes {
name,
id
}
}
}
}
}
}
"""
With this query, you need to provide searchText as variable.

In Github's GraphQL API how to get repositories that do not have a particular repositoryTopic?

With Github's GraphQL API I recently found "Github API: Getting topics of a Github repository" that mentions you can get a count of topics:
{
repository(owner: "twbs", name: "bootstrap") {
repositoryTopics(first: 10) {
edges {
node {
topic {
name
}
}
}
}
}
}
but in the docs and in my search I'm not finding how I can query repositories that do not contain the topic template, example:
query ($github_org: String!, $repo_count: Int!) {
organization(login: $github_org) {
repositories(first: $repo_count, privacy: PUBLIC, isFork: false) {
nodes {
id
name
openGraphImageUrl
createdAt
stargazerCount
url
description
repositoryTopics(first: 10, after: "template") {
edges {
node {
id
}
}
}
}
}
}
}
is the correct implementation to use after? In Github's GraphQL API how to exclude a repository if it contains a certain topic?
My understanding is that with the organization query you cannot do that kind of repository filtering.
Instead, you can use the search query with something like this:
query ($repo_count: Int!) {
search (first: $repo_count,
type: REPOSITORY,
query: "org:my-organization topic:my-topic") {
nodes {
... on Repository {
name
}
}
}
}
The above query searches for all repositories within a given organization that have a given topic.
But you were looking for the repositories without a topic, in theory according to the documentation you should be able to do that with -topic:my-topic. Unfortunately the negation on topic doesn't work, there seems to be a bug. :-(

Distinguish private/internal github repositories in graphql query

I'm trying to fetch all private repositories from our organization. In the following graphql query it returns both internal and private repositories.
For privacy it is only possible to filter on PRIVATE and PUBLIC.
It seems there is only one field called isPrivate, nothing like isInternal as far I can see.
query organizationRepositories($owner: String!) {
organization(login:$owner) {
repositories(first: 100, privacy: PRIVATE) {
totalCount
nodes {
owner {
login
}
name
id
url
isPrivate
}
}
}
}
All results using above query show isPrivate: true, I know for a fact some of these are internal and some are private.
Is there a way to distinguish between private and internal repositories? Either by looping over the results based on some fields combo which indicates private vs internal or by querying in a different way.
You can use a search query:
query {
search(query: "org:<my-org> is:internal", type: REPOSITORY, first: 100) {
repositoryCount
nodes {
... on Repository {
name
}
}
}
}
For more info, see https://docs.github.com/en/github/searching-for-information-on-github/searching-for-repositories#search-by-repository-visibility

GitHub's GraphQL API: How can I get all of the commits I've contributed to a particular public repo?

I'm trying to query GitHub's GraphQL API (v4). What GraphQL query for this API can be used to get all of the commits I've contributed to a particular public repo?
I figured out the solution. (The solution shown below is for getting relevant commits from master, but can easily be adapted for other qualifiedName values.)
First, perform an initial query to obtain user ID:
query {
viewer {
id
}
}
Then the following query can be used to get all contributed commits to a particular repository:
query($repository_owner:String!, $repository_name:String!, $user_id:ID!) {
repository(owner: $repository_owner, name: $repository_name) {
ref(qualifiedName: "master") {
target {
... on Commit {
history(author: {id: $user_id}) {
totalCount
nodes {
id
authoredDate
message
}
}
}
}
}
}
}
The query variables take the following form:
{
"repository_owner": "REPOSITORY_OWNER_STRING",
"repository_name": "REPOSITORY_NAME_STRING",
"user_id": "USER_ID_FROM_STEP_1"
}

How to get total number of commits using GitHub API

I am trying to collect some statistics about our project repositories on GitHub. I am able to get total number of commits for each contributor , but it is for default branch.
curl https://api.github.com/repos/cms-sw/cmssw/stats/contributors
The problem is , how can i get the same info for non-default branches , where i can specify a branch name. Is any such operation possible using GitHub API ?
thanks.
You should be able to use GitHub's GraphQL API to get at this data, although it won't be aggregated for you.
Try the following query in their GraphQL Explorer:
query($owner:String!, $name:String!) {
repository(owner:$owner,name:$name) {
refs(first:30, refPrefix:"refs/heads/") {
edges {
cursor
node {
name
target {
... on Commit {
history(first:30) {
edges {
cursor
node {
author {
email
}
}
}
}
}
}
}
}
}
}
}
With these variables:
{
"owner": "rails",
"name": "rails"
}
That will list out each of the author emails for each of the commits of each of the branches in a given repository. It would be up to you to paginate over the data (adding something like cursor: "b7aa251234357f7ddddccabcbce332af39dd95f6" after the first:30 arguments). You'd also have to aggregate the counts on your end.
Hope this helps.