Skip to content

Commit

Permalink
Option to ignore type in build StatusName (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
mifitous authored Jun 19, 2023
1 parent 2602c3e commit 877fdc2
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class BuildStatusNameCustomPartTrait extends SCMSourceTrait {
private String buildStatusNameCustomPart = "";

private boolean buildStatusNameOverwrite;
private boolean ignoreTypeInStatusName;

/**
* Constructor for stapler.
Expand All @@ -38,12 +39,18 @@ public void setBuildStatusNameOverwrite(@NonNull Boolean buildStatusNameOverwrit
this.buildStatusNameOverwrite = buildStatusNameOverwrite;
}

@DataBoundSetter
public void setIgnoreTypeInStatusName(@NonNull Boolean ignoreTypeInStatusName) {
this.ignoreTypeInStatusName = ignoreTypeInStatusName;
}

@Override
protected void decorateContext(SCMSourceContext<?, ?> context) {
if (context instanceof GitLabSCMSourceContext) {
GitLabSCMSourceContext ctx = (GitLabSCMSourceContext) context;
ctx.withBuildStatusNameCustomPart(getBuildStatusNameCustomPart());
ctx.withBuildStatusNameOverwrite(getBuildStatusNameOverwrite());
ctx.withIgnoreTypeInStatusName(getIgnoreTypeInStatusName());
}
}

Expand All @@ -66,6 +73,15 @@ public boolean getBuildStatusNameOverwrite() {
return buildStatusNameOverwrite;
}

/**
* Getter method for the build status name overwrite
*
* @return build status name overwrite option
*/
public boolean getIgnoreTypeInStatusName() {
return ignoreTypeInStatusName;
}

/**
* Our descriptor.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class GitLabSCMSourceContext extends SCMSourceContext<GitLabSCMSourceCont

private boolean buildStatusNameOverwrite;

private boolean ignoreTypeInStatusName;

private boolean alwaysBuildMROpen = true;

private boolean alwaysBuildMRReOpen = true;
Expand Down Expand Up @@ -189,6 +191,10 @@ public boolean getBuildStatusNameOverwrite() {
return buildStatusNameOverwrite;
}

public boolean getIgnoreTypeInStatusName() {
return ignoreTypeInStatusName;
}

@NonNull
public GitLabSCMSourceContext wantBranches(boolean include) {
wantBranches = wantBranches || include;
Expand Down Expand Up @@ -299,6 +305,11 @@ public final GitLabSCMSourceContext withBuildStatusNameOverwrite(final Boolean b
return this;
}

public final GitLabSCMSourceContext withIgnoreTypeInStatusName(final Boolean ignoreTypeInStatusName) {
this.ignoreTypeInStatusName = ignoreTypeInStatusName;
return this;
}

@NonNull
@Override
public GitLabSCMSourceRequest newRequest(@NonNull SCMSource source, @CheckForNull TaskListener listener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,31 @@ static String getStatusName(
+ revision.getClass().getName() + ", append" + type + " to status name");
}

// Some comments regarding this implementation :
// Old code was :
// GITLAB_PIPELINE_STATUS_PREFIX + GITLAB_PIPELINE_STATUS_DELIMITER + (optional:
// pipelinePrefix + GITLAB_PIPELINE_STATUS_DELIMITER) + type
// New implementation:
// 1- BuildStatusNameCustomPart empty : use default
// GITLAB_PIPELINE_STATUS_PREFIX
// 2- else: depending on BuildStatusNameOverwrite : use
// BuildStatusNameCustomPart appended to GITLAB_PIPELINE_STATUS_PREFIX or use it
// as is
// 3- add type only if required (no ignoreTypeInStatusName)

String pipelinePrefix = sourceContext.getBuildStatusNameCustomPart().trim();
if (!pipelinePrefix.isEmpty()) {
pipelinePrefix = envVars.expand(pipelinePrefix) + GITLAB_PIPELINE_STATUS_DELIMITER;
pipelinePrefix = envVars.expand(pipelinePrefix);
if (!sourceContext.getBuildStatusNameOverwrite()) {
pipelinePrefix = GITLAB_PIPELINE_STATUS_PREFIX + GITLAB_PIPELINE_STATUS_DELIMITER + pipelinePrefix;
}
} else {
pipelinePrefix = GITLAB_PIPELINE_STATUS_PREFIX + GITLAB_PIPELINE_STATUS_DELIMITER;
pipelinePrefix = GITLAB_PIPELINE_STATUS_PREFIX;
}
if (!sourceContext.getIgnoreTypeInStatusName()) {
pipelinePrefix = pipelinePrefix + GITLAB_PIPELINE_STATUS_DELIMITER + type;
}
final String statusName = pipelinePrefix + type;
final String statusName = pipelinePrefix;
LOGGER.log(Level.FINEST, () -> "Retrieved status name is: " + statusName);
return statusName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
<f:entry title="${%Overwrite full name}" field="buildStatusNameOverwrite">
<f:checkbox default="unchecked"/>
</f:entry>
<f:entry title="${%Do not append type in status name}" field="ignoreTypeInStatusName">
<f:checkbox default="unchecked"/>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,41 @@ public void should_set_branch_status_name() {
+ "branch"));
}

@Test
public void should_set_branch_status_name_withBuildStatusNameCustomPart() {
GitLabSCMSourceContext sourceContext = new GitLabSCMSourceContext(null, null);
sourceContext.withBuildStatusNameCustomPart("CUSTOM");
sourceContext.withBuildStatusNameOverwrite(false);

BranchSCMHead head = new BranchSCMHead("head");
SCMRevision revision = new BranchSCMRevision(head, "hash");

String statusName =
GitLabPipelineStatusNotifier.getStatusName(sourceContext, null, revision, new hudson.EnvVars());

assertThat(
statusName,
is(GitLabPipelineStatusNotifier.GITLAB_PIPELINE_STATUS_PREFIX
+ GitLabPipelineStatusNotifier.GITLAB_PIPELINE_STATUS_DELIMITER
+ "CUSTOM"
+ GitLabPipelineStatusNotifier.GITLAB_PIPELINE_STATUS_DELIMITER
+ "branch"));
}

@Test
public void should_set_branch_status_name_withIgnoreTypeInStatusName() {
GitLabSCMSourceContext sourceContext = new GitLabSCMSourceContext(null, null);
sourceContext.withIgnoreTypeInStatusName(true);

BranchSCMHead head = new BranchSCMHead("head");
SCMRevision revision = new BranchSCMRevision(head, "hash");

String statusName =
GitLabPipelineStatusNotifier.getStatusName(sourceContext, null, revision, new hudson.EnvVars());

assertThat(statusName, is(GitLabPipelineStatusNotifier.GITLAB_PIPELINE_STATUS_PREFIX));
}

@Test
public void should_set_merge_request_head_status_name() {
GitLabSCMSourceContext sourceContext = new GitLabSCMSourceContext(null, null);
Expand Down

0 comments on commit 877fdc2

Please sign in to comment.