class OpenSourceStats

Constants

VERSION

Public Class Methods

client() click to toggle source

Authenticated Octokit client instance

# File lib/open_source_stats.rb, line 15
def self.client
  @client ||= Octokit::Client.new :access_token => ENV["GITHUB_TOKEN"]
end
start_time() click to toggle source

Returns a Ruby Time instance coresponding to the earliest possible event timestamp

# File lib/open_source_stats.rb, line 20
def self.start_time
  @start_time ||= Time.now - 24.hours
end
start_time=(time) click to toggle source

Helper method to overide the timestamp

# File lib/open_source_stats.rb, line 25
def self.start_time=(time)
  @start_time = time
end

Public Instance Methods

events() click to toggle source

Returns an array of unique Events accross all Users and Organizations

# File lib/open_source_stats.rb, line 60
def events
  @events ||= user_events.concat(org_events).uniq
end
org_events() click to toggle source

Returns an array of Events across all Organziations

# File lib/open_source_stats.rb, line 55
def org_events
  @org_events ||= orgs.map { |o| o.events }.flatten
end
orgs() click to toggle source

Returns an array of Organizations from the specified list of organizations

# File lib/open_source_stats.rb, line 50
def orgs
  @orgs ||= ENV["GITHUB_ORGS"].split(/, ?/).map { |o| Organization.new(o) }
end
stats(events=events) click to toggle source

Returns the calculated stats hash

# File lib/open_source_stats.rb, line 65
def stats(events=events)
  {
    :repositories_created => event_count(events, :type =>"CreateEvent"),
    :issue_comments => event_count(events, :type =>"IssueCommentEvent"),
    :issues_opened => event_count(events, :type =>"IssuesEvent", :action => "opened"),
    :issues_closed => event_count(events, :type =>"IssuesEvent", :action => "closed"),
    :repos_open_sourced => event_count(events, :type =>"PublicEvent"),
    :pull_requests_opened => event_count(events, :type =>"PullRequestEvent", :action => "opened"),
    :pull_requests_merged => event_count(events, :type =>"PullRequestEvent", :action => "closed", :merged => true),
    :versions_released => event_count(events, :type =>"ReleaseEvent", :action => "published"),
    :pushes => event_count(events, :type =>"PushEvent"),
    :commits => events.map { |e| e.commits }.flatten.inject{|sum,x| sum + x },
    :total_events => event_count(events)
  }
end
team() click to toggle source
# File lib/open_source_stats.rb, line 29
def team
  @team ||= client.team ENV["GITHUB_TEAM_ID"]
end
user_events() click to toggle source

Returns an array of Events across all Users

# File lib/open_source_stats.rb, line 45
def user_events
  @user_events ||= users.map { |u| u.events }.flatten
end
users() click to toggle source

Returns an array of Users from the given team

# File lib/open_source_stats.rb, line 34
def users
  @users ||= begin
    users = client.team_members ENV["GITHUB_TEAM_ID"], :per_page => 100
    while client.last_response.rels[:next] && client.rate_limit.remaining > 0
      users.concat client.get client.last_response.rels[:next].href
    end
    users.map { |u| User.new u[:login] }
  end
end

Private Instance Methods

client() click to toggle source

Helper method to reference the client stored on the class and DRY things up a bit

# File lib/open_source_stats.rb, line 99
def client
  OpenSourceStats.client
end
event_count(events, options={}) click to toggle source

Helper method to count events by set conditions

Options:

:type - the type of event to filter by
:action - the payload action to filter by
:merged - whether the pull request has been merged

Returns an integer reflecting the resulting event count

# File lib/open_source_stats.rb, line 91
def event_count(events, options={})
  events = events.select { |e| e.type == options[:type] } if options[:type]
  events = events.select { |e| e.payload[:action] == options[:action] } if options[:action]
  events = events.select { |e| e.payload[:pull_request][:merged] == options[:merged] } if options[:merged]
  events.count
end