Last active
August 29, 2015 14:16
-
-
Save toolmantim/b464091caf36fea2b3d3 to your computer and use it in GitHub Desktop.
A middleware for apitools.com to control a LIFX build light using Buildkite webhooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return function(request, next_middleware) | |
local next_response = next_middleware() | |
-- Modify these | |
local lifx_access_token = "xxx" | |
local bulb_selector = "id:xxx" | |
local webhook_token = "xxx" | |
-- The rest is standard and doesn't need to be modified | |
if request.headers["X-Buildkite-Token"] ~= webhook_token then | |
return next_response | |
end | |
local event_name = request.headers["X-Buildkite-Event"] | |
local event = json.decode(request.body) | |
local lifx_params = { power_on = false } | |
if event_name == "build.running" then | |
lifx_params.color = "yellow brightness:5%" | |
lifx_params.from_color = "yellow brightness:35%" | |
lifx_params.period = 5 | |
lifx_params.cycles = 9999 | |
lifx_params.persist = true | |
elseif event_name == "build.finished" and event.build.state == "passed" then | |
lifx_params.color = "green brightness:75%" | |
lifx_params.from_color = "green brightness:10%" | |
lifx_params.period = 0.45 | |
lifx_params.cycles = 3 | |
lifx_params.persist = true | |
lifx_params.peak = 0.2 | |
elseif event_name == "build.finished" and event.build.state == "failed" then | |
lifx_params.color = "red brightness:60%" | |
lifx_params.from_color = "red brightness:25%" | |
lifx_params.period = 0.1 | |
lifx_params.cycles = 20 | |
lifx_params.persist = true | |
lifx_params.peak = 0.2 | |
end | |
local lifx_url = "https://api.lifx.com/v1beta1/lights/" .. bulb_selector .. "/effects/breathe.json" | |
local lifx_headers = { Authorization = "Bearer " .. lifx_access_token } | |
local lifx_response = http.json.post(lifx_url, json.encode(lifx_params), { headers = lifx_headers }) | |
console.log(lifx_response) | |
return next_response | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment