Update sonos-music-modes.groovy
[smartapps.git] / official / jenkins-notifier.groovy
1 /*
2  * Copyright (C) 2014 Andrew Reitz
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * Jenkins Notifier
19  *
20  * Checks a Jenkins server at a specific time, if the build fails it will turn on a light.  If the build goes from
21  * failing back to succeeding the light will turn off. Hues can also be used in place of the light in order to create
22  * colors for build statuses
23  */
24
25 // Automatically generated. Make future change here.
26 definition(
27     name: "Jenkins Notifier",
28     namespace: "com.andrewreitz",
29     author: "aj.reitz@gmail.com",
30     description: "Turn off and on devices based on the state that your Jenkins Build is in.",
31     category: "Fun & Social",
32     iconUrl: "http://i.imgur.com/tyIp8wQ.jpg",
33     iconX2Url: "http://i.imgur.com/tyIp8wQ.jpg"
34 )
35
36 preferences {
37     section("The URL to your Jenkins, including the job you want to monitor. Ex. https://jenkins.example.com/job/myproject/") {
38         input "jenkinsUrl", "text", title: "Jenkins URL"
39     }
40     section("Jenkins Username") {
41         input "jenkinsUsername", "text", title: "Jenkins Username"
42     }
43     section("Jenkins Password") {
44         input "jenkinsPassword", "password", title: "Jenkins Password"
45     }
46     section("On Failed Build Turn On...") {
47         input "switches", "capability.switch", multiple: true, required: false
48     }
49     section("Or Change These Bulbs...") {
50         input "hues", "capability.colorControl", title: "Which Hue Bulbs?", required: false, multiple: true
51         input "colorSuccess", "enum", title: "Hue Color On Success?", required: false, multiple: false, options: getHueColors().keySet() as String[]
52         input "colorFail", "enum", title: "Hue Color On Fail?", required: false, multiple: false, options: getHueColors().keySet() as String[]
53         input "lightLevelSuccess", "number", title: "Light Level On Success?", required: false
54         input "lightLevelFail", "number", title: "Light Level On Fail?", required: false
55     }
56     section("Additional settings", hideable: true, hidden: true) {
57         paragraph("Default check time is 15 Minutes")
58         input "refreshInterval", "decimal", title: "Check Server... (minutes)",
59                 description: "Enter time in minutes", defaultValue: 15, required: false
60     }
61 }
62
63 def installed() {
64     log.debug "Installed with settings: ${settings}"
65     initialize()
66 }
67
68 def updated() {
69     log.debug "Updated with settings: ${settings}"
70     unsubscribe()
71     initialize()
72 }
73
74 /** Constants for Hue Colors */
75 Map getHueColors() {
76     return [Red: 0, Green: 39, Blue: 70, Yellow: 25, Orange: 10, Purple: 75, Pink: 83]
77 }
78
79 /** Constant for Saturation */
80 int getSaturation() {
81     return 100;
82 }
83
84 /** Constant for Level */
85 int getMaxLevel() {
86     return 100;
87 }
88
89 def initialize() {
90     def successColor = [switch: "on", hue: getHueColors()[colorSuccess], saturation: getSaturation(), level: lightLevelSuccess ?: getMaxLevel()]
91     def failColor = [switch: "on", hue: getHueColors()[colorFail], saturation: getSaturation(), level: lightLevelFail ?: getMaxLevel()]
92     state.successColor = successColor
93     state.failColor = failColor
94     log.debug "successColor: ${successColor}, failColor: ${failColor}"
95     
96     checkServer()
97     
98     def cron = "* */${refreshInterval ?: 15} * * * ?"
99     schedule(cron, checkServer)
100 }
101
102 def checkServer() {
103     log.debug "Checking Server Now"
104
105         def successColor = state.successColor
106     def failColor = state.failColor
107
108     def basicCredentials = "${jenkinsUsername}:${jenkinsPassword}"
109     def encodedCredentials = basicCredentials.encodeAsBase64().toString()
110     def basicAuth = "Basic ${encodedCredentials}"
111
112     def head = ["Authorization": basicAuth]
113
114     log.debug "Auth ${head}"
115
116         def host = jenkinsUrl.contains("lastBuild/api/json") ? jenkinsUrl : "${jenkinsUrl}/lastBuild/api/json"
117
118     httpGet(uri: host, headers: ["Authorization": "${basicAuth}"]) { resp ->
119         def buildError = (resp.data.result == "FAILURE")
120         def buildSuccess = (resp.data.result == "SUCCESS")
121         log.debug "Build Success? ${buildSuccess}"
122         if (buildError) {
123             switches?.on()
124             hues?.setColor(failColor)
125         } else if (buildSuccess) {
126             switches?.off()
127             hues?.setColor(successColor)
128         } // else in some other state, probably building, do nothing.
129
130     }
131 }