githubv4 icon indicating copy to clipboard operation
githubv4 copied to clipboard

How to query for commits in a given Repository

Open SashaCollins opened this issue 4 years ago • 4 comments

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?

SashaCollins avatar Apr 30 '20 16:04 SashaCollins

@SashaCollins Did you figured out how to do it ?

sshmaxime avatar Dec 21 '20 11:12 sshmaxime

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

Edwin-Luijten avatar Mar 30 '21 08:03 Edwin-Luijten

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

chetan avatar Oct 19 '21 21:10 chetan

@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)"`
}

jay-418 avatar Feb 15 '23 00:02 jay-418