Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

Commit 2051bd4

Browse files
author
Kim Neunert
committed
jenkins-notifier: added notification-stratgey-feature
1 parent a140d25 commit 2051bd4

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

src/scripts/jenkins-notifier.coffee

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,42 @@
1212
# None
1313
#
1414
# URLS:
15-
# POST /hubot/jenkins-notify?room=<room>[&type=<type>]
15+
# POST /hubot/jenkins-notify?room=<room>[&type=<type>][notstrat=<notificationSTrategy>]
1616
#
17+
# Notification Strategy is [Ff][Ss] which stands for "Failure" and "Success"
18+
# Capitalized letter means: notify always
19+
# small letter means: notify only if buildstatus has changed
20+
# "Fs" is the default
21+
#
1722
# Authors:
1823
# spajus
24+
# k9ert (notification strategy feature)
1925

2026
url = require('url')
2127
querystring = require('querystring')
2228

29+
buildStatusChanged = (data, @failing) ->
30+
if data.build.status == 'FAILURE' and data.name in @failing
31+
return false
32+
if data.build.status == 'FAILURE' and not (data.name in @failing)
33+
return true
34+
if data.build.status == 'SUCCESS' and data.name in @failing
35+
return true
36+
if data.build.status == 'SUCCESS' and not (data.name in @failing)
37+
return false
38+
console.log "this should not happen"
39+
40+
shouldNotify = (notstrat, data, @failing) ->
41+
if data.build.status == 'FAILURE'
42+
if /F/.test(notstrat)
43+
return true
44+
return buildStatusChanged(data, @failing)
45+
if data.build.status == 'SUCCESS'
46+
if /S/.test(notstrat)
47+
return true
48+
return buildStatusChanged(data, @failing)
49+
50+
2351
module.exports = (robot) ->
2452

2553
robot.router.post "/hubot/jenkins-notify", (req, res) ->
@@ -29,28 +57,33 @@ module.exports = (robot) ->
2957

3058
res.end('')
3159

32-
envelope = {}
60+
envelope = {notstrat:"Fs"}
3361
envelope.user = {}
3462
envelope.room = query.room if query.room
3563
envelope.user.type = query.type if query.type
36-
64+
envelope.notstrat = query.notstrat if query.notstrat
3765
try
3866
for key of req.body
3967
data = JSON.parse key
4068

4169
if data.build.phase == 'FINISHED'
70+
robot.send envelope, data.name
4271
if data.build.status == 'FAILURE'
4372
if data.name in @failing
4473
build = "is still"
4574
else
4675
build = "started"
47-
robot.send envelope, "#{data.name} build ##{data.build.number} #{build} failing (#{encodeURI(data.build.full_url)})"
76+
robot.send envelope, "#{data.name} build ##{data.build.number} #{build} failing (#{encodeURI(data.build.full_url)})" if shouldNotify(envelope.notstrat, data, @failing)
4877
@failing.push data.name unless data.name in @failing
4978
if data.build.status == 'SUCCESS'
5079
if data.name in @failing
51-
index = @failing.indexOf data.name
52-
@failing.splice index, 1 if index isnt -1
53-
robot.send envelope, "#{data.name} build was restored ##{data.build.number} (#{encodeURI(data.build.full_url)})"
80+
build = "was restored"
81+
robot.send envelope, "woop2"
82+
else
83+
build = "suceeded"
84+
robot.send envelope, "#{data.name} build ##{data.build.number} #{build} (#{encodeURI(data.build.full_url)})" if shouldNotify(envelope.notstrat, data, @failing)
85+
index = @failing.indexOf data.name
86+
@failing.splice index, 1 if index isnt -1
5487

5588
catch error
5689
console.log "jenkins-notify error: #{error}. Data: #{req.body}"

0 commit comments

Comments
 (0)