Class: RailsErrorDashboard::Services::GitHubIssueClient

Inherits:
IssueTrackerClient show all
Defined in:
lib/rails_error_dashboard/services/github_issue_client.rb

Overview

GitHub REST API client for issue management.

API Docs: docs.github.com/en/rest/issues/issues Auth: Personal access token with ‘repo` scope Rate limit: 5,000 requests/hour per authenticated user

Constant Summary

Constants inherited from IssueTrackerClient

IssueTrackerClient::MAX_BODY_LENGTH, IssueTrackerClient::REQUEST_TIMEOUT

Instance Attribute Summary

Attributes inherited from IssueTrackerClient

#api_url, #repo, #token

Instance Method Summary collapse

Methods inherited from IssueTrackerClient

for, from_config

Constructor Details

#initialize(token:, repo:, api_url: nil) ⇒ GitHubIssueClient

Returns a new instance of GitHubIssueClient.



11
12
13
14
# File 'lib/rails_error_dashboard/services/github_issue_client.rb', line 11

def initialize(token:, repo:, api_url: nil)
  super
  @api_url = api_url || "https://api.github.com"
end

Instance Method Details

#add_comment(number:, body:) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rails_error_dashboard/services/github_issue_client.rb', line 51

def add_comment(number:, body:)
  response = http_post(
    "#{@api_url}/repos/#{@repo}/issues/#{number}/comments",
    { body: truncate_body(body) },
    auth_headers
  )

  if response[:status] == 201
    success_response(url: response[:body]["html_url"])
  else
    error_response("GitHub API error (#{response[:status]})")
  end
end

#close_issue(number:) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/rails_error_dashboard/services/github_issue_client.rb', line 31

def close_issue(number:)
  response = http_patch(
    "#{@api_url}/repos/#{@repo}/issues/#{number}",
    { state: "closed" },
    auth_headers
  )

  response[:status] == 200 ? success_response({}) : error_response("GitHub API error (#{response[:status]})")
end

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rails_error_dashboard/services/github_issue_client.rb', line 16

def create_issue(title:, body:, labels: [])
  response = http_post(
    "#{@api_url}/repos/#{@repo}/issues",
    { title: title, body: truncate_body(body), labels: labels },
    auth_headers
  )

  if response[:status] == 201
    data = response[:body]
    success_response(url: data["html_url"], number: data["number"])
  else
    error_response("GitHub API error (#{response[:status]}): #{response[:body]&.dig("message") || response[:error]}")
  end
end

#fetch_comments(number:, per_page: 10) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rails_error_dashboard/services/github_issue_client.rb', line 65

def fetch_comments(number:, per_page: 10)
  response = http_get(
    "#{@api_url}/repos/#{@repo}/issues/#{number}/comments?per_page=#{per_page}&sort=created&direction=desc",
    auth_headers
  )

  if response[:status] == 200
    comments = (response[:body] || []).map { |c|
      {
        author: c.dig("user", "login"),
        avatar_url: c.dig("user", "avatar_url"),
        body: c["body"],
        created_at: c["created_at"],
        url: c["html_url"]
      }
    }
    success_response(comments: comments)
  else
    error_response("GitHub API error (#{response[:status]})")
  end
end

#fetch_issue(number:) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rails_error_dashboard/services/github_issue_client.rb', line 87

def fetch_issue(number:)
  response = http_get(
    "#{@api_url}/repos/#{@repo}/issues/#{number}",
    auth_headers
  )

  if response[:status] == 200
    data = response[:body]
    success_response(
      state: data["state"],
      title: data["title"],
      assignees: (data["assignees"] || []).map { |a|
        { login: a["login"], avatar_url: a["avatar_url"] }
      },
      labels: (data["labels"] || []).map { |l|
        { name: l["name"], color: l["color"] }
      }
    )
  else
    error_response("GitHub API error (#{response[:status]})")
  end
end

#reopen_issue(number:) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/rails_error_dashboard/services/github_issue_client.rb', line 41

def reopen_issue(number:)
  response = http_patch(
    "#{@api_url}/repos/#{@repo}/issues/#{number}",
    { state: "open" },
    auth_headers
  )

  response[:status] == 200 ? success_response({}) : error_response("GitHub API error (#{response[:status]})")
end