Skip to content

Commit e73ddfa

Browse files
authored
Merge pull request #748 from res0nance/deprecation
Use non-deprecated methods and remove unused imports
2 parents 67e7032 + d4a3ba9 commit e73ddfa

File tree

57 files changed

+67
-214
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+67
-214
lines changed

src/main/java/hudson/plugins/git/GitChangeSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ private boolean isUseExistingAccountWithSameEmail() {
522522
@SuppressFBWarnings(value="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE",
523523
justification="Jenkins.getInstance() is not null")
524524
private DescriptorImpl getGitSCMDescriptor() {
525-
return (DescriptorImpl) Jenkins.getInstance().getDescriptor(GitSCM.class);
525+
return (DescriptorImpl) Jenkins.get().getDescriptor(GitSCM.class);
526526
}
527527

528528
@Override

src/main/java/hudson/plugins/git/GitPublisher.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,7 @@ public boolean perform(AbstractBuild<?, ?> build,
212212
listener.getLogger().println("Pushing HEAD to branch " + mergeTarget + " of " + remote.getName() + " repository");
213213

214214
remoteURI = remote.getURIs().get(0);
215-
PushCommand push = git.push().to(remoteURI).ref("HEAD:" + mergeTarget);
216-
if (forcePush) {
217-
push.force();
218-
}
215+
PushCommand push = git.push().to(remoteURI).ref("HEAD:" + mergeTarget).force(forcePush);
219216
push.execute();
220217
} else {
221218
//listener.getLogger().println("Pushing result " + buildnumber + " to origin repository");
@@ -269,10 +266,7 @@ else if (!tagExists) {
269266
+ targetRepo);
270267

271268
remoteURI = remote.getURIs().get(0);
272-
PushCommand push = git.push().to(remoteURI).ref(tagName);
273-
if (forcePush) {
274-
push.force();
275-
}
269+
PushCommand push = git.push().to(remoteURI).ref(tagName).force(forcePush);
276270
push.execute();
277271
} catch (GitException e) {
278272
e.printStackTrace(listener.error("Failed to push tag " + tagName + " to " + targetRepo));
@@ -305,10 +299,7 @@ else if (!tagExists) {
305299
listener.getLogger().println("Pushing HEAD to branch " + branchName + " at repo "
306300
+ targetRepo);
307301
remoteURI = remote.getURIs().get(0);
308-
PushCommand push = git.push().to(remoteURI).ref("HEAD:" + branchName);
309-
if (forcePush) {
310-
push.force();
311-
}
302+
PushCommand push = git.push().to(remoteURI).ref("HEAD:" + branchName).force(forcePush);
312303
push.execute();
313304
} catch (GitException e) {
314305
e.printStackTrace(listener.error("Failed to push branch " + branchName + " to " + targetRepo));
@@ -349,10 +340,7 @@ else if (!tagExists) {
349340
git.appendNote( noteMsg, noteNamespace );
350341

351342
remoteURI = remote.getURIs().get(0);
352-
PushCommand push = git.push().to(remoteURI).ref("refs/notes/*");
353-
if (forcePush) {
354-
push.force();
355-
}
343+
PushCommand push = git.push().to(remoteURI).ref("refs/notes/*").force(forcePush);
356344
push.execute();
357345
} catch (GitException e) {
358346
e.printStackTrace(listener.error("Failed to add note: \n" + noteMsg + "\n******"));

src/main/java/hudson/plugins/git/GitRevisionBuildParameters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public GitRevisionBuildParameters() {
5656
@SuppressFBWarnings(value="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", justification="Jenkins.getInstance() is not null")
5757
public Action getAction(AbstractBuild<?,?> build, TaskListener listener) {
5858
BuildData data = build.getAction(BuildData.class);
59-
if (data == null && Jenkins.getInstance().getPlugin("promoted-builds") != null) {
59+
if (data == null && Jenkins.get().getPlugin("promoted-builds") != null) {
6060
if (build instanceof hudson.plugins.promoted_builds.Promotion) {
6161
// We are running as a build promotion, so have to retrieve the git scm from target job
6262
data = ((hudson.plugins.promoted_builds.Promotion) build).getTarget().getAction(BuildData.class);

src/main/java/hudson/plugins/git/GitSCM.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
import hudson.FilePath;
1818
import hudson.Launcher;
1919
import hudson.init.Initializer;
20-
import hudson.matrix.MatrixBuild;
21-
import hudson.matrix.MatrixRun;
2220
import hudson.model.AbstractBuild;
2321
import hudson.model.AbstractProject;
2422
import hudson.model.Descriptor.FormException;
@@ -31,8 +29,6 @@
3129
import hudson.model.TaskListener;
3230
import hudson.model.queue.Tasks;
3331
import hudson.plugins.git.browser.GitRepositoryBrowser;
34-
import hudson.plugins.git.extensions.GitClientConflictException;
35-
import hudson.plugins.git.extensions.GitClientType;
3632
import hudson.plugins.git.extensions.GitSCMExtension;
3733
import hudson.plugins.git.extensions.GitSCMExtensionDescriptor;
3834
import hudson.plugins.git.extensions.impl.AuthorInChangelog;
@@ -77,9 +73,7 @@
7773
import org.jenkinsci.plugins.gitclient.FetchCommand;
7874
import org.jenkinsci.plugins.gitclient.Git;
7975
import org.jenkinsci.plugins.gitclient.GitClient;
80-
import org.jenkinsci.plugins.gitclient.JGitTool;
8176
import org.kohsuke.stapler.DataBoundConstructor;
82-
import org.kohsuke.stapler.Stapler;
8377
import org.kohsuke.stapler.StaplerRequest;
8478
import org.kohsuke.stapler.export.Exported;
8579

@@ -121,7 +115,6 @@
121115
import static org.apache.commons.collections.CollectionUtils.isEmpty;
122116
import static org.apache.commons.lang.StringUtils.isBlank;
123117

124-
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
125118

126119
/**
127120
* Git SCM.
@@ -698,7 +691,7 @@ private PollingResult compareRemoteRevisionWithImpl(Job<?, ?> project, Launcher
698691

699692
final EnvVars environment = project instanceof AbstractProject ? GitUtils.getPollEnvironment((AbstractProject) project, workspace, launcher, listener, false) : new EnvVars();
700693

701-
GitClient git = createClient(listener, environment, project, Jenkins.getInstance(), null);
694+
GitClient git = createClient(listener, environment, project, Jenkins.get(), null);
702695

703696
for (RemoteConfig remoteConfig : getParamExpandedRepos(lastBuild, listener)) {
704697
String remote = remoteConfig.getName();
@@ -1433,7 +1426,7 @@ public ChangeLogParser createChangeLogParser() {
14331426
} catch (IOException | InterruptedException e) {
14341427
LOGGER.log(Level.WARNING, "Git client using '" + gitTool + "' changelog parser failed, using deprecated changelog parser", e);
14351428
}
1436-
return new GitChangeLogParser(getExtensions().get(AuthorInChangelog.class) != null);
1429+
return new GitChangeLogParser(null, getExtensions().get(AuthorInChangelog.class) != null);
14371430
}
14381431

14391432
@Extension
@@ -1474,7 +1467,7 @@ public List<GitSCMExtensionDescriptor> getExtensionDescriptors() {
14741467

14751468
@SuppressFBWarnings(value="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", justification="Jenkins.getInstance() is not null")
14761469
public boolean showGitToolOptions() {
1477-
return Jenkins.getInstance().getDescriptorByType(GitTool.DescriptorImpl.class).getInstallations().length>1;
1470+
return Jenkins.get().getDescriptorByType(GitTool.DescriptorImpl.class).getInstallations().length>1;
14781471
}
14791472

14801473
/**
@@ -1483,7 +1476,7 @@ public boolean showGitToolOptions() {
14831476
*/
14841477
@SuppressFBWarnings(value="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", justification="Jenkins.getInstance() is not null")
14851478
public List<GitTool> getGitTools() {
1486-
GitTool[] gitToolInstallations = Jenkins.getInstance().getDescriptorByType(GitTool.DescriptorImpl.class).getInstallations();
1479+
GitTool[] gitToolInstallations = Jenkins.get().getDescriptorByType(GitTool.DescriptorImpl.class).getInstallations();
14871480
return Arrays.asList(gitToolInstallations);
14881481
}
14891482

@@ -1876,7 +1869,7 @@ private boolean isRevExcluded(GitClient git, Revision r, TaskListener listener,
18761869
justification = "Tests use null instance, Jenkins 2.60 declares instance is not null")
18771870
@Initializer(after=PLUGINS_STARTED)
18781871
public static void onLoaded() {
1879-
Jenkins jenkins = Jenkins.getInstance();
1872+
Jenkins jenkins = Jenkins.get();
18801873
DescriptorImpl desc = jenkins.getDescriptorByType(DescriptorImpl.class);
18811874

18821875
if (desc.getOldGitExe() != null) {

src/main/java/hudson/plugins/git/GitStatus.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import hudson.plugins.git.extensions.impl.IgnoreNotifyCommit;
1212
import hudson.scm.SCM;
1313
import hudson.security.ACL;
14+
import hudson.security.ACLContext;
1415
import hudson.triggers.SCMTrigger;
1516
import java.io.IOException;
1617
import java.io.PrintWriter;
@@ -26,15 +27,12 @@
2627
import jenkins.model.Jenkins;
2728
import jenkins.scm.api.SCMEvent;
2829
import jenkins.triggers.SCMTriggerItem;
29-
import org.acegisecurity.context.SecurityContext;
30-
import org.acegisecurity.context.SecurityContextHolder;
3130
import org.apache.commons.lang.StringUtils;
3231
import static org.apache.commons.lang.StringUtils.isNotEmpty;
3332
import org.eclipse.jgit.transport.RemoteConfig;
3433
import org.eclipse.jgit.transport.URIish;
3534
import org.kohsuke.stapler.*;
3635

37-
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
3836

3937
/**
4038
* Information screen for the use of Git in Hudson.
@@ -153,7 +151,7 @@ public HttpResponse doNotifyCommit(HttpServletRequest request, @QueryParameter(r
153151
}
154152

155153
final List<ResponseContributor> contributors = new ArrayList<>();
156-
Jenkins jenkins = Jenkins.getInstance();
154+
Jenkins jenkins = Jenkins.get();
157155
String origin = SCMEvent.originOf(request);
158156
for (Listener listener : jenkins.getExtensionList(Listener.class)) {
159157
contributors.addAll(listener.onNotifyCommit(origin, uri, sha1, buildParameters, branchesArray));
@@ -335,12 +333,10 @@ public List<ResponseContributor> onNotifyCommit(String origin, URIish uri, Strin
335333
// run in high privilege to see all the projects anonymous users don't see.
336334
// this is safe because when we actually schedule a build, it's a build that can
337335
// happen at some random time anyway.
338-
SecurityContext old = ACL.impersonate(ACL.SYSTEM);
339-
try {
340-
336+
try (ACLContext ctx = ACL.as(ACL.SYSTEM)) {
341337
boolean scmFound = false,
342338
urlFound = false;
343-
Jenkins jenkins = Jenkins.getInstance();
339+
Jenkins jenkins = Jenkins.getInstanceOrNull();
344340
if (jenkins == null) {
345341
LOGGER.severe("Jenkins.getInstance() is null in GitStatus.onNotifyCommit");
346342
return result;
@@ -462,8 +458,6 @@ public List<ResponseContributor> onNotifyCommit(String origin, URIish uri, Strin
462458

463459
lastStaticBuildParameters = allBuildParameters;
464460
return result;
465-
} finally {
466-
SecurityContextHolder.setContext(old);
467461
}
468462
}
469463

src/main/java/hudson/plugins/git/GitTagAction.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected GitTagAction(Run build, FilePath workspace, Revision revision) {
5656
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
5757
justification = "Tests use null instance, Jenkins 2.60 declares instance is not null")
5858
public Descriptor<GitTagAction> getDescriptor() {
59-
Jenkins jenkins = Jenkins.getInstance();
59+
Jenkins jenkins = Jenkins.get();
6060
return jenkins.getDescriptorOrDie(getClass());
6161
}
6262

@@ -157,21 +157,22 @@ public String getTooltip() {
157157
public synchronized void doSubmit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
158158
getACL().checkPermission(getPermission());
159159

160-
MultipartFormDataParser parser = new MultipartFormDataParser(req);
160+
try (MultipartFormDataParser parser = new MultipartFormDataParser(req)) {
161161

162-
Map<String, String> newTags = new HashMap<>();
162+
Map<String, String> newTags = new HashMap<>();
163163

164-
int i = -1;
165-
for (String e : tags.keySet()) {
166-
i++;
167-
if (tags.size() > 1 && parser.get("tag" + i) == null)
168-
continue; // when tags.size()==1, UI won't show the checkbox.
169-
newTags.put(e, parser.get("name" + i));
170-
}
164+
int i = -1;
165+
for (String e : tags.keySet()) {
166+
i++;
167+
if (tags.size() > 1 && parser.get("tag" + i) == null)
168+
continue; // when tags.size()==1, UI won't show the checkbox.
169+
newTags.put(e, parser.get("name" + i));
170+
}
171171

172-
scheduleTagCreation(newTags, parser.get("comment"));
172+
scheduleTagCreation(newTags, parser.get("comment"));
173173

174-
rsp.sendRedirect(".");
174+
rsp.sendRedirect(".");
175+
}
175176
}
176177

177178
/**

src/main/java/hudson/plugins/git/RevisionParameterAction.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,9 @@ public boolean shouldSchedule(List<Action> actions) {
194194
public void foldIntoExisting(Queue.Item item, Queue.Task owner, List<Action> otherActions) {
195195
// only do this if we are asked to.
196196
if(combineCommits) {
197-
RevisionParameterAction existing = item.getAction(RevisionParameterAction.class);
198-
if (existing!=null) {
199-
//because we cannot modify the commit in the existing action remove it and add self
200-
item.getActions().remove(existing);
201-
item.getActions().add(this);
202-
return;
203-
}
204-
// no CauseAction found, so add a copy of this one
205-
item.getActions().add(this);
197+
//because we cannot modify the commit in the existing action remove it and add self
198+
// or no CauseAction found, so add a copy of this one
199+
item.replaceAction(this);
206200
}
207201
}
208202

src/main/java/hudson/plugins/git/SubmoduleCombinator.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package hudson.plugins.git;
22

3-
import hudson.FilePath;
43
import hudson.model.TaskListener;
54
import hudson.plugins.git.util.GitUtils;
65
import org.eclipse.jgit.lib.ObjectId;

src/main/java/hudson/plugins/git/UserRemoteConfig.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ public static class DescriptorImpl extends Descriptor<UserRemoteConfig> {
9090
public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item project,
9191
@QueryParameter String url,
9292
@QueryParameter String credentialsId) {
93-
if (project == null && !Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER) ||
93+
if (project == null && !Jenkins.get().hasPermission(Jenkins.ADMINISTER) ||
9494
project != null && !project.hasPermission(Item.EXTENDED_READ)) {
9595
return new StandardListBoxModel().includeCurrentValue(credentialsId);
9696
}
9797
if (project == null) {
9898
/* Construct a fake project */
99-
project = new FreeStyleProject(Jenkins.getInstance(), "fake-" + UUID.randomUUID().toString());
99+
project = new FreeStyleProject(Jenkins.get(), "fake-" + UUID.randomUUID().toString());
100100
}
101101
return new StandardListBoxModel()
102102
.includeEmptyValue()
@@ -114,7 +114,7 @@ public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item project,
114114
public FormValidation doCheckCredentialsId(@AncestorInPath Item project,
115115
@QueryParameter String url,
116116
@QueryParameter String value) {
117-
if (project == null && !Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER) ||
117+
if (project == null && !Jenkins.get().hasPermission(Jenkins.ADMINISTER) ||
118118
project != null && !project.hasPermission(Item.EXTENDED_READ)) {
119119
return FormValidation.ok();
120120
}
@@ -162,7 +162,7 @@ public FormValidation doCheckUrl(@AncestorInPath Item item,
162162

163163
// Normally this permission is hidden and implied by Item.CONFIGURE, so from a view-only form you will not be able to use this check.
164164
// (TODO under certain circumstances being granted only USE_OWN might suffice, though this presumes a fix of JENKINS-31870.)
165-
if (item == null && !Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER) ||
165+
if (item == null && !Jenkins.get().hasPermission(Jenkins.ADMINISTER) ||
166166
item != null && !item.hasPermission(CredentialsProvider.USE_ITEM)) {
167167
return FormValidation.ok();
168168
}
@@ -177,7 +177,7 @@ public FormValidation doCheckUrl(@AncestorInPath Item item,
177177

178178
// get git executable on master
179179
EnvVars environment;
180-
final Jenkins jenkins = Jenkins.getActiveInstance();
180+
final Jenkins jenkins = Jenkins.get();
181181
if (item instanceof Job) {
182182
environment = ((Job) item).getEnvironment(jenkins, TaskListener.NULL);
183183
} else {

src/main/java/hudson/plugins/git/browser/AssemblaWeb.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import hudson.scm.RepositoryBrowser;
99
import hudson.util.FormValidation;
1010
import hudson.util.FormValidation.URLCheck;
11-
import hudson.scm.browsers.QueryBuilder;
1211
import jenkins.model.Jenkins;
1312
import net.sf.json.JSONObject;
1413
import org.kohsuke.stapler.DataBoundConstructor;
@@ -19,10 +18,7 @@
1918
import javax.annotation.Nonnull;
2019
import javax.servlet.ServletException;
2120
import java.io.IOException;
22-
import java.io.UnsupportedEncodingException;
23-
import java.net.MalformedURLException;
2421
import java.net.URL;
25-
import java.net.URLEncoder;
2622

2723
/**
2824
* AssemblaWeb Git Browser URLs
@@ -105,7 +101,7 @@ public FormValidation doCheckUrl(@QueryParameter(fixEmpty = true) final String u
105101
return FormValidation.ok();
106102
}
107103
// Connect to URL and check content only if we have admin permission
108-
Jenkins jenkins = Jenkins.getInstance();
104+
Jenkins jenkins = Jenkins.getInstanceOrNull();
109105
if (jenkins == null || !jenkins.hasPermission(Jenkins.ADMINISTER))
110106
return FormValidation.ok();
111107
return new URLCheck() {

0 commit comments

Comments
 (0)