6

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

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

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: https://confluence.atlassian.com/bitbucket/event-payloads-740262817.html

And the sample payload for a push is: https://gist.github.com/ORESoftware/1ef3abe66dba23039ddd90fef1093318

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

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
  • for pullrequests - I am also looking for a reliable way to see the two hashes that would be merged if the PR is merged –  May 25 '19 at 22:13
  • For pull requests, you should use the pull request event instead of the push event. It contains a `pullrequest` field with this kind of object: https://confluence.atlassian.com/bitbucket/event-payloads-740262817.html#EventPayloads-entity_pullrequest which in turn has a `source` and `destination` field that contains the commits you're interested in. – Ophirr33 Jun 01 '19 at 20:58

1 Answers1

0

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 = body.push.changes.map(change => change.new.target.hash);

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.

Ophirr33
  • 175
  • 7
  • the latest hash for a single branch, I suppose, right? using a git repo, I can fetch and then checkout a branch and my HEAD points to the latest commit of that branch, but how to do that using the BitbucketAPI? Is this so hard to understand? –  Jun 01 '19 at 20:50
  • It'd be helpful to explain what problem you're trying to solve. When you say "the latest hash for a single branch", does that mean you're trying to watch all of the changes to a named branch? If so, you would need to search the `changes` array for an object with `old.name == targetBranchName`, and then get `new.target.hash` from that same change object. – Ophirr33 Jun 01 '19 at 20:56
  • This is a push based API, e.g., you're receiving these events as changes happen. When you say "using a git repo, I can fetch and then checkout a branch and my HEAD points to the latest commit", that's an action you're initiating as opposed to an event you're listening for. Is this a problem you can solve just by using git? `git rev-parse origin/targetBranchName` – Ophirr33 Jun 01 '19 at 21:04