/
request.rb
199 lines (168 loc) · 4.86 KB
/
request.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
class Event::Request < ::Event::Base
self.description = 'Request was updated'
self.abstract_class = true
payload_keys :author, :comment, :description, :id, :actions, :state, :when, :who
def self.message_id(id)
domain = URI.parse(::Configuration.first.obs_url)
"obs-request-#{id}@#{domain.host.downcase}"
end
def my_message_id
Event::Request.message_id(payload['id'])
end
def originator
payload_address('who')
end
def custom_headers
mid = my_message_id
h = super
h['In-Reply-To'] = mid
h['References'] = mid
h['X-OBS-Request-Creator'] = payload['author']
h['X-OBS-Request-Id'] = payload['id']
h['X-OBS-Request-State'] = payload['state']
h.merge(headers_for_actions)
end
def headers_for_actions
ret = {}
payload['actions'].each_with_index do |a, index|
if payload['actions'].length == 1 || index == 0
suffix = 'X-OBS-Request-Action'
else
suffix = "X-OBS-Request-Action-#{index}"
end
ret[suffix + '-type'] = a['type']
if a['targetpackage']
ret[suffix + '-target'] = "#{a['targetproject']}/#{a['targetpackage']}"
elsif a['targetproject']
ret[suffix + '-target'] = a['targetproject']
end
if a['sourcepackage']
ret[suffix + '-source'] = "#{a['sourceproject']}/#{a['sourcepackage']}"
elsif a['sourceproject']
ret[suffix + '-source'] = a['sourceproject']
end
end
ret
end
def actions_summary
ret = []
payload['actions'].each do |a|
str = "#{a['type']} #{a['targetproject']}"
str += "/#{a['targetpackage']}" if a['targetpackage']
ret << str
end
ret.join(', ')
end
def calculate_diff(a)
return nil if a['type'] != 'submit'
raise 'We need action_id' unless a['action_id']
action = BsRequestAction.find a['action_id']
begin
action.sourcediff(view: nil, withissues: 0)
rescue BsRequestAction::DiffError
return nil # can't help
end
end
DiffLimit = 200
def payload_with_diff
ret = payload
payload['actions'].each do |a|
diff = calculate_diff(a)
next unless diff
diff = diff.lines
dl = diff.length
if dl > DiffLimit
diff = diff[0..DiffLimit]
diff << "[cut #{dl-DiffLimit} lines to limit mail size]"
end
a['diff'] = diff.join
end
ret
end
def reviewers
ret = []
BsRequest.find(payload['id']).reviews.each do |r|
ret.concat(r.users_for_review)
end
ret.uniq
end
def creators
[User.find_by_login(payload['author']).id]
end
def action_maintainers(prjname, pkgname)
ret = []
payload['actions'].each do |a|
ret.concat _maintainers(a[prjname], a[pkgname])
end
ret.uniq
end
def target_maintainers
action_maintainers('targetproject', 'targetpackage')
end
def source_maintainers
action_maintainers('sourceproject', 'sourcepackage')
end
end
class Event::RequestChange < Event::Request
self.raw_type = 'SRCSRV_REQUEST_CHANGE'
self.description = 'Request XML was updated (admin only)'
end
class Event::RequestCreate < Event::Request
self.raw_type = 'SRCSRV_REQUEST_CREATE'
self.description = 'Request created'
receiver_roles :source_maintainer, :target_maintainer
def custom_headers
base = super
# we're the one they mean
base.delete('In-Reply-To')
base.delete('References')
base.merge({'Message-ID' => my_message_id})
end
def subject
"Request #{payload['id']} created by #{payload['who']} (#{actions_summary})"
end
def expanded_payload
payload_with_diff
end
end
class Event::RequestDelete < Event::Request
self.raw_type = 'SRCSRV_REQUEST_DELETE'
self.description = 'Request was deleted (admin only)'
end
class Event::RequestStatechange < Event::Request
self.raw_type = 'SRCSRV_REQUEST_STATECHANGE'
self.description = 'Request state was changed'
payload_keys :oldstate
receiver_roles :source_maintainer, :target_maintainer, :creator, :reviewer
def subject
"Request #{payload['id']} changed to #{payload['state']} (#{actions_summary})"
end
end
class Event::ReviewWanted < Event::Request
self.description = 'Review was created'
payload_keys :reviewers, :by_user, :by_group, :by_project, :by_package
receiver_roles :reviewer
def subject
"Request #{payload['id']} requires review (#{actions_summary})"
end
def expanded_payload
payload_with_diff
end
def custom_headers
h = super
if payload['by_user']
h['X-OBS-Review-By_User'] = payload['by_user']
elsif payload['by_group']
h['X-OBS-Review-By_Group'] = payload['by_group']
elsif payload['by_package']
h['X-OBS-Review-By_Package'] = "#{payload['by_project']}/#{payload['by_package']}"
else
h['X-OBS-Review-By_Project'] = payload['by_project']
end
h
end
# for review_wanted we ignore all the other reviews
def reviewers
payload['reviewers']
end
end