pr-with-params 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 205374a47996232c17343492935e3fb4a66ced497e2c0dbf2bbfce278e67a9de
4
- data.tar.gz: 3d5a35b0957a4c9b101756164d39a2bfdf85cb98b1e9d30795ba21a814f06046
3
+ metadata.gz: 9d4ad8c0505135845906a907642802d1e6426610e114cfd33be9c090a4866713
4
+ data.tar.gz: 71b40495173262b6ef5cf9ce0ea85f160e677d95505cc2cded45036831e86a90
5
5
  SHA512:
6
- metadata.gz: 8efbfa3dcb16a892718c9e60b0a6093512b05c4226b7ecd990ba7aa46b99368ad7c9b944a02949a704fa583a56173ef21b9c5c269bd3f36fc899452202f3434f
7
- data.tar.gz: 4b21a5705d4e3bfa8046a020656ffc25d92c4466652e213074bcd0fbb08b092d53fe9939905dfebeee6ea3e862a0efe0404c7ce19b8ebf0203833f919f1d122e
6
+ metadata.gz: c592f1a860463ae680e56dc919d91a1e575c0beead05bfc0f64d759fc509b28cd06e2bc42ddd4240a38e85ee40c7d74c9b64b87157cf740579b2fe63c6632fff
7
+ data.tar.gz: 2dd182433b8815efe50dd0405232c9319d2d0795f61479a54dc9c80e4f4be6e51feeb832789ab73286c1c7c3bed1f81513469ab4d2feb8292ca1fb2538f6889e
data/.rubocop.yml CHANGED
@@ -1,3 +1,4 @@
1
+ # Style
1
2
  Style/StringLiterals:
2
3
  Enabled: false
3
4
 
@@ -5,5 +6,18 @@ Style/StringLiteralsInInterpolation:
5
6
  Enabled: true
6
7
  EnforcedStyle: double_quotes
7
8
 
9
+ Style/ClassAndModuleChildren:
10
+ Enabled: false
11
+
12
+ Style/Lambda:
13
+ Enabled: false
14
+
15
+ Style/FrozenStringLiteralComment:
16
+ Enabled: false
17
+
18
+ Style/Documentation:
19
+ Enabled: false
20
+
21
+ # Layout
8
22
  Layout/LineLength:
9
- Max: 120
23
+ Max: 200
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pr-with-params (1.1.0)
4
+ pr-with-params (1.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -34,6 +34,8 @@ For a full list of options, run `$ pr-with-params -h`
34
34
  Gem supports defining options in a yaml file (`.yaml`, `.yml`) like so:
35
35
  ```yaml
36
36
  default:
37
+ validators:
38
+ - conventional_commits
37
39
  base_branch: main
38
40
  template: new_feature_template.md
39
41
  assignees: 2k-joker
@@ -54,11 +56,17 @@ bug_fix:
54
56
  ```
55
57
 
56
58
  **Supported configs**
57
- * base_branch
58
- * template
59
- * title
60
- * labels
61
- * assignees
59
+ | Config | Type | Example |
60
+ | :--- | :---: | :--- |
61
+ | validators | `Array` | \`[conventional_commits]\` |
62
+ | base_branch | `String` | develop |
63
+ | template | `String` | new_feature_template.md |
64
+ | title | `String` | 'Update login screen' |
65
+ | lables | `String` | frontend,WIP |
66
+ | assignees | `String` | 2k-joker |
67
+
68
+ #### Validators
69
+ * **Conventional Commits**: Gem supports validation of conventional commits for PR `:title`. If you want to follow [conventional commit specs](https://www.conventionalcommits.org/en/v1.0.0/#specification) in your codebase, you may want to use this validation. To turn it on, set the `--validate-conventional-commits` flag or add `conventional_commits` to the list of validators in your config file.
62
70
 
63
71
  ## Development
64
72
 
data/exe/pr-with-params CHANGED
@@ -10,6 +10,7 @@ options = { expand: 1 }
10
10
  config_file_path = ''
11
11
  config_scope = nil
12
12
 
13
+ # rubocop:disable Metrics/BlockLength
13
14
  parser = OptionParser.new do |opt|
14
15
  opt.banner = "Usage pr-with-params [options]"
15
16
 
@@ -50,7 +51,12 @@ parser = OptionParser.new do |opt|
50
51
  opt.on('-a', '--assignees ASSIGNEES', "Specify a list of assignees (e.g: 'octocat,codedog').") do |pr_assignees|
51
52
  options[:assignees] = pr_assignees
52
53
  end
54
+
55
+ opt.on('--validate-conventional-commits', 'Validates that your PR title conforms to conventional commits specs.') do |_|
56
+ options[:validators] = options[:validators].to_a << :conventional_commits
57
+ end
53
58
  end
59
+ # rubocop:enable Metrics/BlockLength
54
60
 
55
61
  begin
56
62
  parser.parse!
@@ -64,6 +70,8 @@ begin
64
70
  default_title = `git show-branch --no-name $(git log #{base_branch}..#{branch_name} --pretty=format:"%h" | tail -1)`.chomp
65
71
  options[:title] ||= default_title
66
72
 
73
+ PR::With::Params.validate_options(options)
74
+
67
75
  remote_git_uri = `git config --get remote.origin.url`.sub('git@github.com:', '').sub('.git', '').chomp
68
76
  uri_host = 'www.github.com'
69
77
  uri_path = "/#{remote_git_uri}/compare/#{base_branch}...#{branch_name}"
@@ -8,7 +8,7 @@ module PR
8
8
  attr_reader :config_file_path, :scope, :parsed_config, :filtered_config
9
9
 
10
10
  # Constants
11
- VALID_CONFIG_KEYS = %i[base_branch template title labels assignees].freeze
11
+ VALID_CONFIG_KEYS = %i[validators base_branch template title labels assignees].freeze
12
12
 
13
13
  def initialize(config_file_path:, scope: nil)
14
14
  @config_file_path = config_file_path
@@ -23,7 +23,7 @@ module PR
23
23
  private
24
24
 
25
25
  def parse_yaml_config
26
- @parsed_config = YAML::load(IO.read(config_file_path)).transform_keys(&:to_sym)
26
+ @parsed_config = YAML.safe_load(IO.read(config_file_path)).transform_keys(&:to_sym)
27
27
  @filtered_config = scoped_config.transform_keys(&:to_sym).slice(*VALID_CONFIG_KEYS)
28
28
  end
29
29
 
@@ -36,8 +36,8 @@ module PR
36
36
  end
37
37
 
38
38
  def validate_file_type!
39
- raise TypeError.new('Config file type must be YAML (.yaml or .yml)') unless yaml_file?
40
- raise ArgumentError.new("Config file path is invalid or file does not exist: #{config_file_path}") unless file_exists?
39
+ raise(TypeError, 'Config file type must be YAML (.yaml or .yml)') unless yaml_file?
40
+ raise(ArgumentError, "Config file path is invalid or file does not exist: #{config_file_path}") unless file_exists?
41
41
  end
42
42
 
43
43
  def yaml_file?
@@ -0,0 +1,46 @@
1
+ module PR
2
+ module With
3
+ module Params
4
+ class OptionsValidator
5
+ VALIDATOR_CLASS_MAP = {
6
+ conventional_commits: 'PR::With::Params::ConventionalCommitValidator'
7
+ }.freeze
8
+
9
+ class << self
10
+ def validate!(options, validators: [])
11
+ validators.each do |validator|
12
+ validate_options(validator, options)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def validate_options(validator, options)
19
+ class_name = VALIDATOR_CLASS_MAP[validator.to_sym]
20
+ raise(ArgumentError, "Invalid or undefined validator: #{validator}") if class_name.nil?
21
+
22
+ Object.const_get(class_name).new(options).validate!
23
+ end
24
+ end
25
+ end
26
+
27
+ class ConventionalCommitValidator
28
+ CONVENTIONAL_COMMIT_REGEX = /^((build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(\w+\))?(!)?(: (.*\s*)*))|(^Merge (.*\s*)*)|(^Initial commit$)/.freeze
29
+
30
+ def initialize(options)
31
+ @commit_message = options[:title]
32
+ end
33
+
34
+ def validate!
35
+ raise("Conventional commit specifications not met for commit message: '#{@commit_message}'") unless valid_commit?
36
+ end
37
+
38
+ private
39
+
40
+ def valid_commit?
41
+ CONVENTIONAL_COMMIT_REGEX.match?(@commit_message)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -3,7 +3,7 @@
3
3
  module PR
4
4
  module With
5
5
  module Params
6
- VERSION = "1.1.0"
6
+ VERSION = "1.2.0"
7
7
  end
8
8
  end
9
9
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'params/version'
4
4
  require_relative 'params/config_parser'
5
+ require_relative 'params/options_validator'
5
6
  require 'uri'
6
7
  require 'json'
7
8
  require 'launchy'
@@ -32,6 +33,11 @@ module PR
32
33
 
33
34
  {}
34
35
  end
36
+
37
+ def validate_options(options)
38
+ validators = options.delete(:validators)
39
+ OptionsValidator.validate!(options, validators: validators)
40
+ end
35
41
  end
36
42
  end
37
43
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pr-with-params
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
  - 2k-joker
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-28 00:00:00.000000000 Z
11
+ date: 2022-09-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -32,6 +32,7 @@ files:
32
32
  - exe/pr-with-params
33
33
  - lib/pr/with/params.rb
34
34
  - lib/pr/with/params/config_parser.rb
35
+ - lib/pr/with/params/options_validator.rb
35
36
  - lib/pr/with/params/version.rb
36
37
  - pr-with-params.gemspec
37
38
  homepage: https://github.com/2k-joker/pr-with-params