-
Notifications
You must be signed in to change notification settings - Fork 619
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add merge request labels to environment variables (#1713)
* fix in readme * update readme * collect labels from merge request web hook and fill in to causeData factory * add merge request labels to CauseData * mvn spotless apply * simple test case * mvn spotless apply * simple unit tests * cleanup * mvn spotless apply * unify labels unit tests logic * argument naming fix * merge request labels checker helper function * update readme * update readme * GitLabMergeRequestExistsStepTest * update unit tests for label exists step * 1 more unit test * move unit test resource to pipeline folder * add author comment * Apply suggestions from code review Co-authored-by: Kris Stern <krisstern@outlook.com> * fix names * mvn spotless apply --------- Co-authored-by: Kris Stern <krisstern@outlook.com>
- Loading branch information
Showing
8 changed files
with
316 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/main/java/com/dabsquared/gitlabjenkins/workflow/GitLabMergeRequestLabelExistsStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.dabsquared.gitlabjenkins.workflow; | ||
|
||
import com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause; | ||
import hudson.Extension; | ||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousStepExecution; | ||
import org.jenkinsci.plugins.workflow.steps.Step; | ||
import org.jenkinsci.plugins.workflow.steps.StepContext; | ||
import org.jenkinsci.plugins.workflow.steps.StepDescriptor; | ||
import org.jenkinsci.plugins.workflow.steps.StepExecution; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
import org.kohsuke.stapler.export.ExportedBean; | ||
|
||
/** | ||
* @author Yiftah Waisman | ||
*/ | ||
@ExportedBean | ||
public class GitLabMergeRequestLabelExistsStep extends Step { | ||
|
||
private String label; | ||
|
||
@DataBoundConstructor | ||
public GitLabMergeRequestLabelExistsStep(String label) { | ||
this.label = StringUtils.isEmpty(label) ? null : label; | ||
} | ||
|
||
@Override | ||
public StepExecution start(StepContext context) throws Exception { | ||
return new GitLabMergeRequestLabelExistsStepExecution(context, this); | ||
} | ||
|
||
public String getLabel() { | ||
return label; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setLabel(String label) { | ||
this.label = StringUtils.isEmpty(label) ? null : label; | ||
} | ||
|
||
@Extension | ||
public static class DescriptorImpl extends StepDescriptor { | ||
|
||
@Override | ||
public String getFunctionName() { | ||
return "GitLabMergeRequestLabelExists"; | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Check if a GitLab merge request has a specific label"; | ||
} | ||
|
||
@Override | ||
public Set<? extends Class<?>> getRequiredContext() { | ||
Set<Class<?>> context = new HashSet<>(); | ||
Collections.addAll(context, TaskListener.class, Run.class); | ||
return Collections.unmodifiableSet(context); | ||
} | ||
} | ||
|
||
public static class GitLabMergeRequestLabelExistsStepExecution extends AbstractSynchronousStepExecution<Boolean> { | ||
private static final long serialVersionUID = 1; | ||
|
||
private final transient Run<?, ?> run; | ||
|
||
private final transient GitLabMergeRequestLabelExistsStep step; | ||
|
||
public GitLabMergeRequestLabelExistsStepExecution(StepContext context, GitLabMergeRequestLabelExistsStep step) | ||
throws Exception { | ||
super(context); | ||
this.step = step; | ||
run = context.get(Run.class); | ||
} | ||
|
||
@Override | ||
protected Boolean run() throws Exception { | ||
GitLabWebHookCause cause = run.getCause(GitLabWebHookCause.class); | ||
if (cause == null) { | ||
return false; | ||
} | ||
List<String> labels = cause.getData().getMergeRequestLabels(); | ||
if (labels == null) { | ||
return false; | ||
} | ||
return labels.contains(step.getLabel()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.