Skip to content

Commit

Permalink
Add api wrapper for Gitea
Browse files Browse the repository at this point in the history
  • Loading branch information
krauselukas authored and eduardoj committed Sep 27, 2022
1 parent 67d7730 commit 5d8dcd0
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/api/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ gem 'octokit'
gem 'gitlab'
# Build reusable, testable & encapsulated view components in Ruby on Rails
gem 'view_component'
# Abstraction layer for HTTP requests in custom API wrapper
gem 'faraday'

# FIXME: Required by the mail gem
# See https://github.com/rails/rails/pull/44083
Expand Down
1 change: 1 addition & 0 deletions src/api/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ DEPENDENCIES
down
factory_bot_rails
faker
faraday
flipper
flipper-active_record
flipper-ui
Expand Down
69 changes: 69 additions & 0 deletions src/api/app/lib/gitea_api/v1/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module GiteaAPI
module V1
class Client
HTTP_OK_CODE = 200
HTTP_CREATED_CODE = 201
HTTP_BAD_REQUEST_CODE = 400
HTTP_UNAUTHORIZED_CODE = 401
HTTP_FORBIDDEN_CODE = 403
HTTP_NOT_FOUND_CODE = 404

GiteaApiError = Class.new(StandardError)
BadRequestError = Class.new(GiteaApiError)
UnauthorizedError = Class.new(GiteaApiError)
ForbiddenError = Class.new(GiteaApiError)
NotFoundError = Class.new(GiteaApiError)
ApiError = Class.new(GiteaApiError)

def initialize(api_endpoint:, token:)
@api_endpoint = api_endpoint + '/api/v1/'
@token = token
end

# owner: owner of the repository
# repo: name of the repository
# sha: sha of the commit
# https://try.gitea.io/api/swagger#/repository/repoCreateStatus
def create_commit_status(owner:, repo:, sha:, state:, **kwargs)
@response = client.post(
"repos/#{owner}/#{repo}/statuses/#{sha}",
{ state: state, context: kwargs[:context], description: kwargs[:description],
target_url: kwargs[:target_url] }
)
return @response.body if request_successful?

raise error_class, "HTTP Code: #{@response.status}, response: #{@response.body['message']}"
end

private

def client
@client ||= Faraday.new(@api_endpoint) do |f|
f.headers['Authorization'] = "token #{@token}"
f.request(:json)
f.response(:json) # Faraday decodes response body as JSON
f.adapter(:net_http)
end
end

def request_successful?
[HTTP_OK_CODE, HTTP_CREATED_CODE].include?(@response.status)
end

def error_class
case @response.status
when HTTP_BAD_REQUEST_CODE
BadRequestError
when HTTP_UNAUTHORIZED_CODE
UnauthorizedError
when HTTP_FORBIDDEN_CODE
ForbiddenError
when HTTP_NOT_FOUND_CODE
NotFoundError
else
ApiError
end
end
end
end
end
1 change: 1 addition & 0 deletions src/api/config/initializers/zeitwerk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
autoloader.inflector.inflect(
'api_matcher' => 'APIMatcher',
'cve_parser' => 'CVEParser',
'gitea_api' => 'GiteaAPI',
'meta_xml_validator' => 'MetaXMLValidator',
'obs_quality_categories_finder' => 'OBSQualityCategoriesFinder',
'opensuse_upstream_tarball_url_finder' => 'OpenSUSEUpstreamTarballURLFinder',
Expand Down

0 comments on commit 5d8dcd0

Please sign in to comment.