ehpt 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f3653e66627846fae073f4173a4f51cf683b76284d1b413095347c9f3462a94e
4
- data.tar.gz: f8835fe7726e1f883e9f5608328e7257f06abdf0a8552cd1332662ad64e64ace
3
+ metadata.gz: bee523dd1511046a8f0aeb28f6587c5489c4d7b674b0fdd71eb178cc1576da86
4
+ data.tar.gz: dc47c8db8fe50b4d672261be2106a6c0b5f64e71321528b14d0116bafd5b2af4
5
5
  SHA512:
6
- metadata.gz: ae913c7ffbd558597ca0847638ffe32bdffdc8e5bf412c9352bfdd38faaa107809f8e092fbada9e68080f9704d2fc97f169b525e6cf68b10200978c106720b60
7
- data.tar.gz: 63c49d8f401bf28dbdb407c00878f56d1fafe7b522211ea190197764a106b43d33253109df0fb67a95448892341fd5921986a96fb65e8548babacc7f39f5826a
6
+ metadata.gz: b404e4aa579b442dabda86c2f4654796f24e92715043d6643f39bc16ea045d379c7941520fad7b06263be4aedd245ede884b6b172eba8868125dd61da72948b1
7
+ data.tar.gz: 8c73fd0d71dc064f8673526998d23d8b4bed5828f9d6cd263a94cffe27b18a07d029ebec2f4c8eca4df1c52554ef339e4358763e8b6607401ca77b03ddf70a65
data/lib/ehpt/base.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Ehpt
2
2
  class Base
3
- attr_reader :data, :errors
3
+ attr_reader :data, :errors, :warnings
4
4
 
5
5
  def self.call(*args)
6
6
  new(*args).call
@@ -9,6 +9,7 @@ module Ehpt
9
9
  def initialize(*args)
10
10
  @data = nil
11
11
  @errors = []
12
+ @warnings = []
12
13
  end
13
14
 
14
15
  def success?
@@ -19,6 +20,10 @@ module Ehpt
19
20
  !success?
20
21
  end
21
22
 
23
+ def warning?
24
+ !warnings.empty?
25
+ end
26
+
22
27
  def add_error(error)
23
28
  if error.is_a?(Array)
24
29
  error.each do |err|
@@ -28,5 +33,15 @@ module Ehpt
28
33
  @errors << error
29
34
  end
30
35
  end
36
+
37
+ def add_warning(warning)
38
+ if warning.is_a?(Array)
39
+ warning.each do |err|
40
+ add_warning(err)
41
+ end
42
+ else
43
+ @warnings << warning
44
+ end
45
+ end
31
46
  end
32
47
  end
@@ -1,22 +1,9 @@
1
- require 'csv'
2
- require 'ehpt/base'
3
- require 'ehpt/create_story'
4
-
5
1
  module Ehpt
6
2
  class CreateStories < Base
7
- attr_reader :csv_file, :project
8
-
9
- ARRAY_TYPE_ATTRIBUTES = %w[ labels tasks pull_requests branches blockers comments reviews ]
10
-
11
- INT_ARRAY_TYPE_ATTRIBUTES = %w[ owner_ids label_ids follower_ids ]
12
-
13
- INT_TYPE_ATTRIBUTES = %w[ project_id requested_by_id before_id after_id integration_id ]
14
-
15
- FLOAT_TYPE_ATTRIBUES = %w[ estimate ]
3
+ attr_reader :csv_file
16
4
 
17
- def initialize(csv_file, project)
5
+ def initialize(csv_file)
18
6
  @csv_file = csv_file
19
- @project = project
20
7
  super
21
8
  end
22
9
 
@@ -39,7 +26,10 @@ module Ehpt
39
26
  def create_stories
40
27
  CSV.foreach(csv_file, headers: true) do |row|
41
28
  story_attrs = create_story_attributes(row)
42
- story_creator = Ehpt::CreateStory.new(project, story_attrs)
29
+
30
+ next if error?
31
+
32
+ story_creator = Ehpt::CreateStory.new(story_attrs)
43
33
  story_creator.call
44
34
 
45
35
  if story_creator.success?
@@ -54,14 +44,17 @@ module Ehpt
54
44
  end
55
45
 
56
46
  def create_story_attributes(row)
57
- story_attrs = row.to_h.compact
58
- story_attrs.each do |key, value|
59
- story_attrs[key] = value.to_i if INT_TYPE_ATTRIBUTES.include?(key)
60
- story_attrs[key] = value.to_f if FLOAT_TYPE_ATTRIBUES.include?(key)
61
- story_attrs[key] = value.split(',') if ARRAY_TYPE_ATTRIBUTES.include?(key)
62
- story_attrs[key] = value.split(',').map(&:to_i) if INT_ARRAY_TYPE_ATTRIBUTES.include?(key)
47
+ attr_creator = Ehpt::CreateStoryAttributes.new(row)
48
+ attr_creator.call
49
+
50
+ add_warning(attr_creator.warnings) if attr_creator.warning?
51
+
52
+ if attr_creator.error?
53
+ add_error(attr_creator.errors)
54
+ return
63
55
  end
64
- story_attrs
56
+
57
+ attr_creator.data
65
58
  end
66
59
  end
67
60
  end
@@ -1,11 +1,8 @@
1
- require 'ehpt/base'
2
-
3
1
  module Ehpt
4
2
  class CreateStory < Base
5
- attr_reader :project, :story_attrs
3
+ attr_reader :story_attrs
6
4
 
7
- def initialize(project, story_attrs)
8
- @project = project
5
+ def initialize(story_attrs)
9
6
  @story_attrs = story_attrs
10
7
  super
11
8
  end
@@ -20,7 +17,7 @@ module Ehpt
20
17
  private
21
18
 
22
19
  def create_story
23
- @data = project.create_story(story_attrs)
20
+ @data = Ehpt.project.create_story(story_attrs)
24
21
  end
25
22
 
26
23
  def prefix_story_name_with_id
@@ -0,0 +1,63 @@
1
+ module Ehpt
2
+ class CreateStoryAttributes < Base
3
+ attr_reader :row
4
+
5
+ ARRAY_TYPE_ATTRIBUTES = %w[ labels tasks pull_requests branches blockers comments reviews ]
6
+
7
+ INT_ARRAY_TYPE_ATTRIBUTES = %w[ owner_ids label_ids follower_ids ]
8
+
9
+ INT_TYPE_ATTRIBUTES = %w[ project_id requested_by_id before_id after_id integration_id ]
10
+
11
+ FLOAT_TYPE_ATTRIBUES = %w[ estimate ]
12
+
13
+ def initialize(row)
14
+ @row = row
15
+ super
16
+ end
17
+
18
+ def call
19
+ story_attrs = row.to_h.compact
20
+ story_attrs.each do |key, value|
21
+ story_attrs[key] = value.to_i if INT_TYPE_ATTRIBUTES.include?(key)
22
+ story_attrs[key] = value.to_f if FLOAT_TYPE_ATTRIBUES.include?(key)
23
+ story_attrs[key] = value.split(',') if ARRAY_TYPE_ATTRIBUTES.include?(key)
24
+ story_attrs[key] = value.split(',').map(&:to_i) if INT_ARRAY_TYPE_ATTRIBUTES.include?(key)
25
+ end
26
+
27
+ if story_attrs.has_key?('requested_by')
28
+ user_id = get_user_id_from(story_attrs.delete('requested_by'))
29
+ story_attrs['requested_by_id'] = user_id unless user_id.nil?
30
+ end
31
+
32
+ if story_attrs.has_key?('owners')
33
+ owners = story_attrs.delete('owners').split(',')
34
+ owner_ids = owners.map { |owner| get_user_id_from(owner.strip) }.compact
35
+ story_attrs['owner_ids'] = owner_ids unless owner_ids.empty?
36
+ end
37
+
38
+ @data = story_attrs
39
+ rescue StandardError => e
40
+ add_error({
41
+ row: row.to_h,
42
+ warnings: e.message
43
+ })
44
+ end
45
+
46
+ private
47
+
48
+ def get_user_id_from(initial)
49
+ user_id_getter = Ehpt::GetUserIdFromInitial.new(initial)
50
+ user_id_getter.call
51
+
52
+ if user_id_getter.error?
53
+ add_warning({
54
+ row: row.to_h,
55
+ warnings: user_id_getter.errors
56
+ })
57
+ return
58
+ end
59
+
60
+ user_id_getter.data
61
+ end
62
+ end
63
+ end
@@ -1,6 +1,3 @@
1
- require 'tracker_api'
2
- require 'ehpt/base'
3
-
4
1
  module Ehpt
5
2
  class GetProject < Base
6
3
  attr_reader :token, :project_id
@@ -0,0 +1,49 @@
1
+ module Ehpt
2
+ class GetUserIdFromInitial < Base
3
+ @@memberships = nil
4
+
5
+ attr_reader :initial
6
+
7
+ def self.memberships
8
+ @@memberships
9
+ end
10
+
11
+ def self.memberships=(memberships)
12
+ @@memberships = memberships
13
+ end
14
+
15
+ def initialize(initial)
16
+ @initial = initial
17
+ super
18
+ end
19
+
20
+ def call
21
+ fetch_memberships_from_pt!
22
+
23
+ if user.nil?
24
+ add_error("Not found any user with initial #{initial}")
25
+ else
26
+ @data = user.id
27
+ end
28
+ rescue StandardError => e
29
+ add_error(eval(e.message)[:body])
30
+ end
31
+
32
+ private
33
+
34
+ def memberships
35
+ self.class.memberships
36
+ end
37
+
38
+ def fetch_memberships_from_pt!
39
+ self.class.memberships ||= Ehpt.project.memberships
40
+ end
41
+
42
+ def user
43
+ member = memberships.find do |membership|
44
+ membership.person.initials.downcase == initial.downcase
45
+ end
46
+ member && member.person
47
+ end
48
+ end
49
+ end
data/lib/ehpt.rb CHANGED
@@ -1,9 +1,22 @@
1
+ require 'tracker_api'
1
2
  require 'pp'
2
3
  require 'csv'
3
- require 'ehpt/get_project'
4
+ require 'ehpt/base'
4
5
  require 'ehpt/create_stories'
6
+ require 'ehpt/create_story'
7
+ require 'ehpt/create_story_attributes'
8
+ require 'ehpt/get_project'
9
+ require 'ehpt/get_user_id_from_initial'
5
10
 
6
11
  module Ehpt
12
+ def self.project=(project)
13
+ @@project = project
14
+ end
15
+
16
+ def self.project
17
+ @@project
18
+ end
19
+
7
20
  def self.call(csv_file, token, project_id)
8
21
  project_getter = Ehpt::GetProject.new(token, project_id)
9
22
  project_getter.call
@@ -14,10 +27,10 @@ module Ehpt
14
27
  return
15
28
  end
16
29
 
30
+ self.project = project_getter.data
17
31
  puts "Found project: #{project_getter.data.name}"
18
- project = project_getter.data
19
32
 
20
- stories_creator = Ehpt::CreateStories.new(csv_file, project)
33
+ stories_creator = Ehpt::CreateStories.new(csv_file)
21
34
  stories_creator.call
22
35
 
23
36
  puts "Done"
@@ -26,5 +39,10 @@ module Ehpt
26
39
  puts "===== Errors ====="
27
40
  pp stories_creator.errors
28
41
  end
42
+
43
+ if stories_creator.warning?
44
+ puts "===== Warnings ====="
45
+ pp stories_creator.warnings
46
+ end
29
47
  end
30
48
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ehpt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - phamhoaivu911
@@ -104,7 +104,9 @@ files:
104
104
  - lib/ehpt/base.rb
105
105
  - lib/ehpt/create_stories.rb
106
106
  - lib/ehpt/create_story.rb
107
+ - lib/ehpt/create_story_attributes.rb
107
108
  - lib/ehpt/get_project.rb
109
+ - lib/ehpt/get_user_id_from_initial.rb
108
110
  homepage: https://github.com/phamhoaivu911/ehpt
109
111
  licenses:
110
112
  - MIT