Get commits being pushed to the stash server

I am trying to create a pre-receive hook to retrieve the contents of the files that are being added or updated to the stash server so that I can analyze their content and based on that analysis allow or deny the push.

To start with, am trying to retrieve the commit that is being pushed so that I can get the associated changes. So far I was only able to retrieve old commits and not he ones currently being pushed.

My code:

 public boolean onReceive(RepositoryHookContext context, Collection<RefChange> refChanges, HookResponse hr) {
        Repository repository = context.getRepository();
        for (RefChange refChange : refChanges) {
            final CommitsBetweenRequest request = new CommitsBetweenRequest.Builder(repository)
                    .exclude(refChange.getFromHash()) //this is all 0's for RefChangeType.ADD
                    .include(refChange.getToHash())
                    .build();
            Page<Commit> cs = commitService.getCommitsBetween(request, PageUtils.newRequest(0, 9999));
            for (Commit commit : cs.getValues()) {
                //false for new commits, true for old ones
                if (!commitIndex.isMemberOf(commit.getId(), repository)) {
                    //do stuff;
                }
            }
        }
        return false;
    }

Any help would be apreciated.

Regards,

Pedro chaves

 

 

 

0 answers

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Piotr Plewa
Published Dec 27, 2017 in Bitbucket

Recipe: Deploying AWS Lambda functions with Bitbucket Pipelines

Bitbucket Pipelines helps me manage and automate a number of serverless deployments to AWS Lambda and this is how I do it. I'm building Node.js Lambda functions using node-lambda&nbsp...

1,773 views 1 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you