Class: Eussiror::GithubClient

Inherits:
Object
  • Object
show all
Defined in:
lib/eussiror/github_client.rb

Constant Summary collapse

GITHUB_API_BASE =
"https://api.github.com"
FINGERPRINT_MARKER =

Marker embedded as an HTML comment in every issue body for searching.

"eussiror:fingerprint"

Instance Method Summary collapse

Constructor Details

#initialize(token:, repository:) ⇒ GithubClient

Returns a new instance of GithubClient.



13
14
15
16
# File 'lib/eussiror/github_client.rb', line 13

def initialize(token:, repository:)
  @token      = token
  @repository = repository
end

Instance Method Details

#add_comment(issue_number, body:) ⇒ Object

Adds a comment to an existing issue. Returns the comment id.



50
51
52
53
54
55
56
57
58
59
# File 'lib/eussiror/github_client.rb', line 50

def add_comment(issue_number, body:)
  uri = URI("#{GITHUB_API_BASE}/repos/#{@repository}/issues/#{issue_number}/comments")

  response = post(uri, { body: body })
  data     = JSON.parse(response.body)

  raise_api_error!(response, "add comment") unless response.is_a?(Net::HTTPSuccess)

  data["id"]
end

#create_issue(title:, body:, labels: [], assignees: []) ⇒ Object

Creates a new GitHub issue and returns the issue number.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/eussiror/github_client.rb', line 35

def create_issue(title:, body:, labels: [], assignees: [])
  uri     = URI("#{GITHUB_API_BASE}/repos/#{@repository}/issues")
  payload = { title: title, body: body }
  payload[:labels]    = labels    if labels.any?
  payload[:assignees] = assignees if assignees.any?

  response = post(uri, payload)
  data     = JSON.parse(response.body)

  raise_api_error!(response, "create issue") unless response.is_a?(Net::HTTPSuccess)

  data["number"]
end

#find_issue(fingerprint) ⇒ Object

Searches for an open issue whose body contains the given fingerprint. Returns the issue number (Integer) or nil when none is found.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/eussiror/github_client.rb', line 20

def find_issue(fingerprint)
  query  = "repo:#{@repository} is:issue is:open \"#{FINGERPRINT_MARKER}:#{fingerprint}\" in:body"
  params = URI.encode_www_form(q: query, per_page: 1)
  uri    = URI("#{GITHUB_API_BASE}/search/issues?#{params}")

  response = get(uri)
  data     = JSON.parse(response.body)

  return nil unless response.is_a?(Net::HTTPSuccess)
  return nil if data["items"].blank?

  data["items"].first["number"]
end