gitlab-mergetrain-checker 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 52d691e512abd10e4376611515a57d31b320948dc773327083624635420c032d
4
+ data.tar.gz: 59e0e9385a3d7a08fe0eee1acc5e0d6566ffb8d3dea2b5e59e6c2011a429abb0
5
+ SHA512:
6
+ metadata.gz: 0040ea47f21b21964b4daf7f920261bb00758eb6ddd9e0ed1fbb8f90058b740a5fdde10df80f9714c921fad4cfd8c439524b844f60428cca3f4e7aa6db9904f4
7
+ data.tar.gz: 9cd3d8e441c750f8008faa337f46d95ae8934b3688445da9419250c8ab6f43fe654eff595eb50f29c9e41b828d7d7b43f9802ae9a543ff322dd616f6faf712ab
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mergetrain_check.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gitlab-mergetrain-checker (1.0.0)
5
+ ruby-keychain (~> 0.3.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ corefoundation (0.2.0)
11
+ ffi
12
+ ffi (1.12.2)
13
+ rake (12.3.3)
14
+ ruby-keychain (0.3.2)
15
+ corefoundation (~> 0.2.0)
16
+ ffi
17
+
18
+ PLATFORMS
19
+ ruby
20
+
21
+ DEPENDENCIES
22
+ gitlab-mergetrain-checker!
23
+ rake (~> 12.0)
24
+
25
+ BUNDLED WITH
26
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Fabio Gallonetto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # MergetrainCheck
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mergetrain_check`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'mergetrain_check'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mergetrain_check
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mergetrain_check.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mergetrain_check"
5
+
6
+ begin
7
+ MergetrainCheck::Command.run!(ARGV)
8
+ rescue MergetrainCheck::MissingConfigError => e
9
+ puts "ERROR: #{e.message} parameter is required"
10
+ rescue OptionParser::InvalidArgument => e
11
+ puts "ERROR: #{e.message}"
12
+ rescue JSON::ParserError => e
13
+ puts "Unable to parse server response"
14
+ rescue SocketError => e
15
+ puts "Unable to connect to server"
16
+ end
@@ -0,0 +1,41 @@
1
+ require 'mergetrain_check/config'
2
+ require 'mergetrain_check/args_parser'
3
+ require 'mergetrain_check/checker'
4
+ require 'mergetrain_check/formatter'
5
+
6
+ module MergetrainCheck
7
+ class MissingConfigError < StandardError
8
+ def initialize(description)
9
+ super(description)
10
+ end
11
+ end
12
+
13
+ class Command
14
+ @host = "www.gitlab.com"
15
+ @project = -1
16
+ @token = -1
17
+
18
+ def self.run!(args)
19
+ config = Config.new
20
+ parser = ArgsParser.new
21
+ args = ArgsParser.new.parse(args)
22
+ config.merge! args
23
+
24
+ if config.auth_token.nil?
25
+ raise MissingConfigError, "auth_token"
26
+ end
27
+
28
+ if config.project_id.nil?
29
+ raise MissingConfigError, "project_id"
30
+ end
31
+
32
+ checker = Checker.new(config.gitlab_host, config.auth_token, config.project_id)
33
+ traintable = checker.check
34
+
35
+ formatter = TraintableFormatter.new 80, true
36
+ puts formatter.format traintable
37
+ config.save!
38
+ end
39
+ end
40
+ end
41
+
@@ -0,0 +1,28 @@
1
+ require 'optionparser'
2
+
3
+ module MergetrainCheck
4
+ class ArgsParser
5
+ def parse(args)
6
+ options = {}
7
+ parser = OptionParser.new do |opts|
8
+ opts.banner = "Usage: mergetrain_check [options] [PROJECT-ID]\n" +
9
+ " PROJECT-ID: The project ID to fetch the merge train list from. If none specified, it will try to use the last one used.\n\n"
10
+ opts.on("-t", "--token GITLAB-PERSONAL-TOKEN", "Gitlab API token (go to https://gitlab.com/profile/personal_access_tokens) to generate one.") do |t|
11
+ options[:token] = t
12
+ end
13
+ opts.on("-n", "--host GITLAB-HOSTNAME", "Specify the Gitlab installation host (in case it's not gitlab.com)") do |host|
14
+ options[:host] = host
15
+ end
16
+ opts.on("-h", "--help", "Prints this help") do
17
+ puts opts
18
+ end
19
+ end
20
+ parser.parse!(args)
21
+ options[:project_id] = args[0] if args.length > 0
22
+ options
23
+
24
+ end
25
+
26
+ end
27
+ end
28
+
@@ -0,0 +1,23 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module MergetrainCheck
5
+ class Checker
6
+ def initialize(host, token, id)
7
+ @host = host
8
+ @token = token
9
+ @id = id
10
+ @uri = URI("https://#{host}/api/v4/projects/#{id}/merge_trains")
11
+ end
12
+
13
+ def check
14
+ Net::HTTP.start(@uri.host, @uri.port, :use_ssl => @uri.scheme == 'https') do |http|
15
+ request = Net::HTTP::Get.new @uri
16
+ request['PRIVATE-TOKEN'] = @token
17
+
18
+ response = http.request request
19
+ JSON.parse response.body
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,83 @@
1
+ require 'yaml'
2
+ require 'fileutils'
3
+ require 'keychain'
4
+
5
+ module MergetrainCheck
6
+ class AuthTokenStorage
7
+ KEYCHAIN_SERVICE_NAME = 'mergetrain_check'
8
+ def initialize(host)
9
+ @host = host
10
+ @kitem = Keychain.generic_passwords.where(service: KEYCHAIN_SERVICE_NAME).all.detect { |k| k.account == host }
11
+ @kitem = Keychain.generic_passwords.create(service: KEYCHAIN_SERVICE_NAME, password: 'secret', account: host) if @kitem.nil?
12
+ end
13
+
14
+ def password
15
+ return @kitem.password
16
+ end
17
+
18
+ def password=(value)
19
+ @kitem.password = value
20
+ end
21
+
22
+ def save!
23
+ @kitem.save!
24
+ end
25
+ end
26
+
27
+ DEFAULT_CONFIG_FILE = File.expand_path('~/.mergetraincheck')
28
+
29
+ class FileNotFoundError < StandardError
30
+ def initialize(description)
31
+ super(description)
32
+ end
33
+ end
34
+
35
+ class Config
36
+ def gitlab_host
37
+ @config[:host] || "www.gitlab.com"
38
+ end
39
+
40
+ def gitlab_host=(value)
41
+ @config[:host] = value
42
+ @tokenStorage = AuthTokenStorage.new(value)
43
+ end
44
+
45
+ def auth_token
46
+ @tokenStorage.password
47
+ end
48
+
49
+ def auth_token=(value)
50
+ @tokenStorage.password = value
51
+ end
52
+
53
+ def project_id
54
+ @config[:project_id]
55
+ end
56
+
57
+ def project_id=(value)
58
+ @config[:project_id] = value
59
+ end
60
+
61
+ def merge!(config_hash)
62
+ @config.merge! config_hash.reject { |k,v| k == :token }
63
+ @tokenStorage = AuthTokenStorage.new(gitlab_host)
64
+ @tokenStorage.password = config_hash[:token] unless config_hash[:token].nil?
65
+ end
66
+
67
+ def initialize(file = DEFAULT_CONFIG_FILE)
68
+ if File.exist?(file)
69
+ @config = YAML.load(File.read(file))
70
+ @config = {} if @config.nil?
71
+ else
72
+ @config = {}
73
+ end
74
+ @tokenStorage = AuthTokenStorage.new(gitlab_host)
75
+ end
76
+
77
+ def save!(file = DEFAULT_CONFIG_FILE)
78
+ File.open(file, 'w') { |f| f.write(@config.to_yaml) }
79
+ @tokenStorage.save!
80
+ end
81
+ end
82
+ end
83
+
@@ -0,0 +1,60 @@
1
+ module MergetrainCheck
2
+ class TraintableFormatter
3
+ def initialize(max_length, firstname_only)
4
+ @max_length = max_length
5
+ @firstname_only = firstname_only
6
+ end
7
+
8
+ def format(body)
9
+ values = [['St', 'MR', 'Pipe ID', 'User', 'Title']]
10
+ body.each do |carriage|
11
+ values << [pipeline_status(carriage['status']),
12
+ carriage['merge_request']['iid'],
13
+ carriage['pipeline']['id'],
14
+ @firstname_only ? carriage['user']['name'].split.first : carriage['user']['name'],
15
+ truncate_string(carriage['merge_request']['title'], @max_length)]
16
+ end
17
+ values.to_table
18
+ end
19
+
20
+ private
21
+ def pipeline_status(status)
22
+ return '🚂' if status == 'fresh'
23
+ return '✅' if status == 'merged'
24
+ status
25
+ end
26
+
27
+ def truncate_string(string, len)
28
+ return string if string.length <= len
29
+ "#{string[0...len]}..."
30
+ end
31
+
32
+
33
+ end
34
+ end
35
+
36
+ class Array
37
+ def to_table
38
+ column_sizes = self.reduce([]) do |lengths, row|
39
+ row.each_with_index.map{|iterand, index| [lengths[index] || 0, iterand.to_s.length + count_emojis(iterand.to_s)].max}
40
+ end
41
+ puts head = '-' * (column_sizes.inject(&:+) + (3 * column_sizes.count) + 1)
42
+ self.each_with_index do |row, idx|
43
+ row = row.fill(nil, row.size..(column_sizes.size - 1))
44
+ row = row.each_with_index.map{|v, i| v = v.to_s + ' ' * (column_sizes[i] - v.to_s.length - count_emojis(v.to_s))}
45
+ puts '| ' + row.join(' | ') + ' |'
46
+ if idx == 0
47
+ row = row.each_with_index.map{|v, i| v = '-' * v.to_s.length}
48
+ puts '| ' + row.join(' | ') + ' |'
49
+ end
50
+ end
51
+ puts head
52
+ end
53
+
54
+ private
55
+
56
+ def count_emojis(string)
57
+ string.scan(/[\u{1F300}-\u{1F5FF}|\u{1F1E6}-\u{1F1FF}|\u{2700}-\u{27BF}|\u{1F900}-\u{1F9FF}|\u{1F600}-\u{1F64F}|\u{1F680}-\u{1F6FF}|\u{2600}-\u{26FF}]/).length
58
+ end
59
+
60
+ end
@@ -0,0 +1,3 @@
1
+ module MergetrainCheck
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'lib/mergetrain_check/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "gitlab-mergetrain-checker"
5
+ spec.version = MergetrainCheck::VERSION
6
+ spec.date = %q{2020-04-29}
7
+ spec.authors = ["Fabio Gallonetto"]
8
+ spec.email = 'fabio.gallonetto@curve.com'
9
+ spec.summary = %q{A command line tool to check the status of a merge train in gitlab}
10
+ spec.license = "MIT"
11
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
12
+
13
+ # Specify which files should be added to the gem when it is released.
14
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
15
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
16
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ end
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'ruby-keychain', '~> 0.3.2'
22
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitlab-mergetrain-checker
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fabio Gallonetto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-keychain
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.3.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.3.2
27
+ description:
28
+ email: fabio.gallonetto@curve.com
29
+ executables:
30
+ - mergetrain_check
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - bin/mergetrain_check
41
+ - lib/mergetrain_check.rb
42
+ - lib/mergetrain_check/args_parser.rb
43
+ - lib/mergetrain_check/checker.rb
44
+ - lib/mergetrain_check/config.rb
45
+ - lib/mergetrain_check/formatter.rb
46
+ - lib/mergetrain_check/version.rb
47
+ - mergetrain_check.gemspec
48
+ homepage:
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.3.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.0.3
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: A command line tool to check the status of a merge train in gitlab
71
+ test_files: []