Search code examples
github-apigithub-actions

Is it possible to find out, via the GitHub API, if an issue has been closed via a pull request


I'm using github-script for GitHub actions, which allows you to easily access GitHub API. I'm trying to check if an issue has not been closed by clicking on the "close button", that is, via a commit or via merging a pull request that includes a closing commit (or closes in the PR body). However, there does not seem an easy way to do that. This is the event information the GitHub API returns: enter image description here

  • If the issue has been closed from a commit, it adds the commit_id
  • If the issue has been closed from GitHub app (this does not include the web, apparently), performed_via_github_app is set to non-null.

However, there does not seem to be a special way to signal an issue has been closed by a pull request, apparently. Or is it?


Solution

  • It's possible to check whether a specific issue was closed by pull request, commit or button/API using the GraphQL API with timelineItems and filtering on event with state CLOSED_EVENT :

    {
      repository(name: "material-ui", owner: "mui-org") {
        issue(number: 19641) {
          timelineItems(itemTypes: CLOSED_EVENT, last: 1) {
            nodes {
              ... on ClosedEvent {
                createdAt
                closer {
                   __typename
                  ... on PullRequest {
                    baseRefName
                    baseRepository {
                      nameWithOwner
                    }
                    headRefName
                    headRepository {
                      nameWithOwner
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    Try it in the explorer

    The closer field contains the source of the closing (check __typename value) :

    The following requests are example for the 3 types of closing

    Closing via pull request

    This pull request closed this issue

    {
      repository(name: "material-ui", owner: "mui-org") {
        issue(number: 19641) {
          timelineItems(itemTypes: CLOSED_EVENT, last: 1) {
            nodes {
              ... on ClosedEvent {
                createdAt
                closer {
                  __typename
                }
              }
            }
          }
        }
      }
    }
    

    Output

    {
      "data": {
        "repository": {
          "issue": {
            "timelineItems": {
              "nodes": [
                {
                  "createdAt": "2020-05-20T09:06:11Z",
                  "closer": {
                    "__typename": "PullRequest"
                  }
                }
              ]
            }
          }
        }
      }
    }
    

    Closing via commit message

    This commit closed this issue

    {
      repository(name: "rubinius", owner: "rubinius") {
        issue(number: 1536) {
          timelineItems(itemTypes: CLOSED_EVENT, last: 1) {
            nodes {
              ... on ClosedEvent {
                createdAt
                closer {
                  __typename
                }
              }
            }
          }
        }
      }
    }
    

    Output

    {
      "data": {
        "repository": {
          "issue": {
            "timelineItems": {
              "nodes": [
                {
                  "createdAt": "2012-01-30T22:33:11Z",
                  "closer": {
                    "__typename": "Commit"
                  }
                }
              ]
            }
          }
        }
      }
    }
    

    Closing via button or Github API

    This issue was closed via the close button :

    {
      repository(name: "rubinius", owner: "rubinius") {
        issue(number: 3830) {
          timelineItems(itemTypes: CLOSED_EVENT, last: 1) {
            nodes {
              ... on ClosedEvent {
                createdAt
                closer {
                  __typename
                }
              }
            }
          }
        }
      }
    }
    

    Output

    {
      "data": {
        "repository": {
          "issue": {
            "timelineItems": {
              "nodes": [
                {
                  "createdAt": "2020-02-02T22:31:05Z",
                  "closer": null
                }
              ]
            }
          }
        }
      }
    }
    

    Github app uses Github API to make call to close an issue, performed_via_github_app is set non null if you open an issue from an api call generated via a Github app. But performed_via_github_app doesn't specify by which mean the issue was closed :

    enter image description here