Get author login name (or username) from commit

Viewed 802

It appears I can only get either the author name (John Doe), or the author email (john.doe@acme.org). I can't find a way to get the author login name (johndoe123)

Even the GitHub "Get a commit" API endpoint won't return the login name.

I need to get the login name to be able to put "@johndoe123" in the changelog of a release so it shows like they do here:

enter image description here

Any advice?

4 Answers

You can use the Get a commit API to get information about the user who is associated with a commit as the author or committer. Note that this association is made based on the author and committer email addresses associated with the commit, which suffer from the following limitations:

  • The data can generally be forged unless the commit is signed and the key is known to GitHub.
  • A user may or may not have a GitHub account, or may have removed a previous email address from their GitHub account, so no user information may be provided.

GitHub uses the email address in the commit header to link the commit to a GitHub user.

What does this mean? That the data on the commit doesn't actually have to be equivalent to a GitHub user, since I could have set my commit headers with a different e-mail that the one that I use in GitHub.

On the other hand, if the user who did the commit has a public email address set in GitHub, it's easy to retrieve its user info through the User Search API.

For example, since I have a public email in GitHub if you look for https://api.github.com/search/users?q=dacamposol@gmail.com you'll receive the following response:

{
  total_count: 1,
  incomplete_results: false,
  items: [
    {
      login: "dacamposol",
      id: 16921751,
      node_id: "MDQ6VXNlcjE2OTIxNzUx",
      avatar_url: "https://avatars.githubusercontent.com/u/16921751?v=4",
      gravatar_id: "",
      url: "https://api.github.com/users/dacamposol",
      html_url: "https://github.com/dacamposol",
      followers_url: "https://api.github.com/users/dacamposol/followers",
      following_url: 
      "https://api.github.com/users/dacamposol/following{/other_user}",
      gists_url: "https://api.github.com/users/dacamposol/gists{/gist_id}",
      starred_url: "https://api.github.com/users/dacamposol/starred{/owner}{/repo}",
      subscriptions_url: "https://api.github.com/users/dacamposol/subscriptions",
      organizations_url: "https://api.github.com/users/dacamposol/orgs",
      repos_url: "https://api.github.com/users/dacamposol/repos",
      events_url: "https://api.github.com/users/dacamposol/events{/privacy}",
      received_events_url: "https://api.github.com/users/dacamposol/received_events",
      type: "User",
      site_admin: false,
      score: 1
    }
  ]
}

If the person doesn't have a public email address, maybe you should just refer to the user provided in the user.name header in the commit, since maybe that person values its privacy and that's why it didn't set his email as public in GitHub.

You can use Github GraphQL API to get the commit and return the user object from author field:

{
  repository(owner: "mui-org", name: "material-ui") {
    object(oid: "97e622215eb8e17dc3597fc35bde9483af37efc3") {
      ... on Commit {
        message
        author {
          name
          email
          user {
            login
            email
          }
        }
      }
    }
  }
}

Try it in the explorer

From GraphQL doc for the user object

The GitHub user corresponding to the email field. Null if no such user exists.

But as stated in the other answers, there is no guarantee that it will match an existing user, or that the matched user is defacto the commit author. It doesn't mention either what it chooses when there are multiple user matches for this email.

Note that, in the release example you've found, it probably uses some kind of changelog generator (something like this project). And what you are looking at are issues/pull requests and not commit. Thus, it extracts the user object directly with the pull requests using GET {owner}/{repo}/pulls or search API which will return the user object:

For example to reproduce a changelog like this project, you could get all the pull requests between the target release and previous release and for each one you will get the user object with login like this

If you are able to use the command line, you can use:

git checkout SHA-1

to checkout the commit of interest (replacing SHA-1 with the commit number), and

git log -n 1 --format="%an"

to print the author's name.

Option -n 1 specifies to output for the top commit only and option --format="%an" specifies to print the author name.

Related