pr-with-params 0.1.3 → 1.0.2

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: bd62108def63836659f73059843a5260211596b80e344a2543939688b3dadaba
4
+ data.tar.gz: b538c821030781006c3df77169c082ada6ccf68c7a74d330f23363d93792ccaa
5
5
  SHA512:
6
- metadata.gz: 29e4a1bb172ff62a1ae49ce0fbb4c92ee3860c183c1db8b56614fe535e19e3b08a16cfff76b178d5fd2240b772c901f0c1affbcbb0b23cefddadb26838cff675
7
- data.tar.gz: 4c15b85e016f2cff82f0f2b8e7cde845063b25074f3ae331bfcc3a9d7250f058a635006fc7d6bdaf26f1ef9592f76faecdf84dd2f5294a71f5428423818d1a6d
6
+ metadata.gz: e7d30fbe9b54a10925d688bd9ece749c034ada2b300e0f4bbb3022a0ae0380f335257506aa7b41c8bf7593ceab8639cefc55a84fad88585d288ccf365b33587d
7
+ data.tar.gz: 3fe80d1fdbae5795051485882a56d5ff5ac26e1bdabf01b62121655fe52a34d7f95d0fa3a302b0d064ca64ca6b5a52d6969f6dd541bab0e60ff6526297f11a25
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 (1.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
- # PR::With::Params
2
- A lightweight gem that pushes current local branch to remote with upstream to origin/[local-branch-name]. It also opens a new browser window at a URL with customized params, based on specified options, which allows to open pull request with pre-populated fields.
1
+ # PR::With::Params [![Gem Version](https://badge.fury.io/rb/pr-with-params.svg)](https://badge.fury.io/rb/pr-with-params)
2
+ A lightweight gem that 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.
3
+
4
+ Inspired by GitHub's documentation on [using query params to create pull requets](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/using-query-parameters-to-create-a-pull-request)
3
5
 
4
6
  ## Installation
5
7
 
@@ -27,6 +29,37 @@ $ pr-with-params -t new_feature_template.md -l 'work in progress'
27
29
 
28
30
  For a full list of options, run `$ pr-with-params -h`
29
31
 
32
+ #### Using Config File
33
+
34
+ Gem supports defining options in a yaml file (`.yaml`, `.yml`) like so:
35
+ ```yaml
36
+ default:
37
+ base_branch: main
38
+ template: new_feature_template.md
39
+ assignees: 2k-joker
40
+ labels: enhancement
41
+
42
+ bug_fix:
43
+ template: bug_fix_template.md
44
+ labels: bug,urgent
45
+ ```
46
+
47
+ * 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.
48
+ * If you specify a config file (`--conf`) and also pass options by flag (e.g `--base-branch=develop`), the flag value will override the config value.
49
+ * All your defaults go in the `:default` scope.
50
+ * Only fields defined in another scope will override the defaults. In the example above, the final list of configs will be:
51
+
52
+ ```ruby
53
+ { base_branch: 'main', template: 'bug_fix_template.md', assignees: '2k-joker', labels: 'bug,urgent' }
54
+ ```
55
+
56
+ **Supported configs**
57
+ * base_branch
58
+ * template
59
+ * title
60
+ * labels
61
+ * assignees
62
+
30
63
  ## Development
31
64
 
32
65
  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,21 +55,24 @@ 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 = config_options.merge(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
- system("git push -u origin #{branch_name}")
75
+ system("git push -u origin #{branch_name}", exception: true)
61
76
 
62
77
  open_url_message = "\nOpening pull request browser window..."
63
78
  puts "\e[32m#{open_url_message}\e[0m"
@@ -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 "\n" + "\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 = "1.0.2"
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
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ["2k-joker"]
9
9
  spec.email = ["kum.vanjunior@gmail.com"]
10
10
 
11
- spec.summary = "Pushes current local branch to remote with upstream to origin/[local-branch-name]. It also opens a new browser window at a URL with customized params, based on specified options, which allows to open pull request with pre-populated fields."
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
13
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
14
 
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: 1.0.2
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-10 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
@@ -55,7 +56,8 @@ requirements: []
55
56
  rubygems_version: 3.2.3
56
57
  signing_key:
57
58
  specification_version: 4
58
- summary: Pushes current local branch to remote with upstream to origin/[local-branch-name].
59
- It also opens a new browser window at a URL with customized params, based on specified
60
- options, which allows to open pull request with pre-populated fields.
59
+ summary: Pushes current local branch to remote with upstream at origin/[local-branch-name].
60
+ It also opens a new pull request browser window at a URL with customized query params,
61
+ based on specified options, which pre-populates certain fields in the pull request.
62
+ This is especially useful when supporting multiple PR templates within a code base.
61
63
  test_files: []