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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
#
# SonarQube, open source software quality management tool.
# Copyright (C) 2008-2013 SonarSource
# mailto:contact AT sonarsource DOT com
#
# SonarQube is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# SonarQube is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
class Api::EventsController < Api::ApiController
# GET /api/events?resource=xxx&category=xxx
# Examples :
# - global events : curl -v http://localhost:9000/api/events -u admin:admin
# - resource events : curl -v http://localhost:9000/api/events?resource=org.apache.struts:struts-parent
#
def index
begin
load_resource(:user, params[:resource])
conditions=[]
values={}
if params[:categories]
conditions<<'category IN (:categs)'
values[:categs]=params[:categories].split(',')
end
if @resource
conditions<<'resource_id=:rid'
values[:rid]=@resource.id
else
conditions<<'resource_id IS NULL'
end
from=nil
if params[:fromDateTime]
from=parse_datetime(params[:fromDateTime], false)
elsif params[:fromDate]
from=Date::strptime(params[:fromDate])
end
if from
conditions<<'event_date>=:from'
values[:from]=from
end
to=nil
if params[:toDateTime]
to=parse_datetime(params[:toDateTime], false)
elsif params[:toDate]
to=Date::strptime(params[:toDate])
end
if to
conditions<<'event_date<=:to'
values[:to]=to
end
events=Event.find(:all, :conditions => [conditions.join(' AND '), values], :order => 'event_date DESC')
respond_to do |format|
format.json { render :json => jsonp(events_to_json(events)) }
format.xml { render :xml => events_to_xml(events) }
format.text { render :text => text_not_supported }
end
rescue ApiException => e
render_error(e.msg, e.code)
rescue Exception => e
logger.error("Fails to execute #{request.url} : #{e.message}")
render_error(e.message)
end
end
# GET /api/events/:id
# Example : curl -v http://localhost:9000/api/events/3
def show
begin
event=Event.find(params[:id])
load_resource(:user, event.resource_id)
respond_to do |format|
format.json { render :json => jsonp(events_to_json([event])) }
format.xml { render :xml => events_to_xml([event]) }
format.text { render :text => text_not_supported }
end
rescue ActiveRecord::RecordNotFound => e
render_error(e.message, 404)
rescue ApiException => e
render_error(e.msg, e.code)
rescue Exception => e
logger.error("Fails to execute #{request.url} : #{e.message}")
render_error(e.message)
end
end
#
# POST /api/events
# Required parameters :
# - resource (id or key) - must be a root project.
# - name
# - category
#
# Optional parameters :
# - description
# - dateTime (ISO format : 2010-12-25T23:59:59+0100)
#
# Example :
# curl -d "name=foo&category=bar&dateTime=2010-12-25T23%3A59%3A59%2B0100" http://localhost:9000/api/events -v -u admin:admin
#
def create
begin
load_resource(:admin, params[:resource])
raise "Resource must be a root project" unless @resource.qualifier=='TRK'
root_snapshot=nil
if (params[:dateTime])
# try to find a snapshot on that day
date = parse_datetime(params[:dateTime], true)
root_snapshot = Snapshot.find(:last, :conditions => ["project_id = ? AND created_at >= ? AND created_at <= ?", @resource.id, date, date + 1.day], :order => :created_at)
raise "No snapshot exists for given date" unless root_snapshot
else
root_snapshot = Snapshot.find(:last, :conditions => ["project_id = ?", @resource.id], :order => :created_at)
end
raise "A version already exists on this resource." if params[:category]==EventCategory::KEY_VERSION && root_snapshot.event(EventCategory::KEY_VERSION)
raise "An event '#{params[:name]}' (category '#{params[:category]}') already exists on this resource." if Event.already_exists(@resource.last_snapshot.id, params[:name], params[:category])
# Create events for the root project and every submodule
event_to_return = nil
name = params[:name]
desc = params[:description]
category = params[:category]
snapshots = Snapshot.find(:all, :include => 'events', :conditions => ["(root_snapshot_id = ? OR id = ?) AND scope = 'PRJ'", root_snapshot.id, root_snapshot.id])
snapshots.each do |snapshot|
# if this is a 'Version' event, must propagate the version number to the snapshot
if category==EventCategory::KEY_VERSION
snapshot.version = name
snapshot.save!
end
# and then create the event linked to the updated snapshot
event=Event.new(
:name => name,
:description => desc,
:category => category,
:snapshot => snapshot,
:resource_id => snapshot.project_id,
:event_date => snapshot.created_at
)
event.save!
event_to_return = event if snapshot.project_id = @resource.id
end
respond_to do |format|
format.json { render :json => jsonp(events_to_json([event_to_return])) }
format.xml { render :xml => events_to_xml([event_to_return]) }
format.text { render :text => text_not_supported }
end
rescue ApiException => e
render_error(e.msg, e.code)
rescue Exception => e
render_error(e.message, 400)
end
end
# DELETE /api/events/1
# Example : curl -X DELETE http://localhost:9000/api/events/9 -v
def destroy
begin
event=Event.find(params[:id])
load_resource(:admin, event.resource_id)
events = []
name = event.name
category = event.category
description = event.description
snapshots = Snapshot.find(:all, :include => 'events', :conditions => ["(root_snapshot_id = ? OR id = ?) AND scope = 'PRJ'", event.snapshot_id, event.snapshot_id])
snapshots.each do |snapshot|
snapshot.events.reject {|e| e.name!=name || e.category!=category}.each do |event|
events << event
end
end
Event.delete(events.map {|e| e.id})
render_success("Event deleted")
rescue ActiveRecord::RecordNotFound => e
render_error(e.message, 404)
rescue ApiException => e
render_error(e.msg, e.code)
rescue Exception => e
logger.error("Fails to execute #{request.url} : #{e.message}")
render_error(e.message)
end
end
private
def load_resource(required_resource_role, resource_key=nil)
if resource_key
@resource=Project.by_key(resource_key)
if @resource.nil?
raise ApiException.new 404, "Resource not found: #{resource_key}"
end
unless has_role?(required_resource_role, @resource)
raise ApiException.new 401, "Unauthorized"
end
else
# global events
unless is_admin?
raise ApiException.new 401, "Unauthorized"
end
end
end
def events_to_json(events=[])
json=[]
events.each do |event|
json<<event_to_json(event)
end
json
end
def event_to_json(event)
hash={}
hash[:id]=event.id.to_s
hash[:rk]=event.resource.key if event.resource
hash[:n]=event.name if event.name
hash[:c]=event.category
hash[:dt]=Api::Utils.format_datetime(event.event_date) if event.event_date
hash[:ds]=event.description if event.description
hash
end
def events_to_xml(events, xml=Builder::XmlMarkup.new(:indent => 0))
xml.events do
events.each do |event|
event_to_xml(event, xml)
end
end
end
def event_to_xml(event, xml=Builder::XmlMarkup.new(:indent => 0))
xml.event do
xml.id(event.id.to_s)
xml.name(event.name) if event.name
xml.resourceKey(event.resource.key) if event.resource
xml.category(event.category)
xml.date(Api::Utils.format_datetime(event.event_date)) if event.event_date
xml.description(event.description) if event.description
end
end
end
|