0

Using Github webhooks, what is the best way to count the number of commits by a developer for a particular piece of work.

I was thinking that I could do like this:

  1. Listen for webhooks pertaining to merges of any PR into the main branch
  2. discard the webhook event unless it pertains to the main branch
  3. if the PR merge is into the main branch, then find out all the commits (and developer-related to each commit) related to that merge and calculate a count

are all these commits(and developer usernames) even listed in the webhook event?

Will the commits related to the merge definitely only be comprised of commits after the branch was created up until its merge.. or will they actually go all the way back to the beginning of repository creation?

Had also considered listening on webhooks related to "Tag" type pushes, but would a new tag event be able to tell me about all the commits between that tag and a previous tag.. probably not right? I'm guessing that whatever the tag is that it would always represent all commits from the start of repo creation

1977
  • 2,580
  • 6
  • 26
  • 37

1 Answers1

1

You can use roughly the approach you've outlined. You can extract the base and head revisions from the desired webhook events and then run git rev-list --no-commit-header --format=%aE $base...$head to find the email address associated with each commit. (You can use git log instead of git rev-list --no-commit-header if you're using an older Git).

That will count only the commits that were created in the branch and are not otherwise merged into main. Note that if you're using squash merges, this doesn't work because the merge base doesn't update; I recommend not using squash merges anyway. In the case of a squash merge, the number of commits created is always 1. You can tell whether a squash merge has taken place because git merge-base --is-ancestor $head main will exit 1; that is, the head of the PR branch is not an ancestor of the most recent main.

While this will work to count the commits per developer, I should point out that counting a developer's commits is not a good measure of productivity and shouldn't be used as a performance metric. A developer may be highly skilled and write only a single commit after investigating a difficult problem, but counting commits would rank that person lower than someone who fixes many easy problems. There are also other skills, like communication and collaboration, which cannot be measured in commits.

bk2204
  • 64,793
  • 6
  • 84
  • 100
  • Yes I agree on count of commits not being a good measure of productivity. Actually a "has this developer contributed to ANY commits that were part of this merged PR " boolean would do for now. The stuff you mention above sounds complex as I am actualy trying to achieve this from inside a nodejs server (that will receive the webhooks). Had hoped that the webhook event fired by the PR merge would contain all the metadata needed – 1977 Dec 07 '21 at 10:19
  • Unfortunately, since knowing the number of commits involves computing a merge base and walking the history, you really need to have a clone of the repo to do it effectively. Walking history using the GitHub API is wildly inefficient and will almost certainly lead to you being rate-limited. – bk2204 Dec 07 '21 at 22:47
  • I am thinking that the best way is to listen for the PR "closed" webhook event and then looking up the "commits_url" field to see the commit list for that branch that was just merged – 1977 Dec 08 '21 at 10:56