GitHub GraphQL to Read Repository Contents - github

I'm looking for a way to use the GitHub GraphQL to read repository contents (paths) and then provide a second query to grab the contents of the full path. I started heading down this path for the second query, and it's failing. The former is the more important issue for me right now.
query{
viewer {
login
name
repository(name:"myrepo") {
id
descriptionHTML
object(expression: "branch:readme.md") {
id
}
}
}
}
}

You had an extra } in your query, this is why it was failing.
You also want to replace "branch" with the actual branch name ("master" for example)
Here's a complete example that will also get you the file contents:
{
viewer {
login
name
repository(name: "git-point-playground") {
id
descriptionHTML
object(expression: "master:README.md") {
id
... on Blob {
text
}
}
}
}
}

Related

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

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

How can I access github repository contents using graphql (v4 api)?

If I use github v3 api to access the directory contents of a public repository using the following query:
curl https://api.github.com/repos/w3c/webappsec/contents/
what is the equivalent in graphql?
I can get for example the description of the repository by sending the following to: https://api.github.com/graphql
query TestQuery{
repository(owner:"w3c" name:"webappsec"){
description
}
}
But how can I get the contents of a repository's directory?
You can use object(expression: "branch_name:") and list the tree entries:
{
repository(owner: "w3c", name: "webappsec") {
object(expression: "master:") {
... on Tree {
entries {
name
type
mode
}
}
}
}
}
Try it in the explorer

How to search Github GraphQL users by Company

I want to find all users that have set their "Company" field in their Github Homepage to "Github". I see it is a defined field in the Github Docs at https://developer.github.com/v4/object/user/. However, I can't seem to search for it.
My current query on https://developer.github.com/v4/explorer/ returns no results
{
search(query: "company:Github", type: USER, first: 100) {
userCount
edges {
node {
... on User {
login
name
company
}
}
}
}
}
To search all users by the company, query on the organization. Currently, you are passing a key-value pair as a string to search for instead of value string. Try this query to get all list of employees who have set their company name Github.
{
organization(login: "github") {
email
members(first: 100) {
totalCount
nodes {
... on User {
company
login
name
isEmployee
}
}
}
}
}
See the results in the attached image:

How to get Readme.MD from Github Graphql API?

The v3 has a specific API for retrieving the readme.md file. But in the new V4 GraphQL, there is no such field in the Repository Object.
Does anyone know how to retrieve the readme file?
Thanks!
There is not yet a specific entity to get the README.md file, but you can retrieve it as you would normally retrieve any other file:
{
repository(owner: "gitpoint", name: "git-point") {
object(expression: "master:README.md") {
... on Blob {
text
}
}
}
}
It looks like because the GitObject implements Blob, you can use the "... on" syntax to access it's properties, which will contain the contents of the object.
In order to access the object in question, pass in the branch and filename with the extension in the format "branch:filename.ext" and retrieve the Blob from the result, and the text from that.
Multiple objects can be retrieved simultaneously, allowing you to check for alternate casings, such as lowercase "readme.md" names. Simply provide aliases for the objects. Example below.
{
repository(owner: "owner", name: "name") {
upCase: object(expression: "master:README.md") {
... on Blob {
text
}
}
object(expression: "master:readme.md") {
... on Blob {
text
}
}
otherFile: object(expression: "master:index.js") {
... on Blob {
text
}
}
}
This may help explain the "... on" syntax.
https://graphql.github.io/graphql-spec/June2018/#sec-Inline-Fragments

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.