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

…衆ロ難τιáo~ 提交于 2021-02-18 18:12:54

问题


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:

  • 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?


回答1:


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) :

  • via pull request: PullRequest
  • via commit messages: Commit
  • or via the closed button or Github API : null

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 :




回答2:


Well, short answer is no, there's no way to do this and the GitHub API does not reveal either the PR it's been closed from or the fact that it's been closed from a PR as opposed to a commit. So we'll have to settle for second-best. Often the issue is connected to the PR in the previous event. Unless it's an open PR and you've already merged a commit that mentions the issue, you'll have a connected and closed event the one after the other. So you can use this (or similar) to check for PRs:

issues.forEach( async function( issue ) {
    if ( ! issue.pull_request ) {
          const events = await github.issues.listEvents( { owner: user,
                                                           repo: repo,
                                                           issue_number: issue.number } )
         if ( !events.data ) {
             core.setFailed( "❌ Problema recuperando datos de " + issue.number );
          } else {
             var closing_event
             for (let i = 0; i < events.data.length; i ++ ) {
                  if ( events.data[i].event == 'closed' ) {
                      closing_event = i
                  }
             }
             if ( ! events.data[closing_event].commit_id ) {
                 if ( events.data[closing_event-1].event != 'connected' )   {
                    core.setFailed( "❌ El issue " + issue.number + " no se cerró con un commit o PR");
                 }
             }
         }
     }
                })

In this case, it fails if the issue has not been closed either by a commit (in which case the event that closes will have the commit id) or by a PR (in which case the previous event in the issue will have been of type connected)



来源:https://stackoverflow.com/questions/64203293/is-it-possible-to-find-out-via-the-github-api-if-an-issue-has-been-closed-via

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!