Is it possible to find the project boards an issue is associated with using the Github API? - github

I'm developing a Github webhook that gets notifications on issue updates.
These issues are associated to one of more Github Project Boards
The body payload shows this information related to the updated issue:
Nothing about issue association with project boards. Is there a way to get this information?

You can use GraphQL API to get that information using projectCards under the Issue object :
{
repository(name: "caipy-dashboard", owner: "bertrandmartel") {
issue(number: 26) {
title
projectCards {
nodes {
createdAt
column {
name
}
project {
name
body
}
}
}
}
}
}
Output :
{
"data": {
"repository": {
"issue": {
"title": "fix dead path in startover",
"projectCards": {
"nodes": [
{
"createdAt": "2017-08-28T05:47:49Z",
"column": {
"name": "Done"
},
"project": {
"name": "Startover",
"body": "Show epg / caipy events + startover modes"
}
}
]
}
}
}
}
}
Try it in the explorer

Related

GitHub GraphQL API Filter on custom text field in Project V2

We are using GitHub Projects (Beta). I created a custom text field called oma-project. I want to use the API to filter on this field, e.g., oma-project: "P0001". This should return all issues with this value in the custom field.
Looking at the Projects (beta) docs, I can list the first n issues but I don't know how I can pass a filter based on the value of the custom field.
This is as far as I have gotten.
{
node(id: "nodeid") {
... on ProjectV2 {
items(first: 2) {
nodes {
fieldValues(first: 8) {
nodes {
... on ProjectV2ItemFieldTextValue {
text
field {
... on ProjectV2FieldCommon {
name
}
}
}
}
}
content {
... on Issue {
id
title
}
}
}
}
}
}
}
returns:
{
"data": {
"node": {
"items": {
"nodes": [
{
"fieldValues": {
"nodes": [
{},
{},
{},
{
"text": "Retrieve Notes via OData",
"field": {
"name": "Title"
}
},
{},
{
"text": "P9999",
"field": {
"name": "OMA Project"
}
}
]
},
"content": {
"id": "I_kwDOFT-pvM5AZNLm",
"title": "Retrieve Notes via OData"
}
},
{
"fieldValues": {
"nodes": [
{},
{},
{
"text": "Capex Approval Type",
"field": {
"name": "Title"
}
},
{},
{},
{
"text": "P0708",
"field": {
"name": "OMA Project"
}
}
]
},
"content": {
"id": "I_kwDOFT-pvM5K85HZ",
"title": "Capex Approval Type"
}
}
]
}
}
}
}
I'm having the same issue when working on a GitHub action to move a list of items from a status to another status, and it seems currently the API doesn't support ProjectV2 item filtering.
My current workaround is to query all the items from the project and then later filter out the results using jq or JavaScript

Github GrapQL API returns only the last StatusContext for each context

I'm cross-posting the question from here.
I’m interested in knowing whether it’s possible to fetch all the statuses for all the contexts for a given reference using the GQL API.
The query that I’m currently doing is the following:
{
repository(owner: "owner", name: "name") {
pullRequests(headRefName: "head-ref", last: 1) {
nodes {
id
commits(first: 10) {
nodes {
commit {
oid
status {
contexts {
context
createdAt
id
description
state
}
}
}
}
}
}
}
}
}
This query returns a single status for each status context, and those are the last ones for each:
{
"data": {
"repository": {
"pullRequests": {
"nodes": [
{
"id": "some-id",
"commits": {
"nodes": [
{
"commit": {
"oid": "some-oid",
"status": {
"contexts": [
{
"context": "context-1",
"createdAt": "2021-07-06T21:28:26Z",
"id": "***",
"description": "Your tests passed!",
"state": "SUCCESS"
},
{
"context": "context-2",
"createdAt": "2021-07-06T21:25:26Z",
"id": "***",
"description": "Your tests passed!",
"state": "SUCCESS"
},
]
}
}
}
]
}
}
]
}
}
}
}
On the other hand, if I use the REST API with this query:
curl -i -u se7entyse7en:$(cat token) https://api.github.com/repos/owner/name/commits/some-oid/statuses
where some-oid is the corresponding retrieved with the GQL API, the output contains ALL the statuses. In particular, I can see all the statuses of context-1 and context-2 that happened before those that are returned by the GQL API.
It seems a limitation of the GQL schema given that StatusContext is a node instead of being a list of nodes. Basically, I expect StatusContext to be of type [Status!]! where Status represents a single status for the given context.
Am I missing something? Is this something expected to be changed in the future? Is the REST API the only option?
Thanks.
I opened a support ticket and this is the expected behavior indeed, there are no plans for changing it. The only solution is to use the REST API.
The link to the community forum is this one.

Which private repos have GitHub Pages

I want to downgrade my GitHub account to the free plan. GitHub warns me that I have one private repository that uses GitHub Pages - a feature that the free plan doesn't support. So I want to find this repo to see if it is problematic if it becomes public.
I can't find any way to even list my repos that use Github Pages, let alone the ones that are private.
Is there a way to do this?
Yes, simple via GraphQL API, with or without GraphQL API Explorer, (don't forget personal access token scope if using without the explorer,) if you don't have very much private repos.
Use query:
query {
viewer {
repositories (first:100, privacy: PRIVATE) {
nodes {
name
deployments (first: 1) {
nodes {
environment
}
}
}
}
}
}
Example result:
{
"data": {
"viewer": {
"repositories": {
"nodes": [
{
"name": "repo-without-pages",
"deployments": {
"nodes": []
}
},
{
"name": "repo-with-pages",
"deployments": {
"nodes": [
{
"environment": "github-pages"
}
]
}
}
]
}
}
}
}
Typically the only possible environment is github-pages. If you have more, change the above query.
Note GitHub GraphQL API has a limit: Values of first and last must be within 1-100. If you have more than 100, use:
repositories (first:100, privacy: PRIVATE) {
pageInfo {
endCursor
}
and after getting endCursor:
repositories (first:100, privacy: PRIVATE, after:"endCursorValue==") {
pageInfo {
endCursor
}
to paginate.

Querying github project cards with graphql

I have a Project in Github with some columns, and I'm trying to locate cards (Issues) that are in the closed state.
With GraphQL I'm having trouble constructing a query that can filter the cards. Currently I have this query:
query {
organization(login:"org") {
project(number:3) {
columns(first:1){
nodes{
id,
name,
cards(first:1){
nodes{
content {
... on Issue {
url,
state
}
}
}
}
}
}
}
}
}
This returns the result:
{
"data": {
"organization": {
"project": {
"columns": {
"nodes": [
{
"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"name": "On Deck",
"cards": {
"nodes": [
{
"content": {
"url": "https://github.com/org/repo/issues/606",
"state": "OPEN"
}
}
]
}
}
]
}
}
}
}
}
What I have gathered thus far is that the "Project Cards" are "UNION" objects in GraphQL and I have no idea how to add filters to these unions.
Any suggestions?
Thank you!
I asked the same question on the GitHub Community forum. An admin wrote back:
The way that the GraphQL API is currently built, no, you can't filter project cards on the state of the issue that they're representing using only the query language. You would have to query all cards in the column and then filter it yourself.

Github API: Getting topics of a Github repository

Github API documentation give instructions to filter repositories by topics.
Is there a way to use the API to get the topics from a specific repo?
You can do this with the Github GraphQL API
Query:
{
repository(owner: "twbs", name: "bootstrap") {
repositoryTopics(first: 10) {
edges {
node {
topic {
name
}
}
}
}
}
}
This will return the first 10 topics and the name for each as shown below.
Response:
{
"data": {
"repository": {
"repositoryTopics": {
"edges": [
{
"node": {
"topic": {
"name": "css"
}
}
},
{
"node": {
"topic": {
"name": "bootstrap"
}
}
},
{
"node": {
"topic": {
"name": "javascript"
}
}
},
{
"node": {
"topic": {
"name": "html"
}
}
}
]
}
}
}
}
Test it out in the GitHub GraphQL Explorer
You can do this easily with Github API (it's currently in "preview mode"):
curl -H "Accept: application/vnd.github.mercy-preview+json" https://api.github.com/repos/twbs/bootstrap/topics
{
"names": [
"css",
"bootstrap",
"javascript",
"html",
"jekyll-site",
"scss",
"css-framework",
"sass"
]
}
You need to include extra header Accept: application/vnd.github.mercy-preview+json.
There is one "but", since it's in "preview mode" it's not supported for production use (please read "Note" and "Warning" sections in link below).
See also:
https://docs.github.com/en/rest/reference/repos#get-a-repository
I don't know that there is a way to just get the topics for a repository, but if you do a get for a repository, the repository json object that is returned will have a topics property that is an array of that repositories topics.
At the top of that page of documentation, you will notice that in order to have the topics returned you will need to add a specific header in your GET request: "Accept":"application/vnd.github.mercy-preview+json"
Hope this helps!
I faced similar problem, so I made a node module that only require one line of code to do that which is
var github_topics = require('github-topics');
var topics = github_topics.gettopics('url_of_repo');
for example
var topics = github_topics.gettopics('https://github.com/Aniket965/blog');
It will return array of topics of that github repository , link to that node module is NPM
I added the fetch with Accept Headers:
fetch("https://api.github.com/users/lucksp/repos",
{
method: "GET",
headers: {
Accept: "application/vnd.github.mercy-preview+json"
}
})