git_helper 1.0.1 → 1.3.1

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: aa43ec8474f02e1d7b042c0706870e9b2e148f4c17749615808c7111ffd1657a
4
- data.tar.gz: 4b3a1b77bf3c4c8086077a768e9cdcba41683ea5daf3bcfe39b330ac9a9d142e
3
+ metadata.gz: d7f6658d90f9abf6f52b670fcd9e09ac4fc5964abbf70d0e88d21c6d3838d598
4
+ data.tar.gz: 420511dc29ead38efe88f841b76bb42f1b086b5c76369202056d7856002886ef
5
5
  SHA512:
6
- metadata.gz: 7282a13816db9c1eb5653ca77b9d18c251332a9c19dc30e546d2cf074796c0e4b14e6954b1994c54de30d7f6931413d164e3f6666164e8ca5a6a3db2cfe97b46
7
- data.tar.gz: be5d4519977cc3205aed2776809e0a062b554102b41eb0db92dc4082aaec6a404e5d3317c0482e05c448c199c6e6a28bf1745f8c9f63faec963d833b4ea8f59f
6
+ metadata.gz: e8ee96f3180249acb8a25a549fa937852ff8a3a836a41d445dd7e5ea9f941f5b00408e174378e7c0b259a137cc96ce0ee3f52f3af7c3156f263e10f3253e1c3d
7
+ data.tar.gz: bd1b57e904dcb2a29a2f85688038af87908499240e0b86f0e75c5cebb0ebd6de4e538b25dab598df247ff8a2bc82da665d0a6c57e2c41ac470a47cb20c226a46
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
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,5 +1,5 @@
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")
@@ -2,24 +2,85 @@ 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(merge_options)
47
+ index = ask_options("Merge method?", merge_options)
48
+ merge_options[index]
49
+ end
50
+
51
+ def apply_template?(template_file_name)
52
+ answer = ask("Apply the merge request template from #{template_file_name}? (y/n)")
53
+ !!(answer =~ /^y/i)
54
+ end
55
+
56
+ def template_to_apply(template_options, request_type)
57
+ complete_options = template_options << 'None'
58
+ index = ask_options("Which #{request_type} request template should be applied?", complete_options)
59
+ complete_options[index]
60
+ end
61
+
62
+ #######################
63
+ ### GENERAL METHODS ###
64
+ #######################
65
+
66
+ private def ask(prompt)
67
+ highline_client.ask(prompt) do |conf|
7
68
  conf.readline = true
8
69
  end.to_s
9
70
  end
10
71
 
11
- def ask_options(prompt, choices)
72
+ private def ask_options(prompt, choices)
12
73
  choices_as_string_options = ''
13
74
  choices.each { |choice| choices_as_string_options << "#{choices.index(choice) + 1}. #{choice}\n" }
14
75
  compiled_prompt = "#{prompt}\n#{choices_as_string_options.strip}"
15
76
 
16
- cli.ask(compiled_prompt) do |conf|
77
+ highline_client.ask(compiled_prompt) do |conf|
17
78
  conf.readline = true
18
79
  end.to_i - 1
19
80
  end
20
81
 
21
- private def cli
22
- @cli ||= HighLine.new
82
+ private def highline_client
83
+ @highline_client ||= HighLine.new
23
84
  end
24
85
  end
25
86
  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
@@ -11,11 +12,13 @@ module GitHelper
11
12
  options = {
12
13
  source_branch: local_branch,
13
14
  target_branch: base_branch,
15
+ squash: squash_merge_request,
16
+ remove_source_branch: remove_source_branch,
14
17
  description: new_mr_body
15
18
  }
16
19
 
17
20
  puts "Creating merge request: #{new_mr_title}"
18
- mr = gitlab_client.create_merge_request(local_repo, new_mr_title, options)
21
+ mr = gitlab_client.create_merge_request(local_project, new_mr_title, options)
19
22
 
20
23
  if mr.diff_refs.base_sha == mr.diff_refs.head_sha
21
24
  puts "Merge request was created, but no commits have been pushed to GitLab: #{mr.web_url}"
@@ -36,12 +39,18 @@ module GitHelper
36
39
  # Ask these questions right away
37
40
  mr_id
38
41
  options = {}
39
- options[:should_remove_source_branch] = remove_source_branch?
40
- options[:squash] = squash_merge_request?
41
- options[:squash_commit_message] = existing_mr_title
42
+ options[:should_remove_source_branch] = existing_mr.should_remove_source_branch || existing_mr.force_remove_source_branch
43
+ options[:squash] = existing_mr.squash
44
+ options[:squash_commit_message] = existing_mr.title
42
45
 
43
46
  puts "Merging merge request: #{mr_id}"
44
- merge = gitlab_client.accept_merge_request(local_repo, mr_id, options)
47
+ merge = gitlab_client.accept_merge_request(local_project, mr_id, options)
48
+
49
+ if merge.merge_commit_sha.nil?
50
+ options[:squash] = true
51
+ merge = gitlab_client.accept_merge_request(local_project, mr_id, options)
52
+ end
53
+
45
54
  puts "Merge request successfully merged: #{merge.merge_commit_sha}"
46
55
  rescue Gitlab::Error::MethodNotAllowed => e
47
56
  puts 'Could not merge merge request:'
@@ -55,92 +64,84 @@ module GitHelper
55
64
  end
56
65
  end
57
66
 
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
67
+ private def local_project
68
+ @local_project ||= local_code.name
62
69
  end
63
70
 
64
71
  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
72
+ @local_branch ||= local_code.branch
68
73
  end
69
74
 
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
75
+ private def autogenerated_title
76
+ @autogenerated_title ||= local_code.generate_title(local_branch)
77
77
  end
78
78
 
79
- private def mr_id
80
- @mr_id ||= cli.ask('Merge Request ID?')
79
+ private def default_branch
80
+ @default_branch ||= local_code.default_branch(local_project, gitlab_client, :gitlab)
81
81
  end
82
82
 
83
- private def existing_mr_title
84
- @existing_mr_title ||= gitlab_client.merge_request(local_repo, mr_id).title
83
+ private def mr_template_options
84
+ @mr_template_options ||= local_code.template_options({
85
+ nested_directory_name: "merge_request_templates",
86
+ non_nested_file_name: "merge_request_template"
87
+ })
85
88
  end
86
89
 
87
- private def new_mr_title
88
- @new_mr_title ||= accept_autogenerated_title? ? autogenerated_title : cli.ask('Title?')
90
+ private def mr_id
91
+ @mr_id ||= cli.merge_request_id
89
92
  end
90
93
 
91
- private def new_mr_body
92
- @new_mr_body ||= mr_template_options.empty? ? '' : read_template
94
+ private def squash_merge_request
95
+ @squash_merge_request ||= cli.squash_merge_request?
93
96
  end
94
97
 
95
- private def base_branch
96
- @base_branch ||= base_branch_default? ? default_branch : cli.ask('Base branch?')
98
+ private def remove_source_branch
99
+ @remove_source_branch ||= existing_project.remove_source_branch_after_merge || cli.remove_source_branch?
97
100
  end
98
101
 
99
- private def autogenerated_title
100
- @autogenerated_title ||= local_branch.split('_')[0..-1].join(' ').capitalize
101
- end
102
-
103
- private def default_branch
104
- @default_branch ||= gitlab_client.branches(local_repo).select { |branch| branch.default }.first.name
102
+ private def new_mr_title
103
+ @new_mr_title ||= if cli.accept_autogenerated_title?(autogenerated_title)
104
+ autogenerated_title
105
+ else
106
+ cli.title
107
+ end
105
108
  end
106
109
 
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]
110
+ private def base_branch
111
+ @base_branch ||= if cli.base_branch_default?(default_branch)
112
+ default_branch
113
+ else
114
+ cli.base_branch
115
+ end
112
116
  end
113
117
 
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)
118
+ private def new_mr_body
119
+ @new_mr_body ||= template_name_to_apply ? local_code.read_template(template_name_to_apply) : ''
119
120
  end
120
121
 
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)
124
- end
122
+ private def template_name_to_apply
123
+ return @template_name_to_apply if @template_name_to_apply
124
+ @template_name_to_apply = nil
125
125
 
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
126
+ unless mr_template_options.empty?
127
+ if mr_template_options.count == 1
128
+ apply_single_template = cli.apply_template?(mr_template_options.first)
129
+ @template_name_to_apply = mr_template_options.first if apply_single_template
130
+ else
131
+ response = cli.template_to_apply(mr_template_options, 'merge')
132
+ @template_name_to_apply = response unless response == "None"
133
+ end
134
+ end
130
135
 
131
- private def squash_merge_request?
132
- answer = cli.ask('Squash merge request? (y/n)')
133
- !!(answer =~ /^y/i)
136
+ @template_name_to_apply
134
137
  end
135
138
 
136
- private def remove_source_branch?
137
- answer = cli.ask('Remove source branch after merging? (y/n)')
138
- !!(answer =~ /^y/i)
139
+ private def existing_mr
140
+ @existing_mr ||= gitlab_client.merge_request(local_project, mr_id)
139
141
  end
140
142
 
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)
143
+ private def existing_project
144
+ @existing_project ||= gitlab_client.project(local_project)
144
145
  end
145
146
 
146
147
  private def gitlab_client
@@ -150,5 +151,9 @@ module GitHelper
150
151
  private def cli
151
152
  @cli ||= GitHelper::HighlineCli.new
152
153
  end
154
+
155
+ private def local_code
156
+ @local_code ||= GitHelper::LocalCode.new
157
+ end
153
158
  end
154
159
  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
@@ -35,7 +36,7 @@ module GitHelper
35
36
  merge_method
36
37
 
37
38
  puts "Merging pull request: #{pr_id}"
38
- merge = octokit_client.merge_pull_request(local_repo, pr_id, existing_pr_title, { merge_method: merge_method })
39
+ merge = octokit_client.merge_pull_request(local_repo, pr_id, existing_pr.title, { merge_method: merge_method })
39
40
  puts "Pull request successfully merged: #{merge.sha}"
40
41
  rescue Octokit::UnprocessableEntity => e
41
42
  puts 'Could not merge pull request:'
@@ -67,91 +68,88 @@ 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
88
- end
89
-
90
- private def merge_options
91
- [ 'merge', 'squash', 'rebase' ]
78
+ private def autogenerated_title
79
+ @autogenerated_title ||= local_code.generate_title(local_branch)
92
80
  end
93
81
 
94
- private def pr_id
95
- @pr_id ||= cli.ask('Pull Request ID?')
82
+ private def default_branch
83
+ @default_branch ||= local_code.default_branch(local_repo, octokit_client, :octokit)
96
84
  end
97
85
 
98
- private def existing_pr_title
99
- @existing_pr_title ||= octokit_client.pull_request(local_repo, pr_id).title
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
+ })
100
91
  end
101
92
 
102
- private def new_pr_title
103
- @new_pr_title ||= accept_autogenerated_title? ? autogenerated_title : cli.ask('Title?')
93
+ private def pr_id
94
+ @pr_id ||= cli.pull_request_id
104
95
  end
105
96
 
106
- private def new_pr_body
107
- @new_pr_body ||= pr_template_options.empty? ? '' : read_template
97
+ private def merge_method
98
+ @merge_method ||= merge_options.length == 1 ? merge_options.first : cli.merge_method(merge_options)
108
99
  end
109
100
 
110
- private def base_branch
111
- @base_branch ||= base_branch_default? ? default_branch : cli.ask('Base branch?')
101
+ private def merge_options
102
+ return @merge_options if @merge_options
103
+ merge_options = []
104
+ merge_options << 'merge' if existing_project.allow_merge_commit
105
+ merge_options << 'squash' if existing_project.allow_squash_merge
106
+ merge_options << 'rebase' if existing_project.allow_rebase_merge
107
+ @merge_options = merge_options
112
108
  end
113
109
 
114
- private def autogenerated_title
115
- @autogenerated_title ||= local_branch.split('_')[0..-1].join(' ').capitalize
110
+ private def new_pr_title
111
+ @new_pr_title ||= if cli.accept_autogenerated_title?(autogenerated_title)
112
+ autogenerated_title
113
+ else
114
+ cli.title
115
+ end
116
116
  end
117
117
 
118
- private def default_branch
119
- @default_branch ||= octokit_client.repository(local_repo).default_branch
118
+ private def base_branch
119
+ @base_branch ||= if cli.base_branch_default?(default_branch)
120
+ default_branch
121
+ else
122
+ cli.base_branch
123
+ end
120
124
  end
121
125
 
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]
126
+ private def new_pr_body
127
+ @new_pr_body ||= template_name_to_apply ? local_code.read_template(template_name_to_apply) : ''
126
128
  end
127
129
 
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]
133
- end
130
+ private def template_name_to_apply
131
+ return @template_name_to_apply if @template_name_to_apply
132
+ @template_name_to_apply = nil
134
133
 
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
134
+ unless pr_template_options.empty?
135
+ if pr_template_options.count == 1
136
+ apply_single_template = cli.apply_template?(pr_template_options.first)
137
+ @template_name_to_apply = pr_template_options.first if apply_single_template
138
+ else
139
+ response = cli.template_to_apply(pr_template_options, 'pull')
140
+ @template_name_to_apply = response unless response == "None"
141
+ end
142
+ end
141
143
 
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)
144
+ @template_name_to_apply
145
145
  end
146
146
 
147
- private def accept_autogenerated_title?
148
- answer = cli.ask("Accept the autogenerated pull request title '#{autogenerated_title}'? (y/n)")
149
- !!(answer =~ /^y/i)
147
+ private def existing_pr
148
+ @existing_pr ||= octokit_client.pull_request(local_repo, pr_id)
150
149
  end
151
150
 
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)
151
+ private def existing_project
152
+ @existing_project ||= octokit_client.repository(local_repo)
155
153
  end
156
154
 
157
155
  private def octokit_client
@@ -161,5 +159,9 @@ module GitHelper
161
159
  private def cli
162
160
  @cli ||= GitHelper::HighlineCli.new
163
161
  end
162
+
163
+ private def local_code
164
+ @local_code ||= GitHelper::LocalCode.new
165
+ end
164
166
  end
165
167
  end
@@ -1,3 +1,3 @@
1
1
  module GitHelper
2
- VERSION = '1.0.1'
2
+ VERSION = '1.3.1'
3
3
  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.1
4
+ version: 1.3.1
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
@@ -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,15 +150,16 @@ 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