gitlab-mergetrain-checker 1.1.0 → 1.2.0

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: 0154e482170ce0904d20d1f45010665807562bc8842064b4b57a024dd749d844
4
- data.tar.gz: 73559f786d2d546e3b30f2511392735a8b889df9bcfa4b72f7a0296022a6c6a3
3
+ metadata.gz: d3f0cc6f8a38c160ea6f78ab7d57755f510272fcdae1cf72ab9dfadc9cffdb94
4
+ data.tar.gz: 6da1e48f6974907b70ada6a5f4ac14c673f88b14e0a8931f22f029d8d27bb058
5
5
  SHA512:
6
- metadata.gz: 11b7133b1c0d2940b3298f0324cb0cfd2fe0cc33ae791e327e3ad0ee9b38697ef2fee8999953ee367a564ec0eae1feb15f79499b6ca1c96f3d4ef3baf5f9d72f
7
- data.tar.gz: b0a8b9d889a99a8509f561ede8bbd26b3567230d0278df9a14764d3d2abb3f3a65c68b46faabfa9ec01a959e8cc921db67a6cee36d405a12dc26065a6bf91aba
6
+ metadata.gz: d0f070d495c1ab9b7e7795f2795ef131e5b11cc7e4c793cb55b14ba1780cfb433cb454ceadcc2b1a10be511dcbcd44e5e94dd1592c323a47ffe37a2ca2bb702d
7
+ data.tar.gz: 5ee8e6509fc145d1100d0c0a2c56902b9b09daff1ea10ba054d9ae81431beccd4847c5c3ddef187432720a3dbf71b2eb8860c6b41f5a08dc0413916ce389eb5b
@@ -29,6 +29,7 @@ module MergetrainCheck
29
29
  end
30
30
 
31
31
  checker = Checker.new(config.gitlab_host, config.auth_token, config.project_id)
32
+ checker.max_completed = config.limit
32
33
  traintable = checker.check
33
34
 
34
35
  text_length = config.verbose ? 160: 80
@@ -13,6 +13,9 @@ module MergetrainCheck
13
13
  opts.on("-n", "--host GITLAB-HOSTNAME", "Specify the Gitlab installation host (in case it's not gitlab.com)") do |host|
14
14
  options[:host] = host
15
15
  end
16
+ opts.on("-l", "--limit COUNT", "Limit completed jobs to COUNT entries (default 10)") do |limit|
17
+ options[:limit] = limit
18
+ end
16
19
  opts.on("-v", "--verbose", "Verbose output (larger table)") do |verbose|
17
20
  options[:verbose] = verbose
18
21
  end
@@ -3,21 +3,44 @@ require 'json'
3
3
 
4
4
  module MergetrainCheck
5
5
  class Checker
6
+ def max_count
7
+ @max_count
8
+ end
9
+
10
+ def max_completed=(value)
11
+ @max_completed = value
12
+ end
13
+
6
14
  def initialize(host, token, id)
7
15
  @host = host
8
16
  @token = token
9
17
  @id = id
10
- @uri = URI("https://#{host}/api/v4/projects/#{id}/merge_trains?per_page=100")
18
+ @max_count = 10
11
19
  end
12
20
 
13
21
  def check
14
- Net::HTTP.start(@uri.host, @uri.port, :use_ssl => @uri.scheme == 'https') do |http|
15
- request = Net::HTTP::Get.new @uri
22
+ Net::HTTP.start(active_uri.host, active_uri.port, :use_ssl => active_uri.scheme == 'https') do |http|
23
+ get_and_parse(http, active_uri) + get_and_parse(http, completed_uri)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def get_and_parse(http, uri)
30
+ request = Net::HTTP::Get.new uri
16
31
  request['PRIVATE-TOKEN'] = @token
17
32
 
18
33
  response = http.request request
19
34
  JSON.parse response.body
20
- end
35
+ end
36
+
37
+ def completed_uri
38
+ URI("https://#{@host}/api/v4/projects/#{@id}/merge_trains?per_page=#{@max_completed}&scope=complete")
39
+ end
40
+
41
+ def active_uri
42
+ URI("https://#{@host}/api/v4/projects/#{@id}/merge_trains?per_page=100&scope=active")
21
43
  end
22
44
  end
23
45
  end
46
+
@@ -47,6 +47,14 @@ module MergetrainCheck
47
47
  @tokenStorage = AuthTokenStorage.new(value)
48
48
  end
49
49
 
50
+ def limit
51
+ @config[:limit] || 10
52
+ end
53
+
54
+ def limit=(value)
55
+ @config[:limit] = value
56
+ end
57
+
50
58
  def auth_token
51
59
  @tokenStorage.password
52
60
  end
@@ -22,7 +22,12 @@ module MergetrainCheck
22
22
  @firstname_only ? carriage['user']['name'].split.first : carriage['user']['name'],
23
23
  truncate_string(carriage['merge_request']['title'], @max_length)]
24
24
  end
25
- values.to_table
25
+
26
+ header = ''
27
+ if has_train_finished? body
28
+ header = "\n ✋ 🚉 The train is at the station: There are currently no running merge trains!\n\n"
29
+ end
30
+ header + values.to_table
26
31
  end
27
32
 
28
33
  private
@@ -44,25 +49,30 @@ module MergetrainCheck
44
49
  def pretty_date_difference(from, to)
45
50
  (to.to_time - from.to_time).duration
46
51
  end
52
+
53
+ def has_train_finished?(data)
54
+ data.first['status'] != 'fresh'
55
+ end
47
56
  end
48
57
  end
49
58
 
50
59
  class Array
51
60
  def to_table
61
+ output = ''
52
62
  column_sizes = self.reduce([]) do |lengths, row|
53
63
  row.each_with_index.map{|iterand, index| [lengths[index] || 0, iterand.to_s.length + count_emojis(iterand.to_s)].max}
54
64
  end
55
- puts head = '-' * (column_sizes.inject(&:+) + (3 * column_sizes.count) + 1)
65
+ output += head = '-' * (column_sizes.inject(&:+) + (3 * column_sizes.count) + 1) + "\n"
56
66
  self.each_with_index do |row, idx|
57
67
  row = row.fill(nil, row.size..(column_sizes.size - 1))
58
68
  row = row.each_with_index.map{|v, i| v = v.to_s + ' ' * (column_sizes[i] - v.to_s.length - count_emojis(v.to_s))}
59
- puts '| ' + row.join(' | ') + ' |'
69
+ output += '| ' + row.join(' | ') + ' |' + "\n"
60
70
  if idx == 0
61
71
  row = row.each_with_index.map{|v, i| v = '-' * v.to_s.length}
62
- puts '| ' + row.join(' | ') + ' |'
72
+ output += '| ' + row.join(' | ') + ' |' + "\n"
63
73
  end
64
74
  end
65
- puts head
75
+ output += head
66
76
  end
67
77
 
68
78
  private
@@ -1,3 +1,3 @@
1
1
  module MergetrainCheck
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-mergetrain-checker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabio Gallonetto