Skip to content

Commit

Permalink
Refactoring and simplifying the codebase (#1621)
Browse files Browse the repository at this point in the history
* Few Refactors to the codebase

* Made Requested Changes

---------

Co-authored-by: Kris Stern <[email protected]>
  • Loading branch information
shivajee98 and krisstern authored Jan 16, 2024
1 parent 188d5da commit 56e4509
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.io.ObjectStreamException;
import java.security.SecureRandom;
import java.util.Collection;
import java.util.Objects;
import jenkins.model.Jenkins;
import jenkins.model.ParameterizedJobMixIn;
import jenkins.triggers.SCMTriggerItem.SCMTriggerItems;
Expand Down Expand Up @@ -191,10 +192,10 @@ public GitLabPushTrigger() {}
@Initializer(after = InitMilestone.JOB_LOADED)
public static void migrateJobs() throws IOException {
GitLabPushTrigger.DescriptorImpl oldConfig = Trigger.all().get(GitLabPushTrigger.DescriptorImpl.class);
if (!oldConfig.jobsMigrated) {
GitLabConnectionConfig gitLabConfig =
(GitLabConnectionConfig) Jenkins.getInstance().getDescriptor(GitLabConnectionConfig.class);
gitLabConfig
if (oldConfig != null && !oldConfig.jobsMigrated) {
GitLabConnectionConfig gitLabConfig = (GitLabConnectionConfig)
Objects.requireNonNull(Jenkins.getInstance()).getDescriptor(GitLabConnectionConfig.class);
Objects.requireNonNull(gitLabConfig)
.getConnections()
.add(new GitLabConnection(
oldConfig.gitlabHostUrl,
Expand All @@ -220,8 +221,9 @@ public static void migrateJobs() throws IOException {
oldConfig.jobsMigrated = true;
oldConfig.save();
}
if (!oldConfig.jobsMigrated2) {
for (AbstractProject<?, ?> project : Jenkins.getInstance().getAllItems(AbstractProject.class)) {
if (oldConfig != null && !oldConfig.jobsMigrated2) {
for (AbstractProject<?, ?> project :
Objects.requireNonNull(Jenkins.getInstance()).getAllItems(AbstractProject.class)) {
GitLabPushTrigger trigger = project.getTrigger(GitLabPushTrigger.class);
if (trigger != null) {
if (trigger.addNoteOnMergeRequest) {
Expand Down Expand Up @@ -662,7 +664,7 @@ public String getDisplayName() {

private StringBuilder retrieveProjectUrl(Job<?, ?> project) {
return new StringBuilder()
.append(Jenkins.getInstance().getRootUrl())
.append(Objects.requireNonNull(Jenkins.getInstance()).getRootUrl())
.append(GitLabWebHook.WEBHOOK_URL)
.append(retrieveParentUrl(project))
.append('/')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
Expand All @@ -25,8 +26,8 @@ public static GitLabClientBuilder getGitLabClientBuilderById(String id) {
}

public static List<GitLabClientBuilder> getAllGitLabClientBuilders() {
List<GitLabClientBuilder> builders =
new ArrayList<>(Jenkins.getInstance().getExtensionList(GitLabClientBuilder.class));
List<GitLabClientBuilder> builders = new ArrayList<>(
Objects.requireNonNull(Jenkins.getInstance()).getExtensionList(GitLabClientBuilder.class));
sort(builders);
return builders;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.ProcessingException;
Expand Down Expand Up @@ -245,11 +246,11 @@ private String getNote(Run<?, ?> build, TaskListener listener) {
message = replaceMacros(build, listener, this.getFailureNoteText());
} else {
String icon = getResultIcon(build.getResult());
String buildUrl = Jenkins.getInstance().getRootUrl() + build.getUrl();
String buildUrl = Objects.requireNonNull(Jenkins.getInstance()).getRootUrl() + build.getUrl();
message = MessageFormat.format(
"{0} Jenkins Build {1}\n\nResults available at: [Jenkins [{2} #{3}]]({4})",
icon,
build.getResult().toString(),
Objects.requireNonNull(build.getResult()).toString(),
build.getParent().getDisplayName(),
build.getNumber(),
buildUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import hudson.model.Cause;
import hudson.model.Job;
import hudson.model.Queue;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
Expand All @@ -24,7 +25,7 @@ public class PendingBuildsHandler {
private static final Logger LOGGER = Logger.getLogger(PendingBuildsHandler.class.getName());

public void cancelPendingBuilds(Job<?, ?> job, Integer projectId, String branch) {
Queue queue = Jenkins.getInstance().getQueue();
Queue queue = Objects.requireNonNull(Jenkins.getInstance()).getQueue();
for (Queue.Item item : queue.getItems()) {
if (!job.getName().equals(item.task.getName())) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import org.acegisecurity.Authentication;
Expand Down Expand Up @@ -88,7 +89,8 @@ public void run() {
}

private void checkPermission(Permission permission, Item project) {
if (((GitLabConnectionConfig) Jenkins.get().getDescriptor(GitLabConnectionConfig.class))
if (((GitLabConnectionConfig)
Objects.requireNonNull(Jenkins.get().getDescriptor(GitLabConnectionConfig.class)))
.isUseAuthenticatedEndpoint()) {
if (!project.getACL().hasPermission(authentication, permission)) {
String message = String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hudson.model.Run;
import hudson.util.HttpResponses;
import java.io.IOException;
import java.util.Objects;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.StaplerResponse;

Expand All @@ -21,7 +22,8 @@ protected BuildPageRedirectAction(Run<?, ?> build) {
public void execute(StaplerResponse response) {
if (build != null) {
try {
response.sendRedirect2(Jenkins.getInstance().getRootUrl() + build.getUrl());
response.sendRedirect2(
Objects.requireNonNull(Jenkins.getInstance()).getRootUrl() + build.getUrl());
} catch (IOException e) {
try {
response.sendRedirect2(Jenkins.getInstance().getRootUrl() + build.getBuildStatusUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
import hudson.Extension;
import hudson.model.Run;
import hudson.model.TaskListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.steps.BodyExecution;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
Expand Down Expand Up @@ -132,7 +128,7 @@ public void onFailure(StepContext context, Throwable t) {

CommitStatusUpdater.updateCommitStatus(
run, getTaskListener(context), state, name, step.builds, step.connection);
context.onFailure(t);
context.onFailure(Objects.requireNonNull(t));
}
})
.start();
Expand Down

0 comments on commit 56e4509

Please sign in to comment.