Class: Github::Pulse::GhClient

Inherits:
Object
  • Object
show all
Defined in:
lib/github/pulse/gh_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo:) ⇒ GhClient

Returns a new instance of GhClient.



12
13
14
15
# File 'lib/github/pulse/gh_client.rb', line 12

def initialize(repo:)
  @repo = repo
  validate_gh_cli!
end

Instance Attribute Details

#repoObject (readonly)

Returns the value of attribute repo.



10
11
12
# File 'lib/github/pulse/gh_client.rb', line 10

def repo
  @repo
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/github/pulse/gh_client.rb', line 17

def available?
  @gh_available
end

#commit_activityObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/github/pulse/gh_client.rb', line 89

def commit_activity
  # gh doesn't have a direct equivalent to commit activity stats
  # We can get recent commits and group them by week
  days_back = 52 * 7 # Get a year of data
  since_date = (Date.today - days_back).to_s
  
  url = "repos/#{repo}/commits?since=#{since_date}"
  cmd = ["gh", "api", url]
  
  commits = execute_gh_command(cmd)
  return [] unless commits.is_a?(Array)
  
  # Group by week
  activity = Hash.new(0)
  commits.each do |commit|
    date_str = commit.dig("commit", "author", "date")
    next unless date_str
    
    date = Date.parse(date_str)
    week_start = date - date.cwday + 1
    activity[week_start] += 1
  end
  
  activity.map do |week_start, count|
    days = [0] * 7
    # We don't have daily granularity from this API, so distribute evenly
    days[0] = count
    
    {
      week_start: week_start,
      days: days,
      total: count
    }
  end
end

#commits_data(since: nil, until_date: nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/github/pulse/gh_client.rb', line 125

def commits_data(since: nil, until_date: nil)
  # Fetch commit data from GitHub
  # Build query string for parameters
  params = []
  params << "since=#{since}" if since
  params << "until=#{until_date}" if until_date
  
  url = "repos/#{repo}/commits"
  url += "?#{params.join('&')}" unless params.empty?
  
  cmd = ["gh", "api", url]
  
  commits = execute_gh_command(cmd)
  
  # Group commits by author
  commits_by_author = Hash.new { |h, k| h[k] = [] }
  
  commits.each do |commit|
    author = commit.dig("author", "login") || commit.dig("commit", "author", "email") || "unknown"
    
    commits_by_author[author] << {
      sha: commit["sha"],
      message: commit.dig("commit", "message")&.lines&.first&.strip,
      time: parse_time(commit.dig("commit", "author", "date")),
      additions: 0,  # GitHub API doesn't provide this in commits list
      deletions: 0   # Would need individual commit API calls
    }
  end
  
  commits_by_author
end

#contributors_statsObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/github/pulse/gh_client.rb', line 157

def contributors_stats
  # Use gh api to get contributor statistics
  cmd = ["gh", "api", "repos/#{repo}/stats/contributors", "--cache", "1h"]
  
  stats = execute_gh_command(cmd)
  return [] unless stats && stats.is_a?(Array)
  
  stats.map do |contributor|
    {
      author: contributor.dig("author", "login") || "unknown",
      total_commits: contributor["total"],
      weeks: (contributor["weeks"] || []).map do |week|
        {
          week_start: Time.at(week["w"]).to_date,
          additions: week["a"],
          deletions: week["d"],
          commits: week["c"]
        }
      end
    }
  end
end

#pull_requests(since: nil, until_date: nil, state: "all") ⇒ Object



21
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
# File 'lib/github/pulse/gh_client.rb', line 21

def pull_requests(since: nil, until_date: nil, state: "all")
  limit = 1000
  fields = "number,title,author,createdAt,closedAt,mergedAt,state,additions,deletions,changedFiles"
  
  cmd = ["gh", "pr", "list", "--repo", repo, "--limit", limit.to_s, "--json", fields]
  
  # gh doesn't support filtering by date directly, so we get all and filter
  case state
  when "open"
    cmd += ["--state", "open"]
  when "closed"
    cmd += ["--state", "closed"]
  when "merged"
    cmd += ["--state", "merged"]
  else
    # For "all", we need to make multiple calls
    open_prs = execute_gh_command(cmd + ["--state", "open"])
    closed_prs = execute_gh_command(cmd + ["--state", "closed"])
    merged_prs = execute_gh_command(cmd + ["--state", "merged"])
    prs = open_prs + closed_prs + merged_prs
  end
  
  prs ||= execute_gh_command(cmd)
  
  # Filter by date if needed
  if since || until_date
    prs = filter_by_date(prs, since, until_date) { |pr| Time.parse(pr["createdAt"]) }
  end
  
  prs.map do |pr|
    {
      number: pr["number"],
      title: pr["title"],
      author: pr.dig("author", "login") || "unknown",
      created_at: parse_time(pr["createdAt"]),
      closed_at: parse_time(pr["closedAt"]),
      merged_at: parse_time(pr["mergedAt"]),
      state: pr["state"].downcase,
      additions: pr["additions"] || 0,
      deletions: pr["deletions"] || 0,
      changed_files: pr["changedFiles"] || 0
    }
  end
end

#repository_infoObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/github/pulse/gh_client.rb', line 66

def repository_info
  fields = "name,nameWithOwner,description,createdAt,updatedAt,primaryLanguage,defaultBranchRef,diskUsage,stargazerCount,forkCount,issues"
  cmd = ["gh", "repo", "view", repo, "--json", fields]
  
  data = execute_gh_command(cmd)
  return nil unless data && !data.empty?
  data = data.is_a?(Array) ? data.first : data
  
  {
    name: data["name"],
    full_name: data["nameWithOwner"],
    description: data["description"],
    created_at: parse_time(data["createdAt"]),
    updated_at: parse_time(data["updatedAt"]),
    language: data.dig("primaryLanguage", "name"),
    default_branch: data.dig("defaultBranchRef", "name"),
    size: data["diskUsage"],
    stars: data["stargazerCount"],
    forks: data["forkCount"],
    open_issues: data.dig("issues", "totalCount") || 0
  }
end