-
Notifications
You must be signed in to change notification settings - Fork 615
Expand file tree
/
Copy pathAddGitLabMergeRequestCommentStep.java
More file actions
145 lines (120 loc) · 5.12 KB
/
Copy pathAddGitLabMergeRequestCommentStep.java
File metadata and controls
145 lines (120 loc) · 5.12 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.dabsquared.gitlabjenkins.workflow;
import static com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty.getClient;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.WebApplicationException;
import com.dabsquared.gitlabjenkins.util.CauseUtil;
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;
import com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause;
import com.dabsquared.gitlabjenkins.gitlab.api.GitLabClient;
import com.dabsquared.gitlabjenkins.gitlab.api.model.MergeRequest;
import com.google.common.collect.ImmutableSet;
import hudson.Extension;
import hudson.model.Run;
import hudson.model.TaskListener;
/**
* @author <a href="mailto:robin.mueller@1und1.de">Robin Müller</a>
*/
@ExportedBean
public class AddGitLabMergeRequestCommentStep extends Step {
private static final Logger LOGGER = Logger.getLogger(AddGitLabMergeRequestCommentStep.class.getName());
private String comment;
@DataBoundConstructor
public AddGitLabMergeRequestCommentStep(String comment) {
this.comment = StringUtils.isEmpty(comment) ? null : comment;
}
@Override
public StepExecution start(StepContext context) throws Exception {
return new AddGitLabMergeRequestCommentStepExecution(context, this);
}
public String getComment() {
return comment;
}
@DataBoundSetter
public void setComment(String comment) {
this.comment = StringUtils.isEmpty(comment) ? null : comment;
}
public static class AddGitLabMergeRequestCommentStepExecution extends AbstractSynchronousStepExecution<Void> {
private static final long serialVersionUID = 1;
private final transient Run<?, ?> run;
private final transient AddGitLabMergeRequestCommentStep step;
AddGitLabMergeRequestCommentStepExecution(StepContext context, AddGitLabMergeRequestCommentStep step) throws Exception {
super(context);
this.step = step;
run = context.get(Run.class);
}
@Override
protected Void run() throws Exception {
GitLabWebHookCause cause = CauseUtil.findCauseFromUpstreamCauses(run.getCauses(), GitLabWebHookCause.class);
if (cause != null) {
MergeRequest mergeRequest = cause.getData().getMergeRequest();
if (mergeRequest != null) {
GitLabClient client = getClient(run);
if (client == null) {
println("No GitLab connection configured");
} else {
try {
client.createMergeRequestNote(mergeRequest, step.getComment());
} catch (WebApplicationException | ProcessingException e) {
printf("Failed to add comment on Merge Request for project '%s': %s%n", mergeRequest.getProjectId(), e.getMessage());
LOGGER.log(Level.SEVERE, String.format("Failed to add comment on Merge Request for project '%s'", mergeRequest.getProjectId()), e);
}
}
}
}
return null;
}
private void println(String message) {
TaskListener listener = getTaskListener();
if (listener == null) {
LOGGER.log(Level.FINE, "failed to print message {0} due to null TaskListener", message);
} else {
listener.getLogger().println(message);
}
}
private void printf(String message, Object... args) {
TaskListener listener = getTaskListener();
if (listener == null) {
LOGGER.log(Level.FINE, "failed to print message {0} due to null TaskListener", String.format(message, args));
} else {
listener.getLogger().printf(message, args);
}
}
private TaskListener getTaskListener() {
StepContext context = getContext();
if (!context.isReady()) {
return null;
}
try {
return context.get(TaskListener.class);
} catch (Exception x) {
return null;
}
}
}
@Extension
public static final class DescriptorImpl extends StepDescriptor {
@Override
public String getDisplayName() {
return "Add comment on GitLab Merge Request";
}
@Override
public String getFunctionName() {
return "addGitLabMRComment";
}
@Override
public Set<Class<?>> getRequiredContext() {
return ImmutableSet.of(TaskListener.class, Run.class);
}
}
}