Class: RailsErrorDashboard::Services::CodebergIssueClient

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

Overview

Codeberg/Gitea/Forgejo REST API client for issue management.

Codeberg runs Forgejo (hard fork of Gitea). The API is compatible with Gitea’s /api/v1/ endpoints. Works with any Gitea or Forgejo instance.

API Docs: docs.gitea.com/development/api-usage Codeberg: codeberg.org/api/swagger Auth: Personal access token

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) ⇒ CodebergIssueClient

Returns a new instance of CodebergIssueClient.



14
15
16
17
# File 'lib/rails_error_dashboard/services/codeberg_issue_client.rb', line 14

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

Instance Method Details

#add_comment(number:, body:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rails_error_dashboard/services/codeberg_issue_client.rb', line 56

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("Codeberg API error (#{response[:status]})")
  end
end

#close_issue(number:) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/rails_error_dashboard/services/codeberg_issue_client.rb', line 36

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

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

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rails_error_dashboard/services/codeberg_issue_client.rb', line 19

def create_issue(title:, body:, labels: [])
  response = http_post(
    "#{@api_url}/repos/#{@repo}/issues",
    { title: title, body: truncate_body(body) },
    auth_headers
  )
  # Note: Gitea/Forgejo labels require label IDs, not names.
  # We skip labels in the create call — users can add them on the platform.

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

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rails_error_dashboard/services/codeberg_issue_client.rb', line 70

def fetch_comments(number:, per_page: 10)
  response = http_get(
    "#{@api_url}/repos/#{@repo}/issues/#{number}/comments?limit=#{per_page}",
    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("Codeberg API error (#{response[:status]})")
  end
end

#fetch_issue(number:) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rails_error_dashboard/services/codeberg_issue_client.rb', line 92

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("Codeberg API error (#{response[:status]})")
  end
end

#reopen_issue(number:) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/rails_error_dashboard/services/codeberg_issue_client.rb', line 46

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

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