git_helper 1.0.0 → 1.3.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: 155df8ff3253824dd7a7ba9b8e1c68862bd84c02985b56cc5aae3e4a8111bf02
4
- data.tar.gz: ec4aacae883bf8655dbb8382c738180cf29d150a67f674c6d8b3ed02dea76335
3
+ metadata.gz: 7959639f26bda279bb9154f76ba1d5041802d5ac63fbb02d6df3ca85d55040ca
4
+ data.tar.gz: 59144b9c94dd74ac2bc8c05417363ee37f926a2266fe623e3c13032750910025
5
5
  SHA512:
6
- metadata.gz: 42382a1b731fbcc6a601a733158deb41c38af92141d438148330736c5be2d5934f5283524fec215e41c61375a1d0fef341a318dce2c925c42e857c5d4950b3fc
7
- data.tar.gz: 7307415e608846cd7f55bae4540fff9a322e19bb6813c1cbd22291370cb7432ba44105b393cbd7f32614ccbf9f8821534aea9d6f99ec8048de7a917dd6767cc2
6
+ metadata.gz: 104ed8f0605c4471b0e4163dfe7148a4a5b2139871150e5577ed371035419122d9c64571aa2ff0897d812e5a08982c3cbd544e6d9936d36d792df2f0fbe9248d
7
+ data.tar.gz: 4eef1f32e7c147279228388ade7056a46568cacae9fe61dcd58534b42b50c2eb422a8fd9c043ff9fdf138595dccf8a17d7771be877fa2c2ba6bf13eaaa9ec5ff
@@ -61,7 +61,7 @@ GEM
61
61
  coderay (~> 1.1)
62
62
  method_source (~> 1.0)
63
63
  public_suffix (4.0.6)
64
- rake (10.5.0)
64
+ rake (12.3.3)
65
65
  rb-fsevent (0.10.4)
66
66
  rb-inotify (0.10.1)
67
67
  ffi (~> 1.0)
@@ -90,7 +90,7 @@ DEPENDENCIES
90
90
  git_helper!
91
91
  guard (~> 2.6)
92
92
  guard-rspec (~> 4.3)
93
- rake (~> 10.1)
93
+ rake (~> 12.3.3)
94
94
  rspec (~> 2.99)
95
95
 
96
96
  BUNDLED WITH
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # git_helper [![Maintainability](https://api.codeclimate.com/v1/badges/ce1bdd719cc21b7c22a6/maintainability)](https://codeclimate.com/github/emmasax4/git_helper/maintainability)
1
+ # git_helper [![Maintainability](https://api.codeclimate.com/v1/badges/d53da11f17c38cc81b5b/maintainability)](https://codeclimate.com/github/emmasax4/git_helper/maintainability) ![Develop](https://github.com/emmasax4/git_helper/workflows/Develop/badge.svg)
2
2
 
3
- ## Usage
3
+ ## Gem Usage
4
4
 
5
5
  ```bash
6
6
  gem install git_helper
@@ -17,14 +17,54 @@ Some of the commands in this gem can be used without any additional configuratio
17
17
 
18
18
  To view the help screen, run:
19
19
  ```bash
20
- ghelper --help
20
+ git-helper --help
21
21
  ```
22
22
 
23
23
  To see what version of git_helper you're running, run:
24
24
  ```bash
25
- ghelper --version
25
+ git-helper --version
26
26
  ```
27
27
 
28
+ ## Plugin Usage
29
+
30
+ As an additional option, you can set each of the following commands to be a git plugin, meaning you can call them in a way that feels even more git-native:
31
+
32
+ ```bash
33
+ # As a Gem # As a Plugin
34
+ git-helper clean-branches git clean-branches
35
+ git-helper pull-request --create git pull-request --create
36
+ ```
37
+
38
+ To do this, clone this repository from GitHub, and add the following line to your `~/.bash_profile`:
39
+
40
+ ```
41
+ export PATH=/your/path/to/this/repository/git_helper/plugins:$PATH
42
+ ```
43
+
44
+ And then run `source ~/.bash_profile`.
45
+
46
+ ## Alias Usage
47
+
48
+ To make the commands even shorter, I recommend setting aliases. You can either set aliases through git itself, like this (only possible if you also use the plugin option):
49
+
50
+ ```bash
51
+ git config --global alias.nb new-branch
52
+ ```
53
+
54
+ And then running `git nb` maps to `git new-branch`, which through the plugin, maps to `git-helper new-branch`.
55
+
56
+ Or you can set the alias through your `~/.bashrc` (which is my preferred method because it can make the command even shorter, and doesn't require the plugin usage). To do this, add lines like this to the `~/.bashrc` file and run `source ~/.bashrc`:
57
+
58
+ ```bash
59
+ alias gnb="git new-branch"
60
+ ```
61
+
62
+ And then, running `gnb` maps to `git new-branch`, which again routes to `git-helper new-branch`.
63
+
64
+ For a full list of the git aliases I prefer to use, check out my [Git Aliases gist](https://gist.github.com/emmasax4/e8744fe253fba1f00a621c01a2bf68f5).
65
+
66
+ If you're going to make using git workflows easier, might as well provide lots of options 😃.
67
+
28
68
  ## Commands
29
69
 
30
70
  ### `change-remote`
@@ -34,7 +74,7 @@ This can be used when switching the owners of a GitHub repo. When you switch a u
34
74
  This command will go through every directory in a directory, see if it is a git directory, and then will check to see if the old username is included in the remote URL of that git directory. If it is, then the command will change the remote URL to instead point to the new username's remote URL. To run the command, run:
35
75
 
36
76
  ```bash
37
- ghelper change-remote OLD-OWNER NEW-OWNER
77
+ git-helper change-remote OLD-OWNER NEW-OWNER
38
78
  ```
39
79
 
40
80
  ### `checkout-default`
@@ -42,7 +82,7 @@ ghelper change-remote OLD-OWNER NEW-OWNER
42
82
  This command will check out the default branch of whatever repository you're currently in. It looks at what branch the `origin/HEAD` remote is pointed to on your local machine, versus querying GitHub/GitLab for that, so if your local machine's remotes aren't up to date, then this command won't work as expected. To run this command, run:
43
83
 
44
84
  ```bash
45
- ghelper checkout-default
85
+ git-helper checkout-default
46
86
  ```
47
87
 
48
88
  If your local branches aren't right (run `git branch --remote` to see), then run:
@@ -51,12 +91,12 @@ If your local branches aren't right (run `git branch --remote` to see), then run
51
91
  git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/CORRECT-DEFAULT-BRANCH-GOES-HERE
52
92
  ```
53
93
 
54
- ### `clean-git`
94
+ ### `clean-branches`
55
95
 
56
96
  This command will bring you to the repository's default branch, `git pull`, `git fetch -p`, and will clean up your local branches on your machine by seeing which ones are existing on the remote, and updating yours accordingly. To run:
57
97
 
58
98
  ```bash
59
- ghelper clean-git
99
+ git-helper clean-branches
60
100
  ```
61
101
 
62
102
  ### `empty-commit`
@@ -64,54 +104,62 @@ ghelper clean-git
64
104
  For some reason, I'm always forgetting the commands to create an empty commit. So with this command, it becomes easy. The commit message of this commit will be `'Empty commit'`. To run the command, run:
65
105
 
66
106
  ```bash
67
- ghelper empty-commit
107
+ git-helper empty-commit
68
108
  ```
69
109
 
70
- ### `new-branch`
110
+ ### `merge-request`
71
111
 
72
- This command is useful for making new branches in a repository on the command line. To run the command, run:
112
+ This command can be used to handily make new GitLab merge requests and to accept merge requests from the command line. The command uses the Ruby wrapper [`Gitlab`](https://github.com/NARKOZ/gitlab) to do this, so make sure you have a `.git_config.yml` file set up in the home directory of your computer. For instructions on how to do that, see [Usage](#usage).
113
+
114
+ After setup is complete, you can call the file, and send in a flag indicating whether to create a pull request, `-c`, or to merge a pull request, `-m`.
73
115
 
74
116
  ```bash
75
- ghelper new-branch
117
+ git-helper merge-request -c
76
118
  # OR
77
- ghelper new-branch NEW_BRANCH_NAME
119
+ git-helper merge-request -m
78
120
  ```
79
121
 
80
- The command either accepts a branch name right away or it will ask you for the name of your new branch. Make sure your input does not contain any spaces or special characters.
122
+ If you're trying to create a merge request, the command will provide an autogenerated merge request title based on your branch name. It will separate the branch name by `'_'` if underscores are in the branch, or `'-'` if dashes are present. Then it will join the list of words together by spaces. If there's a pattern in the form of `jira-123` or `jira_123` in the first part of the branch name, then it'll add `JIRA-123` to the first part of the pull request. You can respond 'yes' or 'no'. If you respond 'no', you can provide your own merge request title.
81
123
 
82
- ### `pull-request`
124
+ The command will also ask you if the default branch of the repository is the proper base branch to use. You can say 'yes' or 'no'. If you respond 'no', then you can give the command your chosen base base.
83
125
 
84
- This command can be used to handily make new GitHub pull requests and to merge pull requests from the command line. The command uses the [`Octokit::Client`](https://octokit.github.io/octokit.rb/Octokit/Client.html) to do this, so make sure you have a `.git_config.yml` file set up in the home directory of your computer. For instructions on how to do that, see [Usage](#usage).
126
+ Lastly, it'll ask the user to apply any merge request templates found at any `*/merge_request_template.md` file or any file in `.gitlab/merge_request_templates/*.md`. Applying any template is optional, and from the command's standpoint, a user can make an empty merge request if they desire. If a GitLab project automatically adds a template, then it may create a merge request with a default template anyway.
85
127
 
86
- After setup is complete, you can call the file, and send in a flag indicating whether to create a pull request, `-c`, or to merge a pull request, `-m`.
128
+ If you're requesting to merge a merge request, the command will ask you the number ID of the merge request you wish to accept. The command will also ask you what type of merge to do: regular merging or squashing. The commit message to use during the merge/squash will be the title of the pull request.
129
+
130
+ If you're getting stuck, you can run the command with a `--help` flag instead, to get some more information.
131
+
132
+ ### `new-branch`
133
+
134
+ This command is useful for making new branches in a repository on the command line. To run the command, run:
87
135
 
88
136
  ```bash
89
- ghelper pull-request -c
137
+ git-helper new-branch
90
138
  # OR
91
- ghelper pull-request -m
139
+ git-helper new-branch NEW_BRANCH_NAME
92
140
  ```
93
141
 
94
- If you're trying to create a pull request, the command will provide an autogenerated pull request title based on your branch name (separated by `_`). You can respond 'yes' or 'no'. If you respond 'no', you can provide your own pull request title. The command will also ask you if the default branch of the repository is the proper base branch to use. You can say 'yes' or 'no'. If you respond 'no', then you can give the command your chosen base base. Lastly, it'll ask the user to apply any pull request templates found at any `*/pull_request_template.md` file or any file in `.github/PULL_REQUEST_TEMPLATE/*.md`. Applying any template is optional, and a user can make an empty pull request if they desire.
95
-
96
- If you're requesting to merge a pull request, the command will ask you the number ID of the pull request you wish to merge. The command will also ask you what type of merge to do: regular merging, squashing, or rebasing. The commit message to use during the merge/squash/rebase will be the title of the pull request.
97
-
98
- If you're getting stuck, you can run the command with a `--help` flag instead, to get some more information.
142
+ The command either accepts a branch name right away or it will ask you for the name of your new branch. Make sure your input does not contain any spaces or special characters.
99
143
 
100
- ### `merge-request`
144
+ ### `pull-request`
101
145
 
102
- This command can be used to handily make new GitLab merge requests and to accept merge requests from the command line. The command uses the Ruby wrapper [`Gitlab`](https://github.com/NARKOZ/gitlab) to do this, so make sure you have a `.git_config.yml` file set up in the home directory of your computer. For instructions on how to do that, see [Usage](#usage).
146
+ This command can be used to handily make new GitHub pull requests and to merge pull requests from the command line. The command uses the [`Octokit::Client`](https://octokit.github.io/octokit.rb/Octokit/Client.html) to do this, so make sure you have a `.git_config.yml` file set up in the home directory of your computer. For instructions on how to do that, see [Usage](#usage).
103
147
 
104
148
  After setup is complete, you can call the file, and send in a flag indicating whether to create a pull request, `-c`, or to merge a pull request, `-m`.
105
149
 
106
150
  ```bash
107
- ghelper merge-request -c
151
+ git-helper pull-request -c
108
152
  # OR
109
- ghelper merge-request -m
153
+ git-helper pull-request -m
110
154
  ```
111
155
 
112
- If you're trying to create a merge request, the command will provide an autogenerated merge request title based on your branch name (separated by `_`). You can respond 'yes' or 'no'. If you respond 'no', you can provide your own merge request title. The command will also ask you if the default branch of the repository is the proper base branch to use. You can say 'yes' or 'no'. If you respond 'no', then you can give the command your chosen base base. Lastly, it'll ask the user to apply any merge request templates found at any `*/merge_request_template.md` file or any file in `.gitlab/merge_request_templates/*.md`. Applying any template is optional, and from the command's standpoint, a user can make an empty merge request if they desire. If a GitLab project automatically adds a template, then it may create a merge request with a default template anyway.
156
+ If you're trying to create a pull request, the command will provide an autogenerated pull request title based on your branch name. It will separate the branch name by `'_'` if underscores are in the branch, or `'-'` if dashes are present. Then it will join the list of words together by spaces. If there's a pattern in the form of `jira-123` or `jira_123` in the first part of the branch name, then it'll add `JIRA-123` to the first part of the pull request. You can respond 'yes' or 'no'. If you respond 'no', you can provide your own pull request title.
113
157
 
114
- If you're requesting to merge a merge request, the command will ask you the number ID of the merge request you wish to accept. The command will also ask you what type of merge to do: regular merging or squashing. The commit message to use during the merge/squash will be the title of the pull request.
158
+ The command will also ask you if the default branch of the repository is the proper base branch to use. You can say 'yes' or 'no'. If you respond 'no', then you can give the command your chosen base base.
159
+
160
+ Lastly, it'll ask the user to apply any pull request templates found at any `*/pull_request_template.md` file or any file in `.github/PULL_REQUEST_TEMPLATE/*.md`. Applying any template is optional, and a user can make an empty pull request if they desire.
161
+
162
+ If you're requesting to merge a pull request, the command will ask you the number ID of the pull request you wish to merge. The command will also ask you what type of merge to do: regular merging, squashing, or rebasing. The commit message to use during the merge/squash/rebase will be the title of the pull request.
115
163
 
116
164
  If you're getting stuck, you can run the command with a `--help` flag instead, to get some more information.
117
165
 
@@ -122,3 +170,16 @@ To submit a feature request, bug ticket, etc, please submit an official [GitHub
122
170
  To report any security vulnerabilities, please view this project's [Security Policy](https://github.com/emmasax4/git_helper/security/policy).
123
171
 
124
172
  This repository does have a standard [Code of Conduct](https://github.com/emmasax4/git_helper/blob/main/.github/code_of_conduct.md).
173
+
174
+ ## Releasing
175
+
176
+ To make a new release of this gem:
177
+
178
+ 1. Merge the pull request via the big green button
179
+ 2. Run `git tag vX.X.X` and `git push --tag`
180
+ 3. Make a new release [here](https://github.com/emmasax4/git_helper/releases/new)
181
+ 4. Run `gem build *.gemspec`
182
+ 5. Run `gem push *.gem` to push the new gem to RubyGems
183
+ 6. Run `rm *.gem` to clean up your local repository
184
+
185
+ To set up your local machine to push to RubyGems via the API, see the [RubyGems documentation](https://guides.rubygems.org/publishing/#publishing-to-rubygemsorg).
@@ -28,7 +28,7 @@ command 'change-remote' do |c|
28
28
  end
29
29
  end
30
30
 
31
- desc "Checks out the default branch of the repository based on the repository's local remote branches."
31
+ desc "Checks out the default branch of a repo based on the local remote branches."
32
32
  command 'checkout-default' do |c|
33
33
  c.action do |global_options, options, args|
34
34
  require_relative '../lib/git_helper/checkout_default.rb'
@@ -37,10 +37,10 @@ command 'checkout-default' do |c|
37
37
  end
38
38
 
39
39
  desc "Clean a repository's git branches."
40
- command 'clean-git' do |c|
40
+ command 'clean-branches' do |c|
41
41
  c.action do |global_options, options, args|
42
- require_relative '../lib/git_helper/clean_git.rb'
43
- GitHelper::CleanGit.new.execute
42
+ require_relative '../lib/git_helper/clean_branches.rb'
43
+ GitHelper::CleanBranches.new.execute
44
44
  end
45
45
  end
46
46
 
@@ -59,7 +59,7 @@ command 'merge-request' do |c|
59
59
 
60
60
  c.action do |global_options, options, args|
61
61
  require_relative '../lib/git_helper/merge_request.rb'
62
- raise ArgumentError, 'You must specify an action (either --merge or --create)' unless options[:create] || options[:merge]
62
+ raise ArgumentError, "You must specify an action (either '-m'/'--merge' or '-c'/'--create')" unless options[:create] || options[:merge]
63
63
 
64
64
  options = global_options.merge(options)
65
65
 
@@ -87,7 +87,7 @@ command 'pull-request' do |c|
87
87
 
88
88
  c.action do |global_options, options, args|
89
89
  require_relative '../lib/git_helper/pull_request.rb'
90
- raise ArgumentError, 'You must specify an action (either --merge or --create)' unless options[:create] || options[:merge]
90
+ raise ArgumentError, "You must specify an action (either '-m'/'--merge' or '-c'/'--create')" unless options[:create] || options[:merge]
91
91
 
92
92
  options = global_options.merge(options)
93
93
 
@@ -1,10 +1,10 @@
1
1
  module GitHelper
2
- class CleanGit
2
+ class CleanBranches
3
3
  def execute
4
4
  system("git checkout $(git symbolic-ref refs/remotes/origin/HEAD | sed \"s@^refs/remotes/origin/@@\")")
5
5
  system("git pull")
6
6
  system("git fetch -p")
7
- system("git branch -vv | grep \"origin/.*: gone]\" | awk \"{print \$1}\" | grep -v \"*\" | xargs git branch -D")
7
+ system("git branch -vv | grep \"origin/.*: gone]\" | awk '{print \$1}' | grep -v \"*\" | xargs git branch -D")
8
8
  end
9
9
  end
10
10
  end
@@ -2,24 +2,86 @@ require 'highline'
2
2
 
3
3
  module GitHelper
4
4
  class HighlineCli
5
- def ask(prompt)
6
- cli.ask(prompt) do |conf|
5
+ def new_branch_name
6
+ ask('New branch name?')
7
+ end
8
+
9
+ def title
10
+ ask('Title?')
11
+ end
12
+
13
+ def base_branch
14
+ ask('Base branch?')
15
+ end
16
+
17
+ def merge_request_id
18
+ ask('Merge Request ID?')
19
+ end
20
+
21
+ def pull_request_id
22
+ ask('Pull Request ID?')
23
+ end
24
+
25
+ def accept_autogenerated_title?(autogenerated_title)
26
+ return false unless autogenerated_title
27
+ answer = ask("Accept the autogenerated merge request title '#{autogenerated_title}'? (y/n)")
28
+ !!(answer =~ /^y/i)
29
+ end
30
+
31
+ def base_branch_default?(default_branch)
32
+ answer = ask("Is '#{default_branch}' the correct base branch for your new merge request? (y/n)")
33
+ !!(answer =~ /^y/i)
34
+ end
35
+
36
+ def squash_merge_request?
37
+ answer = ask('Squash merge request? (y/n)')
38
+ !!(answer =~ /^y/i)
39
+ end
40
+
41
+ def remove_source_branch?
42
+ answer = ask('Remove source branch after merging? (y/n)')
43
+ !!(answer =~ /^y/i)
44
+ end
45
+
46
+ def merge_method
47
+ merge_options = [ 'merge', 'squash', 'rebase' ]
48
+ index = ask_options("Merge method?", merge_options)
49
+ merge_options[index]
50
+ end
51
+
52
+ def apply_template?(template_file_name)
53
+ answer = ask("Apply the merge request template from #{template_file_name}? (y/n)")
54
+ !!(answer =~ /^y/i)
55
+ end
56
+
57
+ def template_to_apply(template_options, request_type)
58
+ complete_options = template_options << 'None'
59
+ index = ask_options("Which #{request_type} request template should be applied?", complete_options)
60
+ complete_options[index]
61
+ end
62
+
63
+ #######################
64
+ ### GENERAL METHODS ###
65
+ #######################
66
+
67
+ private def ask(prompt)
68
+ highline_client.ask(prompt) do |conf|
7
69
  conf.readline = true
8
70
  end.to_s
9
71
  end
10
72
 
11
- def ask_options(prompt, choices)
73
+ private def ask_options(prompt, choices)
12
74
  choices_as_string_options = ''
13
75
  choices.each { |choice| choices_as_string_options << "#{choices.index(choice) + 1}. #{choice}\n" }
14
76
  compiled_prompt = "#{prompt}\n#{choices_as_string_options.strip}"
15
77
 
16
- cli.ask(compiled_prompt) do |conf|
78
+ highline_client.ask(compiled_prompt) do |conf|
17
79
  conf.readline = true
18
80
  end.to_i - 1
19
81
  end
20
82
 
21
- private def cli
22
- @cli ||= HighLine.new
83
+ private def highline_client
84
+ @highline_client ||= HighLine.new
23
85
  end
24
86
  end
25
87
  end
@@ -0,0 +1,76 @@
1
+ module GitHelper
2
+ class LocalCode
3
+ def new_branch(branch_name)
4
+ system("git pull")
5
+ system("git branch --no-track #{branch_name}")
6
+ system("git checkout #{branch_name}")
7
+ system("git push --set-upstream origin #{branch_name}")
8
+ end
9
+
10
+ def name
11
+ # Get the repo/project name by looking in the remote URLs for the full name
12
+ `git remote -v`.scan(/\S[\s]*[\S]+.com[\S]{1}([\S]*).git/).first.first
13
+ end
14
+
15
+ def branch
16
+ # Get the current branch by looking in the list of branches for the *
17
+ `git branch`.scan(/\*\s([\S]*)/).first.first
18
+ end
19
+
20
+ def default_branch(project_name, external_client, client_type)
21
+ if client_type == :octokit # GitHub repository
22
+ external_client.repository(project_name).default_branch
23
+ elsif client_type == :gitlab # GitLab project
24
+ page_number = 1
25
+ counter = 1
26
+ branches = []
27
+
28
+ while counter > 0
29
+ break if default_branch = branches.select { |branch| branch.default }.first
30
+ page_branches = external_client.branches(project_name, page: page_number, per_page: 100)
31
+ branches = page_branches
32
+ counter = page_branches.count
33
+ page_number += 1
34
+ end
35
+
36
+ default_branch.name
37
+ end
38
+ end
39
+
40
+ def template_options(template_identifiers)
41
+ nested_templates = Dir.glob(
42
+ File.join("**/#{template_identifiers[:nested_directory_name]}", "*.md"),
43
+ File::FNM_DOTMATCH | File::FNM_CASEFOLD
44
+ )
45
+ non_nested_templates = Dir.glob(
46
+ File.join("**", "#{template_identifiers[:non_nested_file_name]}.md"),
47
+ File::FNM_DOTMATCH | File::FNM_CASEFOLD
48
+ )
49
+ nested_templates.concat(non_nested_templates).uniq
50
+ end
51
+
52
+ def read_template(file_name)
53
+ File.open(file_name).read
54
+ end
55
+
56
+ def generate_title(local_branch)
57
+ branch_arr = local_branch.split(local_branch.include?('_') ? '_' : '-')
58
+
59
+ return if branch_arr.empty?
60
+
61
+ if branch_arr.length == 1
62
+ branch_arr.first.capitalize
63
+ elsif branch_arr[0].scan(/([\w]+)/).any? && branch_arr[1].scan(/([\d]+)/).any? # branch includes jira_123 at beginning
64
+ issue = "#{branch_arr[0].upcase}-#{branch_arr[1]}"
65
+ description = branch_arr[2..-1].join(' ')
66
+ "#{issue} #{description.capitalize}"
67
+ elsif branch_arr[0].scan(/([\w]+-[\d]+)/).any? # branch includes string jira-123 at beginning
68
+ issue = branch_arr[0].upcase
69
+ description = branch_arr[1..-1].join(' ')
70
+ "#{issue} #{description.capitalize}"
71
+ else # plain words
72
+ branch_arr[0..-1].join(' ').capitalize
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,5 +1,6 @@
1
1
  require_relative './gitlab_client.rb'
2
2
  require_relative './highline_cli.rb'
3
+ require_relative './local_code.rb'
3
4
 
4
5
  module GitHelper
5
6
  class GitLabMergeRequest
@@ -15,7 +16,7 @@ module GitHelper
15
16
  }
16
17
 
17
18
  puts "Creating merge request: #{new_mr_title}"
18
- mr = gitlab_client.create_merge_request(local_repo, new_mr_title, options)
19
+ mr = gitlab_client.create_merge_request(local_project, new_mr_title, options)
19
20
 
20
21
  if mr.diff_refs.base_sha == mr.diff_refs.head_sha
21
22
  puts "Merge request was created, but no commits have been pushed to GitLab: #{mr.web_url}"
@@ -36,12 +37,12 @@ module GitHelper
36
37
  # Ask these questions right away
37
38
  mr_id
38
39
  options = {}
39
- options[:should_remove_source_branch] = remove_source_branch?
40
- options[:squash] = squash_merge_request?
40
+ options[:should_remove_source_branch] = remove_source_branch
41
+ options[:squash] = squash_merge_request
41
42
  options[:squash_commit_message] = existing_mr_title
42
43
 
43
44
  puts "Merging merge request: #{mr_id}"
44
- merge = gitlab_client.accept_merge_request(local_repo, mr_id, options)
45
+ merge = gitlab_client.accept_merge_request(local_project, mr_id, options)
45
46
  puts "Merge request successfully merged: #{merge.merge_commit_sha}"
46
47
  rescue Gitlab::Error::MethodNotAllowed => e
47
48
  puts 'Could not merge merge request:'
@@ -55,92 +56,80 @@ module GitHelper
55
56
  end
56
57
  end
57
58
 
58
- private def local_repo
59
- # Get the repository by looking in the remote URLs for the full repository name
60
- remotes = `git remote -v`
61
- return remotes.scan(/\S[\s]*[\S]+.com[\S]{1}([\S]*).git/).first.first
59
+ private def local_project
60
+ @local_project ||= local_code.name
62
61
  end
63
62
 
64
63
  private def local_branch
65
- # Get the current branch by looking in the list of branches for the *
66
- branches = `git branch`
67
- return branches.scan(/\*\s([\S]*)/).first.first
64
+ @local_branch ||= local_code.branch
68
65
  end
69
66
 
70
- private def read_template
71
- if mr_template_options.count == 1
72
- apply_template?(mr_template_options.first) ? File.open(mr_template_options.first).read : ''
73
- else
74
- template_file_name_to_apply = template_to_apply
75
- template_file_name_to_apply == "None" ? '' : File.open(template_file_name_to_apply).read
76
- end
77
- end
78
-
79
- private def mr_id
80
- @mr_id ||= cli.ask('Merge Request ID?')
81
- end
82
-
83
- private def existing_mr_title
84
- @existing_mr_title ||= gitlab_client.merge_request(local_repo, mr_id).title
67
+ private def autogenerated_title
68
+ @autogenerated_title ||= local_code.generate_title(local_branch)
85
69
  end
86
70
 
87
- private def new_mr_title
88
- @new_mr_title ||= accept_autogenerated_title? ? autogenerated_title : cli.ask('Title?')
71
+ private def default_branch
72
+ @default_branch ||= local_code.default_branch(local_project, gitlab_client, :gitlab)
89
73
  end
90
74
 
91
- private def new_mr_body
92
- @new_mr_body ||= mr_template_options.empty? ? '' : read_template
75
+ private def mr_template_options
76
+ @mr_template_options ||= local_code.template_options({
77
+ nested_directory_name: "merge_request_templates",
78
+ non_nested_file_name: "merge_request_template"
79
+ })
93
80
  end
94
81
 
95
- private def base_branch
96
- @base_branch ||= base_branch_default? ? default_branch : cli.ask('Base branch?')
82
+ private def mr_id
83
+ @mr_id ||= cli.merge_request_id
97
84
  end
98
85
 
99
- private def autogenerated_title
100
- @autogenerated_title ||= local_branch.split('_')[0..-1].join(' ').capitalize
86
+ private def squash_merge_request
87
+ @squash_merge_request ||= cli.squash_merge_request?
101
88
  end
102
89
 
103
- private def default_branch
104
- @default_branch ||= gitlab_client.branches(local_repo).select { |branch| branch.default }.first.name
90
+ private def remove_source_branch
91
+ @remove_source_branch ||= cli.remove_source_branch?
105
92
  end
106
93
 
107
- private def template_to_apply
108
- return @template_to_apply if @template_to_apply
109
- complete_options = mr_template_options << 'None'
110
- index = cli.ask_options("Which merge request template should be applied?", complete_options)
111
- @template_to_apply = complete_options[index]
94
+ private def new_mr_title
95
+ @new_mr_title ||= if cli.accept_autogenerated_title?(autogenerated_title)
96
+ autogenerated_title
97
+ else
98
+ cli.title
99
+ end
112
100
  end
113
101
 
114
- private def mr_template_options
115
- return @mr_template_options if @mr_template_options
116
- nested_templates = Dir.glob(File.join("**/merge_request_templates", "*.md"), File::FNM_DOTMATCH | File::FNM_CASEFOLD)
117
- non_nested_templates = Dir.glob(File.join("**", "merge_request_template.md"), File::FNM_DOTMATCH | File::FNM_CASEFOLD)
118
- @mr_template_options = nested_templates.concat(non_nested_templates)
102
+ private def base_branch
103
+ @base_branch ||= if cli.base_branch_default?(default_branch)
104
+ default_branch
105
+ else
106
+ cli.base_branch
107
+ end
119
108
  end
120
109
 
121
- private def base_branch_default?
122
- answer = cli.ask("Is '#{default_branch}' the correct base branch for your new merge request? (y/n)")
123
- !!(answer =~ /^y/i)
110
+ private def new_mr_body
111
+ @new_mr_body ||= template_name_to_apply ? local_code.read_template(template_name_to_apply) : ''
124
112
  end
125
113
 
126
- private def accept_autogenerated_title?
127
- answer = cli.ask("Accept the autogenerated merge request title '#{autogenerated_title}'? (y/n)")
128
- !!(answer =~ /^y/i)
129
- end
114
+ private def template_name_to_apply
115
+ return @template_name_to_apply if @template_name_to_apply
116
+ @template_name_to_apply = nil
130
117
 
131
- private def squash_merge_request?
132
- answer = cli.ask('Squash merge request? (y/n)')
133
- !!(answer =~ /^y/i)
134
- end
118
+ unless mr_template_options.empty?
119
+ if mr_template_options.count == 1
120
+ apply_single_template = cli.apply_template?(mr_template_options.first)
121
+ @template_name_to_apply = mr_template_options.first if apply_single_template
122
+ else
123
+ response = cli.template_to_apply(mr_template_options, 'merge')
124
+ @template_name_to_apply = response unless response == "None"
125
+ end
126
+ end
135
127
 
136
- private def remove_source_branch?
137
- answer = cli.ask('Remove source branch after merging? (y/n)')
138
- !!(answer =~ /^y/i)
128
+ @template_name_to_apply
139
129
  end
140
130
 
141
- private def apply_template?(template_file_name)
142
- answer = cli.ask("Apply the merge request template from #{template_file_name}? (y/n)")
143
- !!(answer =~ /^y/i)
131
+ private def existing_mr_title
132
+ @existing_mr_title ||= gitlab_client.merge_request(local_project, mr_id).title
144
133
  end
145
134
 
146
135
  private def gitlab_client
@@ -150,5 +139,9 @@ module GitHelper
150
139
  private def cli
151
140
  @cli ||= GitHelper::HighlineCli.new
152
141
  end
142
+
143
+ private def local_code
144
+ @local_code ||= GitHelper::LocalCode.new
145
+ end
153
146
  end
154
147
  end
@@ -1,18 +1,20 @@
1
1
  require_relative './highline_cli.rb'
2
+ require_relative './local_code.rb'
2
3
 
3
4
  module GitHelper
4
5
  class NewBranch
5
6
  def execute(new_branch_name = nil)
6
- branch_name = new_branch_name || cli.ask('New branch name?')
7
+ branch_name = new_branch_name || cli.new_branch_name
7
8
  puts "Attempting to create a new branch: #{branch_name}"
8
- system("git pull")
9
- system("git branch --no-track #{branch_name}")
10
- system("git checkout #{branch_name}")
11
- system("git push --set-upstream origin #{branch_name}")
9
+ local_code.new_branch(branch_name)
12
10
  end
13
11
 
14
12
  private def cli
15
13
  @cli ||= GitHelper::HighlineCli.new
16
14
  end
15
+
16
+ private def local_code
17
+ @local_code ||= GitHelper::LocalCode.new
18
+ end
17
19
  end
18
20
  end
@@ -1,5 +1,6 @@
1
1
  require_relative './octokit_client.rb'
2
2
  require_relative './highline_cli.rb'
3
+ require_relative './local_code.rb'
3
4
 
4
5
  module GitHelper
5
6
  class GitHubPullRequest
@@ -67,91 +68,75 @@ module GitHelper
67
68
  end
68
69
 
69
70
  private def local_repo
70
- # Get the repository by looking in the remote URLs for the full repository name
71
- remotes = `git remote -v`
72
- return remotes.scan(/\S[\s]*[\S]+.com[\S]{1}([\S]*).git/).first.first
71
+ @local_project ||= local_code.name
73
72
  end
74
73
 
75
74
  private def local_branch
76
- # Get the current branch by looking in the list of branches for the *
77
- branches = `git branch`
78
- return branches.scan(/\*\s([\S]*)/).first.first
75
+ @local_branch ||= local_code.branch
79
76
  end
80
77
 
81
- private def read_template
82
- if pr_template_options.count == 1
83
- apply_template?(pr_template_options.first) ? File.open(pr_template_options.first).read : ''
84
- else
85
- template_file_name_to_apply = template_to_apply
86
- template_file_name_to_apply == "None" ? '' : File.open(template_file_name_to_apply).read
87
- end
78
+ private def autogenerated_title
79
+ @autogenerated_title ||= local_code.generate_title(local_branch)
88
80
  end
89
81
 
90
- private def merge_options
91
- [ 'merge', 'squash', 'rebase' ]
82
+ private def default_branch
83
+ @default_branch ||= local_code.default_branch(local_repo, octokit_client, :octokit)
92
84
  end
93
85
 
94
- private def pr_id
95
- @pr_id ||= cli.ask('Pull Request ID?')
86
+ private def pr_template_options
87
+ @pr_template_options ||= local_code.template_options({
88
+ nested_directory_name: "PULL_REQUEST_TEMPLATE",
89
+ non_nested_file_name: "pull_request_template"
90
+ })
96
91
  end
97
92
 
98
- private def existing_pr_title
99
- @existing_pr_title ||= octokit_client.pull_request(local_repo, pr_id).title
93
+ private def pr_id
94
+ @pr_id ||= cli.pull_request_id
100
95
  end
101
96
 
102
- private def new_pr_title
103
- @new_pr_title ||= accept_autogenerated_title? ? autogenerated_title : cli.ask('Title?')
97
+ private def merge_method
98
+ @merge_method ||= cli.merge_method
104
99
  end
105
100
 
106
- private def new_pr_body
107
- @new_pr_body ||= pr_template_options.empty? ? '' : read_template
101
+ private def new_pr_title
102
+ @new_pr_title ||= if cli.accept_autogenerated_title?(autogenerated_title)
103
+ autogenerated_title
104
+ else
105
+ cli.title
106
+ end
108
107
  end
109
108
 
110
109
  private def base_branch
111
- @base_branch ||= base_branch_default? ? default_branch : cli.ask('Base branch?')
112
- end
113
-
114
- private def autogenerated_title
115
- @autogenerated_title ||= local_branch.split('_')[0..-1].join(' ').capitalize
116
- end
117
-
118
- private def default_branch
119
- @default_branch ||= octokit_client.repository(local_repo).default_branch
120
- end
121
-
122
- private def merge_method
123
- return @merge_method if @merge_method
124
- index = cli.ask_options("Merge method?", merge_options)
125
- @merge_method = merge_options[index]
110
+ @base_branch ||= if cli.base_branch_default?(default_branch)
111
+ default_branch
112
+ else
113
+ cli.base_branch
114
+ end
126
115
  end
127
116
 
128
- private def template_to_apply
129
- return @template_to_apply if @template_to_apply
130
- complete_options = pr_template_options << 'None'
131
- index = cli.ask_options("Which pull request template should be applied?", complete_options)
132
- @template_to_apply = complete_options[index]
117
+ private def new_pr_body
118
+ @new_pr_body ||= template_name_to_apply ? local_code.read_template(template_name_to_apply) : ''
133
119
  end
134
120
 
135
- private def pr_template_options
136
- return @pr_template_options if @pr_template_options
137
- nested_templates = Dir.glob(File.join("**/PULL_REQUEST_TEMPLATE", "*.md"), File::FNM_DOTMATCH | File::FNM_CASEFOLD)
138
- non_nested_templates = Dir.glob(File.join("**", "pull_request_template.md"), File::FNM_DOTMATCH | File::FNM_CASEFOLD)
139
- @pr_template_options = nested_templates.concat(non_nested_templates)
140
- end
121
+ private def template_name_to_apply
122
+ return @template_name_to_apply if @template_name_to_apply
123
+ @template_name_to_apply = nil
141
124
 
142
- private def base_branch_default?
143
- answer = cli.ask("Is '#{default_branch}' the correct base branch for your new pull request? (y/n)")
144
- !!(answer =~ /^y/i)
145
- end
125
+ unless pr_template_options.empty?
126
+ if pr_template_options.count == 1
127
+ apply_single_template = cli.apply_template?(pr_template_options.first)
128
+ @template_name_to_apply = pr_template_options.first if apply_single_template
129
+ else
130
+ response = cli.template_to_apply(pr_template_options, 'pull')
131
+ @template_name_to_apply = response unless response == "None"
132
+ end
133
+ end
146
134
 
147
- private def accept_autogenerated_title?
148
- answer = cli.ask("Accept the autogenerated pull request title '#{autogenerated_title}'? (y/n)")
149
- !!(answer =~ /^y/i)
135
+ @template_name_to_apply
150
136
  end
151
137
 
152
- private def apply_template?(template_file_name)
153
- answer = cli.ask("Apply the pull request template from #{template_file_name}? (y/n)")
154
- !!(answer =~ /^y/i)
138
+ private def existing_pr_title
139
+ @existing_pr_title ||= octokit_client.pull_request(local_repo, pr_id).title
155
140
  end
156
141
 
157
142
  private def octokit_client
@@ -161,5 +146,9 @@ module GitHelper
161
146
  private def cli
162
147
  @cli ||= GitHelper::HighlineCli.new
163
148
  end
149
+
150
+ private def local_code
151
+ @local_code ||= GitHelper::LocalCode.new
152
+ end
164
153
  end
165
154
  end
@@ -1,3 +1,3 @@
1
1
  module GitHelper
2
- VERSION = '1.0.0'
2
+ VERSION = '1.3.0'
3
3
  end
@@ -0,0 +1,52 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.start do
4
+ add_filter '/spec/'
5
+ end
6
+
7
+ # This file was generated by the `rspec --init` command. Conventionally, all
8
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
9
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
10
+ # this file to always be loaded, without a need to explicitly require it in any
11
+ # files.
12
+ #
13
+ # Given that it is always loaded, you are encouraged to keep this file as
14
+ # light-weight as possible. Requiring heavyweight dependencies from this file
15
+ # will add to the boot time of your test suite on EVERY test run, even for an
16
+ # individual file that may not need all of that loaded. Instead, consider making
17
+ # a separate helper file that requires the additional dependencies and performs
18
+ # the additional setup, and require it from the spec files that actually need
19
+ # it.
20
+ #
21
+ # The `.rspec` file also contains a few flags that are not defaults but that
22
+ # users commonly want.
23
+ #
24
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
25
+ RSpec.configure do |config|
26
+ # rspec-expectations config goes here. You can use an alternate
27
+ # assertion/expectation library such as wrong or the stdlib/minitest
28
+ # assertions if you prefer.
29
+ config.expect_with :rspec do |expectations|
30
+ # This option will default to `true` in RSpec 4. It makes the `description`
31
+ # and `failure_message` of custom matchers include text for helper methods
32
+ # defined using `chain`, e.g.:
33
+ # be_bigger_than(2).and_smaller_than(4).description
34
+ # # => "be bigger than 2 and smaller than 4"
35
+ # ...rather than:
36
+ # # => "be bigger than 2"
37
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
38
+ end
39
+
40
+ config.use_transactional_fixtures = true
41
+ config.filter_run :focus => true
42
+ config.run_all_when_everything_filtered = true
43
+
44
+ # rspec-mocks config goes here. You can use an alternate test double
45
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
46
+ config.mock_with :rspec do |mocks|
47
+ # Prevents you from mocking or stubbing a method that does not exist on
48
+ # a real object. This is generally recommended, and will default to
49
+ # `true` in RSpec 4.
50
+ mocks.verify_partial_doubles = true
51
+ end
52
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emma Sax
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-09 00:00:00.000000000 Z
11
+ date: 2020-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gitlab
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '10.1'
117
+ version: 12.3.3
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '10.1'
124
+ version: 12.3.3
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: rspec
127
127
  requirement: !ruby/object:Gem::Requirement
@@ -140,7 +140,7 @@ description: A set of GitHub and GitLab workflow scripts.
140
140
  email:
141
141
  - emma.sax4@gmail.com
142
142
  executables:
143
- - ghelper
143
+ - git-helper
144
144
  extensions: []
145
145
  extra_rdoc_files: []
146
146
  files:
@@ -150,20 +150,22 @@ files:
150
150
  - LICENSE
151
151
  - README.md
152
152
  - Rakefile
153
- - bin/ghelper
153
+ - bin/git-helper
154
154
  - lib/git_helper.rb
155
155
  - lib/git_helper/change_remote.rb
156
156
  - lib/git_helper/checkout_default.rb
157
- - lib/git_helper/clean_git.rb
157
+ - lib/git_helper/clean_branches.rb
158
158
  - lib/git_helper/empty_commit.rb
159
159
  - lib/git_helper/git_config_reader.rb
160
160
  - lib/git_helper/gitlab_client.rb
161
161
  - lib/git_helper/highline_cli.rb
162
+ - lib/git_helper/local_code.rb
162
163
  - lib/git_helper/merge_request.rb
163
164
  - lib/git_helper/new_branch.rb
164
165
  - lib/git_helper/octokit_client.rb
165
166
  - lib/git_helper/pull_request.rb
166
167
  - lib/git_helper/version.rb
168
+ - spec/spec_helper.rb
167
169
  homepage: https://github.com/emmasax4/git_helper
168
170
  licenses:
169
171
  - MIT
@@ -188,4 +190,5 @@ signing_key:
188
190
  specification_version: 4
189
191
  summary: A set of GitHub and GitLab workflow scripts to provide a smoother development
190
192
  process for your git projects.
191
- test_files: []
193
+ test_files:
194
+ - spec/spec_helper.rb