github-renaming 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e519037df2e9bbe4bc12eba18ca1b0fc1a2faecc015a3fcb80c8db7771ea21ee
4
+ data.tar.gz: c174239f91191baa6d2cb4cf7a1fbceefee56dc57f1735f6da0880c6894113fb
5
+ SHA512:
6
+ metadata.gz: 87b324661b20af37943818d4dbfe1d5e3225557bdc7f4ff26afbec065553f732016a90d1165d3f48135b48bb46d3eff90b754d2d69e9447cdb4de158c16015f5
7
+ data.tar.gz: 111818458a60aafd53acbf05dcd1657d07de296368290ebcb6d6817d9068c2866b55da9f3d7ca294e37cd48b9f56d74070f2a639ac06328c5c29921d3ee603e6
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in github-renaming.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,60 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ github-renaming (0.1.0)
5
+ highline (~> 2.0.3)
6
+ httparty (~> 0.18.1)
7
+ octokit (~> 4.21.0)
8
+ thor (~> 1.1.0)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ addressable (2.8.0)
14
+ public_suffix (>= 2.0.2, < 5.0)
15
+ faraday (1.5.1)
16
+ faraday-em_http (~> 1.0)
17
+ faraday-em_synchrony (~> 1.0)
18
+ faraday-excon (~> 1.1)
19
+ faraday-httpclient (~> 1.0.1)
20
+ faraday-net_http (~> 1.0)
21
+ faraday-net_http_persistent (~> 1.1)
22
+ faraday-patron (~> 1.0)
23
+ multipart-post (>= 1.2, < 3)
24
+ ruby2_keywords (>= 0.0.4)
25
+ faraday-em_http (1.0.0)
26
+ faraday-em_synchrony (1.0.0)
27
+ faraday-excon (1.1.0)
28
+ faraday-httpclient (1.0.1)
29
+ faraday-net_http (1.0.1)
30
+ faraday-net_http_persistent (1.2.0)
31
+ faraday-patron (1.0.0)
32
+ highline (2.0.3)
33
+ httparty (0.18.1)
34
+ mime-types (~> 3.0)
35
+ multi_xml (>= 0.5.2)
36
+ mime-types (3.3.1)
37
+ mime-types-data (~> 3.2015)
38
+ mime-types-data (3.2021.0704)
39
+ multi_xml (0.6.0)
40
+ multipart-post (2.1.1)
41
+ octokit (4.21.0)
42
+ faraday (>= 0.9)
43
+ sawyer (~> 0.8.0, >= 0.5.3)
44
+ public_suffix (4.0.6)
45
+ rake (13.0.6)
46
+ ruby2_keywords (0.0.5)
47
+ sawyer (0.8.2)
48
+ addressable (>= 2.3.5)
49
+ faraday (> 0.8, < 2.0)
50
+ thor (1.1.0)
51
+
52
+ PLATFORMS
53
+ ruby
54
+
55
+ DEPENDENCIES
56
+ github-renaming!
57
+ rake (~> 13.0)
58
+
59
+ BUNDLED WITH
60
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2021 qiuzhifei <qiuzhifei521@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # Renaming the default branch
2
+
3
+ Renaming the default branch of our own repositories from master to main.
4
+
5
+ ## Installation
6
+ ```
7
+ gem install github-renaming
8
+ ```
9
+
10
+ ## Usage
11
+ ```
12
+ github-renaming default-branch <old-branch> <new-branch> -t <token> -r <user/repo>
13
+ ```
14
+ Rename master to main
15
+ ```
16
+ github-renaming default-branch master main -t <token> -r <user/repo>
17
+ ```
18
+
19
+ ## License
20
+
21
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "github/renaming"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "github/renaming"
4
+
5
+ Github::Renaming.start(ARGV)
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/github/renaming/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "github-renaming"
7
+ spec.version = Github::VERSION
8
+ spec.authors = ["qiuzhifei"]
9
+ spec.email = ["qiuzhifei521@gmail.com"]
10
+
11
+ spec.summary = "Renaming the default branch on Github"
12
+ spec.description = "Renaming the default branch on Github"
13
+ spec.homepage = "https://github.com/QiuZhiFei/github-renaming"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.4.0"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ # Uncomment to register a new dependency of your gem
27
+ # spec.add_dependency "example-gem", "~> 1.0"
28
+ spec.add_dependency "thor", "~> 1.1.0"
29
+ spec.add_dependency "octokit", "~> 4.21.0"
30
+ spec.add_dependency "httparty", "~> 0.18.1"
31
+ spec.add_dependency "highline", "~> 2.0.3"
32
+
33
+ # For more information and examples about making a new gem, checkout our
34
+ # guide at: https://bundler.io/guides/creating_gem.html
35
+ end
data/lib/github/cli.rb ADDED
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "octokit"
4
+ require "thor"
5
+ require "httparty"
6
+ require "highline"
7
+ require_relative "constant"
8
+ require_relative "repox"
9
+
10
+ module Github
11
+ module CLI
12
+ include Logging
13
+
14
+ def check_token(*args, options)
15
+ token = options[:token] || ""
16
+ _assert_not_nil_token(token)
17
+
18
+ url = "https://api.github.com/users/codertocat"
19
+ res = HTTParty.get(url, headers: {
20
+ "Authorization" => "token #{token}"})
21
+ data = JSON.parse(res.body)
22
+ return unless !data.member?("login")
23
+ logger.error "Github token is invalid."
24
+ exit 1
25
+ end
26
+
27
+ def rename_default_branch(*args, options)
28
+ token = options[:token]
29
+
30
+ repo_path = options[:repo] || ""
31
+ _assert_not_nil_repo(repo_path)
32
+
33
+ old_branch = args[0] || "master"
34
+ new_branch = args[1] || "main"
35
+ if old_branch == new_branch
36
+ logger.error "Branch #{branch} is the same as #{based_on_branch}"
37
+ exit 1
38
+ end
39
+
40
+ _rename_prompt(repo_path, token, old_branch, new_branch)
41
+
42
+ repo = Github::RepoX.new(token, repo_path)
43
+ # get the sha
44
+ old_branch_sha = repo.branch_sha(old_branch)
45
+ # create new branch
46
+ logger.debug "Creating branch #{new_branch} based on #{old_branch}."
47
+ repo.create_branch(new_branch, old_branch_sha) {
48
+ _delete_branch_prompt(repo_path, token, new_branch)
49
+ logger.debug "Deleting branch #{new_branch}."
50
+ repo.delete_branch(new_branch)
51
+ }
52
+ # set default branch
53
+ logger.debug "Setting #{new_branch} as default branch."
54
+ repo.set_default_branch(new_branch)
55
+ # delete old branch
56
+ logger.debug "Deleting #{old_branch}."
57
+ repo.delete_branch(old_branch)
58
+
59
+ # local
60
+ if new_branch == "main"
61
+ global_default_branch = `git config --global init.defaultBranch`.strip
62
+ if global_default_branch != new_branch
63
+ `git config --global init.defaultBranch #{new_branch}`
64
+ logger.debug "Setting #{new_branch} as global default branch."
65
+ end
66
+ end
67
+
68
+ logger.info "Finished."
69
+ end
70
+
71
+ private
72
+ def _rename_prompt(repo_path, token, old_branch, new_branch)
73
+ answer = HighLine.agree("
74
+ ------------------------------------
75
+ Action: Renaming branch
76
+ Repo: #{repo_path}
77
+ Token: #{token}
78
+ Create new branch: #{new_branch}
79
+ Delete old branch: #{old_branch}
80
+ Rename default branch: #{old_branch} -> #{new_branch}
81
+ ------------------------------------
82
+ Continue? (y/n) ")
83
+ if answer == false
84
+ exit 1
85
+ end
86
+ end
87
+
88
+ def _delete_branch_prompt(repo_path, token, branch)
89
+ answer = HighLine.agree("
90
+ ------------------------------------
91
+ Action: Deleting branch
92
+ Repo: #{repo_path}
93
+ Token: #{token}
94
+ Delete branch: #{branch}
95
+ ------------------------------------
96
+ Continue? (y/n) ")
97
+ if answer == false
98
+ exit 1
99
+ end
100
+ end
101
+
102
+ def _assert_not_nil_token(token)
103
+ return unless token.empty?
104
+ logger.error "You must provide a GitHub token to use this command. See 'github-renaming --help'.
105
+
106
+ Creating a personal access token
107
+ https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token"
108
+ exit 1
109
+ end
110
+
111
+ def _assert_not_nil_repo(repo)
112
+ return unless repo.empty?
113
+ logger.error "You must provide a GitHub repo to use this command. See 'github-renaming --help'."
114
+ exit 1
115
+ end
116
+ end
117
+ end
118
+
119
+ module Github
120
+ extend CLI
121
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "logger"
4
+
5
+ module Logging
6
+ def logger
7
+ Logging.logger
8
+ end
9
+
10
+ def self.logger
11
+ @logger ||= Logger.new(STDOUT)
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+ require "logger"
5
+ require_relative "renaming/version"
6
+ require_relative "cli"
7
+ require_relative "constant"
8
+
9
+ module Github
10
+ class Renaming < Thor
11
+ class_option :token, type: :string, aliases: '-t', desc: "User's Github Token at github.com"
12
+ class_option :repo, type: :string, aliases: '-r', desc: "User's Repo at at github.com"
13
+
14
+ desc "default-branch <old-branch> <new-branch>", "Rename default branch on GitHub"
15
+ map "-d" => :default_branch
16
+ def default_branch(*args)
17
+ prepare(*args)
18
+ Github.rename_default_branch(*args, options)
19
+ end
20
+
21
+ desc "version", "Show github-renaming version number and quit (aliases: `v`)."
22
+ map ["v", "-v", "--version"] => :version
23
+ def version
24
+ say "github-renaming #{Github::VERSION}"
25
+ end
26
+
27
+ private
28
+ def prepare(*args)
29
+ Github.check_token(*args, options)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Github
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "octokit"
4
+ require_relative "constant"
5
+
6
+ module Github
7
+ class RepoX
8
+ include Logging
9
+
10
+ def initialize(token, repo_path)
11
+ @client = Octokit::Client.new(access_token: token)
12
+
13
+ begin
14
+ @repo = @client.repo(repo_path)
15
+ rescue Octokit::Error => e
16
+ logger.error "Repo #{repo_path} does not exist."
17
+ exit 1
18
+ end
19
+ end
20
+
21
+ # description
22
+ def description
23
+ puts <<-EOS
24
+ --------------------------------------
25
+ Repo description
26
+
27
+ name:
28
+ #{@repo.full_name}
29
+
30
+ default_branch:
31
+ #{default_branch}
32
+
33
+ branches:
34
+ #{branches.map { |value| value.name }}
35
+ --------------------------------------
36
+ EOS
37
+ end
38
+
39
+ # get name
40
+ def name
41
+ @repo.full_name
42
+ end
43
+
44
+ # get default branch
45
+ def default_branch
46
+ @repo.default_branch
47
+ end
48
+
49
+ # get all branches
50
+ def branches
51
+ @client.branches(@repo.full_name)
52
+ end
53
+
54
+ # get branch sha
55
+ def branch_sha(branch)
56
+ begin
57
+ ref = @client.ref(@repo.full_name, "heads/#{branch}")
58
+ rescue Octokit::Error => e
59
+ logger.error "Branch #{branch} does not exist."
60
+ exit 1
61
+ end
62
+ if ref.key?(:object)
63
+ return ref.object.sha
64
+ end
65
+ logger.error "Branch #{branch} does not exist."
66
+ exit 1
67
+ end
68
+
69
+ # create a new branch
70
+ def create_branch(branch, sha, &block)
71
+ begin
72
+ @client.create_ref(@repo.full_name, "heads/#{branch}", sha)
73
+ rescue Octokit::Error => e
74
+ if e.message.include?("422 - Reference already exists")
75
+ logger.debug "Branch #{branch} already exists."
76
+ block.call if block_given?
77
+ else
78
+ logger.error "Branch #{branch} already exists."
79
+ exit 1
80
+ end
81
+ end
82
+ end
83
+
84
+ # delete a branch
85
+ def delete_branch(branch)
86
+ @repo.delete_ref("heads/#{branch}")
87
+ end
88
+
89
+ # set default branch
90
+ def set_default_branch(branch)
91
+ @client.edit_repository(@repo.full_name, default_branch: branch)
92
+ end
93
+ end
94
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: github-renaming
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - qiuzhifei
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-07-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: octokit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 4.21.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 4.21.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.18.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.18.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: highline
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.3
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.3
69
+ description: Renaming the default branch on Github
70
+ email:
71
+ - qiuzhifei521@gmail.com
72
+ executables:
73
+ - github-renaming
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - exe/github-renaming
86
+ - github-renaming.gemspec
87
+ - lib/github/cli.rb
88
+ - lib/github/constant.rb
89
+ - lib/github/renaming.rb
90
+ - lib/github/renaming/version.rb
91
+ - lib/github/repox.rb
92
+ homepage: https://github.com/QiuZhiFei/github-renaming
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 2.4.0
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubygems_version: 3.2.23
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Renaming the default branch on Github
115
+ test_files: []