summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlexandru Croitor <acroitor@gitlab.com>2019-04-24 16:08:14 +0300
committerAlexandru Croitor <acroitor@gitlab.com>2019-05-15 10:15:17 +0300
commitf117c032ac6c414e6c1dfeab98184363c1f61608 (patch)
treed3c5beb5363112dccbd2fa0cbcff573121eafda3 /lib
parenta4fbf39eca4518598e893f6f1b81b8b69927c6f9 (diff)
downloadgitlab-ce-f117c032ac6c414e6c1dfeab98184363c1f61608.tar.gz
Add params validations and remove extra params support
Remove label_name and milestone_title params support Add mutually_exclusive validation for author_id and author_username Add mutually_exclusive validation for assignee_id and assignee_username Add validation to allow single value for asignee_username on CE Add separate issue_stats_params helper for statistics params and reuse in issues_params.
Diffstat (limited to 'lib')
-rw-r--r--lib/api/helpers/issues_helpers.rb5
-rw-r--r--lib/api/issues.rb41
-rw-r--r--lib/api/validations/check_assignees_count.rb36
3 files changed, 63 insertions, 19 deletions
diff --git a/lib/api/helpers/issues_helpers.rb b/lib/api/helpers/issues_helpers.rb
index 12bbc598532..fb791691069 100644
--- a/lib/api/helpers/issues_helpers.rb
+++ b/lib/api/helpers/issues_helpers.rb
@@ -24,7 +24,6 @@ module API
args.delete(:id)
args[:milestone_title] ||= args.delete(:milestone)
- args[:milestone_title] ||= args.delete(:milestone_title)
args[:label_name] ||= args.delete(:labels)
args[:scope] = args[:scope].underscore if args[:scope]
@@ -35,10 +34,8 @@ module API
# rubocop: disable CodeReuse/ActiveRecord
finder = issue_finder(args)
issues = finder.execute.with_api_entity_associations
- order_by = declared_params[:sort].present? && %w(asc desc).include?(declared_params[:sort].downcase)
- issues = issues.reorder(order_options_with_tie_breaker) if order_by
- issues
+ issues.reorder(order_options_with_tie_breaker)
# rubocop: enable CodeReuse/ActiveRecord
end
diff --git a/lib/api/issues.rb b/lib/api/issues.rb
index 6977657e356..9fd2d959c22 100644
--- a/lib/api/issues.rb
+++ b/lib/api/issues.rb
@@ -19,15 +19,10 @@ module API
end
end
- params :issues_params do
- optional :labels, :label_name, type: Array[String], coerce_with: Validations::Types::LabelsList.coerce, desc: 'Comma-separated list of label names'
- optional :with_labels_data, type: Boolean, desc: 'Return more label data than just lable title', default: false
+ params :issues_stats_params do
+ optional :labels, type: Array[String], coerce_with: Validations::Types::LabelsList.coerce, desc: 'Comma-separated list of label names'
optional :milestone, type: String, desc: 'Milestone title'
- optional :order_by, type: String, values: %w[created_at updated_at], default: 'created_at',
- desc: 'Return issues ordered by `created_at` or `updated_at` fields.'
- optional :sort, type: String, default: 'desc',
- desc: 'Return issues sorted in `asc` or `desc` order.'
- optional :milestone, :milestone_title, type: String, desc: 'Return issues for a specific milestone'
+ optional :milestone, type: String, desc: 'Return issues for a specific milestone'
optional :iids, type: Array[Integer], desc: 'The IID array of issues'
optional :search, type: String, desc: 'Search issues for text present in the title, description, or any combination of these'
optional :in, type: String, desc: '`title`, `description`, or a string joining them with comma'
@@ -35,23 +30,39 @@ module API
optional :created_before, type: DateTime, desc: 'Return issues created before the specified time'
optional :updated_after, type: DateTime, desc: 'Return issues updated after the specified time'
optional :updated_before, type: DateTime, desc: 'Return issues updated before the specified time'
+
optional :author_id, type: Integer, desc: 'Return issues which are authored by the user with the given ID'
optional :author_username, type: String, desc: 'Return issues which are authored by the user with the given username'
+ mutually_exclusive :author_id, :author_username
+
optional :assignee_id, types: [Integer, String], integer_none_any: true,
desc: 'Return issues which are assigned to the user with the given ID'
- optional :assignee_username, type: Array[String],
+ optional :assignee_username, type: Array[String], check_assignees_count: true,
+ coerce_with: Validations::CheckAssigneesCount.coerce,
desc: 'Return issues which are assigned to the user with the given username'
+ mutually_exclusive :assignee_id, :assignee_username
+
optional :scope, type: String, values: %w[created-by-me assigned-to-me created_by_me assigned_to_me all],
desc: 'Return issues for the given scope: `created_by_me`, `assigned_to_me` or `all`'
optional :my_reaction_emoji, type: String, desc: 'Return issues reacted by the authenticated user by the given emoji'
optional :confidential, type: Boolean, desc: 'Filter confidential or public issues'
- optional :state, type: String, values: %w[opened closed all], default: 'all',
- desc: 'Return opened, closed, or all issues'
- use :pagination
use :issues_params_ee if Gitlab.ee?
end
+ params :issues_params do
+ optional :with_labels_data, type: Boolean, desc: 'Return more label data than just lable title', default: false
+ optional :state, type: String, values: %w[opened closed all], default: 'all',
+ desc: 'Return opened, closed, or all issues'
+ optional :order_by, type: String, values: %w[created_at updated_at], default: 'created_at',
+ desc: 'Return issues ordered by `created_at` or `updated_at` fields.'
+ optional :sort, type: String, values: %w[asc desc], default: 'desc',
+ desc: 'Return issues sorted in `asc` or `desc` order.'
+
+ use :issues_stats_params
+ use :pagination
+ end
+
params :issue_params do
optional :description, type: String, desc: 'The description of an issue'
optional :assignee_ids, type: Array[Integer], desc: 'The array of user IDs to assign issue'
@@ -68,7 +79,7 @@ module API
desc "Get currently authenticated user's issues statistics"
params do
- use :issues_params
+ use :issues_stats_params
optional :scope, type: String, values: %w[created-by-me assigned-to-me created_by_me assigned_to_me all], default: 'created_by_me',
desc: 'Return issues for the given scope: `created_by_me`, `assigned_to_me` or `all`'
end
@@ -131,7 +142,7 @@ module API
desc 'Get statistics for the list of group issues'
params do
- use :issues_params
+ use :issues_stats_params
end
get ":id/issues_statistics" do
group = find_group!(params[:id])
@@ -172,7 +183,7 @@ module API
desc 'Get statistics for the list of project issues'
params do
- use :issues_params
+ use :issues_stats_params
end
get ":id/issues_statistics" do
project = find_project!(params[:id])
diff --git a/lib/api/validations/check_assignees_count.rb b/lib/api/validations/check_assignees_count.rb
new file mode 100644
index 00000000000..e19c88e97b1
--- /dev/null
+++ b/lib/api/validations/check_assignees_count.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+module API
+ module Validations
+ class CheckAssigneesCount < Grape::Validations::Base
+ def self.coerce
+ lambda do |value|
+ case value
+ when String
+ [value]
+ when Array
+ value
+ when CheckAssigneesCount
+ value
+ else
+ []
+ end
+ end
+ end
+
+ def validate_param!(attr_name, params)
+ unless param_allowed?(attr_name, params)
+ raise Grape::Exceptions::Validation,
+ params: [@scope.full_name(attr_name)],
+ message: "allows one value, but found #{params[attr_name].size}: #{params[attr_name].join(", ")}"
+ end
+ end
+
+ private
+
+ def param_allowed?(attr_name, params)
+ params[attr_name].size <= 1
+ end
+ end
+ end
+end