2 * Copyright (C) 2014 Andrew Reitz
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
25 // Automatically generated. Make future change here.
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"
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"
40 section("Jenkins Username") {
41 input "jenkinsUsername", "text", title: "Jenkins Username"
43 section("Jenkins Password") {
44 input "jenkinsPassword", "password", title: "Jenkins Password"
46 section("On Failed Build Turn On...") {
47 input "switches", "capability.switch", multiple: true, required: false
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
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
64 log.debug "Installed with settings: ${settings}"
69 log.debug "Updated with settings: ${settings}"
74 /** Constants for Hue Colors */
76 return [Red: 0, Green: 39, Blue: 70, Yellow: 25, Orange: 10, Purple: 75, Pink: 83]
79 /** Constant for Saturation */
84 /** Constant for Level */
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}"
98 def cron = "* */${refreshInterval ?: 15} * * * ?"
99 schedule(cron, checkServer)
103 log.debug "Checking Server Now"
105 def successColor = state.successColor
106 def failColor = state.failColor
108 def basicCredentials = "${jenkinsUsername}:${jenkinsPassword}"
109 def encodedCredentials = basicCredentials.encodeAsBase64().toString()
110 def basicAuth = "Basic ${encodedCredentials}"
112 def head = ["Authorization": basicAuth]
114 log.debug "Auth ${head}"
116 def host = jenkinsUrl.contains("lastBuild/api/json") ? jenkinsUrl : "${jenkinsUrl}/lastBuild/api/json"
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}"
124 hues?.setColor(failColor)
125 } else if (buildSuccess) {
127 hues?.setColor(successColor)
128 } // else in some other state, probably building, do nothing.