Skip to content

Commit

Permalink
Merge pull request #12154 from vpereira/handle_initial_report
Browse files Browse the repository at this point in the history
Create an initial generic status
  • Loading branch information
vpereira committed Feb 4, 2022
2 parents b17cbb2 + c907548 commit a2c18eb
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 47 deletions.
7 changes: 6 additions & 1 deletion src/api/app/models/token/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ def call(options)
yaml_file = Workflows::YAMLDownloader.new(@scm_webhook.payload, token: self).call
@workflows = Workflows::YAMLToWorkflowsService.new(yaml_file: yaml_file, scm_webhook: @scm_webhook, token: self, workflow_run_id: options[:workflow_run].id).call

@workflows.each(&:call) if validation_errors.none?
return validation_errors unless validation_errors.none?

# This is just an initial generic report to give a feedback asap. Initial status pending
ScmInitialStatusReporter.new(@scm_webhook.payload, @scm_webhook.payload, scm_token).call
@workflows.each(&:call)
ScmInitialStatusReporter.new(@scm_webhook.payload, @scm_webhook.payload, scm_token, 'success').call

# Always returning validation errors to report them back to the SCM in order to help users debug their workflows
validation_errors
Expand Down
5 changes: 1 addition & 4 deletions src/api/app/models/workflow/step/branch_package_step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ def branch_package(workflow_filters = {})
add_branch_request_file(package: target_package)

# SCMs don't support statuses for tags, so we don't need to report back in this case
unless scm_webhook.tag_push_event?
create_or_update_subscriptions(target_package, workflow_filters)
report_to_scm(workflow_filters)
end
create_or_update_subscriptions(target_package, workflow_filters) unless scm_webhook.tag_push_event?

target_package
end
Expand Down
5 changes: 1 addition & 4 deletions src/api/app/models/workflow/step/link_package_step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ def link_package(workflow_filters = {})
add_branch_request_file(package: target_package)

# SCMs don't support statuses for tags, so we don't need to report back in this case
unless scm_webhook.tag_push_event?
create_or_update_subscriptions(target_package, workflow_filters)
report_to_scm(workflow_filters)
end
create_or_update_subscriptions(target_package, workflow_filters) unless scm_webhook.tag_push_event?

target_package
end
Expand Down
14 changes: 14 additions & 0 deletions src/api/app/services/scm_initial_status_reporter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ScmInitialStatusReporter < SCMStatusReporter
attr_accessor :state

def initialize(event_payload, event_subscription_payload, scm_token, event_type = nil)
super(event_payload, event_subscription_payload, scm_token)
@state = event_type.nil? ? 'pending' : 'success'
end

private

def status_options
{ context: 'OBS SCM/CI Workflow Integration started' }
end
end
6 changes: 6 additions & 0 deletions src/api/spec/models/token/workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
allow(Workflows::YAMLToWorkflowsService).to receive(:new).with(yaml_file: yaml_file, scm_webhook: scm_webhook, token: workflow_token,
workflow_run_id: workflow_run.id).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 @@ -81,6 +82,11 @@
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
38 changes: 0 additions & 38 deletions src/api/spec/models/workflow/step/branch_package_step_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,6 @@
it { expect(subject.call.source_file('_branch_request')).to include('123') }
it { expect { subject.call }.to(change(EventSubscription.where(eventtype: 'Event::BuildFail'), :count).by(1)) }
it { expect { subject.call }.to(change(EventSubscription.where(eventtype: 'Event::BuildSuccess'), :count).by(1)) }

# rubocop:disable RSpec/MultipleExpectations, RSpec/ExampleLength, RSpec/MessageSpies
# RSpec/MultipleExpectations, RSpec/ExampleLength - We want to test those expectations together since they depend on each other to be true
# RSpec/MesssageSpies - The method `and_call_original` isn't available on `have_received`, so we need to use `receive`
it 'only reports for repositories and architectures matching the filters' do
expect(SCMStatusReporter).to receive(:new).with({ project: target_project_final_name, package: final_package_name, repository: 'Unicorn_123', arch: 'i586' },
scm_webhook.payload, token.scm_token).and_call_original
expect(SCMStatusReporter).to receive(:new).with({ project: target_project_final_name, package: final_package_name, repository: 'Unicorn_123', arch: 'x86_64' },
scm_webhook.payload, token.scm_token).and_call_original
expect(SCMStatusReporter).not_to receive(:new).with({ project: target_project_final_name, package: final_package_name, repository: 'Unicorn_123', arch: 'ppc' },
scm_webhook.payload, token.scm_token)
expect(SCMStatusReporter).not_to receive(:new).with({ project: target_project_final_name, package: final_package_name, repository: 'Unicorn_123', arch: 'aarch64' },
scm_webhook.payload, token.scm_token)
expect(SCMStatusReporter).not_to receive(:new).with({ project: target_project_final_name, package: final_package_name, repository: 'openSUSE_Tumbleweed', arch: 'x86_64' },
scm_webhook.payload, token.scm_token)
subject.call({ workflow_filters: workflow_filters })
end
# rubocop:enable RSpec/MultipleExpectations, RSpec/ExampleLength, RSpec/MessageSpies
end

RSpec.shared_context 'successful update event when the branch_package already exists' do
Expand Down Expand Up @@ -248,26 +230,6 @@
it { expect(subject.call.source_file('_branch_request')).to include('123') }
it { expect { subject.call }.to(change(EventSubscription.where(eventtype: 'Event::BuildFail'), :count).by(1)) }
it { expect { subject.call }.to(change(EventSubscription.where(eventtype: 'Event::BuildSuccess'), :count).by(1)) }

# rubocop:disable RSpec/MultipleExpectations, RSpec/ExampleLength, RSpec/MessageSpies
# RSpec/MultipleExpectations, RSpec/ExampleLength - We want to test those expectations together since they depend on each other to be true
# RSpec/MesssageSpies - The method `and_call_original` isn't available on `have_received`, so we need to use `receive`
it 'only reports for repositories and architectures matching the filters' do
[final_package_name, "#{final_package_name}:flavor_a", "#{final_package_name}:flavor_b"].each do |package_or_flavor_name|
expect(SCMStatusReporter).to receive(:new).with({ project: target_project_final_name, package: package_or_flavor_name, repository: 'Unicorn_123', arch: 'i586' },
scm_webhook.payload, token.scm_token).and_call_original
expect(SCMStatusReporter).to receive(:new).with({ project: target_project_final_name, package: package_or_flavor_name, repository: 'Unicorn_123', arch: 'x86_64' },
scm_webhook.payload, token.scm_token).and_call_original
expect(SCMStatusReporter).not_to receive(:new).with({ project: target_project_final_name, package: package_or_flavor_name, repository: 'Unicorn_123', arch: 'ppc' },
scm_webhook.payload, token.scm_token)
expect(SCMStatusReporter).not_to receive(:new).with({ project: target_project_final_name, package: package_or_flavor_name, repository: 'Unicorn_123', arch: 'aarch64' },
scm_webhook.payload, token.scm_token)
expect(SCMStatusReporter).not_to receive(:new).with({ project: target_project_final_name, package: package_or_flavor_name, repository: 'openSUSE_Tumbleweed', arch: 'x86_64' },
scm_webhook.payload, token.scm_token)
end
subject.call({ workflow_filters: workflow_filters })
end
# rubocop:enable RSpec/MultipleExpectations, RSpec/ExampleLength, RSpec/MessageSpies
end

context 'for an updated PR event' do
Expand Down

0 comments on commit a2c18eb

Please sign in to comment.