从 Github API 推送事件中获取 PR 号

Joh*_*hon 5 git github github-api

我试图在提交后在一个或多个文件中创建一个或多个注释。

我现在处理 POST 请求的方式是通过 PR 的编号,传递正文、commit_id、路径和位置。它可以很好地对 PR 的编号进行硬编码,因为我有 commit_id,当通过 webhook 发送 PUSH 事件时,它会在有效负载中返回。

如果我做 POST 请求 https://api.github.com/repos/author/repo/pulls/number/comments,它会根据需要在特定行内成功创建评论。但我必须事先知道他们的 PR 号码。

问题是,如果我有 commit_id 或在推送事件的有效负载中收到的任何其他信息,有没有办法获取 PR 号?

当前有效负载如下所示:

{"ref"=>"refs/heads/third",
 "before"=>"0000000000000000000000000000000000000000",
 "after"=>"0000000000000000000000000000000000000001",
 "created"=>true,
 "deleted"=>false,
 "forced"=>false,
 "base_ref"=>nil,
 "compare"=>"https://github.com/author/repo/commit/831a7cb888ab",
 "commits"=>
  [{"id"=>"0000000000000000000000000000000000000001",
    "tree_id"=>"tree_id",
    "distinct"=>true,
    "message"=>"message",
    "timestamp"=>"2018-08-12T03:22:31-03:00",
    "url"=>"https://github.com/author/repo/commit/0000000000000000000000000000000000000001",
    "author"=>{"name"=>"author_name", "email"=>"", "username"=>"author"},
    "committer"=>{"name"=>"author_name", "email"=>"", "username"=>"author"},
    "added"=>["filename"],
    "removed"=>[],
    "modified"=>[]}],
 "head_commit"=>
  {"id"=>"0000000000000000000000000000000000000001",
   "tree_id"=>"tree_id",
   "distinct"=>true,
   "message"=>"#1",
   "timestamp"=>"2018-08-12T03:22:31-03:00",
   "url"=>"https://github.com/author/repo/commit/0000000000000000000000000000000000000001",
   "author"=>{"name"=>"author_name", "email"=>"", "username"=>"author"},
   "committer"=>{"name"=>"author_name", "email"=>"", "username"=>"author"},
   "added"=>["filename"],
   "removed"=>[],
   "modified"=>[]},
 "repository"=>
  {"id"=>repository_id,
   "node_id"=>"node_id==",
   "name"=>"repo",
   "full_name"=>"author/repo",
   "owner"=>
    {"name"=>"author",
     "email"=>"",
     "login"=>"author",
     "id"=>id,
     "node_id"=>"MDQ6VXNlcjExODg4MTkx",
     "avatar_url"=>"https://avatars2.githubusercontent.com/u/id?v=4",
     "gravatar_id"=>"",
     "url"=>"https://api.github.com/users/author",
     "html_url"=>"https://github.com/author",
     "followers_url"=>"https://api.github.com/users/author/followers",
     "following_url"=>"https://api.github.com/users/author/following{/other_user}",
     "gists_url"=>"https://api.github.com/users/author/gists{/gist_id}",
     "starred_url"=>"https://api.github.com/users/author/starred{/owner}{/repo}",
     "subscriptions_url"=>"https://api.github.com/users/author/subscriptions",
     "organizations_url"=>"https://api.github.com/users/author/orgs",
     "repos_url"=>"https://api.github.com/users/author/repos",
     "events_url"=>"https://api.github.com/users/author/events{/privacy}",
     "received_events_url"=>"https://api.github.com/users/author/received_events",
     "type"=>"User",
     "site_admin"=>false},
   "private"=>true,
   "html_url"=>"https://github.com/author/repo",
   "description"=>nil,
   "fork"=>false,
   "url"=>"https://github.com/author/repo",
   "forks_url"=>"https://api.github.com/repos/author/repo/forks",
   "keys_url"=>"https://api.github.com/repos/author/repo/keys{/key_id}",
   "collaborators_url"=>"https://api.github.com/repos/author/repo/collaborators{/collaborator}",
   "teams_url"=>"https://api.github.com/repos/author/repo/teams",
   "hooks_url"=>"https://api.github.com/repos/author/repo/hooks",
   "issue_events_url"=>"https://api.github.com/repos/author/repo/issues/events{/number}",
   "events_url"=>"https://api.github.com/repos/author/repo/events",
   "assignees_url"=>"https://api.github.com/repos/author/repo/assignees{/user}",
   "branches_url"=>"https://api.github.com/repos/author/repo/branches{/branch}",
   "tags_url"=>"https://api.github.com/repos/author/repo/tags",
   "blobs_url"=>"https://api.github.com/repos/author/repo/git/blobs{/sha}",
   "git_tags_url"=>"https://api.github.com/repos/author/repo/git/tags{/sha}",
   "git_refs_url"=>"https://api.github.com/repos/author/repo/git/refs{/sha}",
   "trees_url"=>"https://api.github.com/repos/author/repo/git/trees{/sha}",
   "statuses_url"=>"https://api.github.com/repos/author/repo/statuses/{sha}",
   "languages_url"=>"https://api.github.com/repos/author/repo/languages",
   "stargazers_url"=>"https://api.github.com/repos/author/repo/stargazers",
   "contributors_url"=>"https://api.github.com/repos/author/repo/contributors",
   "subscribers_url"=>"https://api.github.com/repos/author/repo/subscribers",
   "subscription_url"=>"https://api.github.com/repos/author/repo/subscription",
   "commits_url"=>"https://api.github.com/repos/author/repo/commits{/sha}",
   "git_commits_url"=>"https://api.github.com/repos/author/repo/git/commits{/sha}",
   "comments_url"=>"https://api.github.com/repos/author/repo/comments{/number}",
   "issue_comment_url"=>"https://api.github.com/repos/author/repo/issues/comments{/number}",
   "contents_url"=>"https://api.github.com/repos/author/repo/contents/{+path}",
   "compare_url"=>"https://api.github.com/repos/author/repo/compare/{base}...{head}",
   "merges_url"=>"https://api.github.com/repos/author/repo/merges",
   "archive_url"=>"https://api.github.com/repos/author/repo/{archive_format}{/ref}",
   "downloads_url"=>"https://api.github.com/repos/author/repo/downloads",
   "issues_url"=>"https://api.github.com/repos/author/repo/issues{/number}",
   "pulls_url"=>"https://api.github.com/repos/author/repo/pulls{/number}",
   "milestones_url"=>"https://api.github.com/repos/author/repo/milestones{/number}",
   "notifications_url"=>"https://api.github.com/repos/author/repo/notifications{?since,all,participating}",
   "labels_url"=>"https://api.github.com/repos/author/repo/labels{/name}",
   "releases_url"=>"https://api.github.com/repos/author/repo/releases{/id}",
   "deployments_url"=>"https://api.github.com/repos/author/repo/deployments",
   "created_at"=>1501475657,
   "updated_at"=>"2018-07-17T01:30:41Z",
   "pushed_at"=>1534054956,
   "git_url"=>"git://github.com/author/repo.git",
   "ssh_url"=>"git@github.com:author/repo.git",
   "clone_url"=>"https://github.com/author/repo.git",
   "svn_url"=>"https://github.com/author/repo",
   "homepage"=>nil,
   "size"=>55534,
   "stargazers_count"=>0,
   "watchers_count"=>0,
   "language"=>"JavaScript",
   "has_issues"=>true,
   "has_projects"=>true,
   "has_downloads"=>true,
   "has_wiki"=>true,
   "has_pages"=>false,
   "forks_count"=>0,
   "mirror_url"=>nil,
   "archived"=>false,
   "open_issues_count"=>0,
   "license"=>nil,
   "forks"=>0,
   "open_issues"=>0,
   "watchers"=>0,
   "default_branch"=>"master",
   "stargazers"=>0,
   "master_branch"=>"master"},
 "pusher"=>{"name"=>"author", "email"=>""},
 "sender"=>
  {"login"=>"author",
   "id"=>id,
   "node_id"=>"MDQ6VXNlcjExODg4MTkx",
   "avatar_url"=>"https://avatars2.githubusercontent.com/u/id?v=4",
   "gravatar_id"=>"",
   "url"=>"https://api.github.com/users/author",
   "html_url"=>"https://github.com/author",
   "followers_url"=>"https://api.github.com/users/author/followers",
   "following_url"=>"https://api.github.com/users/author/following{/other_user}",
   "gists_url"=>"https://api.github.com/users/author/gists{/gist_id}",
   "starred_url"=>"https://api.github.com/users/author/starred{/owner}{/repo}",
   "subscriptions_url"=>"https://api.github.com/users/author/subscriptions",
   "organizations_url"=>"https://api.github.com/users/author/orgs",
   "repos_url"=>"https://api.github.com/users/author/repos",
   "events_url"=>"https://api.github.com/users/author/events{/privacy}",
   "received_events_url"=>"https://api.github.com/users/author/received_events",
   "type"=>"User",
   "site_admin"=>false},
 "installation"=>{"id"=>installation_id}}
Run Code Online (Sandbox Code Playgroud)

请求的主体非常简单,如下所示:

{
    "body": "body message",
    "commit_id": "commit_id",
    "path": "file",
    "position": position_as_integer
}
Run Code Online (Sandbox Code Playgroud)

小智 0

是的,您可以利用此端点,它将为您提供包含该 COMMIT_SHA 的所有 PR 的列表。

从这里您可以过滤找到关联的号码(即第一个 PR response[0]["number"])。

这是获取第一个关联 PR 的python 代码:

import requests

def build_headers(token, username, password):
    """Format secret(s) for headers of API call."""
    if token:
        headers = {
            "Authorization": f"token { token }",
        }
    elif username and password:
        headers = {"Authorization": f"Basic { username }:{ password }"}
    else:
        raise Exception(
            "Either Authentication Token or Username + Password need to be included in request."
        )
    return headers

def get_pr_id_from_commit_id(
    organization,
    repository,
    commit_id,
    token=None,
    username=None,
    password=None,
    **kwargs,
):
    """Gets a pull_request id, by pulling PRs that include commit_id."""
    headers = build_headers(token, username, password)
    curr_endpoint = f"{ base_url }/repos/{ organization }/{ repository }/commits/{commit_id}/pulls"
    logging.info(f"URL: { curr_endpoint }")
    response = requests.get(curr_endpoint, headers=headers)
    pr_id = json.loads(response.text)[0]["number"]
    return pr_id
Run Code Online (Sandbox Code Playgroud)