'Get PR number from Github API push event

I'm trying to create one or many comments in one or many files after a commit.

The way I'm doing the POST request now is through the PR's number, passing the body, commit_id, path, and position. And it works well hardcoding the number of the PR, because I have the commit_id, which is returned within the payload, when a PUSH event is sent through the webhook.

If I do the POST request to https://api.github.com/repos/author/repo/pulls/number/comments, it creates successfully the comment, within a specific line, as I need. But I must know they PR number beforehand.

The question is, is there a way to get the PR number if I have the commit_id, or any other received within the payload for a push event?

The current payload looks like:

{"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"=>"[email protected]: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}}

The request's body is very simple and looks like:

{
    "body": "body message",
    "commit_id": "commit_id",
    "path": "file",
    "position": position_as_integer
}


Solution 1:[1]

Yes, you can leverage this endpoint which will give you a list of all PRs containing that COMMIT_SHA.

From here you can filter find the associated number (ie for the first PR response[0]["number"]).

Here is python-code to get the first associated PR:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mts