Skip to content

Commit

Permalink
Fix specs and final_scm_state method
Browse files Browse the repository at this point in the history
  • Loading branch information
rubhanazeem committed Jul 15, 2022
1 parent cbb20f7 commit 54ad2a6
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
11 changes: 11 additions & 0 deletions src/api/app/services/github_status_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,15 @@ def call
end
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity

def self.scm_final_state(event_type)
case event_type
when 'Event::BuildFail'
'failure'
when 'Event::BuildSuccess'
'success'
else
'pending'
end
end
end
11 changes: 11 additions & 0 deletions src/api/app/services/gitlab_status_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ def call
rescue_with_handler(e) || raise(e)
RabbitmqBus.send_to_bus('metrics', "scm_status_report,status=fail,scm=#{@event_subscription_payload[:scm]},exception=#{e} value=1") if @workflow_run.present?
end

def self.scm_final_state(event_type)
case event_type
when 'Event::BuildFail'
'failed'
when 'Event::BuildSuccess'
'success'
else
'pending'
end
end
end
15 changes: 8 additions & 7 deletions src/api/app/services/scm_status_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(event_payload, event_subscription_payload, scm_token, workflow_ru
end

def call
if @event_subscription_payload[:scm] == 'github'
if github?
GithubStatusReporter.new(@event_payload,
@event_subscription_payload,
@scm_token,
Expand All @@ -31,6 +31,10 @@ def call
end
end

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

private

def status_options
Expand All @@ -54,13 +58,10 @@ def request_context
end

def scm_final_state(event_type)
case event_type
when 'Event::BuildFail'
@event_subscription_payload[:scm] == 'github' ? 'failure' : 'failed'
when 'Event::BuildSuccess'
'success'
if github?
GithubStatusReporter.scm_final_state(event_type)
else
'pending'
GitlabStatusReporter.scm_final_state(event_type)
end
end
end
6 changes: 0 additions & 6 deletions src/api/spec/models/token/workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
allow(Workflows::YAMLToWorkflowsService).to receive(:new).with(yaml_file: yaml_file, scm_webhook: scm_webhook, token: workflow_token,
workflow_run: workflow_run).and_return(yaml_to_workflows_service)
allow(yaml_to_workflows_service).to receive(:call).and_return(workflows)
allow(ScmInitialStatusReporter).to receive(:new).and_return(proc { true })
end

subject { workflow_token.call(workflow_run: workflow_run, scm_webhook: scm_extractor.call) }
Expand All @@ -99,11 +98,6 @@
end

it { expect { subject }.to change(workflow_token, :triggered_at) & change(workflow_run, :response_url).to('https://api.github.com') }

it do
subject
expect(ScmInitialStatusReporter).to have_received(:new).twice
end
end

context 'with validation errors' do
Expand Down
10 changes: 8 additions & 2 deletions src/api/spec/services/github_status_reporter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
require 'rails_helper'

RSpec.describe GithubStatusReporter, type: :service do
let(:scm_status_reporter) { GithubStatusReporter.new(event_payload, event_subscription_payload, token, event_type) }
let(:scm_status_reporter) { GithubStatusReporter.new(event_payload, event_subscription_payload, token, workflow_run, event_type, initial_report: initial_report) }

describe '.new' do
context 'status pending when event_type is missing' do
let(:event_payload) { {} }
let(:event_subscription_payload) { {} }
let(:token) { 'XYCABC' }
let(:event_type) { nil }
let(:workflow_run) { nil }
let(:initial_report) { false }

subject { scm_status_reporter }

Expand All @@ -20,6 +22,8 @@
let(:event_subscription_payload) { { scm: 'github' } }
let(:token) { 'XYCABC' }
let(:event_type) { 'Event::BuildFail' }
let(:workflow_run) { nil }
let(:initial_report) { false }

subject { scm_status_reporter }

Expand All @@ -29,7 +33,7 @@

describe '#call' do
context 'when sending a report back to SCM fails' do
let(:scm_status_reporter) { GithubStatusReporter.new(event_payload, event_subscription_payload, token, event_type, workflow_run) }
let(:scm_status_reporter) { GithubStatusReporter.new(event_payload, event_subscription_payload, token, workflow_run, event_type, initial_report: false) }

let!(:user) { create(:confirmed_user, :with_home, login: 'jane_doe') }
let!(:package) { create(:package, name: 'bye', project: user.home_project) }
Expand Down Expand Up @@ -112,6 +116,8 @@
let(:token) { 'XYCABC' }
let(:event_type) { nil }
let(:state) { 'pending' }
let(:workflow_run) { nil }
let(:initial_report) { false }
let(:status_options) do
{
context: 'OBS: hello_world - openSUSE_Tumbleweed/x86_64',
Expand Down
8 changes: 7 additions & 1 deletion src/api/spec/services/gitlab_status_reporter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
require 'rails_helper'

RSpec.describe GitlabStatusReporter, type: :service do
let(:scm_status_reporter) { GitlabStatusReporter.new(event_payload, event_subscription_payload, token, event_type) }
let(:scm_status_reporter) { GitlabStatusReporter.new(event_payload, event_subscription_payload, token, workflow_run, event_type, initial_report: initial_report) }

describe '.new' do
context 'status pending when event_type is missing' do
let(:event_payload) { {} }
let(:event_subscription_payload) { {} }
let(:token) { 'XYCABC' }
let(:event_type) { nil }
let(:workflow_run) { nil }
let(:initial_report) { false }

subject { scm_status_reporter }

Expand All @@ -20,6 +22,8 @@
let(:event_subscription_payload) { { scm: 'gitlab' } }
let(:token) { 'XYCABC' }
let(:event_type) { 'Event::BuildFail' }
let(:workflow_run) { nil }
let(:initial_report) { false }

subject { scm_status_reporter }

Expand All @@ -39,6 +43,8 @@
let(:token) { 'XYCABC' }
let(:event_type) { nil }
let(:state) { 'pending' }
let(:workflow_run) { nil }
let(:initial_report) { false }
let(:status_options) do
{
context: 'OBS: hello_world - openSUSE_Tumbleweed/x86_64',
Expand Down

0 comments on commit 54ad2a6

Please sign in to comment.