Class: Vanity::Source::Github

Inherits:
Object
  • Object
show all
Includes:
Vanity::Source
Defined in:
lib/vanity/sources/github.rb

Overview

Track Github commits, watchers and forks.

Instance Method Summary collapse

Methods included from Vanity::Source

all, #description, #display, find, included, load_sources, #name, #register, #unregister

Instance Method Details

#meta(context) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/vanity/sources/github.rb', line 97

def meta(context)
  meta = [ { :title=>"Repository", :text=>context["repo"], :url=>context["url"] },
           { :title=>"Branch", :text=>context["branch"] },
           { :text=>context["description"] },
           { :title=>"Home page", :url=>context["homepage"] } ]
  if last_commit = context["last_commit"]
    meta << { :title=>"Commit", :text=>last_commit["message"], :url=>last_commit["url"] }
  end
  meta
end

#request(http, context, path) ⇒ Object



91
92
93
94
95
# File 'lib/vanity/sources/github.rb', line 91

def request(http, context, path)
  get = Net::HTTP::Get.new(path.gsub(":repo", URI.escape(context["repo"])).gsub(":branch", URI.escape(context["branch"])))
  get.basic_auth "#{context["username"]}/token", context["api_token"] unless context["username"].blank? && context["api_token"].blank?
  http.request(get)
end

#setup(context, params) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/vanity/sources/github.rb', line 7

def setup(context, params)
  repo = params["repo"].strip
  context["metric.name"] = "Github: #{repo}"
  context["metric.columns"] = [{ :id=>"commits", :label=>"Commits" }, { :id=>"watchers", :label=>"Watchers" }, { :id=>"forks", :label=>"Forks" }]
  context["metric.totals"] = true
  context["repo"] = repo
  branch = params["branch"].strip
  context["branch"] = branch.blank? ? "master" : branch
  context["username"] = params["username"]
  context["api_token"] = params["api_token"]
end

#update(context, webhook, &block) ⇒ Object



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

#validate(context) ⇒ Object



19
20
21
22
# File 'lib/vanity/sources/github.rb', line 19

def validate(context)
  raise "Missing repository name" if context["repo"].blank?
  raise "Need user name and repository name, e.g. assaf/vanity" unless context["repo"][/^[\w-]+\/[\w-]+$/]
end