Commit cfd2c0d1 authored by Brett Walker's avatar Brett Walker Committed by Thong Kuah

Move issue_type enum to WorkItem::Type base_type

in preparation for moving issue_type column.
Add the default work item types to the work item type table.

Changelog: changed
parent 3e2a4ca5
......@@ -26,7 +26,7 @@
# updated_after: datetime
# updated_before: datetime
# confidential: boolean
# issue_types: array of strings (one of Issue.issue_types)
# issue_types: array of strings (one of WorkItem::Type.base_types)
#
class IssuesFinder < IssuableFinder
CONFIDENTIAL_ACCESS_LEVEL = Gitlab::Access::REPORTER
......@@ -112,7 +112,7 @@ class IssuesFinder < IssuableFinder
def by_issue_types(items)
issue_type_params = Array(params[:issue_types]).map(&:to_s)
return items if issue_type_params.blank?
return Issue.none unless (Issue.issue_types.keys & issue_type_params).sort == issue_type_params.sort
return Issue.none unless (WorkItem::Type.base_types.keys & issue_type_params).sort == issue_type_params.sort
items.with_issue_type(params[:issue_types])
end
......
......@@ -5,7 +5,7 @@ module Types
graphql_name 'IssueType'
description 'Issue type'
::Issue.issue_types.keys.each do |issue_type|
::WorkItem::Type.base_types.keys.each do |issue_type|
value issue_type.upcase, value: issue_type, description: "#{issue_type.titleize} issue type"
end
end
......
......@@ -87,12 +87,7 @@ class Issue < ApplicationRecord
validates :project, presence: true
validates :issue_type, presence: true
enum issue_type: {
issue: 0,
incident: 1,
test_case: 2, ## EE-only
requirement: 3 ## EE-only
}
enum issue_type: WorkItem::Type.base_types
alias_method :issuing_parent, :project
......
......@@ -11,7 +11,12 @@ class WorkItem::Type < ApplicationRecord
cache_markdown_field :description, pipeline: :single_line
enum base_type: Issue.issue_types
enum base_type: {
issue: 0,
incident: 1,
test_case: 2, ## EE-only
requirement: 3 ## EE-only
}
belongs_to :namespace, optional: true
has_many :work_items, class_name: 'Issue', foreign_key: :work_item_type_id, inverse_of: :work_item_type
......
# frozen_string_literal: true
class CreateBaseWorkItemTypes < ActiveRecord::Migration[6.1]
include Gitlab::Database::MigrationHelpers
module WorkItem
class Type < ActiveRecord::Base
self.table_name = 'work_item_types'
enum base_type: {
issue: 0,
incident: 1,
test_case: 2,
requirement: 3
}
validates :name, uniqueness: { case_sensitive: false, scope: [:namespace_id] }
end
end
def up
# create default types
WorkItem::Type.create(name: 'Issue', namespace_id: nil, base_type: :issue, icon_name: 'issue-type-issue')
WorkItem::Type.create(name: 'Incident', namespace_id: nil, base_type: :incident, icon_name: 'issue-type-incident')
WorkItem::Type.create(name: 'Test Case', namespace_id: nil, base_type: :test_case, icon_name: 'issue-type-test-case')
WorkItem::Type.create(name: 'Requirement', namespace_id: nil, base_type: :requirement, icon_name: 'issue-type-requirements')
end
def down
# We expect this table to be empty at the point of the up migration,
# however there is a remote possibility that issues could already be
# using one of these types, with a tight foreign constraint.
# Therefore we will not attempt to remove any data.
end
end
53d4f6ca18602b25af631a25dee7b0ebf9cb98e7a4f8cd87830f939bc13bb893
\ No newline at end of file
......@@ -6,7 +6,7 @@ module IssueWidgets
included do
# This will mean that non-Requirement issues essentially ignore this relationship and always return []
has_many :test_reports, -> { joins(:requirement_issue).where(issues: { issue_type: Issue.issue_types[:requirement] }) },
has_many :test_reports, -> { joins(:requirement_issue).where(issues: { issue_type: WorkItem::Type.base_types[:requirement] }) },
foreign_key: :issue_id, inverse_of: :requirement_issue, class_name: 'RequirementsManagement::TestReport'
has_one :requirement, class_name: 'RequirementsManagement::Requirement'
end
......
......@@ -56,7 +56,7 @@ RSpec.describe Issues::BuildService do
end
context 'as developer' do
Issue.issue_types.each_key do |issue_type|
WorkItem::Type.base_types.each_key do |issue_type|
it "sets the issue type to #{issue_type}" do
issue = build_issue(issue_type: issue_type)
......
......@@ -23,7 +23,7 @@ module API
expose :issue_type,
as: :type,
format_with: :upcase,
documentation: { type: "String", desc: "One of #{::Issue.issue_types.keys.map(&:upcase)}" }
documentation: { type: "String", desc: "One of #{::WorkItem::Type.base_types.keys.map(&:upcase)}" }
expose :assignee, using: ::API::Entities::UserBasic do |issue|
issue.assignees.first
......
......@@ -74,7 +74,7 @@ module API
desc: 'Return issues sorted in `asc` or `desc` order.'
optional :due_date, type: String, values: %w[0 overdue week month next_month_and_previous_two_weeks] << '',
desc: 'Return issues that have no due date (`0`), or whose due date is this week, this month, between two weeks ago and next month, or which are overdue. Accepts: `overdue`, `week`, `month`, `next_month_and_previous_two_weeks`, `0`'
optional :issue_type, type: String, values: Issue.issue_types.keys, desc: "The type of the issue. Accepts: #{Issue.issue_types.keys.join(', ')}"
optional :issue_type, type: String, values: WorkItem::Type.base_types.keys, desc: "The type of the issue. Accepts: #{WorkItem::Type.base_types.keys.join(', ')}"
use :issues_stats_params
use :pagination
......@@ -91,7 +91,7 @@ module API
optional :due_date, type: String, desc: 'Date string in the format YEAR-MONTH-DAY'
optional :confidential, type: Boolean, desc: 'Boolean parameter if the issue should be confidential'
optional :discussion_locked, type: Boolean, desc: " Boolean parameter indicating if the issue's discussion is locked"
optional :issue_type, type: String, values: Issue.issue_types.keys, desc: "The type of the issue. Accepts: #{Issue.issue_types.keys.join(', ')}"
optional :issue_type, type: String, values: WorkItem::Type.base_types.keys, desc: "The type of the issue. Accepts: #{WorkItem::Type.base_types.keys.join(', ')}"
use :optional_issue_params_ee
end
......
......@@ -5,11 +5,26 @@ FactoryBot.define do
namespace
name { generate(:work_item_type_name) }
icon_name { 'issue' }
base_type { Issue.issue_types['issue'] }
base_type { WorkItem::Type.base_types[:issue] }
icon_name { 'issue-type-issue' }
trait :default do
namespace { nil }
end
trait :incident do
base_type { WorkItem::Type.base_types[:incident] }
icon_name { 'issue-type-incident' }
end
trait :test_case do
base_type { WorkItem::Type.base_types[:test_case] }
icon_name { 'issue-type-test-case' }
end
trait :requirement do
base_type { WorkItem::Type.base_types[:requirement] }
icon_name { 'issue-type-requirements' }
end
end
end
# frozen_string_literal: true
require 'spec_helper'
require_migration!('create_base_work_item_types')
RSpec.describe CreateBaseWorkItemTypes, :migration do
let!(:work_item_types) { table(:work_item_types) }
it 'creates default data' do
reversible_migration do |migration|
migration.before -> {
# Depending on whether the migration has been run before,
# the size could be 4, or 0, so we don't set any expectations
}
migration.after -> {
expect(work_item_types.count).to eq 4
expect(work_item_types.all.pluck(:base_type)).to match_array WorkItem::Type.base_types.values
}
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment