blob: 73048fe388f10c7e97e4884aa26e63d38b5565d4 [file] [log] [blame]
Jakub Josefa6d4c832017-01-19 14:06:12 +01001import logging
2logger = logging.getLogger(__name__)
3
4config_slack_groovy = """\
5jenkins = jenkins.model.Jenkins.getInstance()
6try{{
7slack = jenkins.getDescriptorByType(jenkins.plugins.slack.SlackNotifier.DescriptorImpl)
8if(slack.teamDomain.equals("{team_domain}") &&
9 slack.token.equals("{token}") &&
10 slack.tokenCredentialId.equals("{token_credential_id}") &&
11 slack.room.equals("{room}") &&
12 slack.sendAs.equals("{send_as}")){{
13 print("EXISTS")
14}}else{{
15 slack.teamDomain = "{team_domain}"
16 slack.token = "{token}"
17 slack.tokenCredentialId = "{token_credential_id}"
18 slack.room = "{room}"
19 slack.sendAs = "{send_as}"
20 slack.save()
21 print("SUCCESS")
22}}
23}}catch(all){{
24 print("Cannot instantiate Jenkins Slack plugin, maybe plugin is not installed")
25}}
26""" # noqa
27
28def config(name, team_domain, token, token_credential_id="", room="", send_as=None):
29 """
30 Jenkins Slack config state method
31
32 :param name: configuration name
33 :param team_domain: slack team domain
34 :param token: slack token
35 :param token_credential_id: slack token credential id
36 :param room: slack room
37 :param send_as: slack send as param
38 :returns: salt-specified state dict
39 """
40 test = __opts__['test'] # noqa
41 ret = {
42 'name': name,
43 'changes': {},
44 'result': False,
45 'comment': '',
46 }
47 result = False
48 if test:
49 status = "SUCCESS"
50 ret['changes'][name] = status
51 ret['comment'] = 'Jenkins Slack config %s %s' % (name, status.lower())
52 else:
53 call_result = __salt__['jenkins_common.call_groovy_script'](
54 config_slack_groovy, {"team_domain":team_domain,
55 "token":token,
56 "token_credential_id": token_credential_id if token_credential_id else "",
57 "room": room if room else "",
58 "send_as": send_as if send_as else ""})
59 if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
60 status = call_result["msg"]
61 if status == "SUCCESS":
62 ret['changes'][name] = status
63 ret['comment'] = 'Jenkins Slack config %s %s' % (name, status.lower())
64 result = True
65 else:
66 status = 'FAILED'
67 logger.error(
68 "Jenkins slack API call failure: %s", call_result["msg"])
69 ret['comment'] = 'Jenkins slack API call failure: %s' % (call_result[
70 "msg"])
71 ret['result'] = None if test else result
72 return ret