git_helper 1.1.1 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +17 -13
  3. data/README.md +28 -32
  4. data/Rakefile +1 -37
  5. data/bin/git-helper +10 -28
  6. data/lib/git_helper.rb +3 -5
  7. data/lib/git_helper/change_remote.rb +53 -40
  8. data/lib/git_helper/checkout_default.rb +1 -1
  9. data/lib/git_helper/clean_branches.rb +1 -4
  10. data/lib/git_helper/code_request.rb +95 -0
  11. data/lib/git_helper/empty_commit.rb +1 -1
  12. data/lib/git_helper/forget_local_commits.rb +7 -0
  13. data/lib/git_helper/gitlab_client.rb +0 -1
  14. data/lib/git_helper/highline_cli.rb +72 -6
  15. data/lib/git_helper/local_code.rb +124 -0
  16. data/lib/git_helper/merge_request.rb +59 -97
  17. data/lib/git_helper/new_branch.rb +2 -11
  18. data/lib/git_helper/octokit_client.rb +0 -1
  19. data/lib/git_helper/pull_request.rb +47 -81
  20. data/lib/git_helper/version.rb +1 -1
  21. data/spec/git_helper/change_remote_spec.rb +173 -0
  22. data/spec/git_helper/checkout_default_spec.rb +19 -0
  23. data/spec/git_helper/clean_branches_spec.rb +19 -0
  24. data/spec/git_helper/code_request_spec.rb +259 -0
  25. data/spec/git_helper/empty_commit_spec.rb +19 -0
  26. data/spec/git_helper/forget_local_commits_spec.rb +19 -0
  27. data/spec/git_helper/git_config_reader_spec.rb +60 -0
  28. data/spec/git_helper/gitlab_client_spec.rb +26 -0
  29. data/spec/git_helper/highline_cli_spec.rb +215 -0
  30. data/spec/git_helper/local_code_spec.rb +231 -0
  31. data/spec/git_helper/merge_request_spec.rb +234 -0
  32. data/spec/git_helper/new_branch_spec.rb +44 -0
  33. data/spec/git_helper/octokit_client_spec.rb +26 -0
  34. data/spec/git_helper/pull_request_spec.rb +246 -0
  35. data/spec/spec_helper.rb +0 -7
  36. metadata +40 -23
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 130ab19f392a258f0869dc46d7791b041da124d640d7d403233ea4dedb9c0877
4
- data.tar.gz: 3be0a8d79b78e8a56e09f658dcaa7c19ee944a19b49e0da1c4c49752608454c5
3
+ metadata.gz: a1e24bf2314c2574a9158042ecf88500c5cd00a6cad451f55bdcc1d35a273b85
4
+ data.tar.gz: e3506380e78e33599df212c386a3e65abd2436e732dad95e4cbeff2c0fcc7eb6
5
5
  SHA512:
6
- metadata.gz: 84351ee9438d3f8579ffa655ab10d50f156df16f8cb0421503363bff82870432683e511291a852ec47b8802ff1827b21dd15e1fe2fe4ce18fe8bc5576515ae28
7
- data.tar.gz: 8195c5bfd1760835379de77182302e329579cd16b92b03bb5e646e042c73124a32156477a1b7623e1ed495619745aa33891aae86cbfd3c0c66d12b48aabbf8b4
6
+ metadata.gz: 8a971a9faca09229a62f28dbdc244eb0913292cc7d2e4d667dae6c5d40b5c53240497f808a48409349faf6d3dbe535e6f4571d28e002509c88251aef8ae23c1d
7
+ data.tar.gz: a492d9be7914fb1d9d995ae0b721336bfaa8749ffcea90b1c4dca6d9443f03beac7b254aa9347b6260153060e387c293de382900bb01511692f741ecc9a7778c
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- git_helper (1.0.0)
4
+ git_helper (2.0.1)
5
5
  gitlab (~> 4.16)
6
6
  gli (~> 2.13)
7
7
  highline (~> 2.0)
@@ -61,18 +61,23 @@ GEM
61
61
  coderay (~> 1.1)
62
62
  method_source (~> 1.0)
63
63
  public_suffix (4.0.6)
64
- rake (12.3.3)
64
+ rake (13.0.1)
65
65
  rb-fsevent (0.10.4)
66
66
  rb-inotify (0.10.1)
67
67
  ffi (~> 1.0)
68
- rspec (2.99.0)
69
- rspec-core (~> 2.99.0)
70
- rspec-expectations (~> 2.99.0)
71
- rspec-mocks (~> 2.99.0)
72
- rspec-core (2.99.2)
73
- rspec-expectations (2.99.2)
74
- diff-lcs (>= 1.1.3, < 2.0)
75
- rspec-mocks (2.99.4)
68
+ rspec (3.9.0)
69
+ rspec-core (~> 3.9.0)
70
+ rspec-expectations (~> 3.9.0)
71
+ rspec-mocks (~> 3.9.0)
72
+ rspec-core (3.9.3)
73
+ rspec-support (~> 3.9.3)
74
+ rspec-expectations (3.9.2)
75
+ diff-lcs (>= 1.2.0, < 2.0)
76
+ rspec-support (~> 3.9.0)
77
+ rspec-mocks (3.9.1)
78
+ diff-lcs (>= 1.2.0, < 2.0)
79
+ rspec-support (~> 3.9.0)
80
+ rspec-support (3.9.3)
76
81
  sawyer (0.8.2)
77
82
  addressable (>= 2.3.5)
78
83
  faraday (> 0.8, < 2.0)
@@ -88,10 +93,9 @@ PLATFORMS
88
93
  DEPENDENCIES
89
94
  bundler (~> 2.1)
90
95
  git_helper!
91
- guard (~> 2.6)
92
96
  guard-rspec (~> 4.3)
93
- rake (~> 12.3.3)
94
- rspec (~> 2.99)
97
+ rake (~> 13.0)
98
+ rspec (~> 3.9)
95
99
 
96
100
  BUNDLED WITH
97
101
  2.1.4
data/README.md CHANGED
@@ -71,7 +71,7 @@ If you're going to make using git workflows easier, might as well provide lots o
71
71
 
72
72
  This can be used when switching the owners of a GitHub repo. When you switch a username, GitHub only makes some changes for you. With this command, you no longer have to manually walk through local repo and switch the remotes from each one into a remote with the new username.
73
73
 
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:
74
+ This command will go through every directory in a directory, and see if it is a git directory. It will then ask the user if they wish to process the git directory in question. The command does not yet know if there's any changes to be made. If the user says 'yes', then it 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:
75
75
 
76
76
  ```bash
77
77
  git-helper change-remote OLD-OWNER NEW-OWNER
@@ -93,37 +93,49 @@ git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/CORRECT-DEFAULT-BR
93
93
 
94
94
  ### `clean-branches`
95
95
 
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:
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 clean your local branches, run:
97
97
 
98
98
  ```bash
99
99
  git-helper clean-branches
100
100
  ```
101
101
 
102
- ### `empty-commit`
102
+ ### `code-request`
103
103
 
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:
104
+ This command can be used to handily make new GitHub/GitLab pull/merge requests and to merge those requests from the command line. The command uses either the [`Octokit::Client`](https://octokit.github.io/octokit.rb/Octokit/Client.html) or [`Gitlab` client](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).
105
+
106
+ After setup is complete, you can call the file, and send in a flag indicating whether to create a code request, `-c`, or to merge a code request, `-m`.
105
107
 
106
108
  ```bash
107
- git-helper empty-commit
109
+ git-helper code-request -c
110
+ # OR
111
+ git-helper code-request -m
108
112
  ```
109
113
 
110
- ### `merge-request`
114
+ If you're trying to create a code request, the command will provide an autogenerated code 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 code request. You can respond 'yes' or 'no'. If you respond 'no', you can provide your own code request title.
115
+
116
+ 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.
111
117
 
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).
118
+ Lastly, for GitHub, it'll ask the user to apply any code 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. For GitLab, 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.
113
119
 
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`.
120
+ If you're requesting to merge a code request, the command will ask you the number ID of the code request you wish to merge. For GitHub, 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. For GitLab, 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 merge request.
121
+
122
+ If you're getting stuck, you can run the command with a `--help` flag instead, to get some more information.
123
+
124
+ ### `empty-commit`
125
+
126
+ 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:
115
127
 
116
128
  ```bash
117
- git-helper merge-request -c
118
- # OR
119
- git-helper merge-request -m
129
+ git-helper empty-commit
120
130
  ```
121
131
 
122
- 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.
132
+ ### `forget-local-commits`
123
133
 
124
- 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.
134
+ This command is handy if you locally have a bunch of commits you wish to completely get rid of. This command basically does a hard reset to `origin/HEAD`. Once you forget them, they're completely gone, so run carefully. To test it out, run:
125
135
 
126
- If you're getting stuck, you can run the command with a `--help` flag instead, to get some more information.
136
+ ```bash
137
+ git-helper forget-local-commits
138
+ ```
127
139
 
128
140
  ### `new-branch`
129
141
 
@@ -137,24 +149,6 @@ git-helper new-branch NEW_BRANCH_NAME
137
149
 
138
150
  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.
139
151
 
140
- ### `pull-request`
141
-
142
- 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).
143
-
144
- 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`.
145
-
146
- ```bash
147
- git-helper pull-request -c
148
- # OR
149
- git-helper pull-request -m
150
- ```
151
-
152
- 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.
153
-
154
- 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.
155
-
156
- If you're getting stuck, you can run the command with a `--help` flag instead, to get some more information.
157
-
158
152
  ## Contributing
159
153
 
160
154
  To submit a feature request, bug ticket, etc, please submit an official [GitHub Issue](https://github.com/emmasax4/git_helper/issues/new).
@@ -173,3 +167,5 @@ To make a new release of this gem:
173
167
  4. Run `gem build *.gemspec`
174
168
  5. Run `gem push *.gem` to push the new gem to RubyGems
175
169
  6. Run `rm *.gem` to clean up your local repository
170
+
171
+ 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).
data/Rakefile CHANGED
@@ -1,37 +1 @@
1
- #!/usr/bin/env rake
2
- begin
3
- require 'bundler/setup'
4
- rescue LoadError
5
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
- end
7
- begin
8
- require 'rdoc/task'
9
- rescue LoadError
10
- require 'rdoc/rdoc'
11
- require 'rake/rdoctask'
12
- RDoc::Task = Rake::RDocTask
13
- end
14
-
15
- RDoc::Task.new(:rdoc) do |rdoc|
16
- rdoc.rdoc_dir = 'rdoc'
17
- rdoc.title = 'Hubstats'
18
- rdoc.options << '--line-numbers'
19
- rdoc.rdoc_files.include('README.rdoc')
20
- rdoc.rdoc_files.include('lib/**/*.rb')
21
- end
22
-
23
- APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
- load 'rails/tasks/engine.rake'
25
-
26
- Bundler::GemHelper.install_tasks
27
-
28
- require 'rake/testtask'
29
-
30
- Rake::TestTask.new(:test) do |t|
31
- t.libs << 'lib'
32
- t.libs << 'test'
33
- t.pattern = 'test/**/*_test.rb'
34
- t.verbose = false
35
- end
36
-
37
- task :default => :test
1
+ require "bundler/gem_tasks"
@@ -22,16 +22,14 @@ arg :new_owner
22
22
  desc "Update a repository's remote URLs from an old GitHub owner to a new owner."
23
23
  command 'change-remote' do |c|
24
24
  c.action do |global_options, options, args|
25
- require_relative '../lib/git_helper/change_remote.rb'
26
25
  raise ArgumentError, 'You must specify an old owner and a new owner' unless args.count == 2
27
- GitHelper::ChangeRemote.new.execute(args[0], args[1])
26
+ GitHelper::ChangeRemote.new(args[0], args[1]).execute
28
27
  end
29
28
  end
30
29
 
31
30
  desc "Checks out the default branch of a repo based on the local remote branches."
32
31
  command 'checkout-default' do |c|
33
32
  c.action do |global_options, options, args|
34
- require_relative '../lib/git_helper/checkout_default.rb'
35
33
  GitHelper::CheckoutDefault.new.execute
36
34
  end
37
35
  end
@@ -39,7 +37,6 @@ end
39
37
  desc "Clean a repository's git branches."
40
38
  command 'clean-branches' do |c|
41
39
  c.action do |global_options, options, args|
42
- require_relative '../lib/git_helper/clean_branches.rb'
43
40
  GitHelper::CleanBranches.new.execute
44
41
  end
45
42
  end
@@ -47,27 +44,14 @@ end
47
44
  desc "Creates an empty commit with a basic commit message."
48
45
  command 'empty-commit' do |c|
49
46
  c.action do |global_options, options, args|
50
- require_relative '../lib/git_helper/empty_commit.rb'
51
47
  GitHelper::EmptyCommit.new.execute
52
48
  end
53
49
  end
54
50
 
55
- desc 'Create a GitLab merge request from the current branch.'
56
- command 'merge-request' do |c|
57
- c.switch [:c, :create], desc: 'Create a new pull request'
58
- c.switch [:m, :merge], desc: 'Merge an existing pull request'
59
-
51
+ desc "Reset your local git commits to origin/HEAD."
52
+ command 'forget-local-commits' do |c|
60
53
  c.action do |global_options, options, args|
61
- require_relative '../lib/git_helper/merge_request.rb'
62
- raise ArgumentError, "You must specify an action (either '-m'/'--merge' or '-c'/'--create')" unless options[:create] || options[:merge]
63
-
64
- options = global_options.merge(options)
65
-
66
- if options[:create]
67
- GitHelper::GitLabMergeRequest.new.create
68
- elsif options[:merge]
69
- GitHelper::GitLabMergeRequest.new.merge
70
- end
54
+ GitHelper::ForgetLocalCommits.new.execute
71
55
  end
72
56
  end
73
57
 
@@ -75,26 +59,24 @@ arg :new_branch_name, optional: true
75
59
  desc 'Create a new branch for features, bug fixes, or experimentation.'
76
60
  command 'new-branch' do |c|
77
61
  c.action do |global_options, options, args|
78
- require_relative '../lib/git_helper/new_branch.rb'
79
62
  GitHelper::NewBranch.new.execute(args[0])
80
63
  end
81
64
  end
82
65
 
83
- desc 'Create a GitHub pull request from the current branch.'
84
- command 'pull-request' do |c|
85
- c.switch [:c, :create], desc: 'Create a new pull request'
86
- c.switch [:m, :merge], desc: 'Merge an existing pull request'
66
+ desc 'Create or merge a GitHub or GitLab pull/merge request from the current branch. The command will figure out whether your code project belongs to GitHub or GitLab'
67
+ command 'code-request' do |c|
68
+ c.switch [:c, :create], desc: 'Create a new pull/merge request'
69
+ c.switch [:m, :merge], desc: 'Merge an existing pull/merge request'
87
70
 
88
71
  c.action do |global_options, options, args|
89
- require_relative '../lib/git_helper/pull_request.rb'
90
72
  raise ArgumentError, "You must specify an action (either '-m'/'--merge' or '-c'/'--create')" unless options[:create] || options[:merge]
91
73
 
92
74
  options = global_options.merge(options)
93
75
 
94
76
  if options[:create]
95
- GitHelper::GitHubPullRequest.new.create
77
+ GitHelper::CodeRequest.new.create
96
78
  elsif options[:merge]
97
- GitHelper::GitHubPullRequest.new.merge
79
+ GitHelper::CodeRequest.new.merge
98
80
  end
99
81
  end
100
82
  end
@@ -1,7 +1,5 @@
1
- require 'highline'
2
- require 'yaml'
3
- require 'gitlab'
4
- require 'octokit'
5
- require_relative 'git_helper/version'
1
+ Dir[File.expand_path(File.join(File.dirname(File.absolute_path(__FILE__)), 'git_helper')) + "/**/*.rb"].each do |file|
2
+ require_relative file
3
+ end
6
4
 
7
5
  module GitHelper; end
@@ -1,55 +1,68 @@
1
1
  module GitHelper
2
2
  class ChangeRemote
3
- def execute(old_owner, new_owner)
4
- current_dir = Dir.pwd
5
- nested_dirs = Dir.entries(current_dir).select do |entry|
6
- entry_dir = File.join(current_dir, entry)
3
+ attr_accessor :old_owner, :new_owner
4
+
5
+ def initialize(old_owner, new_owner)
6
+ @old_owner = old_owner
7
+ @new_owner = new_owner
8
+ end
9
+
10
+ def execute
11
+ original_dir = Dir.pwd
12
+ nested_dirs = Dir.entries(original_dir).select do |entry|
13
+ entry_dir = File.join(original_dir, entry)
7
14
  File.directory?(entry_dir) && !(entry == '.' || entry == '..')
8
15
  end
9
16
 
10
- nested_dirs.each do |dir|
11
- Dir.chdir dir
12
- if File.exist?('.git')
13
- puts "Found git directory: #{dir}."
14
- remotes = `git remote -v`.split("\n")
15
- remotes.each do |remote|
16
- if resp.include?(old_owner)
17
- puts " Git directory's remote is pointing to: '#{old_owner}'."
18
- swap_ssh(old_owner, new_owner, remote) if remote.scan(/(git@)/).any?
19
- swap_https(old_owner, new_owner, remote) if remote.scan(/(https:\/\/)/).any?
20
- else
21
- puts " No need to update remote."
22
- end
23
- end
24
- end
25
- Dir.chdir current_dir
17
+ nested_dirs.each do |nested_dir|
18
+ process_dir(nested_dir, original_dir)
19
+ end
20
+ end
21
+
22
+ private def process_dir(current_dir, original_dir)
23
+ Dir.chdir(current_dir)
24
+
25
+ if File.exist?('.git')
26
+ process_git_repository if cli.process_directory_remotes?(current_dir)
26
27
  end
28
+
29
+ Dir.chdir(original_dir)
27
30
  end
28
31
 
29
- private def swap_https(old_owner, new_owner, remote)
30
- repo = remote.scan(/https:\/\/[\S]+\/([\S]*).git/).first.first
31
- remote_name = remote.scan(/([a-zA-z]+)/).first.first
32
- source_name = scan(/https:\/\/([a-zA-z.]+)\//).first.first
33
- puts " Changing the remote URL #{remote_name} to be 'https://#{source_name}/#{new_owner}/#{repo}.git'."
34
- begin
35
- `git remote set-url #{remote_name} https://#{source_name}:#{new_owner}/#{repo}.git`
36
- puts " Done."
37
- rescue Exception => e
38
- puts " Could not complete: #{e.message}"
32
+ private def process_git_repository
33
+ local_code.remotes.each do |remote|
34
+ if remote.include?(old_owner)
35
+ process_remote(remote)
36
+ else
37
+ puts " Found remote is not pointing to #{old_owner}."
38
+ end
39
39
  end
40
+ puts "\n"
40
41
  end
41
42
 
42
- private def swap_ssh(old_owner, new_owner, remote)
43
- repo = remote.scan(/\/([\S]*).git/).first.first
44
- remote_name = remote.scan(/([a-zA-z]+)/).first.first
45
- source_name = remote.scan(/git@([a-zA-z.]+):/).first.first
46
- puts " Changing the remote URL #{remote_name} to be 'git@#{source_name}:#{new_owner}/#{repo}.git'."
47
- begin
48
- `git remote set-url #{remote_name} git@#{source_name}:#{new_owner}/#{repo}.git`
49
- puts " Done."
50
- rescue Exception => e
51
- puts " Could not complete: #{e.message}"
43
+ private def process_remote(remote)
44
+ remote_name = local_code.remote_name(remote)
45
+
46
+ if local_code.ssh_remote?(remote)
47
+ repo = local_code.remote_project(remote)
48
+ source_name = local_code.remote_source(remote)
49
+ remote_url = "git@#{source_name}:#{new_owner}/#{repo}.git"
50
+ elsif local_code.https_remote?(remote)
51
+ repo = local_code.remote_project(remote)
52
+ source_name = local_code.remote_source(remote)
53
+ remote_url = "https://#{source_name}/#{new_owner}/#{repo}.git"
52
54
  end
55
+
56
+ puts " Changing the remote URL #{remote_name} to be '#{remote_url}'."
57
+ local_code.change_remote(remote_name, remote_url)
58
+ end
59
+
60
+ private def local_code
61
+ @local_code ||= GitHelper::LocalCode.new
62
+ end
63
+
64
+ private def cli
65
+ @cli ||= GitHelper::HighlineCli.new
53
66
  end
54
67
  end
55
68
  end
@@ -1,7 +1,7 @@
1
1
  module GitHelper
2
2
  class CheckoutDefault
3
3
  def execute
4
- system("git checkout $(git symbolic-ref refs/remotes/origin/HEAD | sed \"s@^refs/remotes/origin/@@\")")
4
+ GitHelper::LocalCode.new.checkout_default
5
5
  end
6
6
  end
7
7
  end
@@ -1,10 +1,7 @@
1
1
  module GitHelper
2
2
  class CleanBranches
3
3
  def execute
4
- system("git checkout $(git symbolic-ref refs/remotes/origin/HEAD | sed \"s@^refs/remotes/origin/@@\")")
5
- system("git pull")
6
- system("git fetch -p")
7
- system("git branch -vv | grep \"origin/.*: gone]\" | awk '{print \$1}' | grep -v \"*\" | xargs git branch -D")
4
+ GitHelper::LocalCode.new.clean_branches
8
5
  end
9
6
  end
10
7
  end
@@ -0,0 +1,95 @@
1
+ module GitHelper
2
+ class CodeRequest
3
+ def create
4
+ process_project.create({
5
+ base_branch: base_branch,
6
+ new_title: new_code_request_title
7
+ })
8
+ end
9
+
10
+ def merge
11
+ process_project.merge
12
+ end
13
+
14
+ private def process_project
15
+ if local_code.github_repo? # GitHub remotes are found
16
+ # If GitLab remotes are also found, ask for clarification, else proceed with GitHub
17
+ local_code.gitlab_project? ? ask_for_clarification : github_pull_request
18
+ elsif local_code.gitlab_project? # Only GitLab remotes are found
19
+ gitlab_merge_request
20
+ else # Neither GitHub nor GitLab remotes are found
21
+ puts 'Could not locate GitHub or GitLab remote URLs.'
22
+ exit
23
+ end
24
+ end
25
+
26
+ private def ask_for_clarification
27
+ resp = cli.conflicting_remote_clarification
28
+ if resp.include?('hub')
29
+ github_pull_request
30
+ elsif resp.include?('lab')
31
+ gitlab_merge_request
32
+ else
33
+ puts 'The answer we received was not parseable.'
34
+ exit
35
+ end
36
+ end
37
+
38
+ private def github_pull_request
39
+ @github_pull_request ||= GitHelper::GitHubPullRequest.new(options)
40
+ end
41
+
42
+ private def gitlab_merge_request
43
+ @gitlab_merge_request ||= GitHelper::GitLabMergeRequest.new(options)
44
+ end
45
+
46
+ private def options
47
+ {
48
+ local_project: local_project,
49
+ local_branch: local_branch,
50
+ local_code: local_code,
51
+ cli: cli
52
+ }
53
+ end
54
+
55
+ private def local_project
56
+ @local_project ||= local_code.project_name
57
+ end
58
+
59
+ private def default_branch
60
+ @default_branch ||= local_code.default_branch
61
+ end
62
+
63
+ private def base_branch
64
+ @base_branch ||= if cli.base_branch_default?(default_branch)
65
+ default_branch
66
+ else
67
+ cli.base_branch
68
+ end
69
+ end
70
+
71
+ private def local_branch
72
+ @local_branch ||= local_code.branch
73
+ end
74
+
75
+ private def autogenerated_title
76
+ @autogenerated_title ||= local_code.generate_title(local_branch)
77
+ end
78
+
79
+ private def new_code_request_title
80
+ @new_code_request_title ||= if cli.accept_autogenerated_title?(autogenerated_title)
81
+ autogenerated_title
82
+ else
83
+ cli.title
84
+ end
85
+ end
86
+
87
+ private def cli
88
+ @cli ||= GitHelper::HighlineCli.new
89
+ end
90
+
91
+ private def local_code
92
+ @local_code ||= GitHelper::LocalCode.new
93
+ end
94
+ end
95
+ end