Get latest release without prerelease in GraphQL - github

I'm migrating my connection with GitHub REST API to GraphQL API and I'm confused about getting latest release.
When I use this endpoint bellow to get latest release with REST API it will never return Draft releases or prereleases.
/repos/:owner/:repo/releases/latest
But, when I do the same with GraphQL API I can't filter that, using the query bellow I get the latest release but if it is and prerelease I'll have to query again to find another one.
{
InovaFarmaApi: repository(owner: "precisaosistemas", name: "inovafarma-api") {
...releaseData
}
}
fragment releaseData on Repository {
releases (last: 2) {
nodes {
isPrerelease
}
}
}
Can I filter for only release and not Draft releases or prereleases?

It doesn't look like it.
The GitHub developer documentation has a complete listing of all GraphQL object types, their fields, and their associated parameters. A Repository in particular documents its releases field; that has the Relay connection parameters and an ordering, but the only supported ReleaseOrderField values are CREATED_AT and NAME.
That means you need to repeat calls to page through the releases until you find one that meets your criteria.
query GetRelease($owner: String!, $name: String!, $cursor: String) {
repository(owner: $owner, name: $name) {
releases(before: $cursor,
last: 1,
orderBy: {field: CREATED_AT, order: DESC}) {
pageInfo { hasPreviousPage, startCursor }
nodes {
isPrerelease
...OtherReleaseData
}
}
}
}
If you hit a release where isPrerelease is true (which you don't want), and hasPreviousPage is also true, re-run the query passing the previous startCursor value as the cursor argument.

Related

How to query GitHub deployments using GraphQL and use createdAt as a property?

I would like to query my organization deployments using GraphQL and use createdAt as a property (I am doing the same thing with a pull request query : createdAt:"2022-01-01..2022-01-31").
For now, the only way I've found would be to iterate over my repositories, sort deployments by creation date, and stop the query if I reached my limit but I don't think that's efficient.
{
repository(owner: "my_organization", name: "my_repo") {
deployments(orderBy: {field: CREATED_AT, direction: ASC}) {
edges {
node {
state
createdAt
}
}
}
}
}

Getting commit data on File list Github GraphQL API

I'm new to the GraphQL API for Github. I'm trying to get the commit message and pushedAt date on each file, which you typically see on the root webpage of any github repo showing all the files for the branch (e.g. https://github.com/Alamofire/Alamofire ).
.github Updates for Xcode 13.3 (#3576) 3 days ago
Tests Fix 2020 and 2021 Deprecation Warnings (#3555) last month
My graphql seems to be returning nothing ("commitData": {}) in the Explorer for the commit data. What am I doing wrong?
fragment Files on Tree {
entries {
oid
name
type
path
extension
metaData: object {
... on Blob {
byteSize
isBinary
}
}
commitData: object {
... on Commit {
oid
pushedDate
message
}
}
}
}
query Files($owner: String!, $repo: String!, $branch: String!) {
repository(owner: $owner, name: $repo) {
... on Repository {
object(expression: $branch) {
... on Tree {
...Files
}
}
}
}
}
Query Variables:
{
"repo": "Alamofire",
"branch": "HEAD:",
"owner": "Alamofire"
}
This is expected. The TreeEntry object: GitObject does not expose commit information. Rather, it returns a Blob type (for file objects) or Tree type (for directory paths). How do we know this? The documentation for object ("Entry file object") provides a vague hint, but we can be sure by introspecting the GraphQL __typename meta field to get the object type name:
metaData: object {
__typename
}
For a file entry, this results in:
"metaData": {
"__typename": "Blob"
}
Even though Commit also implements the GitObject interface, the TreeEntry type does not return it. In other words, you can't get there from here. Using this API you need a two step query, it seems.

Get info about several repositories from Github Graphql API in a single call

I am trying to create a query to Github GraphQL API that receive a list of repos as a parameter, and returns the info of those repositories in a single API call, does anyone know how to do that?
Something like this (I know this doesn't work)
query myOrgRepos($repos: [String!]!) {
search(query:"""
repo in $repos
""", type: REPOSITORY, first: 100) {
repo: nodes {
... on Repository{
name
description
updatedAt
}
}
}
It took me some time to understand your comment so I will post the answer in full. The key is to use Github's advanced search “syntax” in the query string, see here for examples.
The following query will find all repositories of user “github” as well as the graphql-js repository of user “graphql”:
query {
search(query: "user:github repo:graphql/graphql-js" type: REPOSITORY first: 10) {
nodes {
... on Repository {
name
description
updatedAt
}
}
}
}
My understanding of your question is that you want to input a list of "known" repos and output information about them. In this case you can form a query of the following type.
{
nodes(ids: ["node_id_1", "node_id_2"]) {
... on Repository {
nameWithOwner
createdAt
}
}
}
Here the ids are the node_ids of the repositories you are interested in. You can obtain these by individual GraphQL calls as follows
{
repository(owner: "owner", name: "name") {
id
}
}

GitHub's GraphQL API: How can I get all of the commits I've contributed to a particular public repo?

I'm trying to query GitHub's GraphQL API (v4). What GraphQL query for this API can be used to get all of the commits I've contributed to a particular public repo?
I figured out the solution. (The solution shown below is for getting relevant commits from master, but can easily be adapted for other qualifiedName values.)
First, perform an initial query to obtain user ID:
query {
viewer {
id
}
}
Then the following query can be used to get all contributed commits to a particular repository:
query($repository_owner:String!, $repository_name:String!, $user_id:ID!) {
repository(owner: $repository_owner, name: $repository_name) {
ref(qualifiedName: "master") {
target {
... on Commit {
history(author: {id: $user_id}) {
totalCount
nodes {
id
authoredDate
message
}
}
}
}
}
}
}
The query variables take the following form:
{
"repository_owner": "REPOSITORY_OWNER_STRING",
"repository_name": "REPOSITORY_NAME_STRING",
"user_id": "USER_ID_FROM_STEP_1"
}

GitHub GraphQL search limited to language

I'm trying to use GitHub's GraphQL API to find a list of repos matching a query but limited to a specific language. However I can't find anything in the docs relating to the language filter the typical online search supports or how something like this is typically done with GraphQL.
{
search(query: "query", type: REPOSITORY, first: 10) {
repositoryCount
edges {
node {
... on Repository {
nameWithOwner
}
}
}
}
}
I'm almost guessing this isn't quite possible and I'm going to have to query for everything and filter on the client instead?
In the query parameter you can use the same format as Github search, filter language with language:LANGUAGE :
{
search(query: "language:java", type: REPOSITORY, first: 10) {
repositoryCount
edges {
node {
... on Repository {
nameWithOwner
}
}
}
}
}