It looks like to get the latest hash of a change it's something like:

const hash = body.push.changes[0];

That seems a little awkward though, given the array access - will it always be the first element?

Here are the docs I am looking at:

And the sample payload for a push is:

I really am confused about how to access the latest hash when there's a change/push to a repository.

1 Answers

Ophirr On

From the doc you linked, the push property contains:

The details of the push, which includes the changes property. This property contains an array with all the references that the push updated and the following properties:

So no, you can't just look at the first value. If the user pushes multiple branches at once (git push origin --all, for example), you may have multiple objects in the changes array. For instance, one branch could have been deleted, another one created (old would be null), and a third could have been updated. So in processing these push events, you should make sure to handle multiple possible reference updates at once.

How to most reliably get latest hash

What exactly are you trying to accomplish by getting the latest hash? If you just need to record all the hashes, it should suffice to just map over the changes array, e.g.

const hashes = =>;

But it seems like you're making the assumption that you can handle repository changes one at a time. This is most likely false -- someone can clone a repository, change a ton of branches in diverging ways, and push (or force push!) those changes up.