using graphql github api to get commit information by Id - graphql

I would like to get commit details for a specific commit given that I already have the commit id
for example, if I know the commit id is
12762b76cba8ac4623a6c16e1fe60efafa3b7d1c and the repo is ruby/ruby
how do I get the committedDate and author email?

You can try using a cursor as an edge type, as in this thread:
{
repository(owner: "octocat", name: "Hello-World") {
first: object(expression: "master") {
... on Commit {
history(path: "README", last: 1, before: "762941318ee16e59dabbacb1b4049eec22f0d303 1") {
edges {
node {
committedDate
oid
author
{
email
}
}
}
}
}
}
}
}
you can test it using this explorer https://developer.github.com/v4/explorer/

Related

How can I get the project name for a timelog issue entry using the project ID from a Gitlab GraphQL query

I'm using the graphql explorer for gitlab and I'm trying to get timelog entries for a given user (as below).
The response gives me a projectId which I assume I'd have to use in a subsequent query (I don't believe I can get it in the same query?), but the project object doesn't let me find the project by ID, only fullpath.
Am I correct in understanding that I would need to use the projects query and provide the id there? In trying that (query below), I just get the error 12345678 is not a valid GitLab ID. (obviously I'm using a real ID given previously).
So, two points/questions I guess:
The timelog entry provides a projectId which I can't use in either the project or projects query to get the project name?
I can't just return the project name directly in the same query as the original user timelog one?
Any thoughts greatly appreciated.
{
users(usernames: ["user1"]) {
nodes {
id
username
timelogs(startDate: "2022-05-03T09:00:00") {
nodes {
issue {
projectId
}
timeSpent
}
}
}
}
}
{
projects(ids:[12345678]) {
nodes {
name
}
}
}
I don't know if this helps, but I use:
{
group(fullPath: "groupname") {
name
timelogs(startTime: "2022-04-01", endTime: "2022-04-30", username: "roberto") {
pageInfo {
hasNextPage
endCursor
}
nodes {
user {
username
}
issue {
iid
webUrl
state
projectId
title
}
spentAt
timeSpent
summary
note {
body
url
}
}
}
}
}

Github Graphql API create repo

I am using below query to create repo:
gh api -i graphql -f query='
mutation {
createRepository(input: {name: "gh-create-test", visibility: PRIVATE, ownerId: "ID"}) {
repository {
url
}
}
}
'
THe repo gets created successfully but without admin rights whereas when I create repo from GITHUB UI the repo has admin rights. Can anyone please help what's the issue in this query?
I just created one, Make sure you are getting the id first and pass it to the createRepository
query{
repositoryOwner(login:"yourLoginId"){
id
login
repositories(first:1) {
edges {
node {
id
}
}
}
}
}
and create a repository by passing the ownerid
mutation {
createRepository(input: {name: "gh-create-test", visibility: PRIVATE, ownerId: "idobtainedabove"}) {
repository {
url
}
}
}

How to query data from GitHub Discussions using GitHub's GraphQL API

I want to query the data from https://github.com/prisma/prisma/discussions via GitHub's GraphQL API but I don't know how to access it.
GitHub Discussions are a beta feature that's currently available only on a few repos, prisma/prisma being one of them. I would have expected that I can query the data in a similar fashion to querying issues and pull requests of a repo:
{
repository(
owner: "prisma"
name: "prisma"
) {
pullRequests(first: 5 orderBy: {
field: CREATED_AT,
direction: DESC
}) {
edges {
node {
title
}
}
}
issues(first: 5 orderBy: {
field: CREATED_AT,
direction: DESC
}) {
edges {
node {
title
}
}
}
}
}
I'm basically looking for a query that would allow me to do this:
{
repository(
owner: "prisma"
name: "prisma"
) {
# this doesn't work
discussions(first: 5 orderBy: {
field: CREATED_AT,
direction: DESC
}) {
edges {
node {
title
}
}
}
}
}
But such a discussions property doesn't exist on the Repository type in the GitHub API.
EDIT (Dec 8, 2020): I learned from the GitHub team that the data is not yet available via the public GraphQL API but they're working on releasing it soon. I'll leave the question open until it's available.

How do I get file contents from github apiv4 on the default branch?

I've had a lot of success pulling README.md content from the github v4 syntax found in this issue as follows:
{
repository(owner: "gitpoint", name: "git-point") {
defaultBranchRef {
name
}
object(expression: "master:README.md") {
... on Blob {
text
}
}
}
}
My issue comes when defaultBranchRef.name is not master. We can expect this to be the case more frequently moving forward as people move away from that naming convention for various reasons.
How do I change the expression to reference the repo's default branch name if I don't know it till I query? Or must I make 2 queries per repo?
You can use HEAD:[path] as expression to get the default branch :
{
repository(owner: "gitpoint", name: "git-point") {
object(expression: "HEAD:README.md") {
... on Blob {
text
}
}
}
}

Github GraphQl - How to get a list of commits between tags

With Github GraphQL I want to answer the question:
What commits have been merged into master between releases/tags?
The result should be similar to the results for this question Get commit list between tags in Git if I were to do it on the command line.
I'm using the developer explorer and wondering if I will be able to do this with a single query or if I will need several. I tried the following but it does not give me the commits between tags that have not been tagged, just the tagged commits.
{
repository(owner: "CoolCompany", name: "awesome-new-ui") {
refs(refPrefix: "refs/tags/", first: 2, orderBy: {field: TAG_COMMIT_DATE, direction: DESC}) {
edges {
node {
id
name
target {
oid
... on Commit {
author {
date
email
name
}
message
}
}
}
}
}
}
}
#lee-dohm from the Github GraphQL community helped me arrive at a solution which is posted here
I can paste my solution here as well. It seems this problem is not solve-able with a single query, but it can be done with 2 that work in conjunction with each other:
Step 1: Get the most recent release information. You could modify this for tags as well.
{
repository(owner: "CoolCompany", name: "awesome-ui") {
releases(last: 1) {
edges{
node{
tagName
createdAt
}
}
}
}
}
Step 2: Use the value from the createdAt (associated with the release or tag) and do this:
{
repository(owner: "CoolCompany", name: "awesome-ui") {
nameWithOwner
object(expression: "master") {
... on Commit {
oid
history(first: 100, since: "$createdAtDate") {
nodes {
oid
messageHeadline
author {
user {
login
}
}
committedDate
}
}
}
}
}
}

Resources