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
|
# File 'lib/vanity/sources/github_issues.rb', line 22
def update(context, webhook, &block)
http = Net::HTTP.new("github.com", 443)
http.use_ssl = true
http.start do
update = {}
case response = request(http, context, "open")
when Net::HTTPOK
open = JSON.parse(response.body)["issues"] rescue nil
when Net::HTTPNotFound, Net::HTTPBadRequest
raise "Could not find the repository #{context["repo"]}"
when Net::HTTPUnauthorized
raise "You are not authorized to access this repository, or invalid username/password"
end
if open
update[:open] = open.count
if open_ids = context["open-ids"]
open.reject { |issue| open_ids.include?(issue["number"]) }.reverse.each do |issue|
sha = Digest::SHA1.hexdigest([context["repo"], "open", issue["number"], issue["updated_at"]].join(":"))
url = "http://github.com/#{context["repo"]}/issues#issue/#{issue["number"]}"
html = <<-HTML
opened <a href="#{url}">issue #{issue["number"]}</a> on #{context["repo"]}:
<blockquote>#{h issue["title"]}</blockquote>
HTML
block.call :activity=>{ :uid=>sha, :url=>url, :html=>html, :tags=>%w{issue opened},
:timestamp=>Time.parse(issue["created_at"]).utc }
end
end
context["open-ids"] = open.map { |issue| issue["number"] }
end
case response = request(http, context, "closed")
when Net::HTTPOK
closed = JSON.parse(response.body)["issues"] rescue nil
end
if closed
update[:closed] = closed.count
if closed_ids = context["closed-ids"]
closed.reject { |issue| closed_ids.include?(issue["number"]) }.reverse.each do |issue|
sha = Digest::SHA1.hexdigest([context["repo"], "closed", issue["number"], issue["updated_at"]].join(":"))
url = "http://github.com/#{context["repo"]}/issues#issue/#{issue["number"]}"
html = <<-HTML
closed <a href="#{url}">issue #{issue["number"]}</a> on #{context["repo"]}:
<blockquote>#{h issue["title"]}</blockquote>
HTML
block.call :activity=>{ :uid=>sha, :url=>url, :html=>html, :tags=>%w{issue closed},
:timestamp=>Time.parse(issue["closed_at"]).utc }
end
end
context["closed-ids"] = closed.map { |issue| issue["number"] }
end
raise "Last request didn't go as expected, trying again later" if update.empty?
block.call :set=>update
end
end
|