Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to query for commits in a given Repository #62

Open
SashaCollins opened this issue Apr 30, 2020 · 4 comments
Open

How to query for commits in a given Repository #62

SashaCollins opened this issue Apr 30, 2020 · 4 comments
Labels

Comments

@SashaCollins
Copy link

Hello,

I'm trying to convert this query into a struct:

{
  repository(owner: "google", name: "gson") {
    name
    refs(first: 100, refPrefix: "refs/heads/") {
      edges {
        node {
          name
          target {
            ... on Commit {
              id
              history(first: 2) {
                totalCount
                edges {
                  node {
                    ... on Commit {
                      committer {
                        date
                        email
                        name
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

I got this from https://stackoverflow.com/questions/15919539/github-api-how-to-efficiently-find-the-number-of-commits-for-a-repository

I tested it on the Github explorer just to be safe and its working as it should.
What I'm trying to do is to get the "TotalCount" of commits per contributor to a specific Repository.
I'm not really sure how I'm supposed to do it exactly.
I already got this:

type repositoryInfo struct {
	Repository struct {
		Refs struct {
			TotalCount githubv4.Int //number of branches
			Nodes []ref
			PageInfo pageInfo
		}`graphql:"refs(refPrefix:$prefix,first:$refFirst,after:$refAfter,orderBy:$orderBy)"`
	} `graphql:"repository(owner:$login,name:$repositoryName)"`
}

type ref struct {
	Name githubv4.String
	Prefix githubv4.String

	Target struct {
		AbbreviatedOid githubv4.String
		ID githubv4.GitObjectID
		//History struct {
		//	TotalCount githubv4.Int
		//}`graphql:"history(first:0)"`
	}`graphql:"... on Commit"`
}

But I get this error message:

Fragment on Commit can't be spread inside Ref

However I can't seem to find any useful documentation.
Can anybody tell me what I'm doing wrong please?

@sshmaxime
Copy link

@SashaCollins Did you figured out how to do it ?

@Edwin-Luijten
Copy link

Having the same message on a different query: Fragment on Repository can't be spread inside SearchResultItemEdge

@chetan
Copy link

chetan commented Oct 19, 2021

I got this working with the following:

type LatestContributions struct {
	User struct {
		Repositories struct {
			TotalCount int
			Nodes      []struct {
				Name      string
				IsPrivate bool
				PushedAt  githubv4.DateTime

				DefaultBranchRef struct {
					Name   string
					Target struct {
						SpreadCommits struct {
							History struct {
								TotalCount int
							} `graphql:"history(since:"2021-09-19T23:05:23Z")"`
						} `graphql:"... on Commit"`
					}
				}
			}
		} `graphql:"repositories(first: 10, orderBy: {field: PUSHED_AT, direction: DESC})"`
	} `graphql:"user(login: $login)"`
}

The missing bit in the top post is the stub/placeholder SpreadCommits which gets overridden in the generated query by the tag value ... on Commit

@jay-418
Copy link

jay-418 commented Feb 15, 2023

@chetan 's example helped solve my issue with error Fragment on ProjectV2Field can't be spread inside ProjectV2 while trying to implement a struct for this query:

query {
    repository(owner:"orgName", name:"repoName") {
        projectV2(number:123){
            field(name:"Example") {
	        on ProjectV2Field {
                    name
                    id
                }
            }
        }
    }
}

The solution was to create the Spread struct to be overwritten with ... on ProjectV2Field, much like @chetan used SpreadCommits and ... on Commit:

var q struct {
	Repository struct {
		ProjectV2 struct {
			Field struct {
				Spread struct {
					Name string
					Id   string
				} `graphql:"... on ProjectV2Field"`
			} `graphql:"field(name: $fieldName)"`
		} `graphql:"projectV2(number: $projectNumber)"`
	} `graphql:"repository(owner: $owner, name: $name)"`
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

6 participants