Skip to content

Commit b37e0d6

Browse files
authored
Refactor using StringBuilder (#147)
1 parent ef5eeda commit b37e0d6

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void setRepo(String repo) {
8484
@DataBoundSetter
8585
public void setOwner(String owner) {
8686
this.owner = owner;
87-
}
87+
}
8888

8989
@DataBoundSetter
9090
public void setTitle(String title) {
@@ -123,7 +123,11 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
123123

124124
String pullRequestTitle = title;
125125
if (addDatetime) {
126-
pullRequestTitle = LocalDateTime.now().toString() + title;
126+
StringBuilder newTitle = new StringBuilder();
127+
newTitle.append(LocalDateTime.now().toString());
128+
newTitle.append(" ");
129+
newTitle.append(title);
130+
pullRequestTitle = newTitle.toString();
127131
}
128132

129133
if (build.getResult() == Result.SUCCESS) {
@@ -149,6 +153,29 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
149153
LOGGER.log(Level.INFO, "Pull request {0} generated, {1} -> {2}", LoggerUtil.toArray(title, head, base));
150154
}
151155

156+
if (build.getResult() == Result.SUCCESS) {
157+
PullRequest pr = PullRequestBuilder.pullRequest()
158+
.withRepoOwner(owner)
159+
.withRepoPath(repo)
160+
.withTitle(pullRequestTitle)
161+
.withSourceBranch(head)
162+
.withTargetBranch(base)
163+
.withDescription(body)
164+
.build();
165+
166+
if (!client.getPullRequest(pr).isEmpty()) {
167+
LOGGER.log(Level.INFO, "Pull request {0} -> {1} already exists", LoggerUtil.toArray(head, base));
168+
if (launcher != null) {
169+
launcher.getListener().getLogger().println("Pull request {0} -> {1} already exists");
170+
}
171+
172+
return true;
173+
}
174+
175+
client.createPullRequest(pr);
176+
LOGGER.log(Level.INFO, "Pull request {0} generated, {1} -> {2}", LoggerUtil.toArray(title, head, base));
177+
}
178+
152179
return true;
153180
}
154181

0 commit comments

Comments
 (0)