Implemented Jenkins Slack plugin configuration.
diff --git a/README.rst b/README.rst
index 5c489d0..0631c4d 100644
--- a/README.rst
+++ b/README.rst
@@ -511,6 +511,19 @@
approved_scripts:
- method groovy.json.JsonSlurperClassic parseText java.lang.String
+Slack plugin configuration
+
+.. code-block:: yaml
+
+ jenkins:
+ client:
+ slack:
+ team_domain: example.com
+ token: slack-token
+ room: slack-room
+ token_credential_id: cred_id
+ send_as: Some slack user
+
Users enforcing from master
diff --git a/_states/jenkins_slack.py b/_states/jenkins_slack.py
new file mode 100644
index 0000000..73048fe
--- /dev/null
+++ b/_states/jenkins_slack.py
@@ -0,0 +1,72 @@
+import logging
+logger = logging.getLogger(__name__)
+
+config_slack_groovy = """\
+jenkins = jenkins.model.Jenkins.getInstance()
+try{{
+slack = jenkins.getDescriptorByType(jenkins.plugins.slack.SlackNotifier.DescriptorImpl)
+if(slack.teamDomain.equals("{team_domain}") &&
+ slack.token.equals("{token}") &&
+ slack.tokenCredentialId.equals("{token_credential_id}") &&
+ slack.room.equals("{room}") &&
+ slack.sendAs.equals("{send_as}")){{
+ print("EXISTS")
+}}else{{
+ slack.teamDomain = "{team_domain}"
+ slack.token = "{token}"
+ slack.tokenCredentialId = "{token_credential_id}"
+ slack.room = "{room}"
+ slack.sendAs = "{send_as}"
+ slack.save()
+ print("SUCCESS")
+}}
+}}catch(all){{
+ print("Cannot instantiate Jenkins Slack plugin, maybe plugin is not installed")
+}}
+""" # noqa
+
+def config(name, team_domain, token, token_credential_id="", room="", send_as=None):
+ """
+ Jenkins Slack config state method
+
+ :param name: configuration name
+ :param team_domain: slack team domain
+ :param token: slack token
+ :param token_credential_id: slack token credential id
+ :param room: slack room
+ :param send_as: slack send as param
+ :returns: salt-specified state dict
+ """
+ test = __opts__['test'] # noqa
+ ret = {
+ 'name': name,
+ 'changes': {},
+ 'result': False,
+ 'comment': '',
+ }
+ result = False
+ if test:
+ status = "SUCCESS"
+ ret['changes'][name] = status
+ ret['comment'] = 'Jenkins Slack config %s %s' % (name, status.lower())
+ else:
+ call_result = __salt__['jenkins_common.call_groovy_script'](
+ config_slack_groovy, {"team_domain":team_domain,
+ "token":token,
+ "token_credential_id": token_credential_id if token_credential_id else "",
+ "room": room if room else "",
+ "send_as": send_as if send_as else ""})
+ if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
+ status = call_result["msg"]
+ if status == "SUCCESS":
+ ret['changes'][name] = status
+ ret['comment'] = 'Jenkins Slack config %s %s' % (name, status.lower())
+ result = True
+ else:
+ status = 'FAILED'
+ logger.error(
+ "Jenkins slack API call failure: %s", call_result["msg"])
+ ret['comment'] = 'Jenkins slack API call failure: %s' % (call_result[
+ "msg"])
+ ret['result'] = None if test else result
+ return ret
diff --git a/jenkins/client/init.sls b/jenkins/client/init.sls
index 8ec9b7f..2017ca7 100644
--- a/jenkins/client/init.sls
+++ b/jenkins/client/init.sls
@@ -29,6 +29,9 @@
{%- if client.smtp is defined %}
- jenkins.client.smtp
{%- endif %}
+{%- if client.slack is defined %}
+ - jenkins.client.slack
+{%- endif %}
jenkins_client_install:
pkg.installed:
diff --git a/jenkins/client/slack.sls b/jenkins/client/slack.sls
new file mode 100644
index 0000000..ed71773
--- /dev/null
+++ b/jenkins/client/slack.sls
@@ -0,0 +1,10 @@
+{%- from "jenkins/map.jinja" import client with context %}
+{%- if client.slack is defined %}
+config_jenkins_slack:
+ jenkins_slack.config:
+ - team_domain: {{ client.slack.team_domain }}
+ - token: {{ client.slack.token }}
+ - token_credential_id: {{ client.slack.get('token_credential_id','') }}
+ - send_as: {{ client.slack.get('send_as','') }}
+ - room: {{ client.slack.get('room', '') }}
+{%- endif %}