Github GraphQL API Get all commits of a User - graphql

I'm trying to use the GitHub GraphQL API to get all the additions made by a user (additions can be found from their commits). I've been able to get the additions from pull requests, but I haven't found a way to do the same for commits. How can I get all the commits from a user?
This is my query (I am new to GraphQL):
query AllAdditions($username: String!, $from: DateTime, $to: DateTime) {
user(login: $username) {
name
contributionsCollection(from: $from, to: $to) {
commitContributionsByRepository(maxRepositories: 100) {
repository {
nameWithOwner
}
contributions(first: 30) {
totalCount
# I'm trying to get additions like this, but there is no 'commit' field
# nodes {
# commit {
# additions
# }
# }
}
}
pullRequestContributionsByRepository(maxRepositories: 100) {
repository {
nameWithOwner
}
contributions(first: 30) {
nodes {
pullRequest {
additions
}
}
}
}
}
}
}

{
search(query: "org:test", type: REPOSITORY, last: 100) {
nodes {
... on Repository {
name
defaultBranchRef {
name
target {
... on Commit {
history(first: 100, since: "2013-07-11T00:00:00") {
totalCount
nodes {
... on Commit {
committedDate
additions
author {
name
email
}
}
}
}
}
}
}
}
}
}
}
As for getting all orgs, there isn’t currently a way to do that via the GraphQL API because the search connection requires something in the search query. There’s also no top-level connection that allows you to list all users, orgs, or repositories.
I hope that helps!
answer copied from this URL: GraphQL - Get all commits by all users

Related

GitHub GraphQL API Read custom field in Project V2

We are using Github Projects V2. I have created a custom field say 'MyCustomField'.
I want to read the value of custom field MyCustomField using Github GraphQL API.
I am following Gtihub GraphQL API Docs
So far I have got till reading a few predefined fields on Gtihub Issues like title, url, assignees and labels. I am using Windows PowerShell:
$project_id="MyProjectIDFetchedUsingDifferentQuery"
gh api graphql -f query='
query($project_id: ID!){
node(id: $project_id) {
... on ProjectV2 {
items(last: 20) {
nodes{
id
content{
...on Issue {
title
url
assignees(first: 10) {
nodes{
login
}
}
labels(first:5) {
edges {
node {
name
}
}
}
}
}
}
}
}
}
}' -f project_id=$project_id
I am not able to find a way to get the custom field MyCustomField.
I am expecting to write some query like below:
$project_id="MyProjectIDFetchedUsingDifferentQuery"
gh api graphql -f query='
query($project_id: ID!){
node(id: $project_id) {
... on ProjectV2 {
items(last: 20) {
nodes{
id
content{
...on Issue {
title
url
assignees(first: 10) {
nodes{
login
}
}
labels(first:5) {
edges {
node {
name
}
}
}
customFields(first:5) {
nodes {
name
}
}
}
}
}
}
}
}
}' -f project_id=$project_id
I came up with this which I think should get the custom fields on your board:
query {
node(id: "(my project id)") {
... on ProjectV2 {
items(last: 20) {
nodes {
id
content {
... on Issue {
title
url
state
assignees(first: 10) {
nodes {
login
}
}
}
}
fieldValues(first: 20) {
nodes {
... on ProjectV2ItemFieldSingleSelectValue {
field {
... on ProjectV2SingleSelectField {
name
}
}
name
id
}
... on ProjectV2ItemFieldLabelValue {
labels(first: 20) {
nodes {
id
name
}
}
}
... on ProjectV2ItemFieldTextValue {
text
id
updatedAt
creator {
url
}
}
... on ProjectV2ItemFieldMilestoneValue {
milestone {
id
}
}
... on ProjectV2ItemFieldRepositoryValue {
repository {
id
url
}
}
}
}
}
}
}
}
}
Credit should go to https://stackoverflow.com/users/2312060/rich-kuzsma for posting https://gist.github.com/richkuz/e8842fce354edbd4e12dcbfa9ca40ff6
This had the basic format for querying ProjectV2 fields, and I added the
ProjectV2SingleSelectField bit to include both the field name and its value in the response.

Github GraphQL API v4 Query on CommitAuthor

I am trying to run the following query on Githubs GraphQL api:
{
user(login: "davekaj") {
id
repositories(first: 10, orderBy: {field: NAME, direction: ASC}) {
nodes {
ref(qualifiedName: "master") {
target {
... on Commit {
history(first: 15, author: "WHAT DO I PUT HERE") {
totalCount
nodes {
additions
author {
name
user {
id
}
}
committedDate
deletions
}
}
}
}
}
}
}
}
}
It wants me to filter on a CommitAuthor for history(author: ). I tried passing my username, or my unique user ID, but it doesn't work. I am essentially passing it a string, but it wants the type CommitAuthor. How do I pass a CommitAuthor value?
It isn't clear to me, and I searched through the docs and the schema and I couldn't find anything.
Please help!
Ah, so the answer is actually very simple once I looked at the graphql documentation (rather than just the github documentation). CommitAuthor is an input type, which is described here https://graphql.org/graphql-js/mutations-and-input-types/.
The result is you pass an object of CommitAuthor. In this case I just have to pass the id, which looks like this: author: {id: "MDQ6VXNlcjIyNDE3Mzgy"}
See the completed code below.
{
user(login: "davekaj") {
id
repositories(first: 10, orderBy: {field: NAME, direction: ASC}) {
nodes {
ref(qualifiedName: "master") {
target {
... on Commit {
history(first: 15, author: {id: "MDQ6VXNlcjIyNDE3Mzgy"}) {
totalCount
nodes {
additions
author {
name
user {
id
}
}
committedDate
deletions
}
}
}
}
}
}
}
}
}

How to fetch only public repositories from my Github account

So I'm completely new to GraphQL, could anyone tell me how to change below code into retrieving only public repos from my account?
I tried to use "search" attribute but I can't get it working.
Thanks
const {
github: {
repositoryOwner: {
repositories: { edges },
},
},
} = useStaticQuery(graphql`
{
github {
repositoryOwner(login: "SzBor") {
repositories(first: 6, orderBy: { field: CREATED_AT, direction: ASC }) {
edges {
node {
id
name
url
description
}
}
}
}
}
}
`)
you can use the arguments privacy: PUBLICon repositories Ex:
const {
github: {
repositoryOwner: {
repositories: { edges },
},
},
} = useStaticQuery(graphql`
{
github {
repositoryOwner(login: "SzBor") {
repositories(first: 6, privacy: PUBLIC, orderBy: { field: CREATED_AT, direction: ASC }) {
edges {
node {
id
name
url
description
}
}
}
}
}
}
`)
The stranger is that the (API V4 document) is not updated with this information, but you can test on explorer :)

Github v4 GraphQL API. Get commits by date

New to graphql and the Github API. I'm trying to get a list of commits back, sorted by date. This is what I have so far:
{
repository(owner: "facebook", name: "create-react-app") {
ref(qualifiedName: "master") {
target {
... on Commit {
id
history(first: 20) {
pageInfo {
hasNextPage
}
edges {
node {
messageHeadline
oid
message
}
}
}
}
}
}
This just returns a flat list of the commit history. Is there another object or connection that I can use to group the results by date?
Thanks
There is no notion of "groupBy" or "GroupedBy" in the GraphQL API v4 reference.
All you have is an orderBy argument for repository.
You can see an example here
{
organization(login: "lvtech") {
repositories(first: 3, orderBy: {field: PUSHED_AT, direction: DESC}) {
edges {
node {
name
}
}
}
}
}
But again, that applies to repository attributes, not to commit attributes.

Github GraphQL Repository Query, using two objects

How to use more than one objects when querying Github GraphQL?
The following will break if the 2nd object is uncommented:
query {
repository(owner:"rails", name:"rails") {
object(expression:"master") {
... on Commit {
history {
totalCount
}
}
}
# object(expression: "master:README.md") {... on Blob {byteSize}}
}
}
How to make it working? Thx
Use alias:
query {
repository(owner:"rails", name:"rails") {
object(expression:"master") {
... on Commit {
history {
totalCount
}
}
},
second_object: object(expression: "master:README.md") {... on Blob {byteSize}}
}
}

Resources