Class: BugCourier::GithubClient
- Inherits:
-
Object
- Object
- BugCourier::GithubClient
- Defined in:
- lib/bug_courier/github_client.rb
Constant Summary collapse
- GITHUB_API_BASE =
"https://api.github.com"
Instance Method Summary collapse
- #add_comment(issue_number:, body:) ⇒ Object
- #create_issue(title:, body:, labels: [], assignees: []) ⇒ Object
- #find_open_issue(title:) ⇒ Object
-
#initialize(access_token:, repo:) ⇒ GithubClient
constructor
A new instance of GithubClient.
Constructor Details
#initialize(access_token:, repo:) ⇒ GithubClient
Returns a new instance of GithubClient.
11 12 13 14 |
# File 'lib/bug_courier/github_client.rb', line 11 def initialize(access_token:, repo:) @access_token = access_token @repo = repo end |
Instance Method Details
#add_comment(issue_number:, body:) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/bug_courier/github_client.rb', line 53 def add_comment(issue_number:, body:) uri = URI("#{GITHUB_API_BASE}/repos/#{@repo}/issues/#{issue_number}/comments") response = post(uri, { body: body }) unless response.is_a?(Net::HTTPCreated) log_failure("Failed to add comment to issue ##{issue_number}", response) return nil end JSON.parse(response.body) end |
#create_issue(title:, body:, labels: [], assignees: []) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/bug_courier/github_client.rb', line 16 def create_issue(title:, body:, labels: [], assignees: []) uri = URI("#{GITHUB_API_BASE}/repos/#{@repo}/issues") payload = { title: title, body: body, labels: labels, assignees: assignees }.compact response = post(uri, payload) unless response.is_a?(Net::HTTPCreated) log_failure("Failed to create GitHub issue", response) return nil end JSON.parse(response.body) end |
#find_open_issue(title:) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/bug_courier/github_client.rb', line 36 def find_open_issue(title:) query = "repo:#{@repo} is:issue is:open in:title #{title}" uri = URI("#{GITHUB_API_BASE}/search/issues") uri.query = URI.encode_www_form(q: query, per_page: 1) response = get(uri) unless response.is_a?(Net::HTTPOK) log_failure("Failed to search GitHub issues", response) return nil end data = JSON.parse(response.body) items = data["items"] || [] items.find { |issue| issue["title"] == title } end |