diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-01 18:08:45 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-01 18:08:45 +0300 |
commit | ae1efa2e1d32dee59d8f509ba17b623b5ffe4ba6 (patch) | |
tree | a4cba8561c1671934751508ead7b2b1053e783ec /app/services | |
parent | a4c655515155710b3695699ea88d824f65af6446 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/jira/jql_builder_service.rb | 30 | ||||
-rw-r--r-- | app/services/jira/requests/base.rb | 16 | ||||
-rw-r--r-- | app/services/jira/requests/issues/list_service.rb | 56 | ||||
-rw-r--r-- | app/services/jira/requests/projects.rb | 37 | ||||
-rw-r--r-- | app/services/jira/requests/projects/list_service.rb | 47 |
5 files changed, 146 insertions, 40 deletions
diff --git a/app/services/jira/jql_builder_service.rb b/app/services/jira/jql_builder_service.rb new file mode 100644 index 00000000000..cb8cee8e38a --- /dev/null +++ b/app/services/jira/jql_builder_service.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module Jira + class JqlBuilderService + DEFAULT_SORT = "created" + DEFAULT_SORT_DIRECTION = "DESC" + + def initialize(jira_project_key, params = {}) + @jira_project_key = jira_project_key + @sort = params[:sort] || DEFAULT_SORT + @sort_direction = params[:sort_direction] || DEFAULT_SORT_DIRECTION + end + + def execute + [by_project, order_by].join(' ') + end + + private + + attr_reader :jira_project_key, :sort, :sort_direction + + def by_project + "project = #{jira_project_key}" + end + + def order_by + "order by #{sort} #{sort_direction}" + end + end +end diff --git a/app/services/jira/requests/base.rb b/app/services/jira/requests/base.rb index 0934730d10c..7c6db372257 100644 --- a/app/services/jira/requests/base.rb +++ b/app/services/jira/requests/base.rb @@ -5,12 +5,11 @@ module Jira class Base include ProjectServicesLoggable - attr_reader :jira_service, :project, :query + JIRA_API_VERSION = 2 - def initialize(jira_service, query: nil) + def initialize(jira_service, params = {}) @project = jira_service&.project @jira_service = jira_service - @query = query end def execute @@ -19,8 +18,19 @@ module Jira request end + def base_api_url + "/rest/api/#{api_version}" + end + private + attr_reader :jira_service, :project + + # override this method in the specific request class implementation if a differnt API version is required + def api_version + JIRA_API_VERSION + end + def client @client ||= jira_service.client end diff --git a/app/services/jira/requests/issues/list_service.rb b/app/services/jira/requests/issues/list_service.rb new file mode 100644 index 00000000000..44a3d3966a8 --- /dev/null +++ b/app/services/jira/requests/issues/list_service.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +module Jira + module Requests + module Issues + class ListService < Base + extend ::Gitlab::Utils::Override + + PER_PAGE = 100 + + def initialize(jira_service, params = {}) + super(jira_service, params) + + @jql = params[:jql].to_s + @page = params[:page].to_i || 1 + end + + private + + attr_reader :jql, :page + + override :url + def url + "#{base_api_url}/search?jql=#{CGI.escape(jql)}&startAt=#{start_at}&maxResults=#{PER_PAGE}&fields=*all" + end + + override :build_service_response + def build_service_response(response) + return ServiceResponse.success(payload: empty_payload) if response.blank? || response["issues"].blank? + + ServiceResponse.success(payload: { + issues: map_issues(response["issues"]), + is_last: last?(response), + total_count: response["total"].to_i + }) + end + + def map_issues(response) + response.map { |v| JIRA::Resource::Issue.build(client, v) } + end + + def empty_payload + { issues: [], is_last: true, total_count: 0 } + end + + def last?(response) + response["total"].to_i <= response["startAt"].to_i + response["issues"].size + end + + def start_at + (page - 1) * PER_PAGE + end + end + end + end +end diff --git a/app/services/jira/requests/projects.rb b/app/services/jira/requests/projects.rb deleted file mode 100644 index afb3b45fac1..00000000000 --- a/app/services/jira/requests/projects.rb +++ /dev/null @@ -1,37 +0,0 @@ -# frozen_string_literal: true - -module Jira - module Requests - class Projects < Base - extend ::Gitlab::Utils::Override - - private - - override :url - def url - '/rest/api/2/project' - end - - override :build_service_response - def build_service_response(response) - return ServiceResponse.success(payload: empty_payload) unless response.present? - - ServiceResponse.success(payload: { projects: map_projects(response), is_last: true }) - end - - def map_projects(response) - response.map { |v| JIRA::Resource::Project.build(client, v) }.select(&method(:match_query?)) - end - - def match_query?(jira_project) - query = self.query.to_s.downcase - - jira_project&.key&.downcase&.include?(query) || jira_project&.name&.downcase&.include?(query) - end - - def empty_payload - { projects: [], is_last: true } - end - end - end -end diff --git a/app/services/jira/requests/projects/list_service.rb b/app/services/jira/requests/projects/list_service.rb new file mode 100644 index 00000000000..8ecfd358ffb --- /dev/null +++ b/app/services/jira/requests/projects/list_service.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +module Jira + module Requests + module Projects + class ListService < Base + extend ::Gitlab::Utils::Override + + def initialize(jira_service, params: {}) + super(jira_service, params) + + @query = params[:query] + end + + private + + attr_reader :query + + override :url + def url + "#{base_api_url}/project" + end + + override :build_service_response + def build_service_response(response) + return ServiceResponse.success(payload: empty_payload) unless response.present? + + ServiceResponse.success(payload: { projects: map_projects(response), is_last: true }) + end + + def map_projects(response) + response.map { |v| JIRA::Resource::Project.build(client, v) }.select(&method(:match_query?)) + end + + def match_query?(jira_project) + query = query.to_s.downcase + + jira_project&.key&.downcase&.include?(query) || jira_project&.name&.downcase&.include?(query) + end + + def empty_payload + { projects: [], is_last: true } + end + end + end + end +end |