Class: CollavreGithub::Client
- Inherits:
-
Object
- Object
- CollavreGithub::Client
- Defined in:
- app/services/collavre_github/client.rb
Constant Summary collapse
- MOCK_SERVER_DEFAULT =
"http://localhost:4567"
Instance Method Summary collapse
-
#compare(repo_full_name, base, head) ⇒ Object
Compare two commits and return changed files.
- #create_repository_webhook(repo_full_name, url:, secret:, events:, content_type: "json") ⇒ Object
-
#default_branch(repo_full_name) ⇒ Object
Fetch the default branch name for a repository.
- #delete_repository_webhook(repo_full_name, hook_id) ⇒ Object
-
#file_content(repo_full_name, path, ref: nil) ⇒ Object
Fetch file content (decoded) for a given path and ref.
-
#initialize(account) ⇒ Client
constructor
A new instance of Client.
- #organizations ⇒ Object
- #pull_request_commit_messages(repo_full_name, number) ⇒ Object
- #pull_request_details(repo_full_name, number) ⇒ Object
- #pull_request_diff(repo_full_name, number) ⇒ Object
- #repositories_for_authenticated_user ⇒ Object
- #repositories_for_organization(org) ⇒ Object
- #repository_hooks(repo_full_name) ⇒ Object
-
#tree(repo_full_name, branch) ⇒ Object
Fetch the full recursive tree for a given branch/sha.
- #update_repository_webhook(repo_full_name, hook_id, url:, secret:, events:, content_type: "json") ⇒ Object
Constructor Details
#initialize(account) ⇒ Client
Returns a new instance of Client.
5 6 7 8 9 10 11 |
# File 'app/services/collavre_github/client.rb', line 5 def initialize(account) = { access_token: account.token } api_endpoint = resolve_api_endpoint [:api_endpoint] = api_endpoint if api_endpoint.present? @client = Octokit::Client.new() @client.auto_paginate = true end |
Instance Method Details
#compare(repo_full_name, base, head) ⇒ Object
Compare two commits and return changed files
154 155 156 157 158 159 |
# File 'app/services/collavre_github/client.rb', line 154 def compare(repo_full_name, base, head) client.compare(repo_full_name, base, head) rescue Octokit::Error, Faraday::Error => e Rails.logger.warn("GitHub compare failed: #{e.}") nil end |
#create_repository_webhook(repo_full_name, url:, secret:, events:, content_type: "json") ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/services/collavre_github/client.rb', line 74 def create_repository_webhook(repo_full_name, url:, secret:, events:, content_type: "json") client.create_hook( repo_full_name, "web", { url: url, content_type: content_type, secret: secret }, { events: events, active: true } ) rescue Octokit::Error, Faraday::Error => e Rails.logger.warn("GitHub create webhook failed for #{repo_full_name}: #{e.}") nil end |
#default_branch(repo_full_name) ⇒ Object
Fetch the default branch name for a repository
121 122 123 124 125 126 127 |
# File 'app/services/collavre_github/client.rb', line 121 def default_branch(repo_full_name) repo = client.repository(repo_full_name) repo.default_branch rescue Octokit::Error, Faraday::Error => e Rails.logger.warn("GitHub default branch fetch failed: #{e.}") "main" end |
#delete_repository_webhook(repo_full_name, hook_id) ⇒ Object
113 114 115 116 117 118 |
# File 'app/services/collavre_github/client.rb', line 113 def delete_repository_webhook(repo_full_name, hook_id) client.remove_hook(repo_full_name, hook_id) rescue Octokit::Error, Faraday::Error => e Rails.logger.warn("GitHub delete webhook failed for #{repo_full_name}: #{e.}") nil end |
#file_content(repo_full_name, path, ref: nil) ⇒ Object
Fetch file content (decoded) for a given path and ref
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'app/services/collavre_github/client.rb', line 140 def file_content(repo_full_name, path, ref: nil) opts = ref ? { ref: ref } : {} content = client.contents(repo_full_name, path: path, **opts) if content.encoding == "base64" Base64.decode64(content.content).force_encoding("UTF-8") else content.content end rescue Octokit::Error, Faraday::Error => e Rails.logger.warn("GitHub file content fetch failed for #{path}: #{e.}") nil end |
#organizations ⇒ Object
13 14 15 16 17 18 |
# File 'app/services/collavre_github/client.rb', line 13 def organizations client.organizations rescue Octokit::Error, Faraday::Error => e Rails.logger.warn("GitHub organizations fetch failed: #{e.}") [] end |
#pull_request_commit_messages(repo_full_name, number) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'app/services/collavre_github/client.rb', line 41 def (repo_full_name, number) client .pull_request_commits(repo_full_name, number) .map { |commit| commit.commit&. } .compact rescue Octokit::Error, Faraday::Error => e Rails.logger.warn("GitHub PR commits fetch failed: #{e.}") [] end |
#pull_request_details(repo_full_name, number) ⇒ Object
34 35 36 37 38 39 |
# File 'app/services/collavre_github/client.rb', line 34 def pull_request_details(repo_full_name, number) client.pull_request(repo_full_name, number) rescue Octokit::Error, Faraday::Error => e Rails.logger.warn("GitHub PR fetch failed: #{e.}") nil end |
#pull_request_diff(repo_full_name, number) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/services/collavre_github/client.rb', line 51 def pull_request_diff(repo_full_name, number) files = client.pull_request_files(repo_full_name, number) formatted = files.filter_map do |file| next unless file.patch.present? <<~DIFF.strip diff --git a/#{file.filename} b/#{file.filename} #{file.patch} DIFF end formatted.join("\n\n").presence rescue Octokit::Error, Faraday::Error => e Rails.logger.warn("GitHub PR files fetch failed: #{e.}") nil end |
#repositories_for_authenticated_user ⇒ Object
20 21 22 23 24 25 |
# File 'app/services/collavre_github/client.rb', line 20 def repositories_for_authenticated_user client.repos(nil, type: "all") rescue Octokit::Error, Faraday::Error => e Rails.logger.warn("GitHub user repos fetch failed: #{e.}") [] end |
#repositories_for_organization(org) ⇒ Object
27 28 29 30 31 32 |
# File 'app/services/collavre_github/client.rb', line 27 def repositories_for_organization(org) client.org_repos(org, type: "all") rescue Octokit::Error, Faraday::Error => e Rails.logger.warn("GitHub org repos fetch failed: #{e.}") [] end |
#repository_hooks(repo_full_name) ⇒ Object
67 68 69 70 71 72 |
# File 'app/services/collavre_github/client.rb', line 67 def repository_hooks(repo_full_name) client.hooks(repo_full_name) rescue Octokit::Error, Faraday::Error => e Rails.logger.warn("GitHub hooks fetch failed for #{repo_full_name}: #{e.}") [] end |
#tree(repo_full_name, branch) ⇒ Object
Fetch the full recursive tree for a given branch/sha
130 131 132 133 134 135 136 137 |
# File 'app/services/collavre_github/client.rb', line 130 def tree(repo_full_name, branch) sha = client.ref(repo_full_name, "heads/#{branch}").object.sha result = client.tree(repo_full_name, sha, recursive: true) result.tree rescue Octokit::Error, Faraday::Error => e Rails.logger.warn("GitHub tree fetch failed: #{e.}") [] end |
#update_repository_webhook(repo_full_name, hook_id, url:, secret:, events:, content_type: "json") ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'app/services/collavre_github/client.rb', line 93 def update_repository_webhook(repo_full_name, hook_id, url:, secret:, events:, content_type: "json") client.edit_hook( repo_full_name, hook_id, "web", { url: url, content_type: content_type, secret: secret }, { events: events, active: true } ) rescue Octokit::Error, Faraday::Error => e Rails.logger.warn("GitHub update webhook failed for #{repo_full_name}: #{e.}") nil end |