Permalink
Browse files

first working version

  • Loading branch information...
1 parent 4713daf commit 38a116906e2a08822b6bc3c3eb6a0857fc1bdd1a Alik Kurdyukov committed Nov 29, 2012
View
@@ -0,0 +1,8 @@
+Copyright (c) 2012, Alexander Kurdyukov
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
@@ -1,4 +1,17 @@
bamboo-traffic-light-plugin
===========================
-Atlassian Bamboo plugin for toy traffic light
+Atlassian Bamboo plugin for toy traffic light.
+
+Presents simple TCP server for managing toy traffic light. Listening port is not configurable, it's 9090.
+
+Settings
+--------
+User may set traffic light program (text message) for every plan. See sections 'Pre Build Traffic Light Program' and 'Post Build Traffic Light Program' in job configuration tab 'Miscellaneous'.
+
+Protocol
+--------
+After connecting to port 9090 user must send plan name followed by '\n'. Server disconnects client with unknown plan names.
+
+When plan build starts all listening subscribers receive pre-build program followed with '\n'.
+When plan build finishes (successful or failure) all listening subscribers receive appropriate post-build program followed with '\n'.
View
@@ -0,0 +1,71 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.fvendor</groupId>
+ <artifactId>bamboo-traffic-light-plugin</artifactId>
+ <version>1.0-SNAPSHOT</version>
+
+ <organization>
+ <name>FVendor Inc.</name>
+ <url>http://www.fvendor.com/</url>
+ </organization>
+
+ <name>bamboo-traffic-light-plugin</name>
+ <description>Traffic light control plugin.</description>
+ <packaging>atlassian-plugin</packaging>
+
+ <properties>
+ <bamboo.version>4.2.1</bamboo.version>
+ <bamboo.data.version>3.2.2</bamboo.data.version>
+ <amps.version>4.0</amps.version>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>com.atlassian.bamboo</groupId>
+ <artifactId>atlassian-bamboo-web</artifactId>
+ <version>${bamboo.version}</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.xsocket</groupId>
+ <artifactId>xSocket</artifactId>
+ <version>2.8.15</version>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.6</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>com.atlassian.maven.plugins</groupId>
+ <artifactId>maven-bamboo-plugin</artifactId>
+ <version>${amps.version}</version>
+ <extensions>true</extensions>
+ <configuration>
+ <productVersion>${bamboo.version}</productVersion>
+ <productDataVersion>${bamboo.data.version}</productDataVersion>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
@@ -0,0 +1,24 @@
+package bamboo.traflight;
+
+import com.atlassian.bamboo.build.logger.BuildLogger;
+import com.atlassian.bamboo.task.TaskContext;
+import com.atlassian.bamboo.task.TaskException;
+import com.atlassian.bamboo.task.TaskResult;
+import com.atlassian.bamboo.task.TaskResultBuilder;
+import com.atlassian.bamboo.task.TaskType;
+import org.jetbrains.annotations.NotNull;
+
+public class ExampleTask implements TaskType
+{
+ @NotNull
+ @java.lang.Override
+ public TaskResult execute(@NotNull final TaskContext taskContext) throws TaskException
+ {
+ final BuildLogger buildLogger = taskContext.getBuildLogger();
+
+ final String toSay = taskContext.getConfigurationMap().get("say");
+ buildLogger.addBuildLogEntry(toSay);
+
+ return TaskResultBuilder.create(taskContext).success().build();
+ }
+}
@@ -0,0 +1,67 @@
+package bamboo.traflight;
+
+import com.atlassian.bamboo.collections.ActionParametersMap;
+import com.atlassian.bamboo.task.AbstractTaskConfigurator;
+import com.atlassian.bamboo.task.TaskDefinition;
+import com.atlassian.bamboo.utils.error.ErrorCollection;
+import com.opensymphony.xwork.TextProvider;
+import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.Map;
+
+public class ExampleTaskConfigurator extends AbstractTaskConfigurator
+{
+ private TextProvider textProvider;
+
+ @NotNull
+ @Override
+ public Map<String, String> generateTaskConfigMap(@NotNull final ActionParametersMap params, @Nullable final TaskDefinition previousTaskDefinition)
+ {
+ final Map<String, String> config = super.generateTaskConfigMap(params, previousTaskDefinition);
+ config.put("say", params.getString("say"));
+ return config;
+ }
+
+ @Override
+ public void populateContextForCreate(@NotNull final Map<String, Object> context)
+ {
+ super.populateContextForCreate(context);
+
+ context.put("say", "Hello, World!");
+ }
+
+ @Override
+ public void populateContextForEdit(@NotNull final Map<String, Object> context, @NotNull final TaskDefinition taskDefinition)
+ {
+ super.populateContextForEdit(context, taskDefinition);
+
+ context.put("say", taskDefinition.getConfiguration().get("say"));
+ }
+
+ @Override
+ public void populateContextForView(@NotNull final Map<String, Object> context, @NotNull final TaskDefinition taskDefinition)
+ {
+ super.populateContextForView(context, taskDefinition);
+ context.put("say", taskDefinition.getConfiguration().get("say"));
+ }
+
+ @Override
+ public void validate(@NotNull final ActionParametersMap params, @NotNull final ErrorCollection errorCollection)
+ {
+ super.validate(params, errorCollection);
+
+ final String sayValue = params.getString("say");
+ if (StringUtils.isEmpty(sayValue))
+ {
+ errorCollection.addError("say", textProvider.getText("com.fvendor.say.error"));
+ }
+ }
+
+ public void setTextProvider(final TextProvider textProvider)
+ {
+ this.textProvider = textProvider;
+ }
+}
@@ -0,0 +1,79 @@
+package com.fvendor.bamboo.traflight;
+
+import com.atlassian.bamboo.build.BuildLoggerManager;
+import com.atlassian.bamboo.build.CustomPostBuildCompletedAction;
+import com.atlassian.bamboo.builder.BuildState;
+import com.atlassian.bamboo.v2.build.BaseConfigurablePlugin;
+import com.atlassian.bamboo.v2.build.BuildContext;
+import org.apache.log4j.Logger;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Map;
+
+/**
+ * Post build completed action sending program to traffic light
+ */
+public class PostBuildCompletedAction extends BaseConfigurablePlugin implements CustomPostBuildCompletedAction {
+ private static final Logger log = Logger.getLogger(PostBuildCompletedAction.class);
+
+ private static final String PRE_ENABLED_KEY = "custom.traflight.post.program.enabled";
+ private static final String PRE_PROGRAM_SUCCESS_KEY = "custom.traflight.post.program.success";
+ private static final String PRE_PROGRAM_FAIL_KEY = "custom.traflight.post.program.fail";
+
+ private BuildContext buildContext;
+ private BuildLoggerManager buildLoggerManager;
+ private TrafficLightService trafficLightService;
+
+ @Override
+ public void init(@NotNull BuildContext buildContext) {
+ this.buildContext = buildContext;
+ }
+
+ @NotNull
+ @Override
+ public BuildContext call() throws InterruptedException, Exception {
+ final Map customConfiguration = buildContext.getBuildDefinition().getCustomConfiguration();
+
+ if (!"true".equals(customConfiguration.get(PRE_ENABLED_KEY))) {
+ log.info("Post traffic light disabled, skipping");
+ return buildContext;
+ }
+
+ BuildState state = buildContext.getBuildResult().getBuildState();
+ if (state.equals(BuildState.UNKNOWN)) {
+ if (0 < buildContext.getBuildResult().getBuildReturnCode()) {
+ state = BuildState.FAILED;
+ } else {
+ state = BuildState.SUCCESS;
+ }
+ }
+ String program;
+ if (BuildState.SUCCESS.equals(state)) {
+ program = (String) customConfiguration.get(PRE_PROGRAM_SUCCESS_KEY);
+ } else {
+ program = (String) customConfiguration.get(PRE_PROGRAM_FAIL_KEY);
+ }
+
+ log.info("Post traffic light enabled, setting program " + program);
+ buildLoggerManager.getBuildLogger(buildContext.getPlanResultKey()).addBuildLogEntry(
+ "setting post traffic light program " + program);
+ sendProgram(buildContext, program);
+
+ return buildContext;
+ }
+
+ private void sendProgram(BuildContext context, String program) {
+ if (context.getParentBuildContext() != null) {
+ sendProgram(context.getParentBuildContext(), program);
+ }
+ trafficLightService.setProgram(context.getPlanKey(), program);
+ }
+
+ public void setBuildLoggerManager(BuildLoggerManager buildLoggerManager) {
+ this.buildLoggerManager = buildLoggerManager;
+ }
+
+ public void setTrafficLightService(TrafficLightService trafficLightService) {
+ this.trafficLightService = trafficLightService;
+ }
+}
@@ -0,0 +1,63 @@
+package com.fvendor.bamboo.traflight;
+
+import com.atlassian.bamboo.build.BuildLoggerManager;
+import com.atlassian.bamboo.build.CustomPreBuildAction;
+import com.atlassian.bamboo.v2.build.BaseConfigurableBuildPlugin;
+import com.atlassian.bamboo.v2.build.BuildContext;
+import org.apache.log4j.Logger;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Map;
+
+/**
+ * Pre-build action sends 'build started' command
+ */
+public class PreBuildAction extends BaseConfigurableBuildPlugin implements CustomPreBuildAction {
+ private static final Logger log = Logger.getLogger(PreBuildAction.class);
+
+ private static final String PRE_ENABLED_KEY = "custom.traflight.pre.program.enabled";
+ private static final String PRE_PROGRAM_KEY = "custom.traflight.pre.program";
+
+ private BuildContext buildContext;
+ private BuildLoggerManager buildLoggerManager;
+ private TrafficLightService trafficLightService;
+
+ @Override
+ public void init(@NotNull BuildContext buildContext) {
+ this.buildContext = buildContext;
+ }
+
+ @NotNull
+ @Override
+ public BuildContext call() throws Exception {
+ final Map customConfiguration = buildContext.getBuildDefinition().getCustomConfiguration();
+
+ log.info("Pre traffic light enabled: " + customConfiguration.get(PRE_ENABLED_KEY));
+ if (!"true".equals(customConfiguration.get(PRE_ENABLED_KEY))) {
+ log.info("Pre traffic light disabled, skipping");
+ return buildContext;
+ }
+
+ String program = (String) customConfiguration.get(PRE_PROGRAM_KEY);
+ log.info("Pre traffic light enabled, setting program " + program);
+ buildLoggerManager.getBuildLogger(buildContext.getPlanResultKey()).addBuildLogEntry(
+ "setting pre traffic light program " + program);
+ sendProgram(buildContext, program);
+ return buildContext;
+ }
+
+ private void sendProgram(BuildContext context, String program) {
+ if (context.getParentBuildContext() != null) {
+ sendProgram(context.getParentBuildContext(), program);
+ }
+ trafficLightService.setProgram(context.getPlanKey(), program);
+ }
+
+ public void setBuildLoggerManager(BuildLoggerManager buildLoggerManager) {
+ this.buildLoggerManager = buildLoggerManager;
+ }
+
+ public void setTrafficLightService(TrafficLightService trafficLightService) {
+ this.trafficLightService = trafficLightService;
+ }
+}
Oops, something went wrong.

0 comments on commit 38a1169

Please sign in to comment.