Skip to content

Commit

Permalink
Unassigned requests for StagingWorkflow
Browse files Browse the repository at this point in the history
We have created the association between StagingWorkflow, StagingProject
and BsRequest.

Unassigned requests are all the requests related to the StagingWorkflow's main
project that are not associated to any of the staging projects.

Co-authored-by: Eduardo Navarro <enavarro@suse.com>
Co-authored-by: Victor Pereira <vpereira@suse.com>
Co-authored-by: Moisés Déniz Alemán <mdeniz@suse.com>
  • Loading branch information
4 people authored and dmarcoux committed Nov 5, 2018
1 parent 69bb786 commit 30f9bb9
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/api/app/models/bs_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class SaveError < APIError
has_many :status_reports, as: :checkable, class_name: 'Status::Report', dependent: :destroy
has_many :target_project_objects, through: :bs_request_actions

belongs_to :staging_project, class_name: 'Project', foreign_key: 'staging_project_id'

validates :state, inclusion: { in: VALID_REQUEST_STATES }
validates :creator, presence: true
validate :check_supersede_state
Expand Down
4 changes: 4 additions & 0 deletions src/api/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def autocomplete(search)
has_many :target_of_bs_request_actions, class_name: 'BsRequestAction', foreign_key: 'target_project_id'
has_many :target_of_bs_requests, through: :target_of_bs_request_actions, source: :bs_request

has_one :staging, class_name: 'StagingWorkflow', inverse_of: :project
belongs_to :staging_workflow, inverse_of: :staging_projects
has_many :staged_requests, class_name: 'BsRequest', foreign_key: 'staging_project_id'

default_scope { where('projects.id not in (?)', Relationship.forbidden_project_ids) }

scope :maintenance, -> { where("kind = 'maintenance'") }
Expand Down
15 changes: 15 additions & 0 deletions src/api/app/models/staging_workflow.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class StagingWorkflow < ApplicationRecord
belongs_to :project, inverse_of: :staging
has_many :staging_projects, class_name: 'Project', inverse_of: :staging_workflow, dependent: :nullify

has_many :target_of_bs_requests, through: :project
has_many :staged_requests, class_name: 'BsRequest', through: :staging_projects

def unassigned_requests
target_of_bs_requests.in_states(['new', 'review']) - staged_requests - ignored_requests
end

def ignored_requests
BsRequest.none # TODO: define this method
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddStagedRequestIdToBsRequests < ActiveRecord::Migration[5.2]
def change
change_table :bs_requests, bulk: true do |t|
t.integer :staging_project_id
t.index :staging_project_id
end
end
end
8 changes: 5 additions & 3 deletions src/api/db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,13 @@ CREATE TABLE `bs_requests` (
`number` int(11) DEFAULT NULL,
`updated_when` datetime DEFAULT NULL,
`approver` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`staging_project_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `index_bs_requests_on_number` (`number`),
KEY `index_bs_requests_on_creator` (`creator`) USING BTREE,
KEY `index_bs_requests_on_state` (`state`) USING BTREE,
KEY `index_bs_requests_on_superseded_by` (`superseded_by`)
KEY `index_bs_requests_on_superseded_by` (`superseded_by`),
KEY `index_bs_requests_on_staging_project_id` (`staging_project_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;

CREATE TABLE `channel_binaries` (
Expand Down Expand Up @@ -1407,5 +1409,5 @@ INSERT INTO `schema_migrations` (version) VALUES
('20180906142802'),
('20180911123709'),
('20180924135535');


('20181008150453'),
('20181016103905');
8 changes: 8 additions & 0 deletions src/api/spec/factories/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,13 @@
end
end
end

factory :staging_project do
transient do
sequence(:workflow_project_name) { |n| "workflow_project_#{n}" }
end

sequence(:name, [*'A'..'Z'].cycle) { |letter| "#{workflow_project_name}:Staging:#{letter}" }
end
end
end
15 changes: 15 additions & 0 deletions src/api/spec/factories/staging_workflow.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FactoryBot.define do
factory :staging_workflow do
factory :staging_workflow_with_staging_projects do
transient do
staging_project_count { 2 }
end

after(:create) do |staging_workflow, evaluator|
evaluator.staging_project_count.times do
staging_workflow.staging_projects << create(:staging_project, workflow_project_name: staging_workflow.project.name)
end
end
end
end
end
57 changes: 57 additions & 0 deletions src/api/spec/models/staging_workflow_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'rails_helper'

RSpec.describe StagingWorkflow, type: :model do
let(:project) { create(:project_with_package) }
let(:staging_workflow) { create(:staging_workflow_with_staging_projects, project: project) }
let(:staging_project) { staging_workflow.staging_projects.first }
let(:source_project) { create(:project, name: 'source_project') }
let(:target_package) { create(:package, name: 'target_package', project: project) }
let(:source_package) { create(:package, name: 'source_package', project: source_project) }
let(:bs_request) do
create(:bs_request_with_submit_action,
target_project: project.name,
target_package: target_package.name,
source_project: source_project.name,
source_package: source_package.name)
end

describe '#unassigned_request' do
subject { staging_workflow.unassigned_requests }

context 'without requests in the main project' do
it { expect(subject).to be_empty }
end

context 'with requests but not in staging projects' do
before { bs_request }

it { expect(subject).to contain_exactly(bs_request) }
end

context 'with requests but all of them are in staging projects' do
before do
bs_request.staging_project = staging_project
bs_request.save
end

it { expect(subject).to be_empty }
end

context 'with requests and some are in staging projects and some not' do
let!(:bs_request_2) do
create(:bs_request_with_submit_action,
target_project: project.name,
target_package: target_package.name,
source_project: source_project.name,
source_package: source_package.name)
end

before do
bs_request.staging_project = staging_project
bs_request.save
end

it { expect(subject).to contain_exactly(bs_request_2) }
end
end
end

0 comments on commit 30f9bb9

Please sign in to comment.