diff --git a/README.md b/README.md index f2d4986f..435d729a 100644 --- a/README.md +++ b/README.md @@ -310,8 +310,8 @@ Or, if these values are set both from Jenkins Global Configuration and pipeline |----------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------| | `polaris_server_url` | URL for Polaris server. The URL can also be configured in Jenkins **Global Configuration** or can be passed as **Environment Variable**.
Example: `polaris_server_url: "${env.BRIDGE_POLARIS_SERVERURL}"`
| Mandatory if not configured in Jenkins Global Configuration | | `polaris_access_token` | Access token for Polaris server. The URL can also be configured in Jenkins **Global Configuration** or can be passed as **Environment Variable**.
Example: `polaris_access_token: "${env.BRIDGE_POLARIS_ACCESSTOKEN}"`
| Mandatory if not configured in Jenkins Global Configuration | -| `polaris_application_name` | The application name created in the Polaris server. | Mandatory | -| `polaris_project_name` | The project name you have created in Polaris. | Mandatory | +| `polaris_application_name` | The application name created in the Polaris server. | Optional | +| `polaris_project_name` | The project name you have created in Polaris. | Optional | | `polaris_assessment_types` | Specifies the type of scan you want to run.
Supported values: `SCA` or `SAST` or both SCA and SAST.
Example: `polaris_assessment_types: "SCA, SAST"`
| Mandatory | | `polaris_triage` | Accepts only one value.
Supported values: `REQUIRED` or `NOT_REQUIRED` or `NOT_ENTITLED`.
| Optional | | `polaris_branch_name` | Branch name in the Polaris Server | Optional | 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..c3629101 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,19 +181,46 @@ 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); } } 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 @@