github graphQL API - how to retrieve issue's project items?

386 views Asked by At

I am new to github graphQL API and I have been scrambling for a solution to the following problem, using github Entreprise Cloud:

  • I have a repo "myRepo" containing an issue "myIssue"
  • issue "myIssue" is added to a projectV2 "myProject", which is defined at organization level (not in a repo)
  • project "myProject" contains a field "Status" (default field when creating projects)

Now... I want to automate the re-opening of an issue when it is closed via the merging of a PR, if the value of the "status" of the issue in the project is not "Closed".

Question is: having the node id of issue "myIssue", how can I retrieve the value of the field "Status" for that issue in project "myProject" (assuming I have either the project number or its node id) with a graphQL query?

tried something like

query findProjectItemsForIssueNumber($owner: String!, $repoName: String!, $issueNumber:Int!) {
  viewer {
    organization(login:$owner) {
      repository(name:$repoName) {
        issue(number:$issueNumber) {
          url
          projectItems(first:20) {
            nodes {
              id
            }
          }
        }
      }
    }
  }
}

derived from https://gist.github.com/richkuz/e8842fce354edbd4e12dcbfa9ca40ff6

but projectItems is not an Issue field

1

There are 1 answers

0
sepollig On

so... after learning a bit more about graphQL and going through github's documentation, it does not seem like an event is produced when a custom field's value of an issue is changed. Thus, I had to go the other way around, i.e., use a scheduled workflow that will use a graphQL query to identify all issues pertaining to a project, with the conditions I set. This is what I came up with:

query getIssueDetailsOnProject {
  node (id: "<your project node id>") {
    ... on ProjectV2 {
      number
      title
      shortDescription
      items( first: 100 after: "<cursor value>") {
        totalCount
        pageInfo {
          endCursor
          hasNextPage
          startCursor
        }
        nodes {
          type
          databaseId
          content {
            ... on Issue {
              id
              number
              state
            }
          }
          fieldValueByName( name: "Status") {
             ... on ProjectV2ItemFieldSingleSelectValue {
              status: name
            }
          }
        }
      }
    }
  }
}

this will return something like

{
  "data": {
    "node": {
      "number": 123,
      "title": "my project title",
      "shortDescription": "test project blah blag",
      "items": {
        "totalCount": 321,
        "pageInfo": {
          "endCursor": "Abcd",
          "hasNextPage": true,
          "startCursor": "Abcc"
        },
        "nodes": [
          {
            "type": "ISSUE",
            "databaseId": 12345,
            "content": {
              "id": "JDU6GXNzxKUxUTK2Mui=",
              "number": 4321,
              "state": "OPEN"
            },
            "fieldValueByName": {
              "status": "Closed"
            }
          },
          ...

on which I will iterate as long as hasNextPage is true looking for the cases where the state is OPEN, but the status is Closed, then close those (using a mutation query like

mutation closeMyIssue {
  closeIssue(input: {issueId: "<the node id of the issue from previous query content.id>", stateReason: COMPLETED}) {
    clientMutationId
    issue {
      state
      stateReason
      closed
      closedAt
    }
  }
}```