From e6acd73a69e4542a3f9d8248bdfc047cd91b9c14 Mon Sep 17 00:00:00 2001 From: Vigneshwar M Date: Tue, 21 Jan 2020 02:43:43 -0700 Subject: [PATCH 1/2] Fixed post build query not updating the dashboard with expanded values of variables in additional columns set with build user vars plugin --- .../DashboardBuilder.java | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/environmentdashboard/DashboardBuilder.java b/src/main/java/org/jenkinsci/plugins/environmentdashboard/DashboardBuilder.java index e79710b..a820d53 100644 --- a/src/main/java/org/jenkinsci/plugins/environmentdashboard/DashboardBuilder.java +++ b/src/main/java/org/jenkinsci/plugins/environmentdashboard/DashboardBuilder.java @@ -140,7 +140,15 @@ public void tearDown(Run build, FilePath workspace, Launcher launcher, Tas String passedBuildJob = build.getEnvironment(listener).expand(buildJob); String passedPackageName = build.getEnvironment(listener).expand(packageName); String doDeploy = build.getEnvironment(listener).expand("$UPDATE_ENV_DASH"); - List passedColumnData = Collections.emptyList(); + //List passedColumnData = Collections.emptyList(); + // Pass column data to post-build runQuery + List passedColumnData = new ArrayList(); + if (addColumns) { + for (ListItem item : data) { + passedColumnData.add(new ListItem(build.getEnvironment(listener).expand(item.columnName), + build.getEnvironment(listener).expand(item.contents))); + } + } String returnComment = null; if (passedPackageName == null) { @@ -172,7 +180,7 @@ public void tearDown(Run build, FilePath workspace, Launcher launcher, Tas context.setDisposer(new TearDownImpl()); } - @SuppressWarnings("rawtypes") + private String writeToDB(Run build, TaskListener listener, String envName, String compName, String currentBuildNum, String runTime, String buildJob, Integer numberOfDays, String packageName, List passedColumnData) { @@ -211,12 +219,12 @@ private String writeToDB(Run build, TaskListener listener, String envName, columns = columns + ", " + item.columnName; contents = contents + "', '" + item.contents; try { - stat.execute("ALTER TABLE env_dashboard ADD IF NOT EXISTS " + item.columnName + " VARCHAR;"); + stat.execute("ALTER TABLE env_dashboard ADD COLUMN IF NOT EXISTS " + item.columnName + " VARCHAR;"); } catch (SQLException e) { returnComment = "WARN: Could not alter table env_dashboard to add column " + item.columnName + "."; return returnComment; } - } + } String indexValueofTable = envName + '=' + compName; String currentBuildResult = "UNKNOWN"; if (build.getResult() == null && runTime.equals("PRE")) { @@ -236,19 +244,24 @@ private String writeToDB(Run build, TaskListener listener, String envName, buildJobUrl = ""; } else { buildJobUrl = "job/" + buildJob + "/" + currentBuildNum; - } - + } String runQuery = null; if (runTime.equals("PRE")) { runQuery = "INSERT INTO env_dashboard (envComp, jobUrl, buildNum, buildStatus, envName, compName, created_at, buildJobUrl, packageName" + columns + ") VALUES( '" + indexValueofTable + "', '" + currentBuildUrl + "', '" + currentBuildNum + "', '" + currentBuildResult + "', '" + envName + "', '" + compName + "' , + current_timestamp, '" - + buildJobUrl + "' , '" + packageName + contents + "');"; + + buildJobUrl + "' , '" + packageName + contents + "');"; } else { - if (runTime.equals("POST")) { - runQuery = "UPDATE env_dashboard SET buildStatus = '" + currentBuildResult - + "', created_at = current_timestamp WHERE envComp = '" + indexValueofTable + "' AND joburl = '" - + currentBuildUrl + "';"; + if (runTime.equals("POST")) { + // Updates build no. and additional columns also in post-build update + String updateColumns = ""; + for (ListItem item : passedColumnData) { + updateColumns += "," + item.columnName + "=" + "'" + item.contents + "'"; + } + runQuery = "UPDATE env_dashboard SET buildNum = '" + currentBuildNum + "', buildStatus = '" + currentBuildResult + + "', created_at = current_timestamp " + updateColumns + " WHERE envComp = '" + indexValueofTable + "' AND joburl = '" + + currentBuildUrl + "';"; + listener.getLogger().println("DEBUG: Final Post-Build runQuery:" + runQuery); } else { if (runTime.equals("NODEPLOY")) { runQuery = "DELETE FROM env_dashboard where envComp = '" + indexValueofTable + "' AND joburl = '" @@ -260,7 +273,7 @@ private String writeToDB(Run build, TaskListener listener, String envName, stat.execute(runQuery); } catch (SQLException e) { returnComment = "Error running query " + runQuery + "."; - return returnComment; + return returnComment + e; } if (numberOfDays > 0) { runQuery = "DELETE FROM env_dashboard where created_at <= current_timestamp - " + numberOfDays; From 5a4f5889042097239a35f3c3cb25c9c26c450901 Mon Sep 17 00:00:00 2001 From: Vigneshwar M Date: Fri, 24 Jan 2020 04:46:46 -0700 Subject: [PATCH 2/2] Code review updates --- .../environmentdashboard/DashboardBuilder.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/environmentdashboard/DashboardBuilder.java b/src/main/java/org/jenkinsci/plugins/environmentdashboard/DashboardBuilder.java index a820d53..f16068c 100644 --- a/src/main/java/org/jenkinsci/plugins/environmentdashboard/DashboardBuilder.java +++ b/src/main/java/org/jenkinsci/plugins/environmentdashboard/DashboardBuilder.java @@ -140,7 +140,6 @@ public void tearDown(Run build, FilePath workspace, Launcher launcher, Tas String passedBuildJob = build.getEnvironment(listener).expand(buildJob); String passedPackageName = build.getEnvironment(listener).expand(packageName); String doDeploy = build.getEnvironment(listener).expand("$UPDATE_ENV_DASH"); - //List passedColumnData = Collections.emptyList(); // Pass column data to post-build runQuery List passedColumnData = new ArrayList(); if (addColumns) { @@ -180,7 +179,7 @@ public void tearDown(Run build, FilePath workspace, Launcher launcher, Tas context.setDisposer(new TearDownImpl()); } - + @SuppressWarnings("rawtypes") private String writeToDB(Run build, TaskListener listener, String envName, String compName, String currentBuildNum, String runTime, String buildJob, Integer numberOfDays, String packageName, List passedColumnData) { @@ -224,7 +223,7 @@ private String writeToDB(Run build, TaskListener listener, String envName, returnComment = "WARN: Could not alter table env_dashboard to add column " + item.columnName + "."; return returnComment; } - } + } String indexValueofTable = envName + '=' + compName; String currentBuildResult = "UNKNOWN"; if (build.getResult() == null && runTime.equals("PRE")) { @@ -250,18 +249,18 @@ private String writeToDB(Run build, TaskListener listener, String envName, runQuery = "INSERT INTO env_dashboard (envComp, jobUrl, buildNum, buildStatus, envName, compName, created_at, buildJobUrl, packageName" + columns + ") VALUES( '" + indexValueofTable + "', '" + currentBuildUrl + "', '" + currentBuildNum + "', '" + currentBuildResult + "', '" + envName + "', '" + compName + "' , + current_timestamp, '" - + buildJobUrl + "' , '" + packageName + contents + "');"; + + buildJobUrl + "' , '" + packageName + contents + "');"; } else { - if (runTime.equals("POST")) { + if (runTime.equals("POST")) { // Updates build no. and additional columns also in post-build update String updateColumns = ""; for (ListItem item : passedColumnData) { - updateColumns += "," + item.columnName + "=" + "'" + item.contents + "'"; + updateColumns += "," + item.columnName + "=" + "'" + item.contents + "'"; } runQuery = "UPDATE env_dashboard SET buildNum = '" + currentBuildNum + "', buildStatus = '" + currentBuildResult + "', created_at = current_timestamp " + updateColumns + " WHERE envComp = '" + indexValueofTable + "' AND joburl = '" + currentBuildUrl + "';"; - listener.getLogger().println("DEBUG: Final Post-Build runQuery:" + runQuery); + listener.getLogger().println("DEBUG: Final Post-Build runQuery:" + runQuery); } else { if (runTime.equals("NODEPLOY")) { runQuery = "DELETE FROM env_dashboard where envComp = '" + indexValueofTable + "' AND joburl = '" @@ -273,7 +272,7 @@ private String writeToDB(Run build, TaskListener listener, String envName, stat.execute(runQuery); } catch (SQLException e) { returnComment = "Error running query " + runQuery + "."; - return returnComment + e; + return returnComment; } if (numberOfDays > 0) { runQuery = "DELETE FROM env_dashboard where created_at <= current_timestamp - " + numberOfDays;