From f9e1835bd375a4830b90192dabf2e295ae20adba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Lei=C3=9F?= <5084100+sleiss@users.noreply.github.com> Date: Thu, 8 Sep 2022 21:25:19 +0200 Subject: [PATCH] Add branch name to payload. (#28) --- README.md | 6 +++-- pom.xml | 2 +- .../bamboo/server/BuildCompleteListener.java | 17 ++++++++++++- .../www1/bamboo/server/ResultsContainer.java | 11 +++++++- .../server/ServerNotificationTransport.java | 25 +++++++++++++++---- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index efa9931..f0f10a9 100644 --- a/README.md +++ b/README.md @@ -74,14 +74,16 @@ Sample request ], "id":"f4158f583a02dc3b9bda47e64749fdbf7e9bdcef", - "repositoryName":"Assignment" + "repositoryName":"Assignment", + "branchName": "main" }, { "commits":[ ], "id":"9be1bb278e08d03ab2519b746702196cead996be", - "repositoryName":"tests" + "repositoryName":"tests", + "branchName": "main" } ], "jobs":[ diff --git a/pom.xml b/pom.xml index 81fa599..96ac625 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ 4.0.0 de.tum.in.www1 bamboo-server - 1.5.3 + 1.6.0 LS1 TUM https://ase.in.tum.de diff --git a/src/main/java/de/tum/in/www1/bamboo/server/BuildCompleteListener.java b/src/main/java/de/tum/in/www1/bamboo/server/BuildCompleteListener.java index 82d041e..164c730 100644 --- a/src/main/java/de/tum/in/www1/bamboo/server/BuildCompleteListener.java +++ b/src/main/java/de/tum/in/www1/bamboo/server/BuildCompleteListener.java @@ -1,11 +1,15 @@ package de.tum.in.www1.bamboo.server; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import org.apache.log4j.Logger; import com.atlassian.bamboo.v2.build.CurrentBuildResult; import com.atlassian.bamboo.v2.build.events.PostBuildCompletedEvent; +import com.atlassian.bamboo.vcs.configuration.PlanRepositoryDefinition; +import com.atlassian.bamboo.vcs.configuration.VcsBranchDefinition; import com.atlassian.event.api.EventListener; public class BuildCompleteListener { @@ -17,11 +21,22 @@ public void onPostBuildComplete(final PostBuildCompletedEvent postBuildCompleted long startTime = System.currentTimeMillis(); LoggingUtils.logInfo("onPostBuildComplete: " + postBuildCompletedEvent.getPlanResultKey().toString(), null, postBuildCompletedEvent.getPlanKey(), log); CurrentBuildResult currentBuildResult = postBuildCompletedEvent.getContext().getBuildResult(); + + // We can only access the branch names of the repository from the BuildContext -> Extract it here and add it to the ResultContainer, so that it can be extracted from the + // ServerNotificationTransport + Map repositoryToBranchMap = new HashMap<>(); + for (PlanRepositoryDefinition repository : postBuildCompletedEvent.getContext().getVcsRepositories()) { + VcsBranchDefinition vcsBranchDefinition = repository.getBranch(); + if (vcsBranchDefinition != null) { + repositoryToBranchMap.put(repository.getName(), vcsBranchDefinition.getVcsBranch().getName()); + } + } + ResultsContainer resultsContainer = new ResultsContainer(postBuildCompletedEvent.getPlanResultKey(), currentBuildResult.getSuccessfulTestResults() != null ? currentBuildResult.getSuccessfulTestResults() : Collections.emptySet(), currentBuildResult.getSkippedTestResults() != null ? currentBuildResult.getSkippedTestResults() : Collections.emptySet(), currentBuildResult.getFailedTestResults() != null ? currentBuildResult.getFailedTestResults() : Collections.emptySet(), - currentBuildResult.getTaskResults()); + currentBuildResult.getTaskResults(), repositoryToBranchMap); long containerCreationTime = System.currentTimeMillis() - startTime; LoggingUtils.logInfo("onPostBuildComplete: " + postBuildCompletedEvent.getPlanResultKey().toString() + " - Container created took " + containerCreationTime + "ms", null, diff --git a/src/main/java/de/tum/in/www1/bamboo/server/ResultsContainer.java b/src/main/java/de/tum/in/www1/bamboo/server/ResultsContainer.java index 0b74f5a..2b96b2f 100644 --- a/src/main/java/de/tum/in/www1/bamboo/server/ResultsContainer.java +++ b/src/main/java/de/tum/in/www1/bamboo/server/ResultsContainer.java @@ -1,6 +1,7 @@ package de.tum.in.www1.bamboo.server; import java.util.Collection; +import java.util.Map; import com.atlassian.bamboo.plan.PlanResultKey; import com.atlassian.bamboo.results.tests.TestResults; @@ -20,13 +21,16 @@ public class ResultsContainer { private final Collection taskResults; + private final Map repositoryToBranchMap; + public ResultsContainer(PlanResultKey planResultKey, Collection successfulTests, Collection skippedTests, Collection failedTests, - Collection taskResults) { + Collection taskResults, Map repositoryToBranchMap) { this.planResultKey = planResultKey; this.successfulTests = successfulTests; this.skippedTests = skippedTests; this.failedTests = failedTests; this.taskResults = taskResults; + this.repositoryToBranchMap = repositoryToBranchMap; } public PlanResultKey getPlanResultKey() { @@ -53,6 +57,10 @@ public Collection getTaskResults() { return taskResults; } + public Map getRepositoryToBranchMap() { + return repositoryToBranchMap; + } + @Override public String toString() { return "ResultsContainer{" + @@ -62,6 +70,7 @@ public String toString() { ", skippedTests=" + skippedTests + ", failedTests=" + failedTests + ", taskResults=" + taskResults + + ", repositoriesToBranchMap=" + repositoryToBranchMap.toString() + '}'; } } diff --git a/src/main/java/de/tum/in/www1/bamboo/server/ServerNotificationTransport.java b/src/main/java/de/tum/in/www1/bamboo/server/ServerNotificationTransport.java index 65e9e09..1464cf2 100644 --- a/src/main/java/de/tum/in/www1/bamboo/server/ServerNotificationTransport.java +++ b/src/main/java/de/tum/in/www1/bamboo/server/ServerNotificationTransport.java @@ -6,11 +6,7 @@ import java.nio.charset.StandardCharsets; import java.time.ZoneId; import java.time.ZonedDateTime; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Optional; +import java.util.*; import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; @@ -85,6 +81,9 @@ public class ServerNotificationTransport implements NotificationTransport { @Nullable private final BuildLogFileAccessorFactory buildLogFileAccessorFactory; + @Nullable + private final CustomVariableContext customVariableContext; + // Will be injected by Bamboo private final VariableDefinitionManager variableDefinitionManager = (VariableDefinitionManager) ContainerManager.getComponent("variableDefinitionManager"); @@ -106,6 +105,7 @@ public ServerNotificationTransport(String webhookUrl, @Nullable ImmutablePlan pl this.resultsSummary = resultsSummary; this.buildLoggerManager = buildLoggerManager; this.buildLogFileAccessorFactory = buildLogFileAccessorFactory; + this.customVariableContext = customVariableContext; URI uri; try { @@ -250,6 +250,11 @@ private JSONObject createJSONObject(Notification notification) { buildDetails.put("testSummary", testResultOverview); + // The branch name can only be accessed from the ResultsContainer -> We can only add it later to the changesetDetails JSONObject. + // As we can not access it easily from the vcsDetails JSONArray, we keep a reference to the changesetDetails JSONObject, which will later be used to + // get the correct changesetDetails JSONObject based on the repository name + Map repositoryToVcsDetails = new HashMap<>(); + JSONArray vcsDetails = new JSONArray(); for (RepositoryChangeset changeset : resultsSummary.getRepositoryChangesets()) { JSONObject changesetDetails = new JSONObject(); @@ -268,6 +273,8 @@ private JSONObject createJSONObject(Notification notification) { changesetDetails.put("commits", commits); vcsDetails.put(changesetDetails); + // Put a reference to later access it when adding the branch name + repositoryToVcsDetails.put(changeset.getRepositoryData().getName(), changesetDetails); } buildDetails.put("vcs", vcsDetails); @@ -299,6 +306,14 @@ private JSONObject createJSONObject(Notification notification) { JSONArray taskResults = createTasksJSONArray(resultsContainer.getTaskResults()); jobDetails.put("tasks", taskResults); + + // Put the branch name in the referenced changesetDetails JSONObject + for (Map.Entry repositoryToBranchEntry : resultsContainer.getRepositoryToBranchMap().entrySet()) { + JSONObject changesetDetails = repositoryToVcsDetails.get(repositoryToBranchEntry.getKey()); + if (changesetDetails != null) { + changesetDetails.put("branchName", repositoryToBranchEntry.getValue()); + } + } } else { LoggingUtils.logError("Could not load cached test results!", buildLoggerManager, plan.getPlanKey(), log, null);