Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to implement a new Condition in Bitbucket?

Michael Smith June 12, 2019

My problem really is not around creating the condition so much as how to assess the Settings in a Repo.  So I want to know if a merge check is Enabled or Disabled, and add a banner if it is Enabled.  You will notice I temporarily added code to see what context I had, and how they looked.

I received the following (I deleted some text):

2019-06-12 10:47:14,734 WARN Item : request Object : org.apache.catalina.connector.RequestFacade@4cc9f5fb
2019-06-12 10:47:14,734 WARN Item : principal Object : InternalNormalUser{id=651, username=jdoe}
2019-06-12 10:47:14,735 WARN Item : repository Object : NG/my-repo[228]
2019-06-12 10:47:14,735 WARN 3 Entries found in context!

So great news, I got a repository Object.  Wait how do I get it settings?  This is where I am stuck.

atlassian-plugin.xml

<web-panel name="My Panel" key="my-banner-panel" location="bitbucket.web.repository.banner" weight="2000">
<resource name="view" type="static"><![CDATA[<div class="aui-message">
<p class="title">
<strong>REPO FROZEN</strong>
</p>
<p>An Administrator has currently FROZEN this repository.</p>
</div>]]>
</resource>
</web-panel>

IsMergeCheckEnabledCondition.java

public class IsMergeCheckEnabledCondition implements Condition {

private static final Logger log = LogManager.getLogger("com.atlassian.bitbucket");

public static final String MERGE_CHECK = "mergeCheck";

private String mergeCheckName;

IsMergeCheckEnabledCondition()
{
; //this.repoService = repositoryService;
}

@Override
public void init(Map<String, String> params)
{
mergeCheckName = params.get(MERGE_CHECK);
}

@Nonnull
@Override
public boolean shouldDisplay(Map<String, Object> context)
{
int count = 0;
for (Map.Entry<String, Object> entry : context.entrySet())
{
count++;
log.warn("Item : " + entry.getKey() + " Object : " + entry.getValue());
}
log.warn(count + " Entries found in context!");

return true;
}
}

 

 

1 answer

1 accepted

0 votes
Answer accepted
Michael Smith June 12, 2019

Well I figured everything out, and here is what I ended up with.

atlassian-plugin.xml

<atlassian-plugin key="${atlassian.plugin.key}" name="${project.name}" plugins-version="2">
<plugin-info>
<description>${project.description}</description>
<version>${project.version}</version>
<vendor name="${project.organization.name}" url="${project.organization.url}" />
<param name="plugin-icon">images/pluginIcon.png</param>
<param name="plugin-logo">images/pluginLogo.png</param>
</plugin-info>

<web-panel name="Frozen Panel" key="repo-frozen-banner-panel" location="bitbucket.web.repository.banner" weight="2000">
<condition class="com.myc.bitbucket.freeze.repo.IsMergeCheckEnabledCondition">
<param name="mergeCheck">${atlassian.plugin.key}:freezeRepo</param>
</condition>
<resource name="view" type="static"><![CDATA[<div class="aui-message">
<p class="title">
<strong>REPO FROZEN</strong>
</p>
<p>An Administrator has currently FROZEN this repository.</p>
</div>]]>
</resource>
</web-panel>

<!-- add our i18n resource -->
<resource type="i18n" name="i18n" location="FreezeRepo"/>

<repository-merge-check key="freezeRepo" class="bean:FreezeRepo" name="Freeze Repo"/>
</atlassian-plugin>

IsMergeCheckEnabledCondition.java

package com.myc.bitbucket.freeze.repo;

import java.util.Map;
import com.atlassian.bitbucket.hook.repository.RepositoryHook;
import com.atlassian.bitbucket.hook.repository.RepositoryHookService;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scope.RepositoryScope;

import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.plugin.web.Condition;

import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Nonnull;

public class IsMergeCheckEnabledCondition implements Condition {

public static final String MERGE_CHECK = "mergeCheck";
public static final String REPOSITORY = "repository";

RepositoryHookService repositoryHookService;
private String mergeCheckName;

@Autowired
public IsMergeCheckEnabledCondition(@ComponentImport RepositoryHookService repositoryHookService)
{
this.repositoryHookService = repositoryHookService;
}

@Override
public void init(Map<String, String> params)
{
mergeCheckName = params.get(MERGE_CHECK);
}

@Nonnull
@Override
public boolean shouldDisplay(Map<String, Object> context)
{
Repository repository = (Repository) context.get(REPOSITORY);
RepositoryScope repositoryScope = new RepositoryScope(repository);
RepositoryHook repositoryHook = repositoryHookService.getByKey(repositoryScope, mergeCheckName);

return repositoryHook.isEnabled();
}
}

If your interested in Freeze Repo it is rather simplistic, but here you go.

FreezeRepo.java

package com.myc.bitbucket.freeze.repo;

import java.util.Iterator;
import java.util.Set;
import com.atlassian.bitbucket.hook.repository.*;
import com.atlassian.bitbucket.pull.*;
import org.springframework.stereotype.Component;


import javax.annotation.Nonnull;

@Component("FreezeRepo")
public class FreezeRepo implements RepositoryMergeCheck {

@Nonnull
@Override
public RepositoryHookResult preUpdate(@Nonnull PreRepositoryHookContext context, @Nonnull PullRequestMergeHookRequest request)
{
return RepositoryHookResult.rejected("Not allowed to merge when the repo is \"FROZEN\"!", "This repository will not allow merging when it is in a \"FROZEN\" state.");
}
}

Most likely it will be modified later to allow "Privileged" people to merge like the IsAdmin MergeCheck that Atlassian Provides Plugin detail for.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events