ballantine 0.1.2 → 0.1.3

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: a2188ad3272546c25226bdbb1209075baadee05fb7f011cafe13874b1af45777
4
- data.tar.gz: 0fe5889c616c93c7f62ce8353671543750604fc4c6def9958abdfe3a0f6e5c42
3
+ metadata.gz: 32d67897fd3d252df4a89c17484075ac5bc442c794de2fa098740f03038e6ab9
4
+ data.tar.gz: b084bff0c1b433b92de7362a17d3129016de599ac468a9d464227b6ae77d94ba
5
5
  SHA512:
6
- metadata.gz: ab52789a4ae2fb510d48f34cd7cf8c2920610e5f341fdbbd94f687fee7ace7ff1f1ff7cbce32fe2e4ce78a783762aa29cf1c9053be5f042ef5b12f84f9101b28
7
- data.tar.gz: bda07819f4146ac05b3ab1c39f4debe647f9c72a3a1dc50cfac433bf3381ecc6b96e96c92559775c08ecbf4f4e14b39a235a5a23d8636d2e956b6eddee33ad60
6
+ metadata.gz: 5589a6f22b237a78177ec09b55a3efc2a3965583ef7d79daba8aa441f3888f5fc07dfe236b90651712d7209dfda44d8f7d0b2dfd7c3e250ce29efa1c72596d49
7
+ data.tar.gz: b1c84acf31a520d3bc8d11b384bf38da80d71303bb1886f2d550c5f45eaae9857c2823c0829474a0322bf99a7ad8e5dc91ab651200bbcb689fdcde2f8fe2b6ba
@@ -1,63 +1,65 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- class Author
4
- attr_accessor :name, :commits
3
+ module Ballantine
4
+ class Author
5
+ attr_accessor :name, :commits
6
+
7
+ class << self
8
+ # @param [String] name
9
+ # @return [Author] author
10
+ def find_or_create_by(name)
11
+ @@_collections = {} unless defined?(@@_collections)
12
+ return @@_collections[name] unless @@_collections[name].nil?
13
+ @@_collections[name] = new(name)
14
+ end
15
+
16
+ # @return [Array<Author>] authors
17
+ def all
18
+ return [] unless defined?(@@_collections)
19
+ @@_collections.sort.map(&:last) # sort and take values
20
+ end
21
+ end
5
22
 
6
- class << self
7
23
  # @param [String] name
8
- # @return [Author] author
9
- def find_or_create_by(name)
10
- @@_collections = {} unless defined?(@@_collections)
11
- return @@_collections[name] unless @@_collections[name].nil?
12
- @@_collections[name] = new(name)
24
+ def initialize(name)
25
+ @name = name
26
+ @commits = {}
13
27
  end
14
28
 
15
- # @return [Array<Author>] authors
16
- def all
17
- return [] unless defined?(@@_collections)
18
- @@_collections.sort.map(&:last) # sort and take values
29
+ # @return [NilClass] nil
30
+ def print_commits
31
+ puts "\n" + "@#{name}".green
32
+ @commits.each do |repo, lists|
33
+ count, word = retrieve_count_and_word(lists)
34
+ puts " > #{repo.blue}: #{count} new #{word}\n"
35
+ puts lists
36
+ end
37
+ nil
19
38
  end
20
- end
21
39
 
22
- # @param [String] name
23
- def initialize(name)
24
- @name = name
25
- @commits = {}
26
- end
40
+ # returns an array to use slack attachments field
41
+ # reference: https://api.slack.com/messaging/composing/layouts#building-attachments
42
+ # @return [Hash] result
43
+ def serialize_commits
44
+ message = @commits.map do |repo, lists|
45
+ count, word = retrieve_count_and_word(lists)
46
+ "*#{repo}*: #{count} new #{word}\n#{lists.join("\n")}"
47
+ end.join("\n")
27
48
 
28
- # @return [NilClass] nil
29
- def print_commits
30
- puts "\n" + "@#{name}".green
31
- @commits.each do |repo, lists|
32
- count, word = retrieve_count_and_word(lists)
33
- puts " > #{repo.blue}: #{count} new #{word}\n"
34
- puts lists
49
+ results = {
50
+ 'text' => "- <@#{name}>\n#{message}",
51
+ 'color' => '#00B86A', # green
52
+ }
35
53
  end
36
- nil
37
- end
38
-
39
- # returns an array to use slack attachments field
40
- # reference: https://api.slack.com/messaging/composing/layouts#building-attachments
41
- # @return [Hash] result
42
- def serialize_commits
43
- message = @commits.map do |repo, lists|
44
- count, word = retrieve_count_and_word(lists)
45
- "*#{repo}*: #{count} new #{word}\n#{lists.join("\n")}"
46
- end.join("\n")
47
-
48
- results = {
49
- 'text' => "- <@#{name}>\n#{message}",
50
- 'color' => '#00B86A', # green
51
- }
52
- end
53
54
 
54
- private
55
+ private
55
56
 
56
- # @param [Array<String>] lists
57
- # @param [Array(Integer, String)] count, word
58
- def retrieve_count_and_word(lists)
59
- count = lists.size
60
- word = count == 1 ? 'commit' : 'commits'
61
- [count, word]
57
+ # @param [Array<String>] lists
58
+ # @param [Array(Integer, String)] count, word
59
+ def retrieve_count_and_word(lists)
60
+ count = lists.size
61
+ word = count == 1 ? 'commit' : 'commits'
62
+ [count, word]
63
+ end
62
64
  end
63
65
  end
@@ -1,11 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  module Ballantine
4
- require 'thor'
5
- require 'json'
6
- require_relative './author'
7
- require_relative './string'
8
-
9
4
  class CLI < Thor
10
5
  # reference: https://github.com/desktop/desktop/blob/a7bca44088b105a04714dc4628f4af50f6f179c3/app/src/lib/remote-parsing.ts#L27-L44
11
6
  GITHUB_REGEXES = [
@@ -19,42 +14,42 @@ module Ballantine
19
14
  ].freeze
20
15
 
21
16
  FILE_GITMODULES = '.gitmodules'
22
- FILE_BALLANTINE_CONFIG = '.ballantine.json'
23
17
 
24
18
  TYPE_TERMINAL = 'terminal'
25
19
  TYPE_SLACK = 'slack'
26
20
 
27
21
  DEFAULT_LJUST = 80
28
22
 
29
- AVAILABLE_CONFIG = ['slack_webhook'].freeze
30
-
31
- attr_reader :app_name, :main_path, :sub_path, :slack_webhook, :send_type
23
+ attr_reader :app_name, :main_path, :sub_path, :send_type
32
24
 
33
25
  package_name 'Ballantine'
34
26
 
35
- desc 'init', 'Initialize ballantine configuration'
27
+ option 'force', type: :boolean, aliases: '-f', default: false, desc: "Initialize forcely if already initialized."
28
+ desc 'init', 'Initialize ballantine'
36
29
  def init
37
- puts "🥃 Init ballantine configuration"
38
- slack_webhook = ask("Q. Set slack webhook\n> ")
30
+ conf.init_file(force: options['force'])
39
31
 
40
- config = {
41
- slack_webhook: slack_webhook
42
- }
43
- File.write('./' + FILE_BALLANTINE_CONFIG, JSON.dump(config))
32
+ puts "🥃 Initialized ballantine."
44
33
  end
45
34
 
46
- desc 'config', 'Describe ballantine configuration'
47
- def config
48
- load_config
49
- AVAILABLE_CONFIG.each do |key|
50
- puts "#{key}: #{instance_variable_get('@' + key)}"
35
+ Config::AVAILABLE_ENVIRONMENTS.each{ |env| option env, type: :boolean, default: false, desc: "Set envirment to `#{env}'." }
36
+ desc 'config [--env] [KEY] [VALUE]', "Set ballantine's configuration"
37
+ def config(key = nil, value = nil)
38
+ # check environment value
39
+ if Config::AVAILABLE_ENVIRONMENTS.map{ |key| !options[key] }.reduce(:&)
40
+ raise NotAllowed, "Set environment value (#{Config::AVAILABLE_ENVIRONMENTS.map{ |key| "`--#{key}'" }.join(", ")})"
41
+ elsif Config::AVAILABLE_ENVIRONMENTS.map{ |key| !!options[key] }.reduce(:&)
42
+ raise NotAllowed, "Environment value must be unique."
51
43
  end
44
+ @env = Config::AVAILABLE_ENVIRONMENTS.find{ |key| options[key] }
45
+ raise AssertionFailed, "Environment value must exist: #{@env}" if @env.nil?
46
+
47
+ value ? conf.set_data(key, value) : conf.print_data(key)
52
48
  end
53
49
 
54
50
  desc 'diff [TARGET] [SOURCE]', 'Diff commits between TARGET and SOURCE'
55
- option TYPE_SLACK, type: :boolean, aliases: '-s', default: false, desc: 'send to slack using slack webhook URL.'
51
+ option TYPE_SLACK, type: :boolean, aliases: '-s', default: false, desc: "Send to slack using slack webhook URL."
56
52
  def diff(from, to = `git rev-parse --abbrev-ref HEAD`.chomp)
57
- load_config
58
53
  preprocess(from, to, **options)
59
54
 
60
55
  # check argument is tag
@@ -108,18 +103,9 @@ module Ballantine
108
103
 
109
104
  private
110
105
 
111
- def self.exit_on_failure?
112
- exit 1
113
- end
114
-
115
- def load_config
116
- return if Dir[FILE_BALLANTINE_CONFIG].empty?
106
+ def self.exit_on_failure?; exit 1 end
117
107
 
118
- JSON.parse(File.read('./' + FILE_BALLANTINE_CONFIG)).each do |key, value|
119
- next unless AVAILABLE_CONFIG.include?(key)
120
- instance_variable_set('@' + key, value)
121
- end
122
- end
108
+ def conf; @conf ||= Config.new(@env) end
123
109
 
124
110
  # @param [String] from
125
111
  # @param [String] to
@@ -127,19 +113,19 @@ module Ballantine
127
113
  # @return [NilClass] nil
128
114
  def preprocess(from, to, **options)
129
115
  if Dir['.git'].empty?
130
- raise SystemCallError, "ERROR: There is no \".git\" in #{Dir.pwd}."
116
+ raise NotAllowed, "ERROR: There is no \".git\" in #{Dir.pwd}."
131
117
  end
132
118
 
133
119
  if (uncommitted = `git diff HEAD --name-only`.split("\n")).any?
134
- raise SystemCallError, "ERROR: Uncommitted file exists. stash or commit uncommitted files.\n#{uncommitted.join("\n")}"
120
+ raise NotAllowed, "ERROR: Uncommitted file exists. stash or commit uncommitted files.\n#{uncommitted.join("\n")}"
135
121
  end
136
122
 
137
123
  if from == to
138
- raise ArgumentError, "ERROR: target(#{from}) and source(#{to}) can't be equal."
124
+ raise NotAllowed, "ERROR: target(#{from}) and source(#{to}) can't be equal."
139
125
  end
140
126
 
141
- if options[TYPE_SLACK] && !@slack_webhook
142
- raise ArgumentError, "ERROR: Can't find any slack webhook. Set slack webhook using `ballantine init`."
127
+ if options[TYPE_SLACK] && !conf.get_data(Config::KEY_SLACK_WEBHOOK)
128
+ raise NotAllowed, "ERROR: Can't find any slack webhook. Set slack webhook using `ballantine config --#{Config::ENV_LOCAL} slack_webhook [YOUR_WEBHOOK]'."
143
129
  end
144
130
 
145
131
  nil
@@ -209,6 +195,7 @@ module Ballantine
209
195
  " - "+ "%h".yellow + " %<(#{ljust})%s " + "#{url}/commit/%H".gray
210
196
  when TYPE_SLACK
211
197
  "\\\`<#{url}/commit/%H|%h>\\\` %s - %an"
198
+ else raise AssertionFailed, "Unknown send type: #{@send_type}"
212
199
  end
213
200
  end
214
201
 
@@ -233,16 +220,16 @@ module Ballantine
233
220
  when TYPE_SLACK
234
221
  # set message for each author
235
222
  messages = authors.map(&:serialize_commits)
236
- actor = `git config user.name`
223
+ actor = `git config user.name`.chomp
237
224
 
238
225
  # send message to slack
239
226
  require 'net/http'
240
227
  require 'uri'
241
- uri = URI.parse(@slack_webhook)
228
+ uri = URI.parse(conf.get_data(Config::KEY_SLACK_WEBHOOK))
242
229
  request = Net::HTTP::Post.new(uri)
243
230
  request.content_type = 'application/json'
244
231
  request.body = JSON.dump({
245
- 'text' => ":check: *#{@app_name}* deployment request by <@#{actor}> (\`<#{url}/tree/#{from}|#{from}>\` <- \`<#{url}/tree/#{to}|#{to}>\` <#{url}/compare/#{from}...#{to}|compare>)\n:technologist: Author: #{number}\nLast commit: #{last_commit}",
232
+ 'text' => ":white_check_mark: *#{@app_name}* deployment request by <@#{actor}> (\`<#{url}/tree/#{from}|#{from}>\` <- \`<#{url}/tree/#{to}|#{to}>\` <#{url}/compare/#{from}...#{to}|compare>)\n:technologist: Author: #{number}\nLast commit: #{last_commit}",
246
233
  'attachments' => messages
247
234
  })
248
235
  req_options = { use_ssl: uri.scheme == 'https' }
@@ -250,6 +237,8 @@ module Ballantine
250
237
  http.request(request)
251
238
  end
252
239
  puts response.message
240
+ else
241
+ raise AssertionFailed, "Unknown send type: #{@send_type}"
253
242
  end
254
243
  end
255
244
  end
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ module Ballantine
4
+ class Config
5
+ ENV_LOCAL = 'local'
6
+ ENV_GLOBAL = 'global'
7
+ AVAILABLE_ENVIRONMENTS = [
8
+ ENV_LOCAL,
9
+ ENV_GLOBAL
10
+ ].freeze
11
+
12
+ KEY_SLACK_WEBHOOK = 'slack_webhook'
13
+ AVAILABLE_KEYS = [
14
+ KEY_SLACK_WEBHOOK
15
+ ].freeze
16
+
17
+ FILE_BALLANTINE_CONFIG = '.ballantine.json'
18
+
19
+ attr_reader :env, :data, :loaded
20
+
21
+ def initialize(env)
22
+ @env = env || ENV_LOCAL
23
+ @data = {}
24
+ @loaded = false
25
+ end
26
+
27
+ # @param [Hash] options
28
+ # @return [Boolean] result
29
+ def init_file(**options)
30
+ raise NotAllowed, "#{FILE_BALLANTINE_CONFIG} already exists." if Dir[file_path].any? && !options[:force]
31
+
32
+ File.write(file_path, {})
33
+ @loaded = false
34
+ end
35
+
36
+ # @param [Hash] options
37
+ # @return [Boolean] result
38
+ def load_file(**options)
39
+ return false if @loaded
40
+ raise NotAllowed, "Can't find #{FILE_BALLANTINE_CONFIG}" if Dir[file_path].empty?
41
+
42
+ JSON.parse(File.read(file_path)).each do |key, value|
43
+ next unless AVAILABLE_KEYS.include?(key)
44
+ @data[key] = value
45
+ end
46
+
47
+ @loaded = true
48
+ end
49
+
50
+ # @param [String] key
51
+ # @param [Hash] options
52
+ # @return [Boolean] result
53
+ def print_data(key, **options)
54
+ load_file unless @loaded
55
+
56
+ if key
57
+ raise InvalidParameter, "Key must be within #{AVAILABLE_KEYS}" unless AVAILABLE_KEYS.include?(key)
58
+ puts @data[key]
59
+ else
60
+ @data.each do |key, value|
61
+ puts "#{key}: #{value}"
62
+ end
63
+ end
64
+
65
+ true
66
+ end
67
+
68
+ # @param [String] key
69
+ # @param [String] value
70
+ # @param [Hash] options
71
+ # @return [Stirng] value
72
+ def set_data(key, value, **options)
73
+ load_file unless @loaded
74
+
75
+ @data[key] = value
76
+ File.write(file_path, JSON.dump(@data))
77
+ value
78
+ end
79
+
80
+ # @param [String] key
81
+ # @param [Hash] options
82
+ # @return [Stirng] value
83
+ def get_data(key, **options)
84
+ load_file unless @loaded
85
+ @data[key]
86
+ end
87
+
88
+ private
89
+
90
+ def file_path(env = @env)
91
+ case env
92
+ when ENV_LOCAL then "./#{FILE_BALLANTINE_CONFIG}"
93
+ when ENV_GLOBAL then "#{Dir.home}/#{FILE_BALLANTINE_CONFIG}"
94
+ else raise AssertionFailed, "Unknown environment: #{env}"
95
+ end
96
+ end
97
+ end
98
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ballantine
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/ballantine.rb CHANGED
@@ -1,7 +1,16 @@
1
1
  # frozen_string_literal: true
2
- require_relative "ballantine/cli"
2
+ require 'thor'
3
+ require 'json'
4
+ require_relative 'string'
3
5
  require_relative "ballantine/version"
6
+ require_relative 'ballantine/config'
7
+ require_relative 'ballantine/author'
8
+ require_relative "ballantine/cli"
4
9
 
5
10
  module Ballantine
6
11
  class Error < StandardError; end
12
+ class NotAllowed < Error; end
13
+ class InvalidParameter < Error; end
14
+ class AssertionFailed < Error; end
15
+ class NotImplemented < Error; end
7
16
  end
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ballantine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - oohyun15
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-15 00:00:00.000000000 Z
11
+ date: 2022-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -51,8 +51,9 @@ files:
51
51
  - lib/ballantine.rb
52
52
  - lib/ballantine/author.rb
53
53
  - lib/ballantine/cli.rb
54
- - lib/ballantine/string.rb
54
+ - lib/ballantine/config.rb
55
55
  - lib/ballantine/version.rb
56
+ - lib/string.rb
56
57
  homepage: https://github.com/oohyun15/ballantine
57
58
  licenses:
58
59
  - MIT