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
|
# File 'lib/vanity/sources/github.rb', line 24
def update(context, webhook, &block)
http = Net::HTTP.new("github.com", 443)
http.use_ssl = true
http.start do
case response = request(http, context, "/api/v2/json/repos/show/:repo")
when Net::HTTPOK
json = JSON.parse(response.body)["repository"] 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 json
context.update json.slice(*%w{description url homepage})
block.call :set=>{ :forks=>json["forks"], :watchers=>json["watchers"] }
else
logger.error "Backtweets: #{response.code} #{response.message}"
error = true
end
case response = request(http, context, "/api/v2/json/commits/list/:repo/:branch")
when Net::HTTPOK
commits = JSON.parse(response.body)["commits"] rescue nil
when Net::HTTPNotFound, Net::HTTPBadRequest
raise "Could not find the branch #{context["branch"]}"
end
if commits
last_seen_id = context["last_commit"]["id"] if context["last_commit"]
if last_seen_id
new_ones = commits.take_while { |commit| commit["id"] != last_seen_id }
merged = new_ones.inject([]) do |all, commit|
last = all.last.last unless all.empty?
if last && last["committer"]["email"] == commit["committer"]["email"] && last["committed_date"] == commit["committed_date"]
all.last << commit
else
all << [commit]
end
all
end
merged.reverse.each do |commits|
first = commits.first
committer = first["committer"]
person = { :fullname=>committer["name"], :identities=>%W{github.com:#{committer["name"]}}, :email=>committer["email"] }
messages = commits.map { |commit| %{<blockquote><a href="#{commit["url"]}">#{commit["id"][0,7]}</a> #{h commit["message"].strip.split(/[\n\r]/).first[0,50]}</blockquote>} }
html = %{pushed to #{h context["branch"]} at <a href="http://github.com/#{context["repo"]}">#{h context["repo"]}</a>:\n#{messages.join("\n")}}
block.call :activity=>{ :uid=>first["id"], :html=>html, :url=>first["url"], :tags=>%w{push},
:timestamp=>Time.parse(first["committed_date"]).utc, :person=>person }
end
block.call :inc=>{ :commits=>new_ones.count }
else
block.call :set=>{ :commits=>commits.count }
end
if last_commit = commits.first
context.update "last_commit"=>last_commit.slice("id", "message", "url").
merge("timestamp"=>Time.parse(last_commit["committed_date"]).utc)
end
else
logger.error "Backtweets: #{response.code} #{response.message}"
error = true
end
raise "Last request didn't go as expected, trying again later" if error
end
end
|