Skip to content

Commit

Permalink
Report multibuild flavors if package is multibuild
Browse files Browse the repository at this point in the history
Co-authored-by: Dany Marcoux <dmarcoux@suse.com>
Co-authored-by: Victor Pereira <vpereira@suse.com>
  • Loading branch information
3 people committed Jan 20, 2022
1 parent 550f59c commit 0ae6691
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 27 deletions.
22 changes: 17 additions & 5 deletions src/api/app/models/workflow/step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def target_project_name
end

def target_package
Package.find_by_project_and_name(target_project_name, target_package_name)
Package.get_by_project_and_name(target_project_name, target_package_name, follow_multibuild: true)
rescue Project::Errors::UnknownObjectError, Package::Errors::UnknownObjectError
# We rely on Package.get_by_project_and_name since it's the only way to work with multibuild packages.
# It's possible for a package to not exist, so we simply rescue and do nothing. The package will be created later in the step.
end

def create_or_update_subscriptions(package, workflow_filters)
Expand All @@ -43,7 +46,7 @@ def create_or_update_subscriptions(package, workflow_filters)
enabled: true,
token: @token,
package: package)
subscription.update!(payload: scm_webhook.payload.merge({ workflow_filters: workflow_filters, short_package_name: target_package_name(short_commit_sha: true) }))
subscription.update!(payload: scm_webhook.payload.merge({ workflow_filters: workflow_filters }))
end
end

Expand All @@ -68,6 +71,10 @@ def source_project_name
step_instructions[:source_project]
end

def target_package_names
[target_package_name(short_commit_sha: true)] + multibuild_flavors
end

def target_package_name(short_commit_sha: false)
package_name = step_instructions[:target_package] || source_package_name

Expand All @@ -88,6 +95,10 @@ def target_package_name(short_commit_sha: false)

private

def multibuild_flavors
target_package.multibuild_flavors.collect { |flavor| "#{target_package_name}:#{flavor}" }
end

def target_project_base_name
raise AbstractMethodCalled
end
Expand Down Expand Up @@ -156,9 +167,10 @@ def report_to_scm(workflow_filters)
workflow_repositories(target_project_name, workflow_filters).each do |repository|
# TODO: Fix n+1 queries
workflow_architectures(repository, workflow_filters).each do |architecture|
# We cannot report multibuild flavors here... so they will be missing from the initial report
SCMStatusReporter.new({ project: target_project_name, package: target_package_name, repository: repository.name, arch: architecture.name },
scm_webhook.payload.merge({ short_package_name: target_package_name(short_commit_sha: true) }), @token.scm_token).call
target_package_names.each do |target_package_name_or_flavor|
SCMStatusReporter.new({ project: target_project_name, package: target_package_name_or_flavor, repository: repository.name, arch: architecture.name },
scm_webhook.payload, @token.scm_token).call
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/api/app/services/scm_status_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def github?
end

def status_options
{ context: "OBS: #{@event_subscription_payload[:short_package_name]} - #{@event_payload[:repository]}/#{@event_payload[:arch]}",
{ 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

Expand Down
70 changes: 57 additions & 13 deletions src/api/spec/models/workflow/step/branch_package_step_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,9 @@
# 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.merge({ short_package_name: final_short_package_name }),
token.scm_token).and_call_original
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.merge({ short_package_name: final_short_package_name }),
token.scm_token).and_call_original

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' },
Expand Down Expand Up @@ -175,7 +172,6 @@
let(:package) { create(:package_with_file, name: 'bar_package', project: project) }
let(:target_project_final_name) { "home:#{user.login}:openSUSE:open-build-service:PR-1" }
let(:final_package_name) { package.name }
let(:final_short_package_name) { package.name }

before do
project
Expand Down Expand Up @@ -223,6 +219,57 @@
it_behaves_like 'fails with insufficient write permission on target project'
end

context 'for a multibuild package' do
let(:action) { 'opened' }
let(:package) { create(:multibuild_package, name: 'multibuild_package', project: project) }
let(:octokit_client) { instance_double(Octokit::Client) }
let(:step_instructions) do
{
source_project: package.project.name,
source_package: package.name,
target_project: target_project_name
}
end
let(:workflow_filters) do
{ architectures: { only: ['x86_64', 'i586'] }, repositories: { ignore: ['openSUSE_Tumbleweed'] } }
end

before do
allow(Octokit::Client).to receive(:new).and_return(octokit_client)
allow(octokit_client).to receive(:create_status).and_return(true)

create(:repository, name: 'Unicorn_123', project: package.project, architectures: ['x86_64', 'i586', 'ppc', 'aarch64'])
create(:repository, name: 'openSUSE_Tumbleweed', project: package.project, architectures: ['x86_64'])
end

it { expect { subject.call }.to(change(Package, :count).by(1)) }
it { expect(subject.call.project.name).to eq(target_project_final_name) }
it { expect { subject.call.source_file('_branch_request') }.not_to raise_error }
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
context 'when the branched package already existed' do
it_behaves_like 'successful update event when the branch_package already exists' do
Expand All @@ -233,7 +280,7 @@
end
let(:update_payload) do
{ 'action' => 'synchronize', 'commit_sha' => long_commit_sha, 'event' => 'pull_request', 'pr_number' => 1,
'scm' => 'github', 'source_repository_full_name' => 'reponame', 'short_package_name' => package.name,
'scm' => 'github', 'source_repository_full_name' => 'reponame',
'target_repository_full_name' => 'openSUSE/open-build-service', 'workflow_filters' => {} }
end
let(:existing_branch_request_file) do
Expand Down Expand Up @@ -272,8 +319,7 @@

let(:octokit_client) { instance_double(Octokit::Client) }
let(:target_project_final_name) { "home:#{user.login}" }
let(:final_package_name) { "#{package.name}-#{long_commit_sha}" }
let(:final_short_package_name) { "#{package.name}-#{short_commit_sha}" }
let(:final_package_name) { "#{package.name}-#{short_commit_sha}" }

before do
# branching a package to an existing project doesn't take over the set repositories
Expand Down Expand Up @@ -394,8 +440,7 @@
let(:update_payload) do
{ 'action' => 'update', 'commit_sha' => long_commit_sha, 'event' => 'Merge Request Hook',
'pr_number' => 1, 'scm' => 'gitlab', 'source_repository_full_name' => 'reponame',
'short_package_name' => package.name, 'path_with_namespace' => 'openSUSE/open-build-service',
'workflow_filters' => {} }
'path_with_namespace' => 'openSUSE/open-build-service', 'workflow_filters' => {} }
end
let(:existing_branch_request_file) do
{ object_kind: 'update',
Expand Down Expand Up @@ -426,8 +471,7 @@

let(:gitlab_client) { instance_double(Gitlab::Client) }
let(:target_project_final_name) { "home:#{user.login}" }
let(:final_package_name) { "#{package.name}-#{long_commit_sha}" }
let(:final_short_package_name) { "#{package.name}-#{short_commit_sha}" }
let(:final_package_name) { "#{package.name}-#{short_commit_sha}" }

before do
# branching a package to an existing project doesn't take over the set repositories
Expand Down
4 changes: 2 additions & 2 deletions src/api/spec/models/workflow/step/link_package_step_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
'target_repository_full_name' => 'openSUSE/open-build-service' }
end
let(:update_payload) do
{ 'action' => 'synchronize', 'commit_sha' => '456', 'event' => 'pull_request', 'pr_number' => 1, 'scm' => 'github', 'short_package_name' => 'bar_package',
{ 'action' => 'synchronize', 'commit_sha' => '456', 'event' => 'pull_request', 'pr_number' => 1, 'scm' => 'github',
'source_repository_full_name' => 'reponame', 'target_repository_full_name' => 'openSUSE/open-build-service', 'workflow_filters' => {} }
end
let(:commit_sha) { '456' }
Expand Down Expand Up @@ -439,7 +439,7 @@
'path_with_namespace' => 'openSUSE/open-build-service' }
end
let(:update_payload) do
{ 'action' => 'update', 'commit_sha' => '456', 'event' => 'Merge Request Hook', 'pr_number' => 1, 'scm' => 'gitlab', 'short_package_name' => 'bar_package',
{ 'action' => 'update', 'commit_sha' => '456', 'event' => 'Merge Request Hook', 'pr_number' => 1, 'scm' => 'gitlab',
'source_repository_full_name' => 'reponame', 'path_with_namespace' => 'openSUSE/open-build-service', 'workflow_filters' => {} }
end
let(:commit_sha) { '456' }
Expand Down
10 changes: 4 additions & 6 deletions src/api/spec/services/scm_status_reporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,14 @@
repository: 'openSUSE_Tumbleweed', arch: 'x86_64' }
end
let(:event_subscription_payload) do
{ scm: 'github', short_package_name: 'hello_world-1234567',
target_repository_full_name: 'danidoni/hello_world', commit_sha: '123456789' }
{ scm: 'github', target_repository_full_name: 'danidoni/hello_world', commit_sha: '123456789' }
end
let(:token) { 'XYCABC' }
let(:event_type) { nil }
let(:state) { 'pending' }
let(:status_options) do
{
context: 'OBS: hello_world-1234567 - openSUSE_Tumbleweed/x86_64',
context: 'OBS: hello_world - openSUSE_Tumbleweed/x86_64',
target_url: 'https://unconfigured.openbuildservice.org/package/show/home:danidoni/hello_world'
}
end
Expand All @@ -108,15 +107,14 @@
repository: 'openSUSE_Tumbleweed', arch: 'x86_64' }
end
let(:event_subscription_payload) do
{ scm: 'gitlab', short_package_name: 'hello_world-1234567',
project_id: '26_212_710', commit_sha: '123456789' }
{ scm: 'gitlab', project_id: '26_212_710', commit_sha: '123456789' }
end
let(:token) { 'XYCABC' }
let(:event_type) { nil }
let(:state) { 'pending' }
let(:status_options) do
{
context: 'OBS: hello_world-1234567 - openSUSE_Tumbleweed/x86_64',
context: 'OBS: hello_world - openSUSE_Tumbleweed/x86_64',
target_url: 'https://unconfigured.openbuildservice.org/package/show/home:danidoni/hello_world'
}
end
Expand Down

0 comments on commit 0ae6691

Please sign in to comment.