-
Notifications
You must be signed in to change notification settings - Fork 615
Expand file tree
/
Copy pathGitLabMergeRequestStatusStep.java
More file actions
125 lines (104 loc) · 3.99 KB
/
Copy pathGitLabMergeRequestStatusStep.java
File metadata and controls
125 lines (104 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.dabsquared.gitlabjenkins.workflow;
import com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty;
import com.dabsquared.gitlabjenkins.gitlab.api.GitLabClient;
import com.dabsquared.gitlabjenkins.gitlab.api.model.BuildState;
import com.dabsquared.gitlabjenkins.gitlab.api.model.MergeRequest;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.State;
import com.dabsquared.gitlabjenkins.util.CommitStatusUpdater;
import com.google.common.collect.ImmutableSet;
import hudson.Extension;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.ListBoxModel;
import org.jenkinsci.plugins.workflow.steps.*;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.export.ExportedBean;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author <a href="mailto:robin.mueller@1und1.de">Robin Müller</a>
*/
@ExportedBean
public class GitLabMergeRequestStatusStep extends Step {
private final static Logger LOGGER = Logger.getLogger(GitLabMergeRequestStatusStep.class.getName());
private String projectName;
private String branchName;
@DataBoundConstructor
public GitLabMergeRequestStatusStep(String project_name, String branch_name) {
this.projectName = project_name;
this.branchName = branch_name;
}
public String getProjectName() {
return projectName;
}
@DataBoundSetter
public void setProjectName(String project_name) {
this.projectName = project_name;
}
public String getBranchName() {
return branchName;
}
@DataBoundSetter
public void setBranchName(String branch_name) {
this.branchName = branch_name;
}
@Override
public StepExecution start(StepContext context) throws Exception {
return new GitLabMergeRequestStatusStepExecution(context, this);
}
public static class GitLabMergeRequestStatusStepExecution extends SynchronousNonBlockingStepExecution<MergeRequest> {
private static final long serialVersionUID = 1;
private final transient Run<?, ?> run;
private final transient GitLabMergeRequestStatusStep step;
GitLabMergeRequestStatusStepExecution(StepContext context, GitLabMergeRequestStatusStep step) throws Exception {
super(context);
this.step = step;
run = context.get(Run.class);
}
@Override
protected MergeRequest run() throws Exception {
GitLabClient client = GitLabConnectionProperty.getClient(run);
if (client == null) {
LOGGER.log(Level.WARNING, "Failed to find gitlab connection");
return null;
}
Integer page = 1;
do {
List<MergeRequest> mergeRequests = client.getMergeRequests(step.branchName, State.opened, page, 100);
for (MergeRequest mr : mergeRequests) {
if (mr.getSourceBranch().equals(step.branchName)) {
return mr;
}
}
page = mergeRequests.isEmpty() ? null : page + 1;
} while (page != null);
return null;
}
}
@Extension
public static final class DescriptorImpl extends StepDescriptor {
@Override
public String getDisplayName() {
return "Return the MergeRequest status for the branch, if it exists.";
}
@Override
public String getFunctionName() {
return "gitlabMergeRequestStatus";
}
public ListBoxModel doFillStateItems() {
ListBoxModel options = new ListBoxModel();
for (BuildState buildState : EnumSet.allOf(BuildState.class)) {
options.add(buildState.name());
}
return options;
}
@Override
public Set<Class<?>> getRequiredContext() {
return ImmutableSet.of(TaskListener.class, Run.class);
}
}
}