Class: Soba::Infrastructure::GitHubClient
- Inherits:
-
Object
- Object
- Soba::Infrastructure::GitHubClient
- Includes:
- SemanticLogger::Loggable
- Defined in:
- lib/soba/infrastructure/github_client.rb
Instance Attribute Summary collapse
-
#octokit ⇒ Object
readonly
Returns the value of attribute octokit.
Instance Method Summary collapse
- #close_issue_with_label(repository, issue_number, label:) ⇒ Object
- #create_label(repository, name, color, description) ⇒ Object
- #fetch_closed_issues(repository) ⇒ Object
- #get_pr_issue_number(repository, pr_number) ⇒ Object
- #get_pull_request(repository, pr_number) ⇒ Object
-
#initialize(token: nil) ⇒ GitHubClient
constructor
A new instance of GitHubClient.
- #issue(repository, number) ⇒ Object
- #issues(repository, state: "open") ⇒ Object
- #list_labels(repository) ⇒ Object
- #merge_pull_request(repository, pr_number, merge_method: "squash") ⇒ Object
- #rate_limit_remaining ⇒ Object
- #search_pull_requests(repository:, labels: []) ⇒ Object
- #update_issue_labels(repository, issue_number, from:, to:) ⇒ Object
- #wait_for_rate_limit ⇒ Object
Constructor Details
#initialize(token: nil) ⇒ GitHubClient
Returns a new instance of GitHubClient.
17 18 19 20 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 |
# File 'lib/soba/infrastructure/github_client.rb', line 17 def initialize(token: nil) # If token is explicitly provided, use it if token.nil? # Try to get token from configuration or token provider if defined?(Configuration) && Configuration.respond_to?(:config) && Configuration.config config = Configuration.config if config.github.token.present? token = config.github.token elsif config.github.auth_method # Use GitHubTokenProvider with specified auth_method token_provider = GitHubTokenProvider.new token = token_provider.fetch(auth_method: config.github.auth_method) else # Auto-detect mode token_provider = GitHubTokenProvider.new token = token_provider.fetch(auth_method: nil) end else # Fallback to environment variable token = ENV["GITHUB_TOKEN"] end end stack = build_middleware_stack @octokit = Octokit::Client.new( access_token: token, auto_paginate: true, per_page: 100, connection_options: { builder: stack, } ) end |
Instance Attribute Details
#octokit ⇒ Object (readonly)
Returns the value of attribute octokit.
15 16 17 |
# File 'lib/soba/infrastructure/github_client.rb', line 15 def octokit @octokit end |
Instance Method Details
#close_issue_with_label(repository, issue_number, label:) ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/soba/infrastructure/github_client.rb', line 291 def close_issue_with_label(repository, issue_number, label:) logger.info "Closing issue with label", repository: repository, issue_number: issue_number, label: label with_error_handling do with_rate_limit_check do # Close the issue @octokit.close_issue(repository, issue_number) # Add label @octokit.add_labels_to_an_issue(repository, issue_number, [label]) end end logger.info "Issue closed and labeled successfully", repository: repository, issue_number: issue_number true rescue => e logger.error "Failed to close issue with label", error: e., repository: repository, issue_number: issue_number raise end |
#create_label(repository, name, color, description) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/soba/infrastructure/github_client.rb', line 181 def create_label(repository, name, color, description) logger.info "Creating label", repository: repository, name: name, color: color response = with_error_handling do with_rate_limit_check do @octokit.add_label(repository, name, color, description: description) end end { name: response.name, color: response.color, description: response.description, } rescue Octokit::UnprocessableEntity # Check if error is because label already exists # Octokit will return "Validation failed" as the message logger.info "Label already exists, skipping", repository: repository, name: name nil rescue => e logger.error "Failed to create label", error: e., repository: repository, name: name raise end |
#fetch_closed_issues(repository) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/soba/infrastructure/github_client.rb', line 312 def fetch_closed_issues(repository) logger.info "Fetching closed issues", repository: repository response = with_error_handling do with_rate_limit_check do @octokit.issues(repository, state: "closed") end end map_issues_to_domain(response) rescue => e logger.error "Failed to fetch closed issues", error: e., repository: repository raise end |
#get_pr_issue_number(repository, pr_number) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/soba/infrastructure/github_client.rb', line 275 def get_pr_issue_number(repository, pr_number) logger.info "Extracting issue number from PR", repository: repository, pr_number: pr_number pr = get_pull_request(repository, pr_number) body = pr[:body] || "" # Match patterns like: fixes #123, closes #456, resolves #789 match = body.match(/(?:fixes|closes|resolves|fix|close|resolve)\s+#(\d+)/i) return match[1].to_i if match nil rescue => e logger.error "Failed to extract issue number", error: e., repository: repository, pr_number: pr_number nil end |
#get_pull_request(repository, pr_number) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/soba/infrastructure/github_client.rb', line 253 def get_pull_request(repository, pr_number) logger.info "Fetching pull request", repository: repository, pr_number: pr_number response = with_error_handling do with_rate_limit_check do @octokit.pull_request(repository, pr_number) end end { number: response.number, title: response.title, body: response.body, state: response.state, mergeable: response.mergeable, mergeable_state: response.mergeable_state, } rescue => e logger.error "Failed to fetch pull request", error: e., repository: repository, pr_number: pr_number raise end |
#issue(repository, number) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/soba/infrastructure/github_client.rb', line 67 def issue(repository, number) logger.info "Fetching issue", repository: repository, number: number response = with_error_handling do with_rate_limit_check do @octokit.issue(repository, number) end end map_issue_to_domain(response) rescue Octokit::NotFound logger.warn "Issue not found", repository: repository, number: number nil rescue => e logger.error "Failed to fetch issue", error: e., repository: repository, number: number raise end |
#issues(repository, state: "open") ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/soba/infrastructure/github_client.rb', line 52 def issues(repository, state: "open") logger.info "Fetching issues", repository: repository, state: state response = with_error_handling do with_rate_limit_check do @octokit.issues(repository, state: state) end end map_issues_to_domain(response) rescue => e logger.error "Failed to fetch issues", error: e., repository: repository raise end |
#list_labels(repository) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/soba/infrastructure/github_client.rb', line 160 def list_labels(repository) logger.info "Fetching labels", repository: repository response = with_error_handling do with_rate_limit_check do @octokit.labels(repository) end end response.map do |label| { name: label.name, color: label.color, description: label.description, } end rescue => e logger.error "Failed to fetch labels", error: e., repository: repository raise end |
#merge_pull_request(repository, pr_number, merge_method: "squash") ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/soba/infrastructure/github_client.rb', line 231 def merge_pull_request(repository, pr_number, merge_method: "squash") logger.info "Merging pull request", repository: repository, pr_number: pr_number, merge_method: merge_method response = with_error_handling do with_rate_limit_check do @octokit.merge_pull_request(repository, pr_number, "", merge_method: merge_method) end end { sha: response.sha, merged: response.merged, message: response., } rescue Octokit::MethodNotAllowed => e logger.error "Pull request not mergeable", repository: repository, pr_number: pr_number, error: e. raise MergeConflictError, "Pull request is not mergeable: #{e.}" rescue => e logger.error "Failed to merge pull request", error: e., repository: repository, pr_number: pr_number raise end |
#rate_limit_remaining ⇒ Object
85 86 87 88 89 90 |
# File 'lib/soba/infrastructure/github_client.rb', line 85 def rate_limit_remaining @octokit.rate_limit.remaining rescue => e logger.error "Failed to check rate limit", error: e. nil end |
#search_pull_requests(repository:, labels: []) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/soba/infrastructure/github_client.rb', line 205 def search_pull_requests(repository:, labels: []) logger.info "Searching pull requests", repository: repository, labels: labels query_parts = ["type:pr", "is:open", "repo:#{repository}"] query_parts += labels.map { |label| "label:#{label}" } query = query_parts.join(" ") response = with_error_handling do with_rate_limit_check do @octokit.search_issues(query) end end response.items.map do |pr| { number: pr.number, title: pr.title, state: pr.state, labels: pr.labels.map { |l| { name: l.name } }, } end rescue => e logger.error "Failed to search pull requests", error: e., repository: repository raise end |
#update_issue_labels(repository, issue_number, from:, to:) ⇒ Object
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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/soba/infrastructure/github_client.rb', line 92 def update_issue_labels(repository, issue_number, from:, to:) logger.info "Atomic label update with check", repository: repository, issue: issue_number, from: from, to: to with_error_handling do with_rate_limit_check do # Get current labels to check state issue = @octokit.issue(repository, issue_number) current_labels = issue.labels.map(&:name) # Check if the issue has the expected 'from' label unless current_labels.include?(from) logger.warn "Label state mismatch: expected '#{from}' not found", repository: repository, issue: issue_number, current_labels: current_labels return false end # Check if the issue already has the 'to' label (duplicate transition) if current_labels.include?(to) logger.warn "Duplicate transition detected: '#{to}' already exists", repository: repository, issue: issue_number, current_labels: current_labels return false end # Perform the label update atomically new_labels = current_labels - [from] new_labels << to @octokit.replace_all_labels(repository, issue_number, new_labels) logger.info "Labels updated atomically", repository: repository, issue: issue_number, updated_labels: new_labels true end end rescue => e logger.error "Failed to update labels atomically", error: e., repository: repository, issue: issue_number raise end |
#wait_for_rate_limit ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/soba/infrastructure/github_client.rb', line 144 def wait_for_rate_limit limit_info = @octokit.rate_limit if limit_info.remaining == 0 reset_time = Time.at(limit_info.resets_at.to_i) wait_seconds = reset_time - Time.now if wait_seconds > 0 logger.warn "Rate limit exceeded. Waiting #{wait_seconds.round} seconds..." sleep(wait_seconds + 1) # Add 1 second buffer end end rescue => e logger.error "Failed to wait for rate limit", error: e. end |