Skip to content

Commit

Permalink
Add branch name to payload. (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
sleiss authored Sep 8, 2022
1 parent fb5b41c commit f9e1835
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,16 @@ Sample request

],
"id":"f4158f583a02dc3b9bda47e64749fdbf7e9bdcef",
"repositoryName":"Assignment"
"repositoryName":"Assignment",
"branchName": "main"
},
{
"commits":[

],
"id":"9be1bb278e08d03ab2519b746702196cead996be",
"repositoryName":"tests"
"repositoryName":"tests",
"branchName": "main"
}
],
"jobs":[
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.tum.in.www1</groupId>
<artifactId>bamboo-server</artifactId>
<version>1.5.3</version>
<version>1.6.0</version>
<organization>
<name>LS1 TUM</name>
<url>https://ase.in.tum.de</url>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<String, String> 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,
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/de/tum/in/www1/bamboo/server/ResultsContainer.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,13 +21,16 @@ public class ResultsContainer {

private final Collection<TaskResult> taskResults;

private final Map<String, String> repositoryToBranchMap;

public ResultsContainer(PlanResultKey planResultKey, Collection<TestResults> successfulTests, Collection<TestResults> skippedTests, Collection<TestResults> failedTests,
Collection<TaskResult> taskResults) {
Collection<TaskResult> taskResults, Map<String, String> repositoryToBranchMap) {
this.planResultKey = planResultKey;
this.successfulTests = successfulTests;
this.skippedTests = skippedTests;
this.failedTests = failedTests;
this.taskResults = taskResults;
this.repositoryToBranchMap = repositoryToBranchMap;
}

public PlanResultKey getPlanResultKey() {
Expand All @@ -53,6 +57,10 @@ public Collection<TaskResult> getTaskResults() {
return taskResults;
}

public Map<String, String> getRepositoryToBranchMap() {
return repositoryToBranchMap;
}

@Override
public String toString() {
return "ResultsContainer{" +
Expand All @@ -62,6 +70,7 @@ public String toString() {
", skippedTests=" + skippedTests +
", failedTests=" + failedTests +
", taskResults=" + taskResults +
", repositoriesToBranchMap=" + repositoryToBranchMap.toString() +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");

Expand All @@ -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 {
Expand Down Expand Up @@ -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<String, JSONObject> repositoryToVcsDetails = new HashMap<>();

JSONArray vcsDetails = new JSONArray();
for (RepositoryChangeset changeset : resultsSummary.getRepositoryChangesets()) {
JSONObject changesetDetails = new JSONObject();
Expand All @@ -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);

Expand Down Expand Up @@ -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<String, String> 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);
Expand Down

0 comments on commit f9e1835

Please sign in to comment.