RepositoryMergeCheck - how to issue git commands - SOLVED

I am implementing a RepositoryMergeCheck and want to be able to issue GIT commands against the repo. I cannot figure out how to get an instance of GitScmCommandBuilder or GitCommandBuilderFactory

Would someone know how I can get one of these so I can call builder().command(…)

For this kind of thing I recommend studying the source code of the free Bitbucket plugin called YACC (“Yet Another Commit Checker”)! It’s open source. Here’s the github link for it: GitHub - mohamicorp/yet-another-commit-checker: An Atlassian Bitbucket Server plugin to enforce commit message requirements.

But here’s a sneak preview of the technique for getting your hands on one of these:

@ComponentImport
private final GitCommandBuilderFactory gitBuilderFactory;

Hi

I have the declaration for GitCommandBuilderFactory, using the @ComponentImport. When I compile the code, I get this error:

variable gitCommandBuilderFactory might not have been initialized

So what I am asking is, how do I get an instance of the factory object? I am writting a MergeCheck plugin and the only interface that I have is:

RepositoryMergeCheck and this method:

public RepositoryHookResult preUpdate(@Nonnull PreRepositoryHookContext context,
                                      @Nonnull PullRequestMergeHookRequest request)

In this method I would like to be able to issue git commands against the repo. To do that, I need the factory so I can create the builder.

Does anyone know how to get an instance of the builder factory ?

PS - I downloaded the source code for YACC as you suggested, but I cannot find anywhere in the code where it uses the builder factory or implements the merge check interface.

1 Like

Solved!

I was able to find the builder factory stuff in the YACC plugin code, thanks for the tip. However, it is using the older, deprecated style of <component-import> rather than the preferred Spring Scanner method. Once I figured out how to use the @ComponentImport annotation, everything worked!

My merge check constructor looks like this:

@Autowired
public IsRebaseRequiredMergeCheck(@ComponentImport I18nService i18nService,
                                  @ComponentImport PermissionService permissionService,
                                  @ComponentImport GitCommandBuilderFactory gitCommandBuilderFactory) {
    this.i18nService = i18nService;
    this.permissionService = permissionService;
    this.gitCommandBuilderFactory = gitCommandBuilderFactory;
}
1 Like