Skip to content

Commit 2cd608d

Browse files
committed
Remove duplicated code
1 parent cb0441b commit 2cd608d

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

src/main/java/org/jenkinsci/plugins/gogs/GogsWebHook.java

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ associated documentation files (the "Software"), to deal in the Software without
4545
import java.util.Arrays;
4646
import java.util.List;
4747
import java.util.Map;
48+
import java.util.concurrent.atomic.AtomicBoolean;
49+
import java.util.concurrent.atomic.AtomicReference;
4850
import java.util.logging.Logger;
4951
import java.util.regex.Pattern;
5052
import java.util.stream.Collectors;
@@ -167,45 +169,35 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
167169
body = body.substring(8);
168170
}
169171

170-
String jSecret = null;
171-
boolean foundJob = false;
172+
AtomicReference<String> jSecret = new AtomicReference<>(null);
173+
AtomicBoolean foundJob = new AtomicBoolean(false);
172174
payloadProcessor.setPayload("ref", jsonObject.getString("ref"));
173175
payloadProcessor.setPayload("before", jsonObject.getString("before"));
174176

175177
SecurityContext saveCtx = ACL.impersonate(ACL.SYSTEM);
176178

177179
try {
178-
Job job = GogsUtils.find(jobName, Job.class);
179-
180-
if (job != null) {
181-
foundJob = true;
182-
/* secret is stored in the properties of Job */
183-
final GogsProjectProperty property = (GogsProjectProperty) job.getProperty(GogsProjectProperty.class);
184-
if (property != null) { /* only if Gogs secret is defined on the job */
185-
jSecret = property.getGogsSecret(); /* Secret provided by Jenkins */
186-
}
180+
String ref = (String) jsonObject.get("ref");
181+
String[] components = ref.split("/");
182+
if (components.length > 3) {
183+
/* refs contains branch/tag with a slash */
184+
List<String> test = Arrays.asList(ref.split("/"));
185+
ref = String.join("%2F", test.subList(2, test.size()));
187186
} else {
188-
String ref = (String) jsonObject.get("ref");
189-
String[] components = ref.split("/");
190-
if (components.length > 3) {
191-
/* refs contains branch/tag with a slash */
192-
List<String> test = Arrays.asList(ref.split("/"));
193-
ref = String.join("%2F", test.subList(2, test.size()));
194-
} else {
195-
ref = components[components.length - 1];
196-
}
197-
198-
job = GogsUtils.find(jobName + "/" + ref, Job.class);
187+
ref = components[components.length - 1];
188+
}
199189

190+
Arrays.asList(jobName, jobName + "/" + ref).forEach(j -> {
191+
Job job = GogsUtils.find(j, Job.class);
200192
if (job != null) {
201-
foundJob = true;
193+
foundJob.set(true);
202194
/* secret is stored in the properties of Job */
203195
final GogsProjectProperty property = (GogsProjectProperty) job.getProperty(GogsProjectProperty.class);
204196
if (property != null) { /* only if Gogs secret is defined on the job */
205-
jSecret = property.getGogsSecret(); /* Secret provided by Jenkins */
197+
jSecret.set(property.getGogsSecret()); /* Secret provided by Jenkins */
206198
}
207199
}
208-
}
200+
});
209201
} finally {
210202
SecurityContextHolder.setContext(saveCtx);
211203
}
@@ -215,23 +207,23 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
215207
gSecret = jsonObject.optString("secret", null); /* Secret provided by Gogs < 0.10.x */
216208
} else {
217209
try {
218-
if (gogsSignature.equals(encode(body, jSecret))) {
219-
gSecret = jSecret;
210+
if (gogsSignature.equals(encode(body, jSecret.get()))) {
211+
gSecret = jSecret.get();
220212
// now hex is right, continue to old logic
221213
}
222214
} catch (Exception e) {
223215
LOGGER.warning(e.getMessage());
224216
}
225217
}
226218

227-
if (!foundJob) {
219+
if (!foundJob.get()) {
228220
String msg = String.format("Job '%s' is not defined in Jenkins", jobName);
229221
result.setStatus(404, msg);
230222
LOGGER.warning(msg);
231-
} else if (isNullOrEmpty(jSecret) && isNullOrEmpty(gSecret)) {
223+
} else if (isNullOrEmpty(jSecret.get()) && isNullOrEmpty(gSecret)) {
232224
/* No password is set in Jenkins and Gogs, run without secrets */
233225
result = payloadProcessor.triggerJobs(jobName, gogsDelivery);
234-
} else if (!isNullOrEmpty(jSecret) && jSecret.equals(gSecret)) {
226+
} else if (!isNullOrEmpty(jSecret.get()) && jSecret.equals(gSecret)) {
235227
/* Password is set in Jenkins and Gogs, and is correct */
236228
result = payloadProcessor.triggerJobs(jobName, gogsDelivery);
237229
} else {

src/test/java/org/jenkinsci/plugins/gogs/GogsWebHookTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,6 @@ public void whenUriDoesNotContainUrlNameMustReturnError() throws Exception {
235235
//
236236
// Helper methods
237237
//
238-
239-
240238
private void performDoIndexTest(StaplerRequest staplerRequest, StaplerResponse staplerResponse, File file) throws IOException {
241239
PrintWriter printWriter = new PrintWriter(file.getAbsoluteFile(), "UTF-8");
242240
when(staplerResponse.getWriter()).thenReturn(printWriter);

0 commit comments

Comments
 (0)