Search code examples
javascriptgithub-actionsgithub-api

Getting the subject_id for the subject_type GitHub User API


Using the GitHub API via Octokit.js and looking at the docs for getContextForUser (https://docs.github.com/en/rest/users/users#get-contextual-information-for-a-user), it mentions that when specifying the subject_type you also need to specify the subject_id. For the subject_id, it states, "Uses the ID for the subject_type you specified."

I have no idea where you get the relevant subject_id for the subject_type. I have searched high and low but with no success. Does anybody know where one can find a map that shows the ID for a subject_type?


Solution

  • The subject_id is the ID of the thing you want to have information about. For example, if you want to have information about a repository (in this case the subject type), you'd have to pass the ID of the repository you want to have information about.

    In my opinion, it's a bit confusing that the API docs say this must be a string, while it is typically a numeric ID.

    Repository Sample

    Let's take a look at an example: Say we want to know what Torvald's hovercard on the repository torvalds/linux is. First, we need to get the ID of the repository:

    > curl https://api.github.com/repos/torvalds/linux
    {
      "id": 2325298,
    # ...
    

    With this ID, we can query the subject type repository:

    > curl "https://api.github.com/users/torvalds/hovercard?subject_type=repository&subject_id=2325298"
    {
      "contexts": [
        {
          "message": "Owns this repository",
          "octicon": "repo"
        },
        {
          "message": "Committed to this repository in the past day",
          "octicon": "git-commit"
        }
      ]
    }
    

    Pull Request Sample

    Similarly, you can fetch the ID of pull request.

    You need to be careful which ID you're using though: While pull request within a repository are identified with a typically small number (the one you see in the URL), the API in question does not have context of the repository, so you need to pass the global ID.

    > curl "https://api.github.com/repos/torvalds/linux/pulls/486"
    {
      "url": "https://api.github.com/repos/torvalds/linux/pulls/486",
      "id": 150630598,
    # ...
    

    And then use the subject type pull_request to fetch Linus' role in it:

    curl "https://api.github.com/users/torvalds/hovercard?subject_type=pull_request&subject_id=150630598"
    {
      "contexts": [
        {
          "message": "Owns this repository",
          "octicon": "repo"
        },
        {
          "message": "Committed to this repository in the past day",
          "octicon": "git-commit"
        }
      ]
    }
    

    Note that the endpoint to fetch contextual information requires authentication so you'll have to add a header to the curl samples.

    If you're frequently experimenting with the GitHub API, I recommend installing the GitHub CLI, because it allows you to run authenticated queries like so:

    > gh api /repos/torvals/linux