Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #15 from blackduck-inc/SIGINT-2316
Browse files Browse the repository at this point in the history
Added logic to identify 'Job Type' correctly for all scenarios
  • Loading branch information
jahid1209 authored Oct 1, 2024
2 parents ec13e12 + 4a1c7c0 commit 16f79e6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
22 changes: 17 additions & 5 deletions src/main/java/io/jenkins/plugins/security/scan/global/Utility.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.jenkins.plugins.security.scan.global;

import com.cloudbees.hudson.plugins.folder.Folder;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.Result;
Expand Down Expand Up @@ -202,13 +203,24 @@ public static String jenkinsJobType(EnvVars envVars) {
Jenkins jenkins = Jenkins.getInstanceOrNull();

String jobName = envVars.get(ApplicationConstants.ENV_JOB_NAME_KEY);
String finalJobName =
jobName != null ? jobName.contains("/") ? jobName.substring(0, jobName.indexOf('/')) : jobName : null;

TopLevelItem job = jenkins != null ? jenkins.getItem(finalJobName) : null;
// Extract the part before the last '/' for potential multibranch projects
String jobNameForMultibranchProject = jobName != null
? jobName.contains("/") ? jobName.substring(0, jobName.lastIndexOf('/')) : jobName
: null;

if (job != null) {
return job.getClass().getSimpleName();
// If item is not a 'Folder', then it is a Multibranch pipeline job
TopLevelItem item =
jenkins != null ? jenkins.getItemByFullName(jobNameForMultibranchProject, TopLevelItem.class) : null;

// If 'item' is an instanceof 'Folder', it is either 'WorkflowJob' or 'FreestyleJob'
// Then try to get the item type with actual 'jobName'
if (item instanceof Folder) {
item = jenkins != null ? jenkins.getItemByFullName(jobName, TopLevelItem.class) : null;
}

if (item != null) {
return item.getClass().getSimpleName();
} else {
return "UnknownJobType";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ public Object fetchSCMRepositoryDetails(

public SCMSource findSCMSource() {
String jobName = envVars.get(ApplicationConstants.ENV_JOB_NAME_KEY);
if (jobName == null || !jobName.contains("/")) {
return null;
}
jobName = jobName.substring(0, jobName.indexOf("/"));
jobName = jobName.contains("/") ? jobName.substring(0, jobName.lastIndexOf('/')) : jobName;
logger.info("Jenkins Job name: " + jobName);

Jenkins jenkins = Jenkins.getInstanceOrNull();
Expand Down

0 comments on commit 16f79e6

Please sign in to comment.