Skip to content

Commit b5564dd

Browse files
authored
Add labels to pull request creations (#150)
1 parent 891b4e5 commit b5564dd

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/main/java/com/gitee/jenkins/publisher/GiteeCreatePullRequestPublisher.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
import java.io.IOException;
44
import java.time.LocalDateTime;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collections;
8+
import java.util.List;
59
import java.util.logging.Level;
610
import java.util.logging.Logger;
711

812
import org.kohsuke.stapler.DataBoundConstructor;
913
import org.kohsuke.stapler.DataBoundSetter;
1014

1115
import com.gitee.jenkins.gitee.api.GiteeClient;
16+
import com.gitee.jenkins.gitee.api.model.Label;
1217
import com.gitee.jenkins.gitee.api.model.PullRequest;
1318
import com.gitee.jenkins.gitee.api.model.builder.generated.PullRequestBuilder;
1419
import com.gitee.jenkins.util.LoggerUtil;
@@ -20,8 +25,10 @@
2025
import hudson.matrix.MatrixAggregator;
2126
import hudson.matrix.MatrixBuild;
2227
import hudson.model.AbstractBuild;
28+
import hudson.model.AbstractDescribableImpl;
2329
import hudson.model.AbstractProject;
2430
import hudson.model.BuildListener;
31+
import hudson.model.Descriptor;
2532
import hudson.model.Result;
2633
import hudson.tasks.BuildStepDescriptor;
2734
import hudson.tasks.BuildStepMonitor;
@@ -38,6 +45,7 @@ public class GiteeCreatePullRequestPublisher extends Notifier implements MatrixA
3845
private String base;
3946
private String head;
4047
private String body;
48+
private List<LabelNameEntry> labelNames = Collections.<LabelNameEntry>emptyList();
4149
private boolean addDatetime;
4250

4351
@DataBoundConstructor
@@ -76,6 +84,10 @@ public String getBody() {
7684
return body;
7785
}
7886

87+
public List<LabelNameEntry> getLabelNames() {
88+
return labelNames;
89+
}
90+
7991
@DataBoundSetter
8092
public void setRepo(String repo) {
8193
this.repo = repo;
@@ -111,9 +123,21 @@ public void setBody(String body) {
111123
this.body = body;
112124
}
113125

126+
@DataBoundSetter
127+
public void setLabelNames(List<LabelNameEntry> labelNames) {
128+
this.labelNames = labelNames;
129+
}
130+
114131
@Override
115132
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
116133
throws InterruptedException, IOException {
134+
135+
ArrayList<String> labels = new ArrayList<String>();
136+
for(LabelNameEntry entry : labelNames) {
137+
if (!labels.contains(entry.toString())) {
138+
labels.add(entry.toString());
139+
}
140+
}
117141

118142
GiteeClient client = getClient(build);
119143
if (client == null) {
@@ -138,6 +162,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
138162
.withSourceBranch(head)
139163
.withTargetBranch(base)
140164
.withDescription(body)
165+
.withLabels(labels)
141166
.build();
142167

143168
if (!client.getPullRequest(pr).isEmpty()) {
@@ -203,5 +228,31 @@ public boolean isApplicable(Class<? extends AbstractProject> aClass) {
203228
public String getDisplayName() {
204229
return Messages.GiteeCreatePullRequestPublisher_DisplayName();
205230
}
231+
232+
}
233+
234+
public static final class LabelNameEntry extends AbstractDescribableImpl<LabelNameEntry> {
235+
private final String text;
236+
237+
@DataBoundConstructor public LabelNameEntry(String text) {
238+
this.text = text;
239+
}
240+
241+
public String getText() {
242+
return text;
243+
}
244+
245+
@Override
246+
public String toString() {
247+
return text;
248+
}
249+
250+
@Extension
251+
public static class DescriptorImpl extends Descriptor<LabelNameEntry> {
252+
@Override
253+
public String getDisplayName() {
254+
return "Label";
255+
}
256+
}
206257
}
207258
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?jelly escape-by-default='true'?>
2+
<j:jelly xmlns:j="jelly:core"
3+
xmlns:f="/lib/form">
4+
<f:entry title="Label" field="text">
5+
<f:textbox/>
6+
</f:entry>
7+
</j:jelly>

src/main/resources/com/gitee/jenkins/publisher/GiteeCreatePullRequestPublisher/config.jelly

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
<f:textbox name="head" field="head"/>
2121
</f:entry>
2222
<f:entry title="${%body}" field="body">
23-
<f:textbox name="body" field="body"/>
23+
<f:textarea name="body" field="body"/>
24+
</f:entry>
25+
<f:entry title="labelNames" field="labelNames">
26+
<f:repeatableHeteroProperty hasHeader="true" addCaption="Add label" />
2427
</f:entry>
2528
</j:jelly>

0 commit comments

Comments
 (0)