From 4400ed4f88e06ec67851905a9d9ff6856aa6b35b Mon Sep 17 00:00:00 2001
From: Peter Liljenberg <pliljenberg@gmail.com>
Date: Mon, 27 Jan 2014 11:29:40 +0100
Subject: [PATCH] Added support for Jenkins Campfire plugin

  Basic setup of Campfire notifications from builds.

Change-Id: I4e0bc43a1583fe51e0510743844d66e82b184e23
---
 jenkins_jobs/modules/publishers.py         | 45 ++++++++++++++++++++++
 setup.py                                   |  1 +
 tests/publishers/fixtures/campfire001.xml  | 16 ++++++++
 tests/publishers/fixtures/campfire001.yaml |  6 +++
 tests/publishers/fixtures/campfire002.xml  |  8 ++++
 tests/publishers/fixtures/campfire002.yaml |  2 +
 tests/publishers/fixtures/campfire003.xml  | 16 ++++++++
 tests/publishers/fixtures/campfire003.yaml |  6 +++
 8 files changed, 100 insertions(+)
 create mode 100644 tests/publishers/fixtures/campfire001.xml
 create mode 100644 tests/publishers/fixtures/campfire001.yaml
 create mode 100644 tests/publishers/fixtures/campfire002.xml
 create mode 100644 tests/publishers/fixtures/campfire002.yaml
 create mode 100644 tests/publishers/fixtures/campfire003.xml
 create mode 100644 tests/publishers/fixtures/campfire003.yaml

diff --git a/jenkins_jobs/modules/publishers.py b/jenkins_jobs/modules/publishers.py
index 2ca3cd7a5..3855c575a 100644
--- a/jenkins_jobs/modules/publishers.py
+++ b/jenkins_jobs/modules/publishers.py
@@ -91,6 +91,51 @@ def blame_upstream(parser, xml_parent, data):
                    'BlameUpstreamCommitersPublisher')
 
 
+def campfire(parser, xml_parent, data):
+    """yaml: campfire
+    Send build notifications to Campfire rooms.
+    Requires the Jenkins `Campfire Plugin.
+    <https://wiki.jenkins-ci.org/display/JENKINS/Campfire+Plugin
+
+    Campfire notifications global default values must be configured for
+    the Jenkins instance. Default values will be used if no specific
+    values are specified for each job, so all config params are optional.
+
+    :arg str subdomain: override the default campfire subdomain
+    :arg str token: override the default API token
+    :arg bool ssl: override the default 'use SSL'
+    :arg str room: override the default room name
+
+    Example:
+
+    .. literalinclude::  /../../tests/publishers/fixtures/campfire001.yaml
+
+    """
+
+    root = XML.SubElement(xml_parent,
+                          'hudson.plugins.campfire.'
+                          'CampfireNotifier')
+
+    campfire = XML.SubElement(root, 'campfire')
+
+    if ('subdomain' in data and data['subdomain']):
+        subdomain = XML.SubElement(campfire, 'subdomain')
+        subdomain.text = data['subdomain']
+    if ('token' in data and data['token']):
+        token = XML.SubElement(campfire, 'token')
+        token.text = data['token']
+    if ('ssl' in data):
+        ssl = XML.SubElement(campfire, 'ssl')
+        ssl.text = str(data['ssl']).lower()
+
+    if ('room' in data and data['room']):
+        room = XML.SubElement(root, 'room')
+        name = XML.SubElement(room, 'name')
+        name.text = data['room']
+
+        XML.SubElement(room, 'campfire reference="../../campfire"')
+
+
 def emotional_jenkins(parser, xml_parent, data):
     """yaml: emotional-jenkins
     Emotional Jenkins.
diff --git a/setup.py b/setup.py
index f14a9a510..de05f6cbf 100644
--- a/setup.py
+++ b/setup.py
@@ -124,6 +124,7 @@ setuptools.setup(
             'blame-upstream=jenkins_jobs.modules.publishers:blame_upstream',
             'build-publisher=jenkins_jobs.modules.publishers:build_publisher',
             'checkstyle=jenkins_jobs.modules.publishers:checkstyle',
+            'campfire=jenkins_jobs.modules.publishers:campfire',
             'cifs=jenkins_jobs.modules.publishers:cifs',
             'claim-build=jenkins_jobs.modules.publishers:claim_build',
             'cloverphp=jenkins_jobs.modules.publishers:cloverphp',
diff --git a/tests/publishers/fixtures/campfire001.xml b/tests/publishers/fixtures/campfire001.xml
new file mode 100644
index 000000000..30d9ed799
--- /dev/null
+++ b/tests/publishers/fixtures/campfire001.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<project>
+  <publishers>
+    <hudson.plugins.campfire.CampfireNotifier>
+      <campfire>
+        <subdomain>sub</subdomain>
+        <token>TOKEN</token>
+        <ssl>true</ssl>
+      </campfire>
+      <room>
+        <name>room</name>
+        <campfire reference="../../campfire"/>
+      </room>
+    </hudson.plugins.campfire.CampfireNotifier>
+  </publishers>
+</project>
diff --git a/tests/publishers/fixtures/campfire001.yaml b/tests/publishers/fixtures/campfire001.yaml
new file mode 100644
index 000000000..32f2bba73
--- /dev/null
+++ b/tests/publishers/fixtures/campfire001.yaml
@@ -0,0 +1,6 @@
+publishers:
+    - campfire:
+        subdomain: 'sub'
+        ssl: true
+        token: 'TOKEN'
+        room: 'room'
diff --git a/tests/publishers/fixtures/campfire002.xml b/tests/publishers/fixtures/campfire002.xml
new file mode 100644
index 000000000..a73ff0109
--- /dev/null
+++ b/tests/publishers/fixtures/campfire002.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<project>
+  <publishers>
+    <hudson.plugins.campfire.CampfireNotifier>
+        <campfire/>
+    </hudson.plugins.campfire.CampfireNotifier>
+  </publishers>
+</project>
diff --git a/tests/publishers/fixtures/campfire002.yaml b/tests/publishers/fixtures/campfire002.yaml
new file mode 100644
index 000000000..a42acd778
--- /dev/null
+++ b/tests/publishers/fixtures/campfire002.yaml
@@ -0,0 +1,2 @@
+publishers:
+    - campfire
diff --git a/tests/publishers/fixtures/campfire003.xml b/tests/publishers/fixtures/campfire003.xml
new file mode 100644
index 000000000..431fb21cd
--- /dev/null
+++ b/tests/publishers/fixtures/campfire003.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<project>
+  <publishers>
+    <hudson.plugins.campfire.CampfireNotifier>
+      <campfire>
+        <subdomain>sub</subdomain>
+        <token>TOKEN</token>
+        <ssl>false</ssl>
+      </campfire>
+      <room>
+        <name>room</name>
+        <campfire reference="../../campfire"/>
+      </room>
+    </hudson.plugins.campfire.CampfireNotifier>
+  </publishers>
+</project>
diff --git a/tests/publishers/fixtures/campfire003.yaml b/tests/publishers/fixtures/campfire003.yaml
new file mode 100644
index 000000000..258b2b47f
--- /dev/null
+++ b/tests/publishers/fixtures/campfire003.yaml
@@ -0,0 +1,6 @@
+publishers:
+    - campfire:
+        subdomain: 'sub'
+        ssl: false
+        token: 'TOKEN'
+        room: 'room'