Skip to content

Commit

Permalink
Add status reporter 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 5d8dcd0 commit c9a1370
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
41 changes: 41 additions & 0 deletions src/api/app/services/gitea_status_reporter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class GiteaStatusReporter < SCMExceptionHandler
def initialize(event_payload, event_subscription_payload, scm_token, state, workflow_run = nil, initial_report: false)
super(event_payload, event_subscription_payload, scm_token, workflow_run)

@state = state
@initial_report = initial_report
end

def call
gitea_client = GiteaAPI::V1::Client.new(api_endpoint: @event_subscription_payload[:api_endpoint],
token: @scm_token)
owner, repository_name = @event_subscription_payload[:target_repository_full_name].split('/')
gitea_client.create_commit_status(owner: owner, repo: repository_name,
sha: @event_subscription_payload[:commit_sha],
state: @state, **status_options)
@workflow_run.save_scm_report_success(request_context) if @workflow_run.present?
end

private

def status_options
if @initial_report
{ context: 'OBS SCM/CI Workflow Integration started',
target_url: Rails.application.routes.url_helpers.token_workflow_run_url(@workflow_run.token_id, @workflow_run.id, host: Configuration.obs_url) }
else
{ context: "OBS: #{@event_payload[:package]} - #{@event_payload[:repository]}/#{@event_payload[:arch]}",
target_url: Rails.application.routes.url_helpers.package_show_url(@event_payload[:project], @event_payload[:package], host: Configuration.obs_url) }
end
end

# TODO: extract to a parent class
def request_context
{
api_endpoint: @event_subscription_payload[:api_endpoint],
target_repository_full_name: @event_subscription_payload[:target_repository_full_name],
commit_sha: @event_subscription_payload[:commit_sha],
state: @state,
status_options: status_options
}
end
end
17 changes: 16 additions & 1 deletion src/api/app/services/scm_status_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,35 @@ def call
@state,
@workflow_run,
initial_report: @initial_report).call
else
elsif gitlab?
GitlabStatusReporter.new(@event_payload,
@event_subscription_payload,
@scm_token,
@state,
@workflow_run,
initial_report: @initial_report).call
elsif gitea?
GiteaStatusReporter.new(@event_payload,
@event_subscription_payload,
@scm_token,
@state,
@workflow_run,
initial_report: @initial_report).call
end
end

def github?
@event_subscription_payload[:scm] == 'github'
end

def gitlab?
@event_subscription_payload[:scm] == 'gitlab'
end

def gitea?
@event_subscription_payload[:scm] == 'gitea'
end

private

def scm_final_state(event_type)
Expand Down

0 comments on commit c9a1370

Please sign in to comment.