From 770fb5c15adc9c775ba8d2de32f298208409af89 Mon Sep 17 00:00:00 2001 From: Sadman Anik Date: Tue, 14 Nov 2023 15:56:38 +0600 Subject: [PATCH 1/3] default coverity_stream_name, polaris_application_name & polaris_project_name parameter --- .../scan/global/ApplicationConstants.java | 2 ++ .../scan/service/ScannerArgumentService.java | 28 +++++++++++++++++-- .../polaris/PolarisParametersService.java | 1 - .../pipeline/SecurityScanStep/config.jelly | 4 +-- .../polaris/PolarisParametersServiceTest.java | 1 - 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/synopsys/security/scan/global/ApplicationConstants.java b/src/main/java/io/jenkins/plugins/synopsys/security/scan/global/ApplicationConstants.java index 357914eb..dc6d6f1a 100644 --- a/src/main/java/io/jenkins/plugins/synopsys/security/scan/global/ApplicationConstants.java +++ b/src/main/java/io/jenkins/plugins/synopsys/security/scan/global/ApplicationConstants.java @@ -26,6 +26,8 @@ public class ApplicationConstants { public static final String ENV_JOB_NAME_KEY = "JOB_NAME"; public static final String ENV_CHANGE_ID_KEY = "CHANGE_ID"; public static final String ENV_BRANCH_NAME_KEY = "BRANCH_NAME"; + public static final String ENV_CHANGE_TARGET_KEY = "CHANGE_TARGET"; + public static final String ENV_GIT_URL_KEY = "GIT_URL"; public static final String PRODUCT_KEY = "product"; public static final String BLACKDUCK_URL_KEY = "blackduck_url"; diff --git a/src/main/java/io/jenkins/plugins/synopsys/security/scan/service/ScannerArgumentService.java b/src/main/java/io/jenkins/plugins/synopsys/security/scan/service/ScannerArgumentService.java index cc93ac9d..8a6e5902 100644 --- a/src/main/java/io/jenkins/plugins/synopsys/security/scan/service/ScannerArgumentService.java +++ b/src/main/java/io/jenkins/plugins/synopsys/security/scan/service/ScannerArgumentService.java @@ -181,20 +181,44 @@ private void setScanObject(BridgeInput bridgeInput, Object scanObject, Object sc setCoverityProjectNameAndStreamName(coverity, scmObject); bridgeInput.setCoverity(coverity); } else if (scanObject instanceof Polaris) { + Polaris polaris = (Polaris) scanObject; + setPolarisProjectNameAndApplicationName(polaris ,scmObject); bridgeInput.setPolaris((Polaris) scanObject); } } private void setCoverityProjectNameAndStreamName(Coverity coverity, Object scmObject) { - String repositoryName = getRepositoryName(scmObject); + String gitURL = envVars.get(ApplicationConstants.ENV_GIT_URL_KEY); + String repositoryName = gitURL.replaceFirst("^.*\\/(.+?)\\.git$", "$1"); String branchName = envVars.get(ApplicationConstants.ENV_BRANCH_NAME_KEY); + boolean isEventPullRequest = envVars.get(ApplicationConstants.ENV_CHANGE_ID_KEY) != null; if (Utility.isStringNullOrBlank(coverity.getConnect().getProject().getName())) { coverity.getConnect().getProject().setName(repositoryName); } if (Utility.isStringNullOrBlank(coverity.getConnect().getStream().getName())) { - coverity.getConnect().getStream().setName(repositoryName.concat("-").concat(branchName)); + if(isEventPullRequest){ + String changeTarget = envVars.get(ApplicationConstants.ENV_CHANGE_TARGET_KEY); + coverity.getConnect().getStream().setName(repositoryName.concat("-").concat(changeTarget)); + }else{ + coverity.getConnect().getStream().setName(repositoryName.concat("-").concat(branchName)); + } + } + } + + private void setPolarisProjectNameAndApplicationName(Polaris polaris, Object scmObject) { + String gitURL = envVars.get(ApplicationConstants.ENV_GIT_URL_KEY); + String repositoryName = gitURL.replaceFirst("^.*\\/(.+?)\\.git$", "$1"); + String ownerName = gitURL.replaceFirst("^.*/([^/]+)/[^/]+\\.git$", "$1"); + + if (Utility.isStringNullOrBlank(polaris.getApplicationName().getName())) { + polaris.getApplicationName().setName(ownerName); + } + + if (Utility.isStringNullOrBlank(polaris.getProjectName().getName())) { + polaris.getProjectName().setName(repositoryName); } + } private String getRepositoryName(Object scmObject) { diff --git a/src/main/java/io/jenkins/plugins/synopsys/security/scan/service/scan/polaris/PolarisParametersService.java b/src/main/java/io/jenkins/plugins/synopsys/security/scan/service/scan/polaris/PolarisParametersService.java index 72508c6a..387cb966 100644 --- a/src/main/java/io/jenkins/plugins/synopsys/security/scan/service/scan/polaris/PolarisParametersService.java +++ b/src/main/java/io/jenkins/plugins/synopsys/security/scan/service/scan/polaris/PolarisParametersService.java @@ -27,7 +27,6 @@ public boolean isValidPolarisParameters(Map polarisParameters) { Arrays.asList( ApplicationConstants.POLARIS_SERVER_URL_KEY, ApplicationConstants.POLARIS_ACCESS_TOKEN_KEY, - ApplicationConstants.POLARIS_APPLICATION_NAME_KEY, ApplicationConstants.POLARIS_ASSESSMENT_TYPES_KEY) .forEach(key -> { boolean isKeyValid = polarisParameters.containsKey(key) diff --git a/src/main/resources/io/jenkins/plugins/synopsys/security/scan/extension/pipeline/SecurityScanStep/config.jelly b/src/main/resources/io/jenkins/plugins/synopsys/security/scan/extension/pipeline/SecurityScanStep/config.jelly index 81c178ff..9668b58f 100644 --- a/src/main/resources/io/jenkins/plugins/synopsys/security/scan/extension/pipeline/SecurityScanStep/config.jelly +++ b/src/main/resources/io/jenkins/plugins/synopsys/security/scan/extension/pipeline/SecurityScanStep/config.jelly @@ -52,10 +52,10 @@