How do I query the GitHub v4 API for directory contents at a certain tag? - graphql

How do I query the GitHub API v4 for the contents of a certain directory of a repository at a certain tag?
This is the best I've come up with so far:
query {
repository(owner:"example", name:"example") {
refs(refPrefix: "tags") {
}
}
}

From this post, you can get the GitObject with object to filter on branch:/path/folder and print the Tree. The following will get the tree from gson folder from tag gson-2.4 and print name, type and mode :
query {
repository(owner:"google", name:"gson") {
object(expression: "gson-2.4:gson") {
... on Tree{
entries{
name
type
mode
}
}
}
}
}

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

get rawTextBlob from all repository files matching path glob pattern

GraphQL beginner here. I am looking out for something like a SQL join so that I may feed the list of repository files from one query as criteria for another query.
I can get a nice result if I know the file path from each project like this:
{
group(fullPath: "group/path") {
projects {
nodes {
name
repository {
blobs(paths: ["global.tfvars"]) {
nodes {
rawTextBlob
}
}
}
}
}
}
}
But what if I don't know the exact file paths up front, e.g I need all the files matching environments/*/local.tfvars
If specifying a glob pattern is not an option, then I would think that need some kind of join with something like this:
{
group(fullPath: "group/path") {
id
name
projects {
nodes {
name
repository {
paginatedTree(path: "environments", recursive: true) {
nodes {
blobs {
nodes {
path
}
}
}
}
}
}
}
}
}

GraphQL using a field as argument in the same query

I have constructed the following query in Gitlab's GraphQL Explorer to show my question:
https://gitlab.com/-/graphql-explorer
{
project(fullPath: "gitlab-org/gitlab") {
name
repository {
rootRef
}
mergeRequests(state: merged, targetBranches: "master") {
nodes {
sourceBranch
targetBranch
}
}
}
}
What I would like to achieve is using the value of repository { rootRef } as the argument for targetBranches.
Something like this:
mergeRequests(state: merged, targetBranches: repository { rootRef }) {
Can it be done?
Thank you!

Graphql, return result only if record not null

Totally new in Gatsby (my app is build in here). Also new in Graphql but I go this project from a colleague and now I need to find a way to sort it out.
I have a query in graphql that looks as follow:
query MediasQuery {
allStrapiMedias(sort: { fields: [date], order: DESC }) {
edges {
node {
id
name
description
date(formatString: "Do MMMM, YYYY")
link
poster {
childImageSharp {
fluid {
src
}
}
}
file {
url
}
}
}
}
}
The issue I am facing is that the file record can exist or not, to be more accurate, if link record is empty, then I should find a file (this is how it's been build in Strapi).
If I have at least one record with file then the query run normally, but as soon as I deleted this record from database all the app crash.
How can I make that query to only return file if url is empty or null?
You are querying an object that will have one property (file) filled or not with an url. You just need to add that logic to your JavaScript and React code. When you will print the file, add the new logic, something like:
if (data.allStrapiMedias.edges.node.file) return <ComponentA/>
else return <ComponentB/>
I think that what you are looking for is something that the previous snippet. However, answering your question, you achieve it by adding filters to your query:
query MediasQuery {
allStrapiMedias(
sort: { fields: [date], order: DESC }
filter: { file: {url: {ne: null }}}
) {
edges {
node {
id
name
description
date(formatString: "Do MMMM, YYYY")
link
poster {
childImageSharp {
fluid {
src
}
}
}
file {
url
}
}
}
}
}
More details in GraphQL Query Options Reference.

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

Resources