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

Commit 16f79e6

Browse files
authored
Merge pull request #15 from blackduck-inc/SIGINT-2316
Added logic to identify 'Job Type' correctly for all scenarios
2 parents ec13e12 + 4a1c7c0 commit 16f79e6

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/main/java/io/jenkins/plugins/security/scan/global/Utility.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.jenkins.plugins.security.scan.global;
22

3+
import com.cloudbees.hudson.plugins.folder.Folder;
34
import hudson.EnvVars;
45
import hudson.FilePath;
56
import hudson.model.Result;
@@ -202,13 +203,24 @@ public static String jenkinsJobType(EnvVars envVars) {
202203
Jenkins jenkins = Jenkins.getInstanceOrNull();
203204

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

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

210-
if (job != null) {
211-
return job.getClass().getSimpleName();
212+
// If item is not a 'Folder', then it is a Multibranch pipeline job
213+
TopLevelItem item =
214+
jenkins != null ? jenkins.getItemByFullName(jobNameForMultibranchProject, TopLevelItem.class) : null;
215+
216+
// If 'item' is an instanceof 'Folder', it is either 'WorkflowJob' or 'FreestyleJob'
217+
// Then try to get the item type with actual 'jobName'
218+
if (item instanceof Folder) {
219+
item = jenkins != null ? jenkins.getItemByFullName(jobName, TopLevelItem.class) : null;
220+
}
221+
222+
if (item != null) {
223+
return item.getClass().getSimpleName();
212224
} else {
213225
return "UnknownJobType";
214226
}

src/main/java/io/jenkins/plugins/security/scan/service/scm/SCMRepositoryService.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ public Object fetchSCMRepositoryDetails(
7979

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

8885
Jenkins jenkins = Jenkins.getInstanceOrNull();

0 commit comments

Comments
 (0)