Class: GithubDailyDigest::GithubService
- Inherits:
-
Object
- Object
- GithubDailyDigest::GithubService
- Defined in:
- lib/github_service.rb
Constant Summary collapse
- MAX_RETRIES =
Local retry specifically for rate limits within a method call
3
Instance Method Summary collapse
-
#fetch_active_repos(org_name, since_time) ⇒ Object
Fetches all repositories with activity since a given time.
- #fetch_members(org_name) ⇒ Object
- #fetch_org_repos(org_name) ⇒ Object
-
#fetch_user_commits_in_repo(repo_full_name, username, since_time) ⇒ Object
Fetches commits for a specific user in a specific repo since a given time.
-
#get_current_user ⇒ Object
Get information about the authenticated user.
-
#initialize(token:, logger:, config:) ⇒ GithubService
constructor
A new instance of GithubService.
-
#map_commits_to_users(active_repos) ⇒ Object
Maps commits to users for efficient activity tracking.
-
#search_user_reviews(username, org_name, since_time) ⇒ Object
Searches for PRs reviewed by the user.
Constructor Details
#initialize(token:, logger:, config:) ⇒ GithubService
Returns a new instance of GithubService.
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/github_service.rb', line 10 def initialize(token:, logger:, config:) @logger = logger @config = config @client = Octokit::Client.new(access_token: token) @client.auto_paginate = true # Essential for members/repos verify_authentication rescue Octokit::Unauthorized => e @logger.fatal("GitHub authentication failed. Check GITHUB_TOKEN. Error: #{e.}") raise # Re-raise to stop execution rescue => e @logger.fatal("Failed to initialize GitHub client: #{e.}") raise end |
Instance Method Details
#fetch_active_repos(org_name, since_time) ⇒ Object
Fetches all repositories with activity since a given time
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/github_service.rb', line 110 def fetch_active_repos(org_name, since_time) @logger.info("Fetching active repositories for organization: #{org_name} since #{since_time}") repos = fetch_org_repos(org_name) @logger.info("Checking #{repos.size} repositories for activity") active_repos = {} repos.each_with_index do |repo, index| repo_full_name = repo.full_name @logger.info("Checking for activity in #{repo_full_name} since #{since_time} [#{index+1}/#{repos.size}]") begin # Get all branches for this repository branches = handle_api_errors(catch_conflicts: true) do @client.branches(repo_full_name) end if branches.nil? || branches.empty? @logger.debug("No branches found in #{repo_full_name}") next end @logger.info("Found #{branches.count} branches in #{repo_full_name}") # Find branches with recent activity active_branches = [] all_commits = [] # We'll check each branch in parallel branches.each do |branch| branch_name = branch.name # Get latest commit for the branch latest_commit = branch.commit if latest_commit commit_date = nil # Get full commit details to check date commit_details = handle_api_errors(catch_conflicts: true) do @client.commit(repo_full_name, latest_commit.sha) end if commit_details && commit_details.commit && commit_details.commit. commit_date = commit_details.commit..date end # If this branch has commits since our cutoff date, flag it as active if commit_date && Time.parse(commit_date.to_s) >= Time.parse(since_time.to_s) active_branches << branch_name end end end if active_branches.any? @logger.info("Found #{active_branches.size} active branches in #{repo_full_name}: #{active_branches.join(', ')}") # Now get commits for each active branch active_branches.each do |branch_name| branch_commits = handle_api_errors(catch_conflicts: true) do @client.commits(repo_full_name, { sha: branch_name, since: since_time }) end if branch_commits && branch_commits.any? @logger.debug("Found #{branch_commits.count} commits in branch #{branch_name} of #{repo_full_name}") # Add branch information to each commit branch_commits.each do |commit| commit.branch = branch_name end all_commits.concat(branch_commits) end end # Remove duplicate commits (same SHA across multiple branches) # But preserve branch information unique_commits = {} all_commits.each do |commit| if unique_commits[commit.sha] # If we've seen this commit before, add branch to its branches list unique_commits[commit.sha].branches ||= [] unique_commits[commit.sha].branches << commit.branch unless unique_commits[commit.sha].branches.include?(commit.branch) else # First time seeing this commit commit.branches = [commit.branch] unique_commits[commit.sha] = commit end end commits = unique_commits.values if commits.any? @logger.info("Found #{commits.count} unique commits across active branches in #{repo_full_name}") active_repos[repo_full_name] = commits end else @logger.debug("No active branches found in #{repo_full_name} since #{since_time}") end # Avoid hitting rate limits sleep(0.1) if index > 0 && index % 10 == 0 rescue => e @logger.error("Error checking repo #{repo_full_name}: #{e.}") @logger.error(e.backtrace.join("\n")) next end end if active_repos.empty? @logger.warn("No active repositories found with commits since #{since_time}") else @logger.info("Found #{active_repos.size} active repositories with commits out of #{repos.size} total repos") active_repos.keys.each do |repo_name| @logger.info("Active repo: #{repo_name} with #{active_repos[repo_name].size} commits") end end active_repos end |
#fetch_members(org_name) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/github_service.rb', line 24 def fetch_members(org_name) @logger.info("Fetching members for organization: #{org_name}") members = handle_api_errors { @client.organization_members(org_name) } if members logins = members.map(&:login) @logger.info("Found #{logins.count} members.") logins else @logger.error("Could not fetch members for #{org_name}.") [] # Return empty array on failure after retries end rescue Octokit::NotFound => e @logger.error("Organization '#{org_name}' not found or token lacks permission. Error: #{e.}") [] end |
#fetch_org_repos(org_name) ⇒ Object
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 |
# File 'lib/github_service.rb', line 57 def fetch_org_repos(org_name) @logger.info("Fetching repositories for organization: #{org_name}") # First try with type: 'all' repos = handle_api_errors do @client.organization_repositories(org_name, { type: 'all', per_page: 100 }) end if repos @logger.info("Found #{repos.count} repositories.") # Log some details about the first few repos for debugging if repos.any? sample_repos = repos.take(3) sample_repos.each do |repo| @logger.info("Sample repo: #{repo.full_name}, Private: #{repo.private}, Fork: #{repo.fork}") end end repos # Return the array of Sawyer::Resource objects else @logger.error("Could not fetch repositories for #{org_name}.") [] end end |
#fetch_user_commits_in_repo(repo_full_name, username, since_time) ⇒ Object
Fetches commits for a specific user in a specific repo since a given time
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/github_service.rb', line 84 def fetch_user_commits_in_repo(repo_full_name, username, since_time) @logger.debug("Fetching commits by #{username} in #{repo_full_name} since #{since_time}") = { author: username, since: since_time } commits = handle_api_errors(catch_conflicts: true) do @client.commits_since(repo_full_name, since_time, ) # Alternative if above doesn't work reliably with author filter: # @client.commits(repo_full_name, since: since_time).select { |c| c.author&.login == username } end commits || [] # Return empty array on failure rescue Octokit::Conflict, Octokit::NotFound => e # Repo might be empty, disabled issues/wiki, or inaccessible @logger.warn("Skipping repo #{repo_full_name} for user #{username}. Reason: #{e.}") [] end |
#get_current_user ⇒ Object
Get information about the authenticated user
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/github_service.rb', line 41 def get_current_user handle_api_errors do user = @client.user { login: user.login, name: user.name, email: user.email, avatar_url: user.avatar_url, scopes: @client.scopes } end rescue => e @logger.error("Failed to get current user information: #{e.}") nil end |
#map_commits_to_users(active_repos) ⇒ Object
Maps commits to users for efficient activity tracking
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/github_service.rb', line 231 def map_commits_to_users(active_repos) @logger.info("Mapping commits to users") user_commits = {} active_repos.each do |repo_full_name, commits| commits.each do |commit| = commit.&.login next unless # Skip commits without a valid GitHub author # Fetch commit details to get line changes commit_details = handle_api_errors do @client.commit(repo_full_name, commit.sha) end user_commits[] ||= [] if commit_details # Add commit with line changes information if available user_commits[] << format_commit(commit, repo_full_name, commit_details) else # Fallback to basic commit info if details couldn't be fetched user_commits[] << format_commit(commit, repo_full_name) end end end @logger.info("Found commits from #{user_commits.size} users") user_commits end |
#search_user_reviews(username, org_name, since_time) ⇒ Object
Searches for PRs reviewed by the user
100 101 102 103 104 105 106 107 |
# File 'lib/github_service.rb', line 100 def search_user_reviews(username, org_name, since_time) @logger.debug("Searching PR reviews for user: #{username} since #{since_time}") query = "is:pr reviewed-by:#{username} org:#{org_name} updated:>#{since_time}" results = handle_api_errors { @client.search_issues(query, per_page: 1) } # Fetch 1 to get total_count efficiently count = results ? results.total_count : 0 @logger.debug("Found #{count} PRs reviewed by #{username} via search.") count end |