Class: Github::Org::Stats::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/github/org/stats/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token:, org_name:) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/github/org/stats/client.rb', line 7

def initialize(access_token:, org_name:)
  @client = Octokit::Client.new(access_token: access_token)
  @org_name = org_name
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/github/org/stats/client.rb', line 6

def client
  @client
end

Instance Method Details

#commit_stats_all_repo(week: 0) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/github/org/stats/client.rb', line 38

def commit_stats_all_repo(week: 0)
  repos = org_repos
  result = org_repos.map do |org_repo|
    repo_id = org_repo[:id]
    stats = commit_stats_group_by_user(repo_id: repo_id, week: week)
  end
end

#commit_stats_group_by_user(repo_id:, week: 0) ⇒ Object

in default, get this week num of commits



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/github/org/stats/client.rb', line 24

def commit_stats_group_by_user(repo_id:, week: 0)
  today = Date.today
  week_start_day = today - today.wday - (week * 7)
  github_week_ts = (week_start_day.to_time + 9*60*60).to_i
  user_stats = @client.contributors_stats(repo_id)
  return [] if user_stats.nil? || user_stats.empty?
  stats = user_stats.map do |user_stat|
    commits = user_stat[:weeks].find{|us| us[:w] == github_week_ts}[:c]
    user_name = user_stat[:author][:login]
    {user: user_name, commits: commits}
  end
  stats.reduce({}){|h, v|h.merge({v[:user] => v[:commits]})}
end

#org_reposObject



17
18
19
20
# File 'lib/github/org/stats/client.rb', line 17

def org_repos
  team_id = owner_team[:id]
  @client.team_repositories(team_id)
end

#owner_teamObject



12
13
14
15
# File 'lib/github/org/stats/client.rb', line 12

def owner_team
  teams = @client.org_teams(@org_name)
  teams.find{|t| t[:slug] == "owners"}
end

#summary_commits_all_repo(week: 0) ⇒ Object



46
47
48
49
# File 'lib/github/org/stats/client.rb', line 46

def summary_commits_all_repo(week: 0)
  commits = commit_stats_all_repo(week: week)
  commits.inject(Hash.new(0)){|h, user_hash| user_hash.inject(h){|h2, kv|  h2[kv[0]] += kv[1]; h2}; h}
end