Skip to content

Always read project name from Build Scan data #263

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

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.gradle.maven.extension.api.GradleEnterpriseListener;
import com.gradle.maven.extension.api.scan.BuildScanApi;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.logging.Logger;

import javax.inject.Inject;
Expand All @@ -20,12 +19,10 @@
public class CaptureBuildScansListener implements GradleEnterpriseListener {
private static final String EXPERIMENT_DIR = System.getProperty("com.gradle.enterprise.build_validation.experimentDir");

private final RootProjectExtractor rootProjectExtractor;
private final Logger logger;

@Inject
public CaptureBuildScansListener(RootProjectExtractor rootProjectExtractor, Logger logger) {
this.rootProjectExtractor = rootProjectExtractor;
public CaptureBuildScansListener(Logger logger) {
this.logger = logger;
}

Expand All @@ -36,7 +33,7 @@ public void configure(GradleEnterpriseApi api, MavenSession session) throws Exce
BuildScanApi buildScan = api.getBuildScan();

addCustomDataOnBuildFinished(buildScan);
capturePublishedBuildScan(buildScan, rootProjectExtractor.extractRootProject(session));
capturePublishedBuildScan(buildScan);
}

private static void addCustomDataOnBuildFinished(BuildScanApi buildScan) {
Expand Down Expand Up @@ -71,7 +68,7 @@ private static String urlEncode(String str) {
}
}

private void capturePublishedBuildScan(BuildScanApi buildScan, MavenProject rootProject) {
private void capturePublishedBuildScan(BuildScanApi buildScan) {
buildScan.buildScanPublished(scan -> {
logger.debug("Saving build scan data to build-scans.csv");
String port = scan.getBuildScanUri().getPort() != -1 ? ":" + scan.getBuildScanUri().getPort() : "";
Expand All @@ -80,7 +77,7 @@ private void capturePublishedBuildScan(BuildScanApi buildScan, MavenProject root
try (FileWriter fw = new FileWriter(EXPERIMENT_DIR + "/build-scans.csv", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
out.println(String.format("%s,%s,%s,%s", rootProject.getName(), baseUrl, scan.getBuildScanUri(), scan.getBuildScanId()));
out.println(String.format("%s,%s,%s", baseUrl, scan.getBuildScanUri(), scan.getBuildScanId()));
} catch (IOException e) {
logger.error("Unable to save scan data to build-scans.csv: " + e.getMessage(), e);
throw new RuntimeException("Unable to save scan data to build-scans.csv: " + e.getMessage(), e);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ print_gradle_command() {

fetch_build_cache_metrics() {
if [ "$build_scan_publishing_mode" == "on" ]; then
read_build_scan_metadata
fetch_and_read_build_scan_data build_cache_metrics_only "${build_scan_urls[@]}"
process_build_scan_data_online
else
find_and_read_build_scan_dumps
fi
Expand Down Expand Up @@ -300,7 +299,6 @@ EOF
}

explain_and_print_summary() {
read_build_scan_metadata
local text
if [[ "${build_scan_publishing_mode}" == "on" ]]; then
IFS='' read -r -d '' text <<EOF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ print_gradle_command() {

fetch_build_cache_metrics() {
if [ "$build_scan_publishing_mode" == "on" ]; then
read_build_scan_metadata
fetch_and_read_build_scan_data build_cache_metrics_only "${build_scan_urls[@]}"
process_build_scan_data_online
else
find_and_read_build_scan_dumps
fi
Expand Down Expand Up @@ -304,7 +303,6 @@ EOF
}

explain_and_print_summary() {
read_build_scan_metadata
local text
if [[ "${build_scan_publishing_mode}" == "on" ]]; then
IFS='' read -r -d '' text <<EOF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ print_gradle_command() {

fetch_build_cache_metrics() {
if [ "$build_scan_publishing_mode" == "on" ]; then
read_build_scan_metadata
fetch_and_read_build_scan_data build_cache_metrics_only "${build_scan_urls[@]}"
process_build_scan_data_online
else
find_and_read_build_scan_dumps
fi
Expand Down Expand Up @@ -307,7 +306,6 @@ EOF

explain_measure_build_results() {
local text

if [[ "${build_scan_publishing_mode}" == "on" ]]; then
IFS='' read -r -d '' text <<EOF
$(print_separator)
Expand Down Expand Up @@ -342,7 +340,6 @@ EOF
}

explain_and_print_summary() {
read_build_scan_metadata
local text

if [[ "${build_scan_publishing_mode}" == "on" ]]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ execute() {
execute_build

print_bl
fetch_build_cache_metrics
process_build_scan_data_online

print_bl
print_summary
Expand Down Expand Up @@ -132,7 +132,7 @@ wizard_execute() {
print_bl
explain_measure_build_results
print_bl
fetch_build_cache_metrics
process_build_scan_data_online
print_bl
explain_and_print_summary
}
Expand Down Expand Up @@ -213,11 +213,6 @@ print_experiment_specific_summary_info() {
summary_row "Custom value mapping file:" "${mapping_file:-<none>}"
}

fetch_build_cache_metrics() {
read_build_scan_metadata
fetch_and_read_build_scan_data build_cache_metrics_only "${build_scan_urls[@]}"
}

# Overrides info.sh#print_performance_metrics
print_performance_metrics() {
print_performance_characteristics
Expand Down Expand Up @@ -505,7 +500,6 @@ generate_command_to_repeat_experiment() {
}

explain_and_print_summary() {
read_build_scan_metadata
local text
IFS='' read -r -d '' text <<EOF
The ‘Summary‘ section below captures the configuration of the experiment and the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ if (isTopLevelBuild) {
if (!settings.pluginManager.hasPlugin("com.gradle.enterprise")) {
throw new IllegalStateException("The com.gradle.enterprise plugin is missing from the project (see https://docs.gradle.com/enterprise/gradle-plugin/#gradle_6_x_and_later).")
}
registerCallbacks(settings.extensions["gradleEnterprise"], settings.rootProject.name)
registerCallbacks(settings.extensions["gradleEnterprise"])
}
} else if (atLeastGradle5) {
projectsEvaluated { gradle ->
if (!gradle.rootProject.pluginManager.hasPlugin("com.gradle.build-scan")) {
throw new IllegalStateException("The com.gradle.build-scan plugin is missing from the project (see https://docs.gradle.com/enterprise/gradle-plugin/#gradle_5_x).")
}
registerCallbacks(gradle.rootProject.extensions["gradleEnterprise"], gradle.rootProject.name)
registerCallbacks(gradle.rootProject.extensions["gradleEnterprise"])
}
} else {
throw new IllegalStateException("Build validation not supported for Gradle ${GradleVersion.current()}. Upgrade your project's build to Gradle 5 or newer.")
}
}

def registerCallbacks(gradleEnterprise, rootProjectName) {
void registerCallbacks(gradleEnterprise) {
gradleEnterprise.with {
buildScan {
def scanFile = new File(experimentDir, "build-scans.csv")
Expand All @@ -35,7 +35,7 @@ def registerCallbacks(gradleEnterprise, rootProjectName) {
def buildScanId = buildScan.buildScanId
def port = (buildScanUri.port != -1) ? ":" + buildScanUri.port : ""
def baseUrl = "${buildScanUri.scheme}://${buildScanUri.host}${port}"
scanFile.append("${rootProjectName},${baseUrl},${buildScanUri},${buildScanId}\n")
scanFile.append("${baseUrl},${buildScanUri},${buildScanId}\n")
}
def errorFile = new File(experimentDir, "build-scan-publish-error.txt")
onError { error ->
Expand Down
29 changes: 23 additions & 6 deletions components/scripts/lib/build_scan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@

readonly FETCH_BUILD_SCAN_DATA_JAR="${LIB_DIR}/export-api-clients/fetch-build-scan-data-cmdline-tool-${SCRIPT_VERSION}-all.jar"

# This is a helper function for the common pattern of reading Build Scan metadata
# from the build-scans.csv file, then retrieving build metrics using the Gradle
# Enterprise API.
process_build_scan_data_online() {
read_build_scan_metadata
fetch_and_read_build_scan_data build_cache_metrics_only "${build_scan_urls[@]}"
}

read_build_scan_metadata() {
# This isn't the most robust way to read a CSV,
# but we control the CSV so we don't have to worry about various CSV edge cases
if [ -f "${BUILD_SCAN_FILE}" ]; then
while IFS=, read -r field_1 field_2 field_3 field_4; do
project_names+=("$field_1")
base_urls+=("$field_2")
build_scan_urls+=("$field_3")
build_scan_ids+=("$field_4")
done < "${BUILD_SCAN_FILE}"
local build_scan_metadata
build_scan_metadata=$(< "${BUILD_SCAN_FILE}")

if [[ "$_arg_debug" == "on" ]]; then
debug "Raw Build Scan metadata (build-scans.csv)"
debug "---------------------------"
debug "${build_scan_metadata}"
debug ""
fi

while IFS=, read -r field_1 field_2 field_3; do
base_urls+=("$field_1")
build_scan_urls+=("$field_2")
build_scan_ids+=("$field_3")
done <<< "${build_scan_metadata}"
fi
}

Expand Down
2 changes: 0 additions & 2 deletions components/scripts/lib/info.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ print_warnings() {
}

print_summary() {
#defined in build_scan.sh
read_build_scan_metadata
#defined in build_scan.sh
detect_warnings_from_build_scans

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ print_maven_command() {

fetch_build_cache_metrics() {
if [ "$build_scan_publishing_mode" == "on" ]; then
read_build_scan_metadata
fetch_and_read_build_scan_data build_cache_metrics_only "${build_scan_urls[@]}"
process_build_scan_data_online
else
find_and_read_build_scan_dumps
fi
Expand Down Expand Up @@ -305,7 +304,6 @@ EOF
}

explain_and_print_summary() {
read_build_scan_metadata
local text
if [[ "${build_scan_publishing_mode}" == "on" ]]; then
IFS='' read -r -d '' text <<EOF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ print_maven_command() {

fetch_build_cache_metrics() {
if [ "$build_scan_publishing_mode" == "on" ]; then
read_build_scan_metadata
fetch_and_read_build_scan_data build_cache_metrics_only "${build_scan_urls[@]}"
process_build_scan_data_online
else
find_and_read_build_scan_dumps
fi
Expand Down Expand Up @@ -341,7 +340,6 @@ EOF
}

explain_and_print_summary() {
read_build_scan_metadata
local text
if [[ "${build_scan_publishing_mode}" == "on" ]]; then
IFS='' read -r -d '' text <<EOF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ execute() {
execute_build

print_bl
fetch_build_cache_metrics
process_build_scan_data_online

print_bl
print_summary
Expand Down Expand Up @@ -130,7 +130,7 @@ wizard_execute() {
print_bl
explain_measure_build_results
print_bl
fetch_build_cache_metrics
process_build_scan_data_online
print_bl
explain_and_print_summary
}
Expand Down Expand Up @@ -212,11 +212,6 @@ print_experiment_specific_summary_info() {
summary_row "Custom value mapping file:" "${mapping_file:-<none>}"
}

fetch_build_cache_metrics() {
read_build_scan_metadata
fetch_and_read_build_scan_data build_cache_metrics_only "${build_scan_urls[@]}"
}

# Overrides info.sh#print_performance_metrics
print_performance_metrics() {
print_performance_characteristics
Expand Down Expand Up @@ -512,7 +507,6 @@ generate_command_to_repeat_experiment() {
}

explain_and_print_summary() {
read_build_scan_metadata
local text
IFS='' read -r -d '' text <<EOF
The ‘Summary‘ section below captures the configuration of the experiment and the
Expand Down