PostRepositoryHook, get which user triggered the hook

I wrote a small plugin which logs when a branch is created or deleted. My plugin uses https://developer.atlassian.com/static/javadoc/bitbucket-server/5.2.3/spi/reference/com/atlassian/bitbucket/hook/repository/PostRepositoryHook.html .

The code looks something like this…

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();
}

I want to get which user triggered the hook, is this possible if so how can I do it?

You can; just inject the AuthenticationContext into your hook’s constructor and then use AuthenticationContext.getCurrentUser() to determine the current user.

Some small nits about the code snippet (sorry, can’t help myself). You could use try-with-resources to make sure the writer is always closed, and since you’re already using .append, there’s no need for string concatenation:

        try (FileWriter fw = new FileWriter(new File("PATH/log"));
             BufferedWriter bw = new BufferedWriter(fw)) {
            bw.append("New entry\n");
            for (RefChange change : request.getRefChanges()) {
                bw.append("Id: ").append(change.getRef().getId())
                        .append("\nDisplayId: ").append(change.getRef().getDisplayId())
                        .append("\nClass: ").append(change.getRef().getClass().getName())
                        .append("\nType: ").append(change.getRef().getType().toString()).append('\n');
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
1 Like

By adding it to my hooks constructor I assume you mean something like this?

public class LoggingPostRepositoryHook implements PostRepositoryHook<RepositoryHookRequest> {
	
   private final AuthenticationContext authenticationContext;

    public LoggingPostRepositoryHook(AuthenticationContext authenticationContext) {
        this.authenticationContext = authenticationContext;
    }
	@Override
    public void postUpdate(@Nonnull PostRepositoryHookContext context, 
                           @Nonnull RepositoryHookRequest hookRequest) {
    ApplicationUser username = authenticationContext.getCurrentUser();
}

This does not work it looks fine in my eclipse environment and it compiles, but when uploading the jar to my server it says it could not be loaded due to one or more errors. Do you see what went wrong?

Most likely,AuthenticationContext isn’t being imported from OSGI yet. Each service or component that’s provided by the host application or another plugin needs to be imported from OSGI. Assuming you’re using atlassian-spring-scanner, the easiest way to do this is to create a simple class in your plugin where you mark your imports:

/**
 * Central class where all component imports are defined. The class itself has no function beyond that.
  **/
@Component
public class MyImports {

    @Autowire
    public MyImports(
        @ComponentImport AuthenticationContext authenticationContext,
        @ComponentImport RepositoryService repositortyService) { // add a parameter for each component you depend on
    }
}

Thank you a lot! You saved me a lot of headache, I weren’t aware of this OSGI component imports but I read up on spring runner scanner. buy the way I’m new to atlassian plugin development… I like your here on the forum.