From cc9a701c4a6d74a50ad526d99245ee4ed005928b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kaio=20Magalh=C3=A3es?= Date: Mon, 25 Mar 2024 04:32:33 +0000 Subject: [PATCH] improve issues --- .rubocop_todo.yml | 18 ++++++------------ app/controllers/issues_controller.rb | 12 +++++++++++- app/models/issue.rb | 2 ++ app/services/issues_creator.rb | 19 ++++++++++++------- app/utils/clients/tts/azure/issue.rb | 3 ++- .../azure/parsers/ministry_brands_parser.rb | 6 +++++- app/views/issues/_issue.json.jbuilder | 6 +----- ...0240325011015_add_reported_on_to_issues.rb | 7 +++++++ ...20240325040617_add_identifier_to_issues.rb | 7 +++++++ db/schema.rb | 4 +++- spec/factories/issues.rb | 2 ++ spec/models/issue_spec.rb | 2 ++ 12 files changed, 60 insertions(+), 28 deletions(-) create mode 100644 db/migrate/20240325011015_add_reported_on_to_issues.rb create mode 100644 db/migrate/20240325040617_add_identifier_to_issues.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 918cc56..2fc3cb3 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,28 +1,22 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-12-20 18:20:53 UTC using RuboCop version 1.56.2. +# on 2024-03-25 17:29:24 UTC using RuboCop version 1.56.2. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 1 -# Configuration parameters: AllowedMethods, AllowedPatterns. -Lint/NestedMethodDefinition: - Exclude: - - 'app/models/maintenance_contract_model.rb' - -# Offense count: 4 +# Offense count: 7 # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. Metrics/AbcSize: Max: 34 -# Offense count: 4 +# Offense count: 12 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. Metrics/MethodLength: Max: 23 -# Offense count: 3 +# Offense count: 4 # Configuration parameters: AllowedMethods. # AllowedMethods: respond_to_missing? Style/OptionalBooleanParameter: @@ -31,9 +25,9 @@ Style/OptionalBooleanParameter: - 'app/models/maintenance_contract_model.rb' - 'app/utils/analytics/finances/models/financial_statements_of_work.rb' -# Offense count: 2 +# Offense count: 9 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. # URISchemes: http, https Layout/LineLength: - Max: 124 + Max: 130 diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 30235ab..c7ca358 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -4,7 +4,13 @@ class IssuesController < ApplicationController before_action :set_project, only: %i[index] def index - @issues = Issue.where.not(closed_date: nil).where(project: @project, closed_date: start_date..end_date) + @issues = Issue.where(project: @project) + + @issues = @issues.where.not(closed_date: nil) if closed == 'true' + + @issues = @issues.where( + '(closed_date BETWEEN :start_date AND :end_date) OR (reported_at BETWEEN :start_date AND :end_date)', start_date:, end_date: + ) end private @@ -20,4 +26,8 @@ def start_date def end_date params[:end_date] end + + def closed + params[:closed] + end end diff --git a/app/models/issue.rb b/app/models/issue.rb index 1e415f2..82a46b2 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -8,12 +8,14 @@ # closed_date :datetime # effort :float # issue_type :string +# reported_at :datetime # state :string # title :string # created_at :datetime not null # updated_at :datetime not null # issue_id :string # project_id :bigint not null +# tts_id :string # user_id :bigint not null # # Indexes diff --git a/app/services/issues_creator.rb b/app/services/issues_creator.rb index 0499033..f84bc5e 100644 --- a/app/services/issues_creator.rb +++ b/app/services/issues_creator.rb @@ -7,16 +7,21 @@ def initialize(project) end def call - @project.issues.destroy_all + ActiveRecord::Base.transaction do + @project.issues.destroy_all - issues = issues_client_class.new(@project).list - issues.map do |issue| - next unless issue.user + issues = issues_client_class.new(@project).list + issues.map do |issue| + next unless issue.user - Issue.create!(project: @project, effort: issue.effort, user: issue.user, state: issue.state, - closed_date: issue.closed_date, issue_id: issue.issue_id, title: issue.title, - issue_type: issue.issue_type) + Issue.create!(project: @project, effort: issue.effort, user: issue.user, state: issue.state, + closed_date: issue.closed_date, title: issue.title, + issue_type: issue.issue_type, reported_at: issue.reported_at, tts_id: issue.tts_id) + end end + rescue StandardError => e + Rails.logger.error("Failed to process issues due to error: #{e.message}") + raise end private diff --git a/app/utils/clients/tts/azure/issue.rb b/app/utils/clients/tts/azure/issue.rb index e3163bf..ddb12f5 100644 --- a/app/utils/clients/tts/azure/issue.rb +++ b/app/utils/clients/tts/azure/issue.rb @@ -8,6 +8,7 @@ def list issues_list = ::Request.post(url, customer_authorization, body) urls = issues_list['workItems'].pluck('url') work_items = urls.map { |url| ::Request.get(url, customer_authorization) } + parsed_items = work_items.map do |work_item| parser.new(work_item, project) end @@ -16,7 +17,7 @@ def list end def filtered_items(parsed_items) - parsed_items.select { |item| item.issue_type == 'Product Backlog Item' } + parsed_items end def parser diff --git a/app/utils/clients/tts/azure/parsers/ministry_brands_parser.rb b/app/utils/clients/tts/azure/parsers/ministry_brands_parser.rb index 242a889..4671603 100644 --- a/app/utils/clients/tts/azure/parsers/ministry_brands_parser.rb +++ b/app/utils/clients/tts/azure/parsers/ministry_brands_parser.rb @@ -40,7 +40,11 @@ def issue_type 'Task' end - def issue_id + def reported_at + json.dig('fields', 'System.CreatedDate') + end + + def tts_id json['id'].to_s end end diff --git a/app/views/issues/_issue.json.jbuilder b/app/views/issues/_issue.json.jbuilder index d499ddb..542de4a 100644 --- a/app/views/issues/_issue.json.jbuilder +++ b/app/views/issues/_issue.json.jbuilder @@ -1,7 +1,3 @@ # frozen_string_literal: true -json.extract! issue, :id, :effort, :user_id, :state, :closed_date - -json.user do |json| - json.partial! 'users/user', user: issue.user -end +json.extract! issue, :id, :effort, :user_id, :state, :closed_date, :title, :issue_type, :reported_at, :tts_id diff --git a/db/migrate/20240325011015_add_reported_on_to_issues.rb b/db/migrate/20240325011015_add_reported_on_to_issues.rb new file mode 100644 index 0000000..705a2e6 --- /dev/null +++ b/db/migrate/20240325011015_add_reported_on_to_issues.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddReportedOnToIssues < ActiveRecord::Migration[7.0] + def change + add_column :issues, :reported_at, :datetime + end +end diff --git a/db/migrate/20240325040617_add_identifier_to_issues.rb b/db/migrate/20240325040617_add_identifier_to_issues.rb new file mode 100644 index 0000000..bd53226 --- /dev/null +++ b/db/migrate/20240325040617_add_identifier_to_issues.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddIdentifierToIssues < ActiveRecord::Migration[7.0] + def change + add_column :issues, :tts_id, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 6bc2128..ae763ce 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_03_21_110927) do +ActiveRecord::Schema[7.0].define(version: 2024_03_25_040617) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "plpgsql" @@ -76,6 +76,8 @@ t.string "issue_id" t.string "issue_type" t.string "title" + t.datetime "reported_at" + t.string "tts_id" t.index ["project_id"], name: "index_issues_on_project_id" t.index ["user_id"], name: "index_issues_on_user_id" end diff --git a/spec/factories/issues.rb b/spec/factories/issues.rb index 8b5e35a..897af17 100644 --- a/spec/factories/issues.rb +++ b/spec/factories/issues.rb @@ -8,12 +8,14 @@ # closed_date :datetime # effort :float # issue_type :string +# reported_at :datetime # state :string # title :string # created_at :datetime not null # updated_at :datetime not null # issue_id :string # project_id :bigint not null +# tts_id :string # user_id :bigint not null # # Indexes diff --git a/spec/models/issue_spec.rb b/spec/models/issue_spec.rb index d1b028f..8b943b0 100644 --- a/spec/models/issue_spec.rb +++ b/spec/models/issue_spec.rb @@ -8,12 +8,14 @@ # closed_date :datetime # effort :float # issue_type :string +# reported_at :datetime # state :string # title :string # created_at :datetime not null # updated_at :datetime not null # issue_id :string # project_id :bigint not null +# tts_id :string # user_id :bigint not null # # Indexes