# BitBucket PostRepositoryHook plugin question

**URL:** <https://community.developer.atlassian.com/t/bitbucket-postrepositoryhook-plugin-question/10029>\
**Category:** Atlassian Developer Tools\
**Tags:** bitbucket, plugin-sdk, bitbucket-server, bitbucket-pipelines\
**Created:** [October 16, 2017, 1:51pm UTC](https://community.developer.atlassian.com/t/bitbucket-postrepositoryhook-plugin-question/10029 "2017-10-16T13:51:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratboyDC](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@ratboyDC](https://community.developer.atlassian.com/u/ratboyDC)\
**Post date:** [October 16, 2017, 1:51pm UTC](https://community.developer.atlassian.com/t/bitbucket-postrepositoryhook-plugin-question/10029/1 "2017-10-16T13:51:23Z")

</div>

Hello,

I am a new-bee to BitBucket plugin development… I am trying to create a plugin that will call a SonarQube scan when a branch is merged into another branch (from a PR to a ‘master’ branch - after the PR has been merged); our workflow is that we maintain a ‘baseline’ scan on Master branch. This Master branch needs to be scanned by SonarQube sonar-scanner (currently calling from command line but I have a Maven configuration ready to go);

I am predicating my code on the example found here:

[https://developer.atlassian.com/bitbucket/server/docs/latest/how-tos/hooks-merge-checks-guide.html](https://developer.atlassian.com/bitbucket/server/docs/latest/how-tos/hooks-merge-checks-guide.html)

I suppose I just replace the code in the postUpdate method to call out to Maven? I have found some ways to do that thanks to Stack, but not sure if it is “how it is done” or just “how it can be done” - there’s a difference, I’m sure… what is the _preferred way_ to do this?

example code

````auto
 public void postUpdate(@Nonnull PostRepositoryHookContext context, 
                           @Nonnull RepositoryHookRequest hookRequest) {
        Logger.info("[{}] {} updated [{}]",
                hookRequest.getRepository(),
                hookRequest.getTrigger().getId(),
                hookRequest.getRefChanges().stream()
                    .map(change -> change.getRef().getId())
                    .collect(Collectors.joining(", ")));
    }```

I appreciate your replies!
````

---

<div class="post-metadata">

**Author:** ![acalantog](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/acalantog/32/37988_2.png) [@acalantog](https://community.developer.atlassian.com/u/acalantog)\
**Post date:** [October 17, 2017, 5:34am UTC](https://community.developer.atlassian.com/t/bitbucket-postrepositoryhook-plugin-question/10029/2 "2017-10-17T05:34:38Z")

</div>

Hi @ratboyDC,

Welcome to Atlassian! I’m curious whether your code worked or didn’t? Or is the question mainly for best practice?

Cheers,  
Anne

---

<div class="post-metadata">

**Author:** ![ratboyDC](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@ratboyDC](https://community.developer.atlassian.com/u/ratboyDC)\
**Post date:** [October 17, 2017, 11:57am UTC](https://community.developer.atlassian.com/t/bitbucket-postrepositoryhook-plugin-question/10029/3 "2017-10-17T11:57:14Z")

</div>

Hello,

It is a “what is the _best_ way to do this?” as opposed to a “will this work?”

I have a discussion with an engineer at my current company who has done this in accordance with their internal S&P’s later today, so I’ll discuss it with her and get her advice on how they prefer it implemented.

Thanks!

---

<div class="post-metadata">

**Author:** ![jpalacios](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/jpalacios/32/36381_2.png) [@jpalacios](https://community.developer.atlassian.com/u/jpalacios)\
**Post date:** [November 2, 2017, 3:07am UTC](https://community.developer.atlassian.com/t/bitbucket-postrepositoryhook-plugin-question/10029/4 "2017-11-02T03:07:14Z")

</div>

Hi @ratboyDC,

There are two ways in which I think you can do this. If any change to `master` (adding or removing commits) needs to trigger a rescan then adding a `RepositoryRefsChangedEvent` listener should be enough. The event carries a collection of refs which have been updated so finding `refs/heads/master` there should allow you to trigger the rescan.

```auto
    @EventListener
    public void onRepositoryRefsChanged(RepositoryRefsChangedEvent event) throws Exception {
        if (!shouldScan(event.getRepository())) {
            return;
        }
        event.getRefChanges().stream()
                .filter(refChange -> Objects.equals(refChange.getRef().getId(), "refs/heads/master"))
                .findFirst()
                .ifPresent(refChange -> scanMaster());
    }

```

If you need finer control over when the scanning should happen, our `PostRepositoryHook` SPI should allow you to get the information you need.

It’s worth noting that this SPI will not tell you if branch A was merged into branch B. Our merge triggers refer to merges executed by the system (e.g.: for a pull request), but a user could merge two branches remotely and push the ref updates. It will however let you know if commits were added or removed to a specific ref, thus letting you decided if you need to re-scan `master`.

In your `PostRepositoryHook` implementation you can check if the repository should be scanned. If so, you’ll need to register a `RepositoryHookCommitCallback` using the `RepositoryHookCommitFilter#ADDED_TO_ANY_REF` filter and `onCommitAdded` check the ref ID to see if it’s `master`. Not sure if you also care about commits being removed from `master` in which case you’ll also need the `RepositoryHookCommitFilter#REMOVED_FROM_ANY_REF` filter and implement `onCommitRemoved`. Finally you can implement `onEnd` to trigger your scan.

Something like this:

```auto
    public class SonarScannerHookCommitCallback implements RepositoryHookCommitCallback {

        private boolean rescan;

        @Override
        public boolean onCommitAdded(@Nonnull CommitAddedDetails commitDetails) {
            if (Objects.equals(commitDetails.getRef().getId(), "refs/heads/master")) {
                rescan = true;

                //We already know we need to rescan so return false so we'll stop being fed commits
                //and end the git command execution early
                return false;
            }
            return true;
        }

        public void onEnd() {
            if (rescan) {
                //TODO: Trigger Sonar rescan
            }
        }
    }

```

Hope this answers your question!

Regards,  
Juan Palacios

---

<div class="post-metadata">

**Author:** ![ratboyDC](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@ratboyDC](https://community.developer.atlassian.com/u/ratboyDC)\
**Post date:** [November 6, 2017, 12:47pm UTC](https://community.developer.atlassian.com/t/bitbucket-postrepositoryhook-plugin-question/10029/5 "2017-11-06T12:47:22Z")

</div>

Hi, jpalacios - very much appreciate the reply. I’ll follow your instructions and crank something out in the next couple of days to see if I can get this functionality delivered to my architect.
