Search code examples
gitgithubgithub-apipull-request

Generate line pointer URL into Github Pull Request


I am trying to auto-generate a URL that can point to a line number into the GitHub Pull request's file diff view.

Basically, given the URL https://github.com/weppos/whois/pull/90/files I'd like to generate URL like https://github.com/weppos/whois/pull/90/files#diff-ce6bf647d5a531e54ef0502c7fe799deR27 that can point to the line 27 of file in the GitHub pull request. Looking at the URL, seems like it's combination of

https://github.com/weppos/whois/pull/90/ + files/#diff- + ce6bf647d5a531e54ef0502c7fe799de + R27

I am not sure how to retrieve number ce6bf647d5a531e54ef0502c7fe799de given everything else is known. It'd be great if someone can provide some pointers. Thanks!


Solution

  • The Github API docs (found here) suggest the following API endpoint:

    GET /repos/weppos/whois/pulls/90/files
    

    which would result in something similar to the following:

    [
        {
            "blob_url": "https://github.com/weppos/whois/blob/aa16f66c9dca556b7db131b68b0b99d435bc43d8/lib/whois/errors.rb", 
            "filename": "lib/whois/errors.rb", 
            "sha": "beec7aad75671e40a21532044c0e4dc23f7f226a", 
            "status": "modified",
            ...
        }, 
        ...
    ]
    

    The value you are looking for here is the MD5 hash of filename lib/whois/errors.rb which is ce6bf647d5a531e54ef0502c7fe799de.