pr-with-params 0.1.3 → 0.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: 2ab35532543684c0467e8c85b4f05d6bb34c2f37a31f09d6d19f546b8f973ca0
4
- data.tar.gz: 79e8ae8b681c506ebad08b38c7cf4ce624a89715cddbe37181f9c7aaa2e3fef5
3
+ metadata.gz: eba5280a74bea8222e60654a6798ac5cdf60b659a9e085903e0a74a374a900a8
4
+ data.tar.gz: 78acb1fcda0cd7043fd25e51169365ed66c3ce36616209c8856a569bdf6e76cd
5
5
  SHA512:
6
- metadata.gz: 29e4a1bb172ff62a1ae49ce0fbb4c92ee3860c183c1db8b56614fe535e19e3b08a16cfff76b178d5fd2240b772c901f0c1affbcbb0b23cefddadb26838cff675
7
- data.tar.gz: 4c15b85e016f2cff82f0f2b8e7cde845063b25074f3ae331bfcc3a9d7250f058a635006fc7d6bdaf26f1ef9592f76faecdf84dd2f5294a71f5428423818d1a6d
6
+ metadata.gz: efd0f0b2bc418846d6eb9ca8e37b8c15c03feb77be602e65e811432592108eb22841e8509506050f82f8a1deba547c89eae86ca58d375f38ad0b588d894a621d
7
+ data.tar.gz: bc35fade6fcf7ddda8ffd4e6ccdaf12e2075060806d326c770b300ccf86a179055a8fe262ad529fc094ae4873156fd72ad7a412877417ded357fa6d7e520d118
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pr-with-params (0.1.3)
4
+ pr-with-params (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -27,6 +27,36 @@ $ pr-with-params -t new_feature_template.md -l 'work in progress'
27
27
 
28
28
  For a full list of options, run `$ pr-with-params -h`
29
29
 
30
+ #### Using Config File
31
+
32
+ Gem supports defining options in a yaml file (`.yaml`, `.yml`) like so:
33
+ ```yaml
34
+ default:
35
+ base_branch: main
36
+ template: new_feature_template.md
37
+ assignees: 2k-joker
38
+ labels: enhancement
39
+
40
+ bug_fix:
41
+ template: bug_fix_template.md
42
+ labels: bug,urgent
43
+ ```
44
+
45
+ * To run with config file, use `$ pr-with-params --conf='path/to/file.yml' --scope=bug_fix`. If `--scope` option is not specified, only `:default` scope will apply.
46
+ * All your defaults go in the `:default` scope
47
+ * Only fields defined in another scope will override the defaults. In the example above, the final list of configs will be:
48
+
49
+ ```ruby
50
+ { base_branch: 'main', template: 'bug_fix_template.md', assignees: '2k-joker', labels: 'bug,urgent' }
51
+ ```
52
+
53
+ **Supported configs**
54
+ * base_branch
55
+ * template
56
+ * title
57
+ * labels
58
+ * assignees
59
+
30
60
  ## Development
31
61
 
32
62
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/exe/pr-with-params CHANGED
@@ -4,8 +4,11 @@ require 'bundler/setup' # support local testing/development
4
4
  require_relative '../lib/pr/with/params'
5
5
  require 'json'
6
6
  require 'optparse'
7
+ require 'open3'
7
8
 
8
9
  options = { expand: 1 }
10
+ config_file_path = ''
11
+ config_scope = nil
9
12
 
10
13
  parser = OptionParser.new do |opt|
11
14
  opt.banner = "Usage pr-with-params [options]"
@@ -19,6 +22,15 @@ parser = OptionParser.new do |opt|
19
22
 
20
23
  opt.separator ''
21
24
 
25
+ opt.on('--conf FILE_PATH', 'Path to yaml file where options are defined. NOTE that relative paths are not supported.') do |conf_file|
26
+ stdout, _stderr, _status = Open3.capture3("echo #{conf_file}")
27
+ config_file_path = stdout.chomp
28
+ end
29
+
30
+ opt.on('--scope CONFIG_SCOPE', 'Specify the scope name under which options are defined.') do |scope|
31
+ config_scope = scope
32
+ end
33
+
22
34
  opt.on('--base-branch BRANCH', "Specify the base branch for your PR (e.g: 'develop'). Will use default branch otherwise.") do |pr_base_branch|
23
35
  options[:base_branch] = pr_base_branch
24
36
  end
@@ -43,18 +55,21 @@ end
43
55
  begin
44
56
  parser.parse!
45
57
 
46
- branch_name = `git rev-parse --abbrev-ref HEAD`
47
- base_branch = options.delete(:base_branch) || `git remote show origin | grep "HEAD branch" | sed 's/.*: //'`
58
+ config_options = PR::With::Params.parse_config(config_file_path, config_scope)
59
+ options.merge!(config_options)
60
+
61
+ branch_name = `git rev-parse --abbrev-ref HEAD`.chomp
62
+ base_branch = options.delete(:base_branch) || `git remote show origin | grep "HEAD branch" | sed 's/.*: //'`.chomp
48
63
  remote_git_uri = `git config --get remote.origin.url`.sub('git@github.com:', '').sub('.git', '').chomp
49
64
 
50
65
  uri_host = 'www.github.com'
51
- uri_path = "/#{remote_git_uri}/compare/#{base_branch.chomp}...#{branch_name.chomp}"
66
+ uri_path = "/#{remote_git_uri}/compare/#{base_branch}...#{branch_name}"
52
67
 
53
- puts "current branch: \e[36m#{branch_name.chomp}\e[0m"
54
- puts "base branch: \e[36m#{base_branch.chomp}\e[0m"
68
+ puts "current branch: \e[36m#{branch_name}\e[0m"
69
+ puts "base branch: \e[36m#{base_branch}\e[0m"
55
70
  puts "repo path: \e[36m#{remote_git_uri}\e[0m"
56
71
 
57
- push_message = "\nPushing your local branch to origin/#{branch_name.chomp}..."
72
+ push_message = "\nPushing your local branch to origin/#{branch_name}..."
58
73
  puts "\e[32m#{push_message}\e[0m"
59
74
  `sleep 1`
60
75
  system("git push -u origin #{branch_name}")
@@ -67,9 +82,9 @@ rescue StandardError => e
67
82
  error_message = {
68
83
  message: 'An error occurred while building or opening your custom pull request URL',
69
84
  reason: e.message,
70
- backtrace: e.backtrace.last(10)
85
+ backtrace: e.backtrace&.last(10)
71
86
  }.to_json
72
87
 
73
- STDERR.puts error_message
88
+ STDERR.puts "\e[31mERROR\e[0m: " + error_message + "\n"
74
89
  exit 1
75
90
  end
@@ -0,0 +1,53 @@
1
+ require 'yaml'
2
+
3
+ module PR
4
+ module With
5
+ module Params
6
+ class ConfigParser
7
+ # Attributes
8
+ attr_reader :config_file_path, :scope, :parsed_config, :filtered_config
9
+
10
+ # Constants
11
+ VALID_CONFIG_KEYS = %i[base_branch template title labels assignees].freeze
12
+
13
+ def initialize(config_file_path:, scope: nil)
14
+ @config_file_path = config_file_path
15
+ @scope = scope&.to_sym || :default
16
+ end
17
+
18
+ def parse!
19
+ validate_file_type!
20
+ parse_yaml_config
21
+ end
22
+
23
+ private
24
+
25
+ def parse_yaml_config
26
+ @parsed_config = YAML::load(IO.read(config_file_path)).transform_keys(&:to_sym)
27
+ @filtered_config = scoped_config.transform_keys(&:to_sym).slice(*VALID_CONFIG_KEYS)
28
+ end
29
+
30
+ def scoped_config
31
+ if scope == :default || parsed_config.fetch(scope, {}).empty?
32
+ parsed_config.fetch(:default, {})
33
+ else
34
+ parsed_config.fetch(:default, {}).merge(parsed_config[scope])
35
+ end
36
+ end
37
+
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?
41
+ end
42
+
43
+ def yaml_file?
44
+ config_file_path.end_with?('.yaml') || config_file_path.end_with?('yml')
45
+ end
46
+
47
+ def file_exists?
48
+ File.file?(config_file_path)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -3,7 +3,7 @@
3
3
  module PR
4
4
  module With
5
5
  module Params
6
- VERSION = "0.1.3"
6
+ VERSION = "0.2.0"
7
7
  end
8
8
  end
9
9
  end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "params/version"
3
+ require_relative 'params/version'
4
+ require_relative 'params/config_parser'
4
5
  require 'uri'
6
+ require 'json'
5
7
  require 'launchy'
6
8
 
7
9
  module PR
@@ -9,11 +11,27 @@ module PR
9
11
  module Params
10
12
  class Error < StandardError; end
11
13
 
12
- def self.open(host:, path:, query:)
14
+ extend self
15
+
16
+ def open(host:, path:, query:)
13
17
  uri_query = URI.encode_www_form(query)
14
18
  url_string = URI::HTTPS.build(host: host, path: path, query: uri_query).to_s
15
19
  Launchy.open(url_string)
16
20
  end
21
+
22
+ def parse_config(file_path, scope)
23
+ config_options = ConfigParser.new(config_file_path: file_path, scope: scope).parse!
24
+ rescue StandardError => e
25
+ error_message = {
26
+ message: "Error parsing config file. Using defaults",
27
+ reason: e.message,
28
+ backtrace: e.backtrace&.last(10)
29
+ }.to_json
30
+
31
+ STDERR.puts "\e[35mWARNING\e[0m: " + error_message + "\n"
32
+
33
+ {}
34
+ end
17
35
  end
18
36
  end
19
37
  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: 0.1.3
4
+ version: 0.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-06 00:00:00.000000000 Z
11
+ date: 2022-08-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -31,6 +31,7 @@ files:
31
31
  - bin/setup
32
32
  - exe/pr-with-params
33
33
  - lib/pr/with/params.rb
34
+ - lib/pr/with/params/config_parser.rb
34
35
  - lib/pr/with/params/version.rb
35
36
  - pr-with-params.gemspec
36
37
  homepage: https://github.com/2k-joker/pr-with-params