Bitbucket plugin, PostRepositoryHook detect branch deletion and creation

I’m working on a small plugin whit the sole purpose of writing information about if a branch has been deleted or created to a log file. The code below provides information about which branch is being used when pushing but I’d like to log whenever a branch is created or deleted. Anyone know how to get more detailed information about the changes in the push?

public void postUpdate(@Nonnull PostRepositoryHookContext context, 
                       @Nonnull RepositoryHookRequest hookRequest) {

	try {
		FileWriter fw = new FileWriter(new File("PATH/log"));
		BufferedWriter bw = new BufferedWriter(fw);
		bw.append("New entry\n");
    	Collection<RefChange> changes = hookRequest.getRefChanges();
    	for (RefChange changes2 : changes) {
    		bw.append("Id: " + changes2.getRef().getId().toString()+"\n");
    		bw.append("DisplayId: " + changes2.getRef().getDisplayId().toString()+"\n");
    		bw.append("Class: " + changes2.getRef().getClass().toString()+"\n");
    		bw.append("Type: " + changes2.getRef().getType().toString()+"\n");
    	}
		bw.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
1 Like

Can you be more specific about what ‘detailed information’ you want?
Any particular reason you are going with a hook rather than making an event listener for BranchCreatedEvent and BranchDeletedEvent

2 Likes

Yes I’ve been looking into them but they are limited to branch delete/creation from the REST API and the UI. I need to catch branch deletes and creations from pushes as well. Do you know if this is possible with the Java API? I run a 5.2.3 server.

We are using a mirroring technique on multiple bitbucket instances but we have no way of knowing whether a branch has been created or deleted. We’d like to get that information at push time in order to properly deal with branch deletes and creations.
Thankful for any help.

What you have will catch branch deletions and creations from pushes. The ref type will give you whether it was a branch or a tag and the change type will give you whether it was created or deleted.

2 Likes

Thank you I worked it out now!

Hello, how do you distinguish between branch deletion and branch creation instead of file creation?