是否可以通过 GitHub API 找出问题是否已通过拉取请求关闭

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

我正在使用 github-script 进行 GitHub 操作,这样您就可以轻松访问 GitHub API。我正在尝试通过单击“关闭按钮”检查问题是否 关闭,即通过提交或通过合并包含关闭提交的拉取请求 (或在 PR 正文中关闭)。但是,似乎没有一种简单的方法可以做到这一点。这是GitHubAPIreturns的事件信息:

但是,显然,似乎没有一种特殊的方式来表示问题已通过拉取请求关闭。或者是?

好吧,简短的回答是否定的,没有办法做到这一点,而且 GitHub API 没有透露它已被关闭的 PR 或它已被 PR 关闭的事实而不是提交。 所以我们将不得不接受 second-best。通常问题是 connected 到上一个事件中的 PR。除非它是一个公开的 PR,并且你已经合并了一个提到这个问题的提交,否则你将有一个接一个的 connectedclosed 事件。所以你可以使用这个(或类似的)来检查 PR:

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");
                 }
             }
         }
     }
                })

在这种情况下,如果问题尚未通过提交(在这种情况下关闭的事件将具有提交 ID)或 PR(在这种情况下 the问题 中的上一个事件的类型为 connected)

可以使用 GraphQL API with timelineItems 检查特定问题是否已通过拉取请求、提交或 button/API 关闭,并过滤状态为 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

closer 字段包含关闭的来源(检查 __typename 值):

以下请求是 3 种关闭类型的示例

通过拉取请求关闭

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
            }
          }
        }
      }
    }
  }
}

输出

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

通过提交消息关闭

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
            }
          }
        }
      }
    }
  }
}

输出

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

通过按钮关闭或 Github API

This issue 已通过关闭按钮关闭:

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

输出

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

Github 应用程序使用 Github API 调用来关闭问题,如果您 performed_via_github_app 设置为非 null open 来自 api 通过 Github 应用生成的调用的问题。但是 performed_via_github_app 没有说明问题是通过什么方式关闭的: