Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

init

  • Loading branch information...
commit 801a5178170c307e19c1964efef2137ae4140c05 0 parents
authored December 06, 2011
35  pom.xml
... ...
@@ -0,0 +1,35 @@
  1
+<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">
  2
+  <modelVersion>4.0.0</modelVersion>
  3
+  <parent>
  4
+    <groupId>org.jenkins-ci.plugins</groupId>
  5
+    <artifactId>plugin</artifactId>
  6
+    <version>1.399</version>
  7
+  </parent>
  8
+
  9
+  <artifactId>xmas</artifactId>
  10
+  <version>1.0-SNAPSHOT</version>
  11
+  <packaging>hpi</packaging>
  12
+  <name>Jenkins X&apos;mas plugin</name>
  13
+
  14
+  <dependencies>
  15
+    <dependency>
  16
+      <groupId>org.jenkins-ci.main</groupId>
  17
+      <artifactId>maven-plugin</artifactId>
  18
+      <version>1.399</version>
  19
+    </dependency>
  20
+  </dependencies>
  21
+
  22
+  <repositories>
  23
+    <repository>
  24
+      <id>m.g.o-public</id>
  25
+      <url>http://maven.glassfish.org/content/groups/public/</url>
  26
+    </repository>
  27
+  </repositories>
  28
+
  29
+  <pluginRepositories>
  30
+    <pluginRepository>
  31
+      <id>m.g.o-public</id>
  32
+      <url>http://maven.glassfish.org/content/groups/public/</url>
  33
+    </pluginRepository>
  34
+  </pluginRepositories>
  35
+</project>
74  src/main/java/org/jenkinsci/plugins/xmas/XmasBuildWrapper.java
... ...
@@ -0,0 +1,74 @@
  1
+package org.jenkinsci.plugins.xmas;
  2
+
  3
+import hudson.Extension;
  4
+import hudson.Launcher;
  5
+import hudson.model.BuildListener;
  6
+import hudson.model.AbstractBuild;
  7
+import hudson.model.AbstractProject;
  8
+import hudson.tasks.BuildWrapper;
  9
+import hudson.tasks.BuildWrapperDescriptor;
  10
+
  11
+import java.io.IOException;
  12
+import java.util.Calendar;
  13
+
  14
+import net.sf.json.JSONObject;
  15
+
  16
+import org.kohsuke.stapler.DataBoundConstructor;
  17
+import org.kohsuke.stapler.StaplerRequest;
  18
+
  19
+public class XmasBuildWrapper extends BuildWrapper {
  20
+
  21
+    private boolean force;
  22
+
  23
+    @DataBoundConstructor
  24
+    public XmasBuildWrapper(boolean force) {
  25
+        this.force = force;
  26
+    }
  27
+
  28
+    public boolean isForce() {
  29
+        return force;
  30
+    }
  31
+
  32
+    public void setForce(boolean force) {
  33
+        this.force = force;
  34
+    }
  35
+
  36
+    @Override
  37
+    public Environment setUp(@SuppressWarnings("rawtypes") AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
  38
+        return new BuildWrapper.Environment() {
  39
+            @Override
  40
+            public boolean tearDown(@SuppressWarnings("rawtypes") AbstractBuild build, BuildListener listener) throws IOException, InterruptedException {
  41
+                if (isForce() || isXmasWeek(Calendar.getInstance())) {
  42
+                    listener.getLogger().println("爆発しろ!");
  43
+                    return false;
  44
+                }
  45
+                return true;
  46
+            }
  47
+        };
  48
+    }
  49
+
  50
+    @Extension
  51
+    public static final class DescriptorImpl extends BuildWrapperDescriptor {
  52
+
  53
+        @Override
  54
+        public boolean isApplicable(AbstractProject<?, ?> item) {
  55
+            return true;
  56
+        }
  57
+
  58
+        @Override
  59
+        public String getDisplayName() {
  60
+            return "X'mas";
  61
+        }
  62
+
  63
+        @Override
  64
+        public BuildWrapper newInstance(StaplerRequest req, JSONObject formData) throws hudson.model.Descriptor.FormException {
  65
+            return req.bindJSON(clazz, formData);
  66
+        }
  67
+    }
  68
+
  69
+    public boolean isXmasWeek(Calendar calendar) {
  70
+        int month = calendar.get(Calendar.MONTH);
  71
+        int days = calendar.get(Calendar.DAY_OF_MONTH);
  72
+        return month == 11 && days >= 19 && days <= 25;
  73
+    }
  74
+}
58  src/main/java/org/jenkinsci/plugins/xmas/XmasItemListener.java
... ...
@@ -0,0 +1,58 @@
  1
+package org.jenkinsci.plugins.xmas;
  2
+
  3
+import hudson.Extension;
  4
+import hudson.XmlFile;
  5
+import hudson.init.InitMilestone;
  6
+import hudson.init.Initializer;
  7
+import hudson.maven.MavenModuleSet;
  8
+import hudson.model.Saveable;
  9
+import hudson.model.TopLevelItem;
  10
+import hudson.model.Descriptor;
  11
+import hudson.model.Hudson;
  12
+import hudson.model.Project;
  13
+import hudson.model.listeners.SaveableListener;
  14
+import hudson.tasks.BuildWrapper;
  15
+import hudson.util.DescribableList;
  16
+
  17
+import java.io.IOException;
  18
+import java.util.logging.Level;
  19
+import java.util.logging.Logger;
  20
+
  21
+@Extension
  22
+public class XmasItemListener extends SaveableListener {
  23
+
  24
+    private static final Logger LOGGER = Logger.getLogger(XmasItemListener.class.getName());
  25
+
  26
+    @Initializer(after=InitMilestone.JOB_LOADED)
  27
+    public static void init() {
  28
+        for (TopLevelItem item : Hudson.getInstance().getItems()) {
  29
+            addXmas(item);
  30
+        }
  31
+    }
  32
+
  33
+    @Override
  34
+    public void onChange(Saveable o, XmlFile file) {
  35
+        addXmas(o);
  36
+    }
  37
+
  38
+    @SuppressWarnings({ "unchecked", "rawtypes" })
  39
+    protected static void addXmas(Saveable o) {
  40
+        if (o instanceof Project) {
  41
+            addXmas(((Project) o).getBuildWrappersList());
  42
+        }
  43
+        else if (o instanceof MavenModuleSet) {
  44
+            addXmas(((MavenModuleSet) o).getBuildWrappersList());
  45
+        }
  46
+    }
  47
+
  48
+    @SuppressWarnings("unchecked")
  49
+    protected static void addXmas(DescribableList<BuildWrapper, Descriptor<BuildWrapper>> buildWrappers) {
  50
+        if (!buildWrappers.contains(Hudson.getInstance().getDescriptor(XmasBuildWrapper.class))) {
  51
+            try {
  52
+                buildWrappers.add(new XmasBuildWrapper(true));
  53
+            } catch (IOException e) {
  54
+                LOGGER.log(Level.SEVERE, e.getMessage(), e);
  55
+            }
  56
+        }
  57
+    }
  58
+}
3  src/main/resources/index.jelly
... ...
@@ -0,0 +1,3 @@
  1
+<div>
  2
+  We Wish You a Merry Christmas.
  3
+</div>
0  src/main/resources/org/jenkinsci/plugins/xmas/Messages.properties
No changes.
10  src/main/resources/org/jenkinsci/plugins/xmas/XmasBuildWrapper/config.jelly
... ...
@@ -0,0 +1,10 @@
  1
+<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
  2
+  <f:entry title="${%Force fail}" field="force">
  3
+    <f:checkbox id="xmas-force"/>
  4
+  </f:entry>
  5
+  <script>
  6
+  Behaviour.addLoadEvent(function() {
  7
+    document.getElementById('xmas-force').parentNode.parentNode.style.display = 'none'
  8
+  })
  9
+  </script>
  10
+</j:jelly>
6  src/main/webapp/help-globalConfig.html
... ...
@@ -0,0 +1,6 @@
  1
+<div>
  2
+  This HTML fragment will be injected into the configuration screen
  3
+  when the user clicks the 'help' icon. See global.jelly for how the
  4
+  form decides which page to load.
  5
+  You can have any <tt>HTML</tt> fragment here.
  6
+</div>
27  src/test/java/org/jenkinsci/plugins/xmas/XmasBuildWrapperTest.java
... ...
@@ -0,0 +1,27 @@
  1
+package org.jenkinsci.plugins.xmas;
  2
+
  3
+import hudson.model.FreeStyleBuild;
  4
+import hudson.model.Result;
  5
+import hudson.model.FreeStyleProject;
  6
+import hudson.tasks.Shell;
  7
+
  8
+import java.io.IOException;
  9
+import java.util.concurrent.ExecutionException;
  10
+
  11
+import org.apache.commons.io.FileUtils;
  12
+import org.jvnet.hudson.test.HudsonTestCase;
  13
+
  14
+public class XmasBuildWrapperTest extends HudsonTestCase {
  15
+
  16
+    public void testBuildShouldFail() throws IOException, InterruptedException, ExecutionException {
  17
+        FreeStyleProject project = createFreeStyleProject();
  18
+        project.getBuildersList().add(new Shell("echo hello"));
  19
+        project.getBuildWrappersList().replace(new XmasBuildWrapper(true));
  20
+
  21
+        FreeStyleBuild build = project.scheduleBuild2(0).get();
  22
+        assertEquals(Result.FAILURE, build.getResult());
  23
+
  24
+        String s = FileUtils.readFileToString(build.getLogFile());
  25
+        assertTrue(s.contains("爆発しろ!"));
  26
+    }
  27
+}
39  src/test/java/org/jenkinsci/plugins/xmas/XmasBuildWrapperUnitTest.java
... ...
@@ -0,0 +1,39 @@
  1
+package org.jenkinsci.plugins.xmas;
  2
+
  3
+import static org.junit.Assert.assertFalse;
  4
+import static org.junit.Assert.assertTrue;
  5
+
  6
+import java.text.ParseException;
  7
+import java.text.SimpleDateFormat;
  8
+import java.util.Calendar;
  9
+
  10
+import org.junit.Test;
  11
+
  12
+public class XmasBuildWrapperUnitTest {
  13
+
  14
+    @Test
  15
+    public void testIsXmas() {
  16
+        XmasBuildWrapper wrapper = new XmasBuildWrapper(false);
  17
+        assertFalse(wrapper.isXmasWeek(createCalendar("20111218")));
  18
+        assertTrue(wrapper.isXmasWeek(createCalendar("20111219")));
  19
+        assertTrue(wrapper.isXmasWeek(createCalendar("20111220")));
  20
+        assertTrue(wrapper.isXmasWeek(createCalendar("20111221")));
  21
+        assertTrue(wrapper.isXmasWeek(createCalendar("20111222")));
  22
+        assertTrue(wrapper.isXmasWeek(createCalendar("20111223")));
  23
+        assertTrue(wrapper.isXmasWeek(createCalendar("20111224")));
  24
+        assertTrue(wrapper.isXmasWeek(createCalendar("20111225")));
  25
+        assertFalse(wrapper.isXmasWeek(createCalendar("20111226")));
  26
+    }
  27
+
  28
+    private Calendar createCalendar(String yyyyMMdd) {
  29
+        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
  30
+        try {
  31
+            Calendar result = Calendar.getInstance();
  32
+            result.setTime(fmt.parse(yyyyMMdd));
  33
+            return result;
  34
+        } catch (ParseException e) {
  35
+            throw new IllegalArgumentException(yyyyMMdd, e);
  36
+        }
  37
+        
  38
+    }
  39
+}
25  src/test/java/org/jenkinsci/plugins/xmas/XmasItemListenerTest.java
... ...
@@ -0,0 +1,25 @@
  1
+package org.jenkinsci.plugins.xmas;
  2
+
  3
+import hudson.maven.MavenModuleSet;
  4
+import hudson.model.FreeStyleProject;
  5
+
  6
+import java.io.IOException;
  7
+
  8
+import org.jenkinsci.plugins.xmas.XmasBuildWrapper.DescriptorImpl;
  9
+import org.jvnet.hudson.test.HudsonTestCase;
  10
+import org.jvnet.hudson.test.recipes.LocalData;
  11
+
  12
+public class XmasItemListenerTest extends HudsonTestCase {
  13
+
  14
+    @LocalData
  15
+    public void testAddXmas_without_BuildWrappers() throws IOException {
  16
+        DescriptorImpl descriptor = (DescriptorImpl) hudson.getDescriptor(XmasBuildWrapper.class);
  17
+
  18
+        FreeStyleProject freeStyleProject = (FreeStyleProject) hudson.getItem("free");
  19
+        assertTrue(freeStyleProject.getBuildWrappersList().contains(descriptor));
  20
+
  21
+        MavenModuleSet mavenModuleSet = (MavenModuleSet) hudson.getItem("maven");
  22
+        assertTrue(mavenModuleSet.getBuildWrappersList().contains(descriptor));
  23
+    }
  24
+
  25
+}
27  ...t/resources/org/jenkinsci/plugins/xmas/XmasItemListenerTest/testAddXmas_without_BuildWrappers/config.xml
... ...
@@ -0,0 +1,27 @@
  1
+<?xml version='1.0' encoding='UTF-8'?>
  2
+<hudson>
  3
+  <version>1.399</version>
  4
+  <numExecutors>2</numExecutors>
  5
+  <mode>NORMAL</mode>
  6
+  <authorizationStrategy class="hudson.security.AuthorizationStrategy$Unsecured"/>
  7
+  <securityRealm class="hudson.security.SecurityRealm$None"/>
  8
+  <jdks/>
  9
+  <clouds/>
  10
+  <slaves/>
  11
+  <quietPeriod>5</quietPeriod>
  12
+  <scmCheckoutRetryCount>0</scmCheckoutRetryCount>
  13
+  <views>
  14
+    <hudson.model.AllView>
  15
+      <owner class="hudson" reference="../../.."/>
  16
+      <name>All</name>
  17
+      <filterExecutors>false</filterExecutors>
  18
+      <filterQueue>false</filterQueue>
  19
+    </hudson.model.AllView>
  20
+  </views>
  21
+  <primaryView>All</primaryView>
  22
+  <slaveAgentPort>0</slaveAgentPort>
  23
+  <label></label>
  24
+  <nodeProperties/>
  25
+  <globalNodeProperties/>
  26
+  <disabledAdministrativeMonitors/>
  27
+</hudson>
17  ...s/org/jenkinsci/plugins/xmas/XmasItemListenerTest/testAddXmas_without_BuildWrappers/jobs/free/config.xml
... ...
@@ -0,0 +1,17 @@
  1
+<?xml version='1.0' encoding='UTF-8'?>
  2
+<project>
  3
+  <actions/>
  4
+  <description></description>
  5
+  <keepDependencies>false</keepDependencies>
  6
+  <properties/>
  7
+  <scm class="hudson.scm.NullSCM"/>
  8
+  <canRoam>true</canRoam>
  9
+  <disabled>false</disabled>
  10
+  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  11
+  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  12
+  <triggers class="vector"/>
  13
+  <concurrentBuild>false</concurrentBuild>
  14
+  <builders/>
  15
+  <publishers/>
  16
+  <buildWrappers/>
  17
+</project>
23  .../org/jenkinsci/plugins/xmas/XmasItemListenerTest/testAddXmas_without_BuildWrappers/jobs/maven/config.xml
... ...
@@ -0,0 +1,23 @@
  1
+<?xml version='1.0' encoding='UTF-8'?>
  2
+<maven2-moduleset>
  3
+  <keepDependencies>false</keepDependencies>
  4
+  <properties/>
  5
+  <scm class="hudson.scm.NullSCM"/>
  6
+  <canRoam>false</canRoam>
  7
+  <disabled>false</disabled>
  8
+  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  9
+  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  10
+  <triggers class="vector"/>
  11
+  <concurrentBuild>false</concurrentBuild>
  12
+  <aggregatorStyleBuild>true</aggregatorStyleBuild>
  13
+  <incrementalBuild>false</incrementalBuild>
  14
+  <usePrivateRepository>false</usePrivateRepository>
  15
+  <ignoreUpstremChanges>false</ignoreUpstremChanges>
  16
+  <archivingDisabled>false</archivingDisabled>
  17
+  <resolveDependencies>false</resolveDependencies>
  18
+  <processPlugins>false</processPlugins>
  19
+  <mavenValidationLevel>-1</mavenValidationLevel>
  20
+  <reporters/>
  21
+  <publishers/>
  22
+  <buildWrappers/>
  23
+</maven2-moduleset>

0 notes on commit 801a517

Please sign in to comment.
Something went wrong with that request. Please try again.