Skip to content

Commit

Permalink
Use "get" prefix for configuration attribute getters (#328)
Browse files Browse the repository at this point in the history
The configuration-as-code plugin expects a plugins configuration
attributes to be accessible with setters prefixed with "set" and
getters prefixed with "get" or "is".

Change and add the getters for the configurations attributes that
don't follow this convention so this plugin can be fully configured
with configuration as code.
  • Loading branch information
laudrup authored Apr 8, 2024
1 parent d0794c1 commit 4bbf898
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public String getHookUrl() {
return hookUrl;
}

public boolean shouldNotifyBitBucket() {
public boolean getNotifyBitBucket() {
return notifyBitBucket;
}

Expand All @@ -75,7 +75,7 @@ public void setNotifyBitBucket(@CheckForNull boolean notifyBitBucket) {
this.notifyBitBucket = notifyBitBucket;
}

public boolean shouldUseJobNameAsBuildKey() {
public boolean getUseJobNameAsBuildKey() {
return useJobNameAsBuildKey;
}

Expand All @@ -89,6 +89,8 @@ public void setCredentialsId(@CheckForNull String credentialsId) {
this.credentialsId = credentialsId;
}

public String getCredentialsId() { return credentialsId; }

@DataBoundSetter
public void setSingleJob(String singleJob) {
if (isEmpty(singleJob)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public void run(BitBucketPPREventType eventType) throws Exception {
BitBucketPPRPluginConfig config = getGlobalConfig();
switch (eventType) {
case BUILD_STARTED:
if (config.shouldNotifyBitBucket()) {
if (config.getNotifyBitBucket()) {
setBuildStatusInProgress();
}
break;
case BUILD_FINISHED:
if (config.shouldNotifyBitBucket()) {
if (config.getNotifyBitBucket()) {
setBuildStatusOnFinished();
setApprovedOrDeclined();
}
Expand All @@ -74,7 +74,7 @@ protected BitBucketPPRPluginConfig getGlobalConfig() {
}

protected String computeBitBucketBuildKey(BitBucketPPREventContext context) {
if (getGlobalConfig().shouldUseJobNameAsBuildKey()) {
if (getGlobalConfig().getUseJobNameAsBuildKey()) {
Job<?, ?> job = context.getRun().getParent();
return job.getDisplayName();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.jenkins.plugins.bitbucketpushandpullrequest.config;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertTrue;

@RunWith(MockitoJUnitRunner.class)
public class BitBucketPPRConfigAttributesTest {

final private Class<BitBucketPPRPluginConfig> config = BitBucketPPRPluginConfig.class;

@Test
public void settersHaveGetters() {
for (Method method : config.getMethods()) {
final String methodName = method.getName();
if (method.getParameterCount() != 1 || !methodName.startsWith("set")) {
// Not an accessor, ignore
continue;
}

final String s = methodName.substring(3);
assertTrue(s, hasGetter(s));
}
}

private boolean hasGetter(String s) {
List<String> candidates = Arrays.asList("get" + s, "is" + s);
for (Method m : config.getMethods()) {
if (m.getParameterCount() == 0 && candidates.contains(m.getName())) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ public void testComputeBitBucketBuildKeyForInProgressBuild() {
Mockito.doReturn(config).when(spyObserver).getGlobalConfig();

// When it's configured to not use the job name
Mockito.when(config.shouldUseJobNameAsBuildKey()).thenReturn(false);
Mockito.when(config.getUseJobNameAsBuildKey()).thenReturn(false);

// Then the build number shall be the key
assertEquals(String.valueOf(buildNumber), spyObserver.computeBitBucketBuildKey(context));

// When it's configured to use the job name
Mockito.when(config.shouldUseJobNameAsBuildKey()).thenReturn(true);
Mockito.when(config.getUseJobNameAsBuildKey()).thenReturn(true);

// Then the the job name shall be the key
assertEquals(jobName, spyObserver.computeBitBucketBuildKey(context));
Expand All @@ -144,13 +144,13 @@ public void testComputeBitBucketBuildKeyForFinishedBuild() {


// When it's configured to not use the job name
Mockito.when(config.shouldUseJobNameAsBuildKey()).thenReturn(false);
Mockito.when(config.getUseJobNameAsBuildKey()).thenReturn(false);

// Then the build number shall be the key
assertEquals(String.valueOf(buildNumber), spyObserver.computeBitBucketBuildKey(context));

// When it's configured to use the job name
Mockito.when(config.shouldUseJobNameAsBuildKey()).thenReturn(true);
Mockito.when(config.getUseJobNameAsBuildKey()).thenReturn(true);

// Then the the job name shall be the key
assertEquals(jobName, spyObserver.computeBitBucketBuildKey(context));
Expand Down

0 comments on commit 4bbf898

Please sign in to comment.