12
12
# None
13
13
#
14
14
# URLS:
15
- # POST /hubot/jenkins-notify?room=<room>[&type=<type>]
15
+ # POST /hubot/jenkins-notify?room=<room>[&type=<type>][notstrat=<notificationSTrategy>]
16
16
#
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
+ #
17
22
# Authors:
18
23
# spajus
24
+ # k9ert (notification strategy feature)
19
25
20
26
url = require (' url' )
21
27
querystring = require (' querystring' )
22
28
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
+
23
51
module .exports = (robot ) ->
24
52
25
53
robot .router .post " /hubot/jenkins-notify" , (req , res ) ->
@@ -29,28 +57,33 @@ module.exports = (robot) ->
29
57
30
58
res .end (' ' )
31
59
32
- envelope = {}
60
+ envelope = {notstrat : " Fs " }
33
61
envelope .user = {}
34
62
envelope .room = query .room if query .room
35
63
envelope .user .type = query .type if query .type
36
-
64
+ envelope . notstrat = query . notstrat if query . notstrat
37
65
try
38
66
for key of req .body
39
67
data = JSON .parse key
40
68
41
69
if data .build .phase == ' FINISHED'
70
+ robot .send envelope, data .name
42
71
if data .build .status == ' FAILURE'
43
72
if data .name in @failing
44
73
build = " is still"
45
74
else
46
75
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 )
48
77
@failing .push data .name unless data .name in @failing
49
78
if data .build .status == ' SUCCESS'
50
79
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
54
87
55
88
catch error
56
89
console .log " jenkins-notify error: #{ error} . Data: #{ req .body } "
0 commit comments