-
Notifications
You must be signed in to change notification settings - Fork 0
/
lighthouse.rb
366 lines (311 loc) · 8.85 KB
/
lighthouse.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
require 'rubygems'
begin
require 'uri'
require 'addressable/uri'
module URI
def decode(*args)
Addressable::URI.decode(*args)
end
def escape(*args)
Addressable::URI.escape(*args)
end
def parse(*args)
Addressable::URI.parse(*args)
end
end
rescue LoadError
puts "Install the Addressable gem to support accounts with subdomains."
puts "# sudo gem install addressable"
puts
end
require 'activesupport'
require 'activeresource'
# Ruby lib for working with the Lighthouse API's XML interface.
# The first thing you need to set is the account name. This is the same
# as the web address for your account.
#
# Lighthouse.account = 'activereload'
#
# Then, you should set the authentication. You can either use your login
# credentials with HTTP Basic Authentication or with an API Tokens. You can
# find more info on tokens at http://lighthouseapp.com/help/using-beacons.
#
# # with basic authentication
# Lighthouse.authenticate('[email protected]', 'spacemonkey')
#
# # or, use a token
# Lighthouse.token = 'abcdefg'
#
# If no token or authentication info is given, you'll only be granted public access.
#
# This library is a small wrapper around the REST interface. You should read the docs at
# http://lighthouseapp.com/api.
#
module Lighthouse
class Error < StandardError; end
class << self
attr_accessor :email, :password, :host_format, :domain_format, :protocol, :port
attr_reader :account, :token
# Sets the account name, and updates all the resources with the new domain.
def account=(name)
resources.each do |klass|
klass.site = klass.site_format % (host_format % [protocol, domain_format % name, ":#{port}"])
end
@account = name
end
# Sets up basic authentication credentials for all the resources.
def authenticate(email, password)
@email = email
@password = password
end
# Sets the API token for all the resources.
def token=(value)
resources.each do |klass|
klass.headers['X-LighthouseToken'] = value
end
@token = value
end
def resources
@resources ||= []
end
end
self.host_format = '%s://%s%s'
self.domain_format = '%s.lighthouseapp.com'
self.protocol = 'http'
self.port = ''
class Base < ActiveResource::Base
def self.inherited(base)
Lighthouse.resources << base
class << base
attr_accessor :site_format
end
base.site_format = '%s'
super
end
end
# Find projects
#
# Lighthouse::Project.find(:all) # find all projects for the current account.
# Lighthouse::Project.find(44) # find individual project by ID
#
# Creating a Project
#
# project = Lighthouse::Project.new(:name => 'Ninja Whammy Jammy')
# project.save
# # => true
#
# Creating an OSS project
#
# project = Lighthouse::Project.new(:name => 'OSS Project')
# project.access = 'oss'
# project.license = 'mit'
# project.save
#
# OSS License Mappings
#
# 'mit' => "MIT License",
# 'apache-2-0' => "Apache License 2.0",
# 'artistic-gpl-2' => "Artistic License/GPLv2",
# 'gpl-2' => "GNU General Public License v2",
# 'gpl-3' => "GNU General Public License v3",
# 'lgpl' => "GNU Lesser General Public License"
# 'mozilla-1-1' => "Mozilla Public License 1.1"
# 'new-bsd' => "New BSD License",
# 'afl-3' => "Academic Free License v. 3.0"
#
# Updating a Project
#
# project = Lighthouse::Project.find(44)
# project.name = "Lighthouse Issues"
# project.public = false
# project.save
#
# Finding tickets
#
# project = Lighthouse::Project.find(44)
# project.tickets
#
class Project < Base
def tickets(options = {})
Ticket.find(:all, :params => options.update(:project_id => id))
end
def messages(options = {})
Message.find(:all, :params => options.update(:project_id => id))
end
def milestones(options = {})
Milestone.find(:all, :params => options.update(:project_id => id))
end
def bins(options = {})
Bin.find(:all, :params => options.update(:project_id => id))
end
def changesets(options = {})
Changeset.find(:all, :params => options.update(:project_id => id))
end
def memberships(options = {})
ProjectMembership.find(:all, :params => options.update(:project_id => id))
end
def tags(options = {})
TagResource.find(:all, :params => options.update(:project_id => id))
end
end
class User < Base
def memberships(options = {})
Membership.find(:all, :params => {:user_id => id})
end
end
class Membership < Base
site_format << '/users/:user_id'
def save
raise Error, "Cannot modify memberships from the API"
end
end
class ProjectMembership < Base
self.element_name = 'membership'
site_format << '/projects/:project_id'
def url
respond_to?(:account) ? account : project
end
def save
raise Error, "Cannot modify memberships from the API"
end
end
class Token < Base
def save
raise Error, "Cannot modify Tokens from the API"
end
end
# Find tickets
#
# Lighthouse::Ticket.find(:all, :params => { :project_id => 44 })
# Lighthouse::Ticket.find(:all, :params => { :project_id => 44, :q => "state:closed tagged:committed" })
#
# project = Lighthouse::Project.find(44)
# project.tickets
# project.tickets(:q => "state:closed tagged:committed")
#
# Creating a Ticket
#
# ticket = Lighthouse::Ticket.new(:project_id => 44)
# ticket.title = 'asdf'
# ...
# ticket.tags << 'ruby' << 'rails' << '@high'
# ticket.save
#
# Updating a Ticket
#
# ticket = Lighthouse::Ticket.find(20, :params => { :project_id => 44 })
# ticket.state = 'resolved'
# ticket.tags.delete '@high'
# ticket.save
#
class Ticket < Base
attr_writer :tags
site_format << '/projects/:project_id'
def id
attributes['number'] ||= nil
number
end
def tags
attributes['tag'] ||= nil
@tags ||= tag.blank? ? [] : parse_with_spaces(tag)
end
def body
attributes['body'] ||= ''
end
def body=(value)
attributes['body'] = value
end
def body_html
attributes['body_html'] ||= ''
end
def body_html=(value)
attributes['body_html'] = value
end
def save_with_tags
self.tag = @tags.collect do |tag|
tag.include?(' ') ? tag.inspect : tag
end.join(" ") if @tags.is_a?(Array)
@tags = nil ; save_without_tags
end
alias_method_chain :save, :tags
private
# taken from Lighthouse Tag code
def parse_with_spaces(list)
tags = []
# first, pull out the quoted tags
list.gsub!(/\"(.*?)\"\s*/ ) { tags << $1; "" }
# then, get whatever's left
tags.concat list.split(/\s/)
cleanup_tags(tags)
end
def cleanup_tags(tags)
returning tags do |tag|
tag.collect! do |t|
unless tag.blank?
t = Tag.new(t)
t.downcase!
t.gsub! /(^')|('$)/, ''
t.gsub! /[^a-z0-9 \-_@\!']/, ''
t.strip!
t.prefix_options = prefix_options
t
end
end
tag.compact!
tag.uniq!
end
end
end
class Message < Base
site_format << '/projects/:project_id'
end
class Milestone < Base
site_format << '/projects/:project_id'
def tickets(options = {})
Ticket.find(:all, :params => options.merge(prefix_options).update(:q => %{milestone:"#{title}"}))
end
end
class Bin < Base
site_format << '/projects/:project_id'
def tickets(options = {})
Ticket.find(:all, :params => options.merge(prefix_options).update(:q => query))
end
end
class Changeset < Base
site_format << '/projects/:project_id'
end
class Change < Array; end
class TagResource < Base
self.element_name = 'tag'
site_format << '/projects/:project_id'
def name
@name ||= Tag.new(attributes['name'], prefix_options[:project_id])
end
def tickets(options = {})
name.tickets(options)
end
end
class Tag < String
attr_writer :prefix_options
attr_accessor :project_id
def initialize(s, project_id)
@project_id = project_id
super(s)
end
def prefix_options
@prefix_options || {}
end
def tickets(options = {})
options[:project_id] ||= @project_id
Ticket.find(:all, :params => options.merge(prefix_options).update(:q => %{tagged:"#{self}"}))
end
end
end
# module ActiveResource
# class Connection
# private
# def authorization_header
# (Lighthouse.email || Lighthouse.password ? { 'Authorization' => 'Basic ' + ["#{Lighthouse.email}:#{Lighthouse.password}"].pack('m').delete("\r\n") } : {})
# end
# end
# end