Class: DashFu::Mario::Github

Inherits:
Object
  • Object
show all
Includes:
DashFu::Mario
Defined in:
lib/dash-fu/marios/github.rb

Overview

Track Github commits, watchers and forks.

Instance Method Summary collapse

Methods included from DashFu::Mario

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

Instance Method Details

#meta(source) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/dash-fu/marios/github.rb', line 90

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

#setup(source, params) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/dash-fu/marios/github.rb', line 6

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

#update(source, request, &block) ⇒ Object



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
# File 'lib/dash-fu/marios/github.rb', line 23

def update(source, request, &block)
  http = Net::HTTP.new("github.com", 443)
  http.use_ssl = true
  http.start do
    case response = http_request(http, source, "/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 #{source["repo"]}"
    when Net::HTTPUnauthorized
      raise "You are not authorized to access this repository, or invalid username/password"
    end
    if json
      source.update json.slice(*%w{description url homepage})
      block.call :set=>{ :forks=>json["forks"], :watchers=>json["watchers"] }
    else
      logger.error "Github: #{response.code} #{response.message}"
      error = true
    end

    case response = http_request(http, source, "/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 #{source["branch"]}"
    end

    if commits
      last_seen_id = source["last_commit"]["id"] if source["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"]
            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["login"]}}, :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 source["branch"]} at <a href="http://github.com/#{source["repo"]}">#{h source["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
        source.update "last_commit"=>last_commit.slice("id", "message", "url").
          merge("timestamp"=>Time.parse(last_commit["committed_date"]).utc)
      end
    else
      logger.error "Github: #{response.code} #{response.message}"
      error = true
    end

    raise "Last request didn't go as expected, trying again later" if error
  end
end

#validate(source) ⇒ Object



18
19
20
21
# File 'lib/dash-fu/marios/github.rb', line 18

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