Using CodeService on a thread pool

Hello,

I’m writing bitbucket plugin and trying to offload some bitbucket specific work into thread pool but I’m getting “AuthenticationCredentialsNotFoundException”.

I’m guessing it’s due to bitbucket API objects having thread affinity.
Is there any way to copy thread context to ExecutorService?

I was trying to use Spring Security but it’s not provided by bitbucket (cannot import it so easily)…
I was trying to get “ThreadLocalDelegateExecutorFactory” from SAL but Spring cannot resolve it…

Any other ideas?

Exception in question:

and link to the code:

Thanks a lot!

Try to use com.atlassian.bitbucket.user.SecurityService and EscalatedSecurityContext

Example

    private void downloadRepository(Repository repository, String commitId, Path filePath) throws IOException {
        securityService.withPermission(Permission.REPO_READ, "download repository").call(() -> {
            try (FileOutputStream fileOutputStream = new FileOutputStream(filePath.toFile())) {
                contentService.streamArchive(
                        new ArchiveRequest.Builder(repository, commitId).build(),
                        fileType -> fileOutputStream);
            }
        });
    }
2 Likes

I tend to use
<component-import key="executorService" interface="java.util.concurrent.ScheduledExecutorService"/>
to offload tasks to a Bitbucket controlled threadpool and this seems to include the auth context.

executorService.schedule(new Callable<Object>() {
                @Override
                public Object call() throws Exception { ...
                    pullRequestService.getById(...)
                }
}, ...)

It worked - thanks!