【问题标题】:Is it possible to find out, via the GitHub API, if an issue has been closed via a pull request是否可以通过 GitHub API 找出问题是否已通过拉取请求关闭
【发布时间】:2021-01-20 00:06:42
【问题描述】:

我将github-script 用于 GitHub 操作,它允许您轻松访问 GitHub API。我正在尝试通过单击“关闭按钮”来检查问题是否关闭,即通过提交或通过合并包含关闭提交(或关闭公关机构)。但是,似乎没有一种简单的方法可以做到这一点。这是 GitHub API 返回的事件信息:

  • 如果问题已从提交中关闭,则会添加 commit_id
  • 如果问题已从 GitHub 应用程序关闭(这显然不包括网络),performed_via_github_app 设置为非空。

但是,显然,似乎没有一种特殊的方式来表明问题已被拉取请求关闭。还是这样?

【问题讨论】:

    标签: github-api github-actions


    【解决方案1】:

    可以使用GraphQL APItimelineItems 并通过状态CLOSED_EVENT 过滤事件来检查特定问题是否已通过拉取请求、提交或按钮/API 关闭:

    {
      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关闭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关闭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 进行调用以关闭问题,如果您打开通过 Github 应用生成的 api 调用的问题,performed_via_github_app 设置为非 null。但是performed_via_github_app 没有说明问题是通过什么方式关闭的:

    【讨论】:

      【解决方案2】:

      嗯,简短的回答是否定的,没有办法做到这一点,而且 GitHub API 不会显示它被关闭的 PR 或它已经从 PR 关闭而不是提交的事实。 所以我们必须满足于次优。通常问题是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(在这种情况下 问题的类型为connected)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 2021-08-01
        • 2020-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多