Skip to content

Commit 4b4c2a2

Browse files
committed
jenkinsci#2 fix findbugs warnings
1 parent 2124823 commit 4b4c2a2

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

src/main/java/io/fabric8/jenkins/openshiftsync/BuildSyncRunListener.java

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
/**
2-
* Licensed to the Apache Software Foundation (ASF) under one or more
3-
* contributor license agreements. See the NOTICE file distributed with
4-
* this work for additional information regarding copyright ownership.
5-
* The ASF licenses this file to You under the Apache License, Version 2.0
6-
* (the "License"); you may not use this file except in compliance with
7-
* the License. You may obtain a copy of the License at
8-
* <p/>
9-
* http://www.apache.org/licenses/LICENSE-2.0
10-
* <p/>
2+
* Copyright (C) 2016 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
1110
* Unless required by applicable law or agreed to in writing, software
1211
* distributed under the License is distributed on an "AS IS" BASIS,
1312
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -21,11 +20,9 @@
2120
import com.squareup.okhttp.Response;
2221
import com.thoughtworks.xstream.annotations.XStreamOmitField;
2322
import hudson.Extension;
24-
import hudson.model.Job;
2523
import hudson.model.Result;
2624
import hudson.model.Run;
2725
import hudson.model.TaskListener;
28-
import hudson.model.TopLevelItem;
2926
import hudson.model.listeners.RunListener;
3027
import io.fabric8.kubernetes.api.model.ObjectMeta;
3128
import io.fabric8.openshift.api.model.Build;
@@ -34,9 +31,7 @@
3431
import io.fabric8.openshift.api.model.BuildConfigSpec;
3532
import io.fabric8.openshift.api.model.BuildList;
3633
import io.fabric8.openshift.api.model.BuildSpec;
37-
import io.fabric8.openshift.api.model.BuildStatus;
3834
import io.fabric8.openshift.client.OpenShiftClient;
39-
import jenkins.model.Jenkins;
4035
import jenkins.util.Timer;
4136
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
4237
import org.kohsuke.stapler.DataBoundConstructor;
@@ -161,11 +156,7 @@ protected void pollLoop() {
161156
protected void pollRun(String url, Run run) {
162157
BuildName buildName = BuildName.parseBuildUrl(url);
163158

164-
// TODO is there a better place to find this?
165-
String root = Jenkins.getInstance().getRootUrl();
166-
if (root == null || root.length() == 0) {
167-
root = "http://localhost:8080/jenkins/";
168-
}
159+
String root = JenkinsUtils.getRootUrl();
169160

170161
String fullUrl = joinPaths(root, url, "/wfapi/describe");
171162
logger.info("Polling URL: " + fullUrl + " for " + buildName);
@@ -260,18 +251,18 @@ private void upsertBuild(BuildName buildName, Run run, String json, String url)
260251
phase = BuildPhases.RUNNING;
261252
} else {
262253
Result result = run.getResult();
263-
if (result.equals(Result.SUCCESS)) {
264-
phase = BuildPhases.COMPLETE;
265-
} else if (result.equals(Result.ABORTED)) {
266-
phase = BuildPhases.CANCELLED;
267-
} else if (result.equals(Result.FAILURE)) {
268-
phase = BuildPhases.FAILED;
269-
} else if (result.equals(Result.UNSTABLE)) {
270-
phase = BuildPhases.FAILED;
271-
} else if (result.equals(Result.NOT_BUILT)) {
272-
phase = BuildPhases.PENDING;
273-
} else {
274-
phase = BuildPhases.PENDING;
254+
if (result != null) {
255+
if (result.equals(Result.SUCCESS)) {
256+
phase = BuildPhases.COMPLETE;
257+
} else if (result.equals(Result.ABORTED)) {
258+
phase = BuildPhases.CANCELLED;
259+
} else if (result.equals(Result.FAILURE)) {
260+
phase = BuildPhases.FAILED;
261+
} else if (result.equals(Result.UNSTABLE)) {
262+
phase = BuildPhases.FAILED;
263+
} else {
264+
phase = BuildPhases.PENDING;
265+
}
275266
}
276267
}
277268
}

src/main/java/io/fabric8/jenkins/openshiftsync/JenkinsUtils.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
*/
2525
public class JenkinsUtils {
2626
public static Job getJob(String job) {
27-
TopLevelItem item = Jenkins.getInstance().getItem(job);
28-
if (item instanceof Job) {
29-
return (Job) item;
27+
Jenkins jenkins = Jenkins.getInstance();
28+
if (jenkins != null) {
29+
TopLevelItem item = jenkins.getItem(job);
30+
if (item instanceof Job) {
31+
return (Job) item;
32+
}
3033
}
3134
return null;
3235
}
@@ -42,4 +45,17 @@ public static Run getRun(String jobName, String buildName) {
4245
public static Run getRun(BuildName buildName) {
4346
return getRun(buildName.getJobName(), buildName.getBuildName());
4447
}
48+
49+
public static String getRootUrl() {
50+
// TODO is there a better place to find this?
51+
String root = null;
52+
Jenkins jenkins = Jenkins.getInstance();
53+
if (jenkins != null) {
54+
root = jenkins.getRootUrl();
55+
}
56+
if (root == null || root.length() == 0) {
57+
root = "http://localhost:8080/jenkins/";
58+
}
59+
return root;
60+
}
4561
}

0 commit comments

Comments
 (0)