Skip to content

Commit

Permalink
commiting to some of the code issues I got
Browse files Browse the repository at this point in the history
  • Loading branch information
shivajee98 committed Jan 13, 2024
1 parent 10985b7 commit 6c6288f
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
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 @@ -187,9 +189,11 @@ public GitLabPushTrigger() {}
@Initializer(after = InitMilestone.JOB_LOADED)
public static void migrateJobs() throws IOException {
GitLabPushTrigger.DescriptorImpl oldConfig = Trigger.all().get(GitLabPushTrigger.DescriptorImpl.class);
assert oldConfig != null;
if (!oldConfig.jobsMigrated) {
GitLabConnectionConfig gitLabConfig =
(GitLabConnectionConfig) Jenkins.getInstance().getDescriptor(GitLabConnectionConfig.class);
(GitLabConnectionConfig) Objects.requireNonNull(Jenkins.getInstance()).getDescriptor(GitLabConnectionConfig.class);
assert gitLabConfig != null;
gitLabConfig
.getConnections()
.add(new GitLabConnection(
Expand Down Expand Up @@ -217,7 +221,7 @@ public static void migrateJobs() throws IOException {
oldConfig.save();
}
if (!oldConfig.jobsMigrated2) {
for (AbstractProject<?, ?> project : Jenkins.getInstance().getAllItems(AbstractProject.class)) {
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 @@ -655,7 +659,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 @@ -30,6 +30,8 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.interceptor.RequirePOST;

import java.util.Objects;

/**
* @author Robin Müller
*/
Expand Down Expand Up @@ -86,9 +88,7 @@ public static GitLabClient getClient(@NonNull Run<?, ?> build) {
Job<?, ?> job = build.getParent();
if (job != null) {
final GitLabConnectionProperty connectionProperty = job.getProperty(GitLabConnectionProperty.class);
if (connectionProperty != null) {
return connectionProperty.getClient();
}
if (connectionProperty != null) return connectionProperty.getClient();
}
return null;
}
Expand All @@ -109,13 +109,15 @@ public boolean isApplicable(Class<? extends Job> jobType) {

@Override
public JobProperty<?> newInstance(StaplerRequest req, JSONObject formData) throws FormException {
assert req != null;
return req.bindJSON(GitLabConnectionProperty.class, formData);
}

public ListBoxModel doFillGitLabConnectionItems() {
ListBoxModel options = new ListBoxModel();
GitLabConnectionConfig descriptor =
(GitLabConnectionConfig) Jenkins.getInstance().getDescriptor(GitLabConnectionConfig.class);
(GitLabConnectionConfig) Objects.requireNonNull(Jenkins.getInstance()).getDescriptor(GitLabConnectionConfig.class);
assert descriptor != null;
for (GitLabConnection connection : descriptor.getConnections()) {
options.add(connection.getName(), connection.getName());
}
Expand Down Expand Up @@ -154,7 +156,8 @@ public FormValidation doTestConnection(
try {
GitLabConnection gitLabConnectionTested = null;
GitLabConnectionConfig descriptor =
(GitLabConnectionConfig) Jenkins.getInstance().getDescriptor(GitLabConnectionConfig.class);
(GitLabConnectionConfig) Objects.requireNonNull(Jenkins.getInstance()).getDescriptor(GitLabConnectionConfig.class);
assert descriptor != null;
for (GitLabConnection connection : descriptor.getConnections()) {
if (gitLabConnection.equals(connection.getName())) {
gitLabConnectionTested = connection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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 @@ -26,7 +28,7 @@ public static GitLabClientBuilder getGitLabClientBuilderById(String id) {

public static List<GitLabClientBuilder> getAllGitLabClientBuilders() {
List<GitLabClientBuilder> builders =
new ArrayList<>(Jenkins.getInstance().getExtensionList(GitLabClientBuilder.class));
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 @@ -115,7 +115,7 @@ private boolean containsNoBranches(@QueryParameter String value) {
private String[] getProjectBranchesAsArray(Job<?, ?> job) {
try {
List<String> branches = getProjectBranches(job);
return branches.toArray(new String[branches.size()]);
return branches.toArray(new String[0]);
} catch (GitLabProjectBranchesService.BranchLoadingException e) {
LOGGER.log(
Level.FINEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected Action[] createActions(Job<?, ?> job, H hook) {
"unknown handled situation, dont know what revision to build for req {0} for job {1}",
new Object[] {hook, (job != null ? job.getFullName() : null)});
}
return actions.toArray(new Action[actions.size()]);
return actions.toArray(new Action[0]);
}

protected void cancelPendingBuildsIfNecessary(Job<?, ?> job, H hook) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
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 +26,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 @@ -132,7 +132,7 @@ protected CauseData retrieveCauseData(PipelineHook hook) {
? ""
: hook.getRepository().getGitSshUrl())
.withSourceRepoHttpUrl(
hook.getRepository() == null || hook.getRepository() == null
hook.getRepository() == null
? ""
: hook.getRepository().getGitHttpUrl())
.withMergeRequestTitle("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private void handleMergeRequest(
List<Action> actions = Arrays.<Action>asList(
new CauseAction(new GitLabWebHookCause(retrieveCauseData(hook, project, mergeRequest, branch))),
new RevisionParameterAction(commit, retrieveUrIish(hook)));
scheduleBuild(job, actions.toArray(new Action[actions.size()]));
scheduleBuild(job, actions.toArray(new Action[0]));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private boolean containsNoLabel(@QueryParameter String value) {
private String[] getProjectLabelsAsArray(Job<?, ?> job) {
try {
List<String> labels = getProjectLabels(job);
return labels.toArray(new String[labels.size()]);
return labels.toArray(new String[0]);
} catch (GitLabProjectLabelsService.LabelLoadingException e) {
LOGGER.log(
Level.FINEST,
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,7 @@ 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,8 @@
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 +23,7 @@ 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 @@ -132,6 +132,7 @@ public void onFailure(StepContext context, Throwable t) {

CommitStatusUpdater.updateCommitStatus(
run, getTaskListener(context), state, name, step.builds, step.connection);
assert t != null;
context.onFailure(t);
}
})
Expand Down

0 comments on commit 6c6288f

Please sign in to comment.