Skip to content
This repository has been archived by the owner on Nov 30, 2017. It is now read-only.

Commit

Permalink
Updating the gradle plugin to create a report when the cehck policies is
Browse files Browse the repository at this point in the history
run
  • Loading branch information
jrichard committed Nov 23, 2016
1 parent c57027e commit 0a93267
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies {
compile gradleApi()
compile localGroovy()
compile 'com.blackducksoftware.integration:build-tool-common:2.2.0'
compile 'com.blackducksoftware.integration:hub-common:1.5.2'
compile 'com.blackducksoftware.integration:hub-common:3.0.0-SNAPSHOT'
compile 'commons-io:commons-io:2.5'
compile 'com.google.code.gson:gson:2.7'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.gradle.api.Project;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -33,9 +35,18 @@
import com.blackducksoftware.integration.build.DependencyNode;
import com.blackducksoftware.integration.build.utils.BdioDependencyWriter;
import com.blackducksoftware.integration.build.utils.FlatDependencyListWriter;
import com.blackducksoftware.integration.hub.api.HubServicesFactory;
import com.blackducksoftware.integration.hub.api.bom.BomImportRestService;
import com.blackducksoftware.integration.hub.api.policy.PolicyStatusItem;
import com.blackducksoftware.integration.hub.dataservices.DataServicesFactory;
import com.blackducksoftware.integration.hub.api.project.ProjectItem;
import com.blackducksoftware.integration.hub.api.project.ProjectRestService;
import com.blackducksoftware.integration.hub.api.project.version.ProjectVersionItem;
import com.blackducksoftware.integration.hub.api.project.version.ProjectVersionRestService;
import com.blackducksoftware.integration.hub.api.report.HubRiskReportData;
import com.blackducksoftware.integration.hub.api.report.ReportCategoriesEnum;
import com.blackducksoftware.integration.hub.api.report.ReportFormatEnum;
import com.blackducksoftware.integration.hub.api.report.ReportRestService;
import com.blackducksoftware.integration.hub.api.report.RiskReportResourceCopier;
import com.blackducksoftware.integration.hub.dataservices.policystatus.PolicyStatusDataService;
import com.blackducksoftware.integration.hub.dataservices.scan.ScanStatusDataService;
import com.blackducksoftware.integration.hub.exception.BDRestException;
Expand All @@ -44,7 +55,6 @@
import com.blackducksoftware.integration.hub.exception.ProjectDoesNotExistException;
import com.blackducksoftware.integration.hub.exception.ResourceDoesNotExistException;
import com.blackducksoftware.integration.hub.exception.UnexpectedHubResponseException;
import com.blackducksoftware.integration.hub.rest.RestConnection;
import com.blackducksoftware.integration.log.Slf4jIntLogger;

public class PluginHelper {
Expand All @@ -68,22 +78,19 @@ public void createHubOutput(Project project, final String hubProjectName, final
bdioDependencyWriter.write(outputDirectory, hubProjectName, rootNode);
}

public void deployHubOutput(final Slf4jIntLogger logger, final RestConnection restConnection,
public void deployHubOutput(final Slf4jIntLogger logger, final HubServicesFactory services,
final File outputDirectory, final String hubProjectName) throws IOException, ResourceDoesNotExistException, URISyntaxException, BDRestException {
final DataServicesFactory dataServicesFactory = new DataServicesFactory(restConnection);
final BomImportRestService bomImportRestService = dataServicesFactory.getBomImportRestService();

String filename = BdioDependencyWriter.getFilename(hubProjectName);
final File file = new File(outputDirectory, filename);
BomImportRestService bomImportRestService = services.createBomImportRestService();
bomImportRestService.importBomFile(file, Constants.BDIO_FILE_MEDIA_TYPE);

logger.info(String.format(Constants.UPLOAD_FILE_MESSAGE, file, restConnection.getBaseUrl()));
logger.info(String.format(Constants.UPLOAD_FILE_MESSAGE, file, bomImportRestService.getRestConnection().getBaseUrl()));
}

public void waitForHub(final RestConnection restConnection, final String hubProjectName,
public void waitForHub(final HubServicesFactory services, final String hubProjectName,
final String hubProjectVersion, final long scanStartedTimeout, final long scanFinishedTimeout) {
final DataServicesFactory dataServicesFactory = new DataServicesFactory(restConnection);
final ScanStatusDataService scanStatusDataService = dataServicesFactory.createScanStatusDataService();
ScanStatusDataService scanStatusDataService = services.createScanStatusDataService();
try {
scanStatusDataService.assertBomImportScanStartedThenFinished(hubProjectName, hubProjectVersion,
scanStartedTimeout * 1000, scanFinishedTimeout * 1000, new Slf4jIntLogger(logger));
Expand All @@ -93,12 +100,36 @@ public void waitForHub(final RestConnection restConnection, final String hubProj
}
}

public PolicyStatusItem checkPolicies(final RestConnection restConnection, final String hubProjectName,
public void createRiskReport(final Slf4jIntLogger logger, final HubServicesFactory services,
final File outputDirectory, String projectName, String projectVersionName)
throws IOException, BDRestException, URISyntaxException, HubIntegrationException, InterruptedException, UnexpectedHubResponseException,
ProjectDoesNotExistException {
ProjectRestService projectRestService = services.createProjectRestService();
ProjectVersionRestService projectVersionRestService = services.createProjectVersionRestService();
ReportRestService reportRestService = services.createReportRestService(logger);
ProjectItem project = projectRestService.getProjectByName(projectName);
ProjectVersionItem version = projectVersionRestService.getProjectVersion(project, projectVersionName);
final ReportCategoriesEnum[] categories = { ReportCategoriesEnum.VERSION, ReportCategoriesEnum.COMPONENTS };
HubRiskReportData riskreportData = reportRestService.generateHubReport(version, ReportFormatEnum.JSON, categories);
RiskReportResourceCopier copier = new RiskReportResourceCopier(outputDirectory.getCanonicalPath());
List<File> writtenFiles = copier.copy();
File htmlFile = null;
for (File file : writtenFiles) {
if (file.getName().equals("riskreport.html")) {
htmlFile = file;
break;
}
}
String htmlFileString = FileUtils.readFileToString(htmlFile, "UTF-8");
String reportString = services.getRestConnection().getGson().toJson(riskreportData);
htmlFileString = htmlFileString.replace("TOKEN_RISK_REPORT_JSON_TOKEN", reportString);
FileUtils.writeStringToFile(htmlFile, htmlFileString, "UTF-8");
}

public PolicyStatusItem checkPolicies(final HubServicesFactory services, final String hubProjectName,
final String hubProjectVersion) throws IOException, URISyntaxException, BDRestException, ProjectDoesNotExistException, HubIntegrationException,
MissingUUIDException, UnexpectedHubResponseException {
final DataServicesFactory dataServicesFactory = new DataServicesFactory(restConnection);
final PolicyStatusDataService policyStatusDataService = dataServicesFactory.createPolicyStatusDataService();

PolicyStatusDataService policyStatusDataService = services.createPolicyStatusDataService();
final PolicyStatusItem policyStatusItem = policyStatusDataService
.getPolicyStatusForProjectAndVersion(hubProjectName, hubProjectVersion);
return policyStatusItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.gradle.api.GradleException;

import com.blackducksoftware.integration.exception.EncryptionException;
import com.blackducksoftware.integration.hub.api.HubServicesFactory;
import com.blackducksoftware.integration.hub.api.policy.PolicyStatusItem;
import com.blackducksoftware.integration.hub.exception.BDRestException;
import com.blackducksoftware.integration.hub.exception.HubIntegrationException;
Expand All @@ -49,7 +50,8 @@ public void performTask() {
final HubServerConfig hubServerConfig = getHubServerConfigBuilder().build();
try {
final RestConnection restConnection = new CredentialsRestConnection(hubServerConfig);
final PolicyStatusItem policyStatusItem = PLUGIN_HELPER.checkPolicies(restConnection, getHubProjectName(),
HubServicesFactory services = new HubServicesFactory(restConnection);
final PolicyStatusItem policyStatusItem = PLUGIN_HELPER.checkPolicies(services, getHubProjectName(),
getHubVersionName());
handlePolicyStatusItem(policyStatusItem);
} catch (IllegalArgumentException | URISyntaxException | BDRestException | EncryptionException | IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
import static com.blackducksoftware.integration.build.Constants.DEPLOY_HUB_OUTPUT_AND_CHECK_POLICIES_STARTING;
import static com.blackducksoftware.integration.build.Constants.DEPLOY_HUB_OUTPUT_ERROR;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

import org.gradle.api.GradleException;

import com.blackducksoftware.integration.exception.EncryptionException;
import com.blackducksoftware.integration.hub.api.HubServicesFactory;
import com.blackducksoftware.integration.hub.api.policy.PolicyStatusItem;
import com.blackducksoftware.integration.hub.exception.BDRestException;
import com.blackducksoftware.integration.hub.exception.HubIntegrationException;
Expand Down Expand Up @@ -62,19 +64,30 @@ public void performTask() {

final HubServerConfig hubServerConfig = getHubServerConfigBuilder().build();
final RestConnection restConnection;
Slf4jIntLogger intLogger = new Slf4jIntLogger(logger);
HubServicesFactory services;
try {
restConnection = new CredentialsRestConnection(hubServerConfig);
PLUGIN_HELPER.deployHubOutput(new Slf4jIntLogger(logger), restConnection, getOutputDirectory(),
services = new HubServicesFactory(restConnection);
PLUGIN_HELPER.deployHubOutput(intLogger, services, getOutputDirectory(),
getHubProjectName());
} catch (IllegalArgumentException | URISyntaxException | BDRestException | EncryptionException | IOException
| ResourceDoesNotExistException e) {
throw new GradleException(String.format(DEPLOY_HUB_OUTPUT_ERROR, e.getMessage()), e);
}

try {
PLUGIN_HELPER.waitForHub(restConnection, getHubProjectName(), getHubVersionName(), getHubScanStartedTimeout(),
PLUGIN_HELPER.waitForHub(services, getHubProjectName(), getHubVersionName(), getHubScanStartedTimeout(),
getHubScanFinishedTimeout());
final PolicyStatusItem policyStatusItem = PLUGIN_HELPER.checkPolicies(restConnection, getHubProjectName(),
File reportOutput = new File(getOutputDirectory(), "report");
try {
PLUGIN_HELPER.createRiskReport(intLogger, services, reportOutput, getHubProjectName(), getHubVersionName());
} catch (IllegalArgumentException | URISyntaxException | BDRestException | IOException
| ProjectDoesNotExistException | HubIntegrationException | InterruptedException | UnexpectedHubResponseException e) {
throw new GradleException(String.format("Could not create Hub Report, check the logs for specific issues: %s", e.getMessage()), e);
}

final PolicyStatusItem policyStatusItem = PLUGIN_HELPER.checkPolicies(services, getHubProjectName(),
getHubVersionName());
handlePolicyStatusItem(policyStatusItem);
} catch (IllegalArgumentException | URISyntaxException | BDRestException | IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.gradle.api.GradleException;

import com.blackducksoftware.integration.exception.EncryptionException;
import com.blackducksoftware.integration.hub.api.HubServicesFactory;
import com.blackducksoftware.integration.hub.exception.BDRestException;
import com.blackducksoftware.integration.hub.exception.ResourceDoesNotExistException;
import com.blackducksoftware.integration.hub.global.HubServerConfig;
Expand All @@ -52,9 +53,11 @@ public void performTask() {

final HubServerConfig hubServerConfig = getHubServerConfigBuilder().build();
RestConnection restConnection;
HubServicesFactory services;
try {
restConnection = new CredentialsRestConnection(hubServerConfig);
PLUGIN_HELPER.deployHubOutput(new Slf4jIntLogger(logger), restConnection, getOutputDirectory(),
services = new HubServicesFactory(restConnection);
PLUGIN_HELPER.deployHubOutput(new Slf4jIntLogger(logger), services, getOutputDirectory(),
getHubProjectName());
} catch (IllegalArgumentException | URISyntaxException | BDRestException | EncryptionException | IOException
| ResourceDoesNotExistException e) {
Expand Down

0 comments on commit 0a93267

Please sign in to comment.