Skip to content

Commit

Permalink
Merge pull request #4 from ls1intum/feature/chain-activity-log
Browse files Browse the repository at this point in the history
Add logging for chain actitivy log.
  • Loading branch information
sleiss authored Oct 26, 2019
2 parents b7b5087 + 92cb7e0 commit ed175a6
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 9 deletions.
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.1.1</version>
<version>1.1.5</version>
<organization>
<name>LS1 TUM</name>
<url>http://www1.in.tum.de/</url>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.tum.in.www1.bamboo.server;

import com.atlassian.bamboo.build.BuildLoggerManager;
import com.atlassian.bamboo.deployments.results.DeploymentResult;
import com.atlassian.bamboo.notification.NotificationRecipient;
import com.atlassian.bamboo.notification.NotificationTransport;
Expand Down Expand Up @@ -36,6 +37,7 @@ public class ServerNotificationRecipient extends AbstractNotificationRecipient i
private ResultsSummary resultsSummary;
private DeploymentResult deploymentResult;
private CustomVariableContext customVariableContext;
private static BuildLoggerManager buildLoggerManager;

// Time in seconds before removing TestResultsContainer
private static final int TESTRESULTSCONTAINER_REMOVE_TIME = 60;
Expand Down Expand Up @@ -132,7 +134,7 @@ public String getViewHtml()
public List<NotificationTransport> getTransports()
{
List<NotificationTransport> list = Lists.newArrayList();
list.add(new ServerNotificationTransport(webhookUrl, plan, resultsSummary, deploymentResult, customVariableContext));
list.add(new ServerNotificationTransport(webhookUrl, plan, resultsSummary, deploymentResult, customVariableContext, buildLoggerManager));
return list;
}

Expand All @@ -156,6 +158,16 @@ public void setResultsSummary(@Nullable final ResultsSummary resultsSummary)
this.resultsSummary = resultsSummary;
}

public void setBuildLoggerManager(@Nullable final BuildLoggerManager buildLoggerManager)
{
this.buildLoggerManager = buildLoggerManager;
}

public static BuildLoggerManager getBuildLoggerManager()
{
return buildLoggerManager;
}

public static Map<String, TestResultsContainer> getCachedTestResults() {
return cachedTestResults;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.tum.in.www1.bamboo.server;

import com.atlassian.bamboo.build.BuildLoggerManager;
import com.atlassian.bamboo.build.logger.BuildLogger;
import com.atlassian.bamboo.chains.ChainResultsSummary;
import com.atlassian.bamboo.chains.ChainStageResult;
import com.atlassian.bamboo.commit.Commit;
Expand All @@ -18,23 +20,25 @@
import com.atlassian.bamboo.variable.VariableDefinition;
import com.atlassian.bamboo.variable.VariableDefinitionManager;
import com.atlassian.spring.container.ContainerManager;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
Expand All @@ -56,19 +60,26 @@ public class ServerNotificationTransport implements NotificationTransport
private final ResultsSummary resultsSummary;
@Nullable
private final DeploymentResult deploymentResult;
@Nullable
private final BuildLoggerManager buildLoggerManager;

private VariableDefinitionManager variableDefinitionManager = (VariableDefinitionManager) ContainerManager.getComponent("variableDefinitionManager"); // Will be injected by Bamboo

// Maximum length for the feedback text. The feedback will be truncated afterwards
private static int FEEDBACK_DETAIL_TEXT_MAX_CHARACTERS = 5000;

public ServerNotificationTransport(String webhookUrl,
@Nullable ImmutablePlan plan,
@Nullable ResultsSummary resultsSummary,
@Nullable DeploymentResult deploymentResult,
CustomVariableContext customVariableContext)
CustomVariableContext customVariableContext,
BuildLoggerManager buildLoggerManager)
{
this.webhookUrl = customVariableContext.substituteString(webhookUrl);
this.plan = plan;
this.resultsSummary = resultsSummary;
this.deploymentResult = deploymentResult;
this.buildLoggerManager = buildLoggerManager;

URI uri;
try
Expand All @@ -77,6 +88,7 @@ public ServerNotificationTransport(String webhookUrl,
}
catch (URISyntaxException e)
{
logErrorToBuildLog("Unable to set up proxy settings, invalid URI encountered: " + e);
log.error("Unable to set up proxy settings, invalid URI encountered: " + e);
return;
}
Expand All @@ -96,6 +108,7 @@ public ServerNotificationTransport(String webhookUrl,

public void sendNotification(@NotNull Notification notification)
{
logToBuildLog("Sending notification");
try
{
HttpPost method = setupPostMethod();
Expand All @@ -104,21 +117,48 @@ public void sendNotification(@NotNull Notification notification)
String secret = (String) jsonObject.get("secret");
method.addHeader("Authorization", secret);
} catch (JSONException e) {
logErrorToBuildLog("Error while getting secret from JSONObject: " + e.getMessage());
log.error("Error while getting secret from JSONObject: " + e.getMessage(), e);
}

method.setEntity(new StringEntity(jsonObject.toString(), ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8)));

try {
logToBuildLog("Executing call to " + method.getURI().toString());
log.debug(method.getURI().toString());
log.debug(method.getEntity().toString());
client.execute(method);
} catch (IOException e) {
CloseableHttpResponse closeableHttpResponse = client.execute(method);
logToBuildLog("Call executed");
if (closeableHttpResponse != null) {
logToBuildLog("Response is not null: " + closeableHttpResponse.toString());

StatusLine statusLine = closeableHttpResponse.getStatusLine();
if (statusLine != null) {
logToBuildLog("StatusLine is not null: " + statusLine.toString());
logToBuildLog("StatusCode is: " + statusLine.getStatusCode());
} else {
logErrorToBuildLog("Statusline is null");
}

HttpEntity httpEntity = closeableHttpResponse.getEntity();
if (httpEntity != null) {
String response = EntityUtils.toString(httpEntity);
logToBuildLog("Response from entity is: " + response);
EntityUtils.consume(httpEntity);
} else {
logErrorToBuildLog("Httpentity is null");
}
} else {
logErrorToBuildLog("Response is null");
}
} catch (Exception e) {
logErrorToBuildLog("Error while sending payload: " + e.getMessage());
log.error("Error while sending payload: " + e.getMessage(), e);
}
}
catch(URISyntaxException e)
{
logErrorToBuildLog("Error parsing webhook url: " + e.getMessage());
log.error("Error parsing webhook url: " + e.getMessage(), e);
}
}
Expand All @@ -131,6 +171,7 @@ private HttpPost setupPostMethod() throws URISyntaxException
}

private JSONObject createJSONObject(Notification notification) {
logToBuildLog("Creating JSON object");
JSONObject jsonObject = new JSONObject();
try {
// Variable name contains "password" to ensure that the secret is hidden in the UI
Expand Down Expand Up @@ -203,8 +244,10 @@ private JSONObject createJSONObject(Notification notification) {

jobDetails.put("id", buildResultsSummary.getId());

logToBuildLog("Loading cached test results");
TestResultsContainer testResultsContainer = ServerNotificationRecipient.getCachedTestResults().get(buildResultsSummary.getPlanResultKey().toString());
if (testResultsContainer != null) {
logToBuildLog("Tests results found");
JSONArray successfulTestDetails = createTestsResultsJSONArray(testResultsContainer.getSuccessfulTests(), false);
jobDetails.put("successfulTests", successfulTestDetails);

Expand All @@ -213,6 +256,8 @@ private JSONObject createJSONObject(Notification notification) {

JSONArray failedTestDetails = createTestsResultsJSONArray(testResultsContainer.getFailedTests(), true);
jobDetails.put("failedTests", failedTestDetails);
} else {
logErrorToBuildLog("Could not load cached test results!");
}
jobs.put(jobDetails);
}
Expand All @@ -228,13 +273,16 @@ private JSONObject createJSONObject(Notification notification) {


} catch (JSONException e) {
logErrorToBuildLog("JSON construction error :" + e.getMessage());
log.error("JSON construction error :" + e.getMessage(), e);
}

return jsonObject;
logToBuildLog("JSON object created");
return jsonObject;
}

private JSONObject createTestsResultsJSONObject(TestResults testResults, boolean addErrors) throws JSONException {
logToBuildLog("Creating test results JSON object for " + testResults.getActualMethodName());
JSONObject testResultsJSON = new JSONObject();
testResultsJSON.put("name", testResults.getActualMethodName());
testResultsJSON.put("methodName", testResults.getMethodName());
Expand All @@ -243,7 +291,11 @@ private JSONObject createTestsResultsJSONObject(TestResults testResults, boolean
if (addErrors) {
JSONArray testCaseErrorDetails = new JSONArray();
for(TestCaseResultError testCaseResultError : testResults.getErrors()) {
testCaseErrorDetails.put(testCaseResultError.getContent());
String errorMessageString = testCaseResultError.getContent();
if(errorMessageString != null && errorMessageString.length() > FEEDBACK_DETAIL_TEXT_MAX_CHARACTERS) {
errorMessageString = errorMessageString.substring(0, FEEDBACK_DETAIL_TEXT_MAX_CHARACTERS);
}
testCaseErrorDetails.put(errorMessageString);
}
testResultsJSON.put("errors", testCaseErrorDetails);
}
Expand All @@ -252,11 +304,30 @@ private JSONObject createTestsResultsJSONObject(TestResults testResults, boolean
}

private JSONArray createTestsResultsJSONArray(Collection<TestResults> testResultsCollection, boolean addErrors) throws JSONException {
logToBuildLog("Creating test results JSON array");
JSONArray testResultsArray = new JSONArray();
for (TestResults testResults : testResultsCollection) {
testResultsArray.put(createTestsResultsJSONObject(testResults, addErrors));
}

return testResultsArray;
}

private void logToBuildLog(String s) {
if (buildLoggerManager != null && plan != null) {
BuildLogger buildLogger = buildLoggerManager.getLogger(plan.getPlanKey());
if (buildLogger != null) {
buildLogger.addBuildLogEntry("[BAMBOO-SERVER-NOTIFICATION] " + s);
}
}
}

private void logErrorToBuildLog(String s) {
if (buildLoggerManager != null && plan != null) {
BuildLogger buildLogger = buildLoggerManager.getLogger(plan.getPlanKey());
if (buildLogger != null) {
buildLogger.addErrorLogEntry("[BAMBOO-SERVER-NOTIFICATION] " + s);
}
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/atlassian-plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<interface>com.atlassian.bamboo.variable.VariableDefinitionManager</interface>
</component-import>

<component-import key="buildLoggerManager" interface="com.atlassian.bamboo.build.BuildLoggerManager"/>

<bambooEventListener key="buildCompleteListener"
class="de.tum.in.www1.bamboo.server.BuildCompleteListener"/>
</atlassian-plugin>

0 comments on commit ed175a6

Please sign in to comment.