Class: Vanity::Source::GithubIssues

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

Overview

Track open/closed Github issues.

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



85
86
87
# File 'lib/vanity/sources/github_issues.rb', line 85

def meta(context)
  [ { :title=>"On Github", :url=>"http://github.com/#{context["repo"]}/issues" } ]
end

#request(http, context, state) ⇒ Object



78
79
80
81
82
83
# File 'lib/vanity/sources/github_issues.rb', line 78

def request(http, context, state)
  path = "/api/v2/json/issues/list/#{URI.escape context["repo"]}/#{state}"
  get = Net::HTTP::Get.new(path)
  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
# File 'lib/vanity/sources/github_issues.rb', line 7

def setup(context, params)
  repo = params["repo"].to_s.strip
  context["metric.name"] = "Github Issues for #{repo}"
  context["metric.columns"] = [{ :id=>"open", :label=>"Open issues" }, { :id=>"closed", :label=>"Closed issues" }]
  context["metric.totals"] = true
  context["repo"] = repo
  context["username"] = params["username"]
  context["api_token"] = params["api_token"]
end

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



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

#validate(context) ⇒ Object



17
18
19
20
# File 'lib/vanity/sources/github_issues.rb', line 17

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