pr-with-params 1.0.2 → 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: bd62108def63836659f73059843a5260211596b80e344a2543939688b3dadaba
4
- data.tar.gz: b538c821030781006c3df77169c082ada6ccf68c7a74d330f23363d93792ccaa
3
+ metadata.gz: 9d4ad8c0505135845906a907642802d1e6426610e114cfd33be9c090a4866713
4
+ data.tar.gz: 71b40495173262b6ef5cf9ce0ea85f160e677d95505cc2cded45036831e86a90
5
5
  SHA512:
6
- metadata.gz: e7d30fbe9b54a10925d688bd9ece749c034ada2b300e0f4bbb3022a0ae0380f335257506aa7b41c8bf7593ceab8639cefc55a84fad88585d288ccf365b33587d
7
- data.tar.gz: 3fe80d1fdbae5795051485882a56d5ff5ac26e1bdabf01b62121655fe52a34d7f95d0fa3a302b0d064ca64ca6b5a52d6969f6dd541bab0e60ff6526297f11a25
6
+ metadata.gz: c592f1a860463ae680e56dc919d91a1e575c0beead05bfc0f64d759fc509b28cd06e2bc42ddd4240a38e85ee40c7d74c9b64b87157cf740579b2fe63c6632fff
7
+ data.tar.gz: 2dd182433b8815efe50dd0405232c9319d2d0795f61479a54dc9c80e4f4be6e51feeb832789ab73286c1c7c3bed1f81513469ab4d2feb8292ca1fb2538f6889e
data/.rubocop.yml CHANGED
@@ -1,10 +1,23 @@
1
+ # Style
1
2
  Style/StringLiterals:
2
- Enabled: true
3
- EnforcedStyle: double_quotes
3
+ Enabled: false
4
4
 
5
5
  Style/StringLiteralsInInterpolation:
6
6
  Enabled: true
7
7
  EnforcedStyle: double_quotes
8
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
9
22
  Layout/LineLength:
10
- 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.0.2)
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
 
@@ -39,7 +40,7 @@ parser = OptionParser.new do |opt|
39
40
  options[:template] = pr_template
40
41
  end
41
42
 
42
- opt.on('-d', '--description DESC', 'Specify a custom PR title') do |pr_description|
43
+ opt.on('-d', '--description DESC', 'Specify a custom PR title. Will use the first branch commit message otherwise.') do |pr_description|
43
44
  options[:title] = pr_description
44
45
  end
45
46
 
@@ -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!
@@ -60,8 +66,13 @@ begin
60
66
 
61
67
  branch_name = `git rev-parse --abbrev-ref HEAD`.chomp
62
68
  base_branch = options.delete(:base_branch) || `git remote show origin | grep "HEAD branch" | sed 's/.*: //'`.chomp
63
- remote_git_uri = `git config --get remote.origin.url`.sub('git@github.com:', '').sub('.git', '').chomp
64
69
 
70
+ default_title = `git show-branch --no-name $(git log #{base_branch}..#{branch_name} --pretty=format:"%h" | tail -1)`.chomp
71
+ options[:title] ||= default_title
72
+
73
+ PR::With::Params.validate_options(options)
74
+
75
+ remote_git_uri = `git config --get remote.origin.url`.sub('git@github.com:', '').sub('.git', '').chomp
65
76
  uri_host = 'www.github.com'
66
77
  uri_path = "/#{remote_git_uri}/compare/#{base_branch}...#{branch_name}"
67
78
 
@@ -85,6 +96,6 @@ rescue StandardError => e
85
96
  backtrace: e.backtrace&.last(10)
86
97
  }.to_json
87
98
 
88
- STDERR.puts "\n" + "\e[31mERROR:\e[0m " + error_message + "\n"
99
+ warn "\e[31mERROR\e[0m: " + error_message + "\n"
89
100
  exit 1
90
101
  end
@@ -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 ArgumentError.new('Config file path is invalid or file does not exist.') unless file_exists?
40
- raise TypeError.new('Config file type must be YAML (.yaml or .yml)') unless yaml_file?
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.0.2"
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'
@@ -20,7 +21,7 @@ module PR
20
21
  end
21
22
 
22
23
  def parse_config(file_path, scope)
23
- config_options = ConfigParser.new(config_file_path: file_path, scope: scope).parse!
24
+ file_path.empty? ? {} : ConfigParser.new(config_file_path: file_path, scope: scope).parse!
24
25
  rescue StandardError => e
25
26
  error_message = {
26
27
  message: "Error parsing config file. Using defaults",
@@ -28,10 +29,15 @@ module PR
28
29
  backtrace: e.backtrace&.last(10)
29
30
  }.to_json
30
31
 
31
- STDERR.puts "\e[35mWARNING\e[0m: " + error_message + "\n"
32
-
32
+ warn "\e[35mWARNING\e[0m: " + error_message + "\n"
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
@@ -10,6 +10,7 @@ Gem::Specification.new do |spec|
10
10
 
11
11
  spec.summary = "Pushes current local branch to remote with upstream at origin/[local-branch-name]. It also opens a new pull request browser window at a URL with customized query params, based on specified options, which pre-populates certain fields in the pull request. This is especially useful when supporting multiple PR templates within a code base."
12
12
  spec.homepage = "https://github.com/2k-joker/pr-with-params"
13
+ spec.licenses = ["MIT"]
13
14
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
15
 
15
16
  spec.metadata["homepage_uri"] = spec.homepage
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.0.2
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-10 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,10 +32,12 @@ 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
38
- licenses: []
39
+ licenses:
40
+ - MIT
39
41
  metadata:
40
42
  homepage_uri: https://github.com/2k-joker/pr-with-params
41
43
  post_install_message: