James Dumay avatar James Dumay committed 912ae28

Init labeler task

Comments (0)

Files changed (7)

+<?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.atlassian.bamboo.plugins.labeler</groupId>
+    <artifactId>bamboo-labeler-task-plugin</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <organization>
+        <name>Example Company</name>
+        <url>http://www.example.com/</url>
+    </organization>
+
+    <name>bamboo-labeler-task-plugin</name>
+    <description>This is the com.atlassian.bamboo.plugins.labeler:bamboo-labeler-task-plugin plugin for Atlassian Bamboo.</description>
+    <packaging>atlassian-plugin</packaging>
+
+    <properties>
+        <bamboo.version>4.0</bamboo.version>
+        <bamboo.data.version>3.2.2</bamboo.data.version>
+        <amps.version>3.10.4</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>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>

src/main/java/com/atlassian/bamboo/plugins/labeler/AddLabelsBuildProcessor.java

+package com.atlassian.bamboo.plugins.labeler;
+
+import com.atlassian.bamboo.build.CustomBuildProcessorServer;
+import com.atlassian.bamboo.labels.LabelManager;
+import com.atlassian.bamboo.v2.build.BuildContext;
+import org.apache.commons.lang.StringUtils;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Map;
+
+public class AddLabelsBuildProcessor implements CustomBuildProcessorServer
+{
+    static final String LABELS = "com.atlassian.bamboo.plugins.labeler.labels";
+
+    private LabelManager labelManager;
+
+    private BuildContext buildContext;
+
+    public void setLabelManager(LabelManager labelManager)
+    {
+        this.labelManager = labelManager;
+    }
+
+    @Override
+    public void init(@NotNull BuildContext buildContext)
+    {
+        this.buildContext  = buildContext;
+    }
+
+    @NotNull
+    @Override
+    public BuildContext call() throws InterruptedException, Exception
+    {
+        Map<String,String> customBuildData = buildContext.getBuildResult().getCustomBuildData();
+        final String labelsAsString = customBuildData.get(LABELS);
+        if (StringUtils.isNotEmpty(labelsAsString))
+        {
+            customBuildData.remove(LABELS);
+
+            final String[] labels = StringUtils.split(labelsAsString, ",");
+
+            for (String label : labels)
+            {
+                labelManager.addLabel(label, buildContext.getPlanResultKey(), null);
+            }
+        }
+
+        return buildContext;
+    }
+}

src/main/java/com/atlassian/bamboo/plugins/labeler/LabelTask.java

+package com.atlassian.bamboo.plugins.labeler;
+
+import com.atlassian.bamboo.build.LogEntry;
+import com.atlassian.bamboo.build.logger.BuildLogFileAccessor;
+import com.atlassian.bamboo.build.logger.BuildLogFileAccessorFactory;
+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 com.google.common.collect.Lists;
+import com.google.gson.Gson;
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.lang.StringUtils;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+public class LabelTask implements TaskType
+{
+    private final BuildLogFileAccessorFactory buildLogFileAccessorFactory;
+
+    public LabelTask(BuildLogFileAccessorFactory buildLogFileAccessorFactory)
+    {
+        this.buildLogFileAccessorFactory = buildLogFileAccessorFactory;
+    }
+
+    @NotNull
+    @java.lang.Override
+    public TaskResult execute(@NotNull final TaskContext taskContext) throws TaskException
+    {
+        final BuildLogger buildLogger = taskContext.getBuildLogger();
+
+        TaskResultBuilder builder = TaskResultBuilder.create(taskContext);
+
+        final String regex = taskContext.getConfigurationMap().get("regex");
+        final String labelsAsString = taskContext.getConfigurationMap().get("labels");
+
+        final String[] labels = StringUtils.split(labelsAsString, ",");
+
+        final Pattern pattern = Pattern.compile(regex);
+
+
+        BuildLogFileAccessor logFileAccessor = buildLogFileAccessorFactory.createBuildLogFileAccessor(taskContext.getBuildContext().getPlanKey(), taskContext.getBuildContext().getBuildNumber());
+        logFileAccessor.openFileForIteration();
+
+        while (logFileAccessor.hasNext())
+        {
+            final String line = logFileAccessor.nextLine();
+            if (pattern.matcher(line).matches())
+            {
+                buildLogger.addBuildLogEntry("Found match for '" + regex + "', labels '" + labelsAsString + "' will be applied when the build finishes");
+
+                Map<String,String> customBuildData = taskContext.getBuildContext().getBuildResult().getCustomBuildData();
+
+                //Extract existing labels
+                final String existingLabelsAsString = customBuildData.get(AddLabelsBuildProcessor.LABELS);
+                final String[] existingLabels = StringUtils.split(existingLabelsAsString, ",");
+
+                //Combine existing labels with this tasks labels
+                String[] combinedLabels = (String[])ArrayUtils.addAll(existingLabels, labels);
+                final String combinedLabelsAsString = StringUtils.join(combinedLabels, ", ");
+
+                //Add them to the build processor for labeling on the other side
+                customBuildData.put(AddLabelsBuildProcessor.LABELS, combinedLabelsAsString);
+
+                break;
+            }
+        }
+
+        logFileAccessor.closeFileForIteration();
+
+        return builder.success().build();
+    }
+}

src/main/java/com/atlassian/bamboo/plugins/labeler/LabelTaskConfigurator.java

+package com.atlassian.bamboo.plugins.labeler;
+
+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;
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+public class LabelTaskConfigurator extends AbstractTaskConfigurator
+{
+    @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("regex", params.getString("regex"));
+        config.put("labels", StringUtils.lowerCase(params.getString("labels")));
+        return config;
+    }
+
+    @Override
+    public void populateContextForCreate(@NotNull final Map<String, Object> context)
+    {
+        super.populateContextForCreate(context);
+    }
+
+    @Override
+    public void populateContextForEdit(@NotNull final Map<String, Object> context, @NotNull final TaskDefinition taskDefinition)
+    {
+        super.populateContextForEdit(context, taskDefinition);
+
+        context.put("regex", taskDefinition.getConfiguration().get("regex"));
+        context.put("labels", taskDefinition.getConfiguration().get("labels"));
+    }
+
+    @Override
+    public void populateContextForView(@NotNull final Map<String, Object> context, @NotNull final TaskDefinition taskDefinition)
+    {
+        super.populateContextForView(context, taskDefinition);
+        context.put("regex", taskDefinition.getConfiguration().get("regex"));
+        context.put("labels", taskDefinition.getConfiguration().get("labels"));
+    }
+
+    @Override
+    public void validate(@NotNull final ActionParametersMap params, @NotNull final ErrorCollection errorCollection)
+    {
+        super.validate(params, errorCollection);
+
+        final String regex = params.getString("regex");
+        if (StringUtils.isEmpty(regex))
+        {
+            errorCollection.addError("regex", getI18nBean().getText("com.atlassian.bamboo.plugins.labeler.regex"));
+        }
+        else
+        {
+            try
+            {
+                Pattern.compile(regex);
+            }
+            catch (PatternSyntaxException exception)
+            {
+                errorCollection.addError("regex", getI18nBean().getText("com.atlassian.bamboo.plugins.labeler.regex.error"));
+            }
+        }
+
+        final String labels = params.getString("labels");
+        if (StringUtils.isEmpty(labels))
+        {
+            errorCollection.addError(labels, getI18nBean().getText("com.atlassian.bamboo.plugins.labeler.labels.error"));
+        }
+    }
+}

src/main/resources/atlassian-plugin.xml

+<atlassian-plugin key="${project.groupId}.${project.artifactId}" name="${project.name}" plugins-version="2">
+    <plugin-info>
+        <description>${project.description}</description>
+        <version>${project.version}</version>
+        <vendor name="${project.organization.name}" url="${project.organization.url}" />
+    </plugin-info>
+
+    <resource type="i18n" name="Labeler Task Language" location="com.atlassian.bamboo.plugins.labeler.i18n"/>
+
+    <taskType name="Label build result" class="com.atlassian.bamboo.plugins.labeler.LabelTask" key="labelTask">
+      <description>Label a build result when a regex is matched in the logs.</description>
+      <configuration class="com.atlassian.bamboo.plugins.labeler.LabelTaskConfigurator"/>
+      <resource type="freemarker" name="edit" location="com/atlassian/bamboo/plugins/labeler/editLabelTask.ftl"/>
+    </taskType>
+
+    <buildProcessorServer name="addLabelsBuildProcessor" class="com.atlassian.bamboo.plugins.labeler.AddLabelsBuildProcessor" key="addLabelsBuildProcessor">
+        <description>Adds the labels to the result when the build is completed.</description>
+    </buildProcessorServer>
+</atlassian-plugin>

src/main/resources/com/atlassian/bamboo/plugins/labeler/editLabelTask.ftl

+[@ww.textfield labelKey="com.atlassian.bamboo.plugins.labeler.regex" name="regex" required='true'/]
+[@ww.textfield labelKey="com.atlassian.bamboo.plugins.labeler.labels" name="labels" required='true'/]

src/main/resources/com/atlassian/bamboo/plugins/labeler/i18n.properties

+com.atlassian.bamboo.plugins.labeler.regex = Regular expression
+com.atlassian.bamboo.plugins.labeler.regex.description = A regular expression describing a pattern that appears in builds to be labelled.
+com.atlassian.bamboo.plugins.labeler.regex.error = You did not specify a valid regular expression.
+
+com.atlassian.bamboo.plugins.labeler.labels = Labels
+com.atlassian.bamboo.plugins.labeler.labels.description = The label(s) to be applied to matching builds. A label of the form "\n" will be substituted with the nth capturing group from the pattern match
+com.atlassian.bamboo.plugins.labeler.labels.error = You did not specify any labels.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.