reconquest / atlassian-external-hooks

External Hooks plugin for Atlassian Bitbucket
https://external-hooks.reconquest.io
Other
44 stars 37 forks source link

External merge check hook fires when pull request is created #47

Closed shawng closed 4 years ago

shawng commented 7 years ago

I'm seeing the external merge check hook fire when the pull request is created and when it is merged. I wasn't expecting the external merge check hook to fire until the actual merge happens. Does anyone know if this is a bug or intended behavior?

If it's intended behavior, is there a way to distinguish between creating the pull request and merging the pull request?

Ultimately what I'm trying to do is run a cleanup script when a merge happens and the user marks the branch to be deleted. I'm using the latest version of Bitbucket Server with the latest version of the plugin.

As an example, here are the hashes and ref names for a simple sequence of events creating a commit on an new branch, creating a pull request, and then merging the pull request:

1) First commit to branch (external pre-receive hook: old hash, new hash, ref): 0000000000000000000000000000000000000000 0867166de6bd16eaade962a1e4f4628608db7b87 refs/heads/test-branch

2) Create pull request (external merge check hook: old hash, new hash, ref): e86689163b8a06e658550e1bf6263335b8e815d9 0867166de6bd16eaade962a1e4f4628608db7b87 refs/heads/master

3) Merge pull request (external merge check hook: old hash, new hash, ref): e86689163b8a06e658550e1bf6263335b8e815d9 0867166de6bd16eaade962a1e4f4628608db7b87 refs/heads/master

I would have only expected the merge hook to fire on steps 1 and 3, not on step 2.

shawng commented 7 years ago

@andreineculau @seletskiy @greened it looks like you were involved in implementing the external merge check hook. Do you happen to know if there is a way to differentiate between the merge of a pull request and the creation of a pull request when firing off an external merge check git hook?

I'm looking for a way to run some cleanup with a pull request deletes a branch, but the pre-receive and post-receive hooks don't fire when the pull request is merged and the branch is deleted, and the external merge check hook seems to also fires when the pull request is created. Is there an environment variable or something that can be checked to see whether the pull request is actually being merged?

Thanks! :)

asmirn01 commented 7 years ago

Facing the same issue. I wasn't able to distinguish between pull request creation and merge events from inside of a script called by the merge hook. Which makes it pretty useless

Gi0rgi0s commented 7 years ago

Bump ... Same here

andreineculau commented 7 years ago

FWIW " I wasn't expecting the external merge check hook to fire until the actual merge happens. Does anyone know if this is a bug or intended behavior?" -> YES!

BitBucket indirectly runs all the merge hooks, in order to give you the green checkmark/red x regarding merge-ability of the pull-request. Under the hood, BitBucket actually creates a merge-commit for that (which triggers the merge hooks) - if it doesn't succeed, this is how it tells you that the pull-request cannot be merged.

You should be able to see pull-request refs on the git bare repo (residing on the bitbucket server itself) like

So by checking if the new-ref is among refs/pull-requests/*/merge you can detect if BitBucket is running the hook just to be able to decide if the pull request can be merged or not. Best of luck @shawng !

NOTE, just to be clear:

  1. I am not working with BitBucket anymore, so the above is purely advisory
  2. atlassian-external-hooks is doing nothing wrong, nothing less nor more than what it should, so this github issue is a non-issue @ngsru