Skip to content

Commit

Permalink
Merge pull request #10 from ashujha301/refactor
Browse files Browse the repository at this point in the history
Update: Code Scanning alerts resolved
  • Loading branch information
ginilpg authored Jul 9, 2024
2 parents 1e67e60 + 43984c3 commit 92d8c9d
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/main/java/io/jenkins/plugins/AppknoxPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import hudson.util.ListBoxModel;

import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.verb.POST;

import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
Expand Down Expand Up @@ -106,7 +107,6 @@ private boolean executeAppknoxCommands(Run<?, ?> run, FilePath workspace, String

Map<String, String> env = new HashMap<>(System.getenv());
env.put("APPKNOX_ACCESS_TOKEN", accessToken);

String appknoxPath = downloadAndInstallAppknox(osName, listener);
String uploadOutput = uploadFile(appknoxPath, listener, env);
String fileID = extractFileID(uploadOutput, listener);
Expand Down Expand Up @@ -215,7 +215,8 @@ private void addPathToEnvironment(String path, TaskListener listener) {
System.setProperty("PATH", newPath);
}

private String uploadFile(String appknoxPath, TaskListener listener, Map<String, String> env) throws IOException, InterruptedException {
private String uploadFile(String appknoxPath, TaskListener listener, Map<String, String> env)
throws IOException, InterruptedException {
String accessToken = getAccessToken(listener);
if (accessToken == null) {
return null;
Expand All @@ -224,7 +225,7 @@ private String uploadFile(String appknoxPath, TaskListener listener, Map<String,
command.add(appknoxPath);
command.add("upload");
command.add(filePath);

ProcessBuilder pb = new ProcessBuilder(command);
pb.environment().putAll(env);
pb.redirectErrorStream(true);
Expand All @@ -237,7 +238,7 @@ private String uploadFile(String appknoxPath, TaskListener listener, Map<String,
while ((line = reader.readLine()) != null) {
lastLine = line;
}

if (lastLine != null) {
listener.getLogger().println("Upload Command Output :");
listener.getLogger().println("File ID = " + lastLine.trim());
Expand All @@ -250,8 +251,8 @@ private String uploadFile(String appknoxPath, TaskListener listener, Map<String,
process.waitFor();
}
}

private boolean runCICheck(String appknoxPath, Run<?, ?> run, String fileID, TaskListener listener, Map<String, String> env)
private boolean runCICheck(String appknoxPath, Run<?, ?> run, String fileID, TaskListener listener,
Map<String, String> env)
throws IOException, InterruptedException {
String accessToken = getAccessToken(listener);
if (accessToken == null) {
Expand Down Expand Up @@ -427,6 +428,7 @@ public String getDisplayName() {
}

@SuppressWarnings("deprecation")
@POST
public ListBoxModel doFillCredentialsIdItems(@AncestorInPath ItemGroup<?> context) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
return new StandardListBoxModel()
Expand All @@ -439,21 +441,27 @@ public ListBoxModel doFillCredentialsIdItems(@AncestorInPath ItemGroup<?> contex
CredentialsMatchers.instanceOf(StringCredentials.class));
}

@POST
public FormValidation doCheckCredentialsId(@QueryParameter String value) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
if (value.isEmpty()) {
return FormValidation.error("Appknox Access Token must be selected");
}
return FormValidation.ok();
}

@POST
public FormValidation doCheckFilePath(@QueryParameter String value) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
if (value.isEmpty()) {
return FormValidation.error("File Path must not be empty");
}
return FormValidation.ok();
}

@POST
public FormValidation doCheckRiskThreshold(@QueryParameter String value) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
if (value.isEmpty() || (!value.equals("LOW") && !value.equals("MEDIUM") && !value.equals("HIGH")
&& !value.equals("CRITICAL"))) {
return FormValidation.error("Risk Threshold must be one of: LOW, MEDIUM, HIGH, CRITICAL");
Expand Down

0 comments on commit 92d8c9d

Please sign in to comment.