Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default parameter for Coverity and Polaris #29

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**. <br> Example: `polaris_server_url: "${env.BRIDGE_POLARIS_SERVERURL}"` </br> | 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**. <br> Example: `polaris_access_token: "${env.BRIDGE_POLARIS_ACCESSTOKEN}"` </br> | 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. <br> Supported values: `SCA` or `SAST` or both SCA and SAST. <br> Example: `polaris_assessment_types: "SCA, SAST"` </br> | Mandatory |
| `polaris_triage` | Accepts only one value. <br> Supported values: `REQUIRED` or `NOT_REQUIRED` or `NOT_ENTITLED`.</br> | Optional |
| `polaris_branch_name` | Branch name in the Polaris Server | Optional |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,46 @@
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");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to check, should we extract repository name from git url ?

Can we use currentBuild.projectName from Global Variables Reference? As I can see, GitLab and ADO are using BUILD information in Integrations Default Values confluence page.

In the confluence page, GitHub, GitLab and ADO default values are specified, so we should decide first what values we are going to use for Jenkins.

cc: @jahid1209

String branchName = envVars.get(ApplicationConstants.ENV_BRANCH_NAME_KEY);
boolean isEventPullRequest = envVars.get(ApplicationConstants.ENV_CHANGE_ID_KEY) != null;

Check warning on line 194 in src/main/java/io/jenkins/plugins/synopsys/security/scan/service/ScannerArgumentService.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 194 is only partially covered, one branch is missing

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) {

Check warning on line 200 in src/main/java/io/jenkins/plugins/synopsys/security/scan/service/ScannerArgumentService.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 200 is only partially covered, one branch is missing
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));

Check warning on line 208 in src/main/java/io/jenkins/plugins/synopsys/security/scan/service/ScannerArgumentService.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 206-208 are not covered by tests
}
}
}

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())) {

Check warning on line 218 in src/main/java/io/jenkins/plugins/synopsys/security/scan/service/ScannerArgumentService.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 218 is only partially covered, one branch is missing
polaris.getApplicationName().setName(ownerName);

Check warning on line 219 in src/main/java/io/jenkins/plugins/synopsys/security/scan/service/ScannerArgumentService.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 219 is not covered by tests
}

if (Utility.isStringNullOrBlank(polaris.getProjectName().getName())) {

Check warning on line 222 in src/main/java/io/jenkins/plugins/synopsys/security/scan/service/ScannerArgumentService.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 222 is only partially covered, one branch is missing
polaris.getProjectName().setName(repositoryName);

Check warning on line 223 in src/main/java/io/jenkins/plugins/synopsys/security/scan/service/ScannerArgumentService.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 223 is not covered by tests
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public boolean isValidPolarisParameters(Map<String, Object> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@

<div id="polaris" style="display: none;">
<f:section title="Polaris Parameters">
<f:entry field="polaris_application_name" title="Polaris Application Name (Mandatory)">
<f:entry field="polaris_application_name" title="Polaris Application Name (Optional)">
<f:textbox/>
</f:entry>
<f:entry field="polaris_project_name" title="Polaris Project Name (Mandatory)">
<f:entry field="polaris_project_name" title="Polaris Project Name (Optional)">
<f:textbox/>
</f:entry>
<f:entry field="polaris_assessment_types" title="Polaris Assessment Types (Mandatory)">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ void setUp() {
Mockito.doReturn("fake-branch").when(envVarsMock).get(ApplicationConstants.ENV_BRANCH_NAME_KEY);
Mockito.doReturn("fake-job/branch").when(envVarsMock).get(ApplicationConstants.ENV_JOB_NAME_KEY);
Mockito.doReturn("0").when(envVarsMock).get(ApplicationConstants.ENV_CHANGE_ID_KEY);
Mockito.doReturn("https://github.com//scm/TEST_JENKINS/node-pipeline-test.git")
.when(envVarsMock)
.get(ApplicationConstants.ENV_GIT_URL_KEY);
Mockito.doReturn("new-fake-branch").when(envVarsMock).get(ApplicationConstants.ENV_CHANGE_TARGET_KEY);

scannerArgumentService = new ScannerArgumentService(listenerMock, envVarsMock, workspace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ void validScanParametersTest() {

polarisParameters.put(ApplicationConstants.POLARIS_SERVER_URL_KEY, TEST_POLARIS_SERVER_URL);
polarisParameters.put(ApplicationConstants.POLARIS_ACCESS_TOKEN_KEY, TEST_POLARIS_ACCESS_TOKEN);
polarisParameters.put(ApplicationConstants.POLARIS_APPLICATION_NAME_KEY, TEST_APPLICATION_NAME);
polarisParameters.put(ApplicationConstants.POLARIS_ASSESSMENT_TYPES_KEY, TEST_POLARIS_ASSESSMENT_TYPES);

assertTrue(polarisParametersService.isValidPolarisParameters(polarisParameters));
Expand Down
Loading