gitlab-branch-rename 0.1.0 → 0.2.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 +4 -4
- data/Gemfile.lock +1 -1
- data/Makefile +16 -0
- data/README.md +4 -0
- data/bin/console +1 -1
- data/exe/gitlab-branch-rename +2 -2
- data/gitlab-branch-rename.gemspec +7 -5
- data/gitlab-branch-rename.gemspec.lock +3 -0
- data/lib/gitlab_branch_rename/configuration.rb +81 -0
- data/lib/gitlab_branch_rename/gitlab_configure.rb +14 -0
- data/lib/gitlab_branch_rename/logger_configure.rb +16 -0
- data/lib/gitlab_branch_rename/main.rb +32 -0
- data/lib/gitlab_branch_rename/operation.rb +133 -0
- data/lib/{version.rb → gitlab_branch_rename/version.rb} +1 -1
- metadata +12 -7
- data/lib/rename.rb +0 -9
- data/rename.rb +0 -153
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60976abc74b540c7fdd90ee169c5c7f49e67d71514728f4ee41194cf2b70c1fc
|
4
|
+
data.tar.gz: b91a114cbc9603200823ddad6d36c0e0fd8a6d1988c36a9a5fbb67c03a10722e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2382c0b192541311af1488e071ee3c1415d24c59b57ff4f512bae9fed1f3d8c479ca2359a6ba6637f3cd0ba5d282d350472bf3b6927bb944462b8eeda255ac47
|
7
|
+
data.tar.gz: af8db546cbdcd9850f17f3b9944403ff162f053a71ce0c65e4acf7665ade7f8bb0ed570aff174f5c6cd9f8f76dfa0ce601964820a954ab6e0d7a59ed37504a20
|
data/Gemfile.lock
CHANGED
data/Makefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
all:
|
2
|
+
rake
|
3
|
+
|
4
|
+
build:
|
5
|
+
rake
|
6
|
+
gem build ./gitlab-branch-rename.gemspec
|
7
|
+
|
8
|
+
clean:
|
9
|
+
rm .rspec_status
|
10
|
+
rm gitlab-branch-rename-*.gem
|
11
|
+
|
12
|
+
install: build
|
13
|
+
gem install ./gitlab-branch-rename-*.gem
|
14
|
+
|
15
|
+
push: clean build
|
16
|
+
gem push gitlab-branch-rename-*.gem
|
data/README.md
CHANGED
@@ -30,6 +30,10 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
30
30
|
|
31
31
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
32
|
|
33
|
+
During development, I used these references:
|
34
|
+
- https://docs.gitlab.com/ce/api/projects.html#edit-project
|
35
|
+
- https://www.rubydoc.info/gems/gitlab/Gitlab/Client/Projects
|
36
|
+
|
33
37
|
## Contributing
|
34
38
|
|
35
39
|
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gitlab-branch-rename. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/gitlab-branch-rename/blob/master/CODE_OF_CONDUCT.md).
|
data/bin/console
CHANGED
data/exe/gitlab-branch-rename
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative 'lib/version'
|
1
|
+
require_relative 'lib/gitlab_branch_rename/version'
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
4
|
spec.name = "gitlab-branch-rename"
|
@@ -6,10 +6,10 @@ Gem::Specification.new do |spec|
|
|
6
6
|
spec.authors = ["AP Fritts"]
|
7
7
|
spec.email = ["ap@apfritts.com"]
|
8
8
|
|
9
|
-
spec.summary =
|
10
|
-
spec.description = "To support Black Lives Matter and try to create safer spaces, I wanted to rename" +
|
9
|
+
spec.summary = "Tool to rename your Gitlab project branches en-mass"
|
10
|
+
spec.description = "To support Black Lives Matter and try to create safer spaces, I wanted to rename " +
|
11
11
|
"all of my own default branches in Gitlab. I wasn't able to find any tools to do that, so I wrote " +
|
12
|
-
"this one. If you're unsure why this is important or want to better understand my personal understanding" +
|
12
|
+
"this one. If you're unsure why this is important or want to better understand my personal understanding " +
|
13
13
|
"of why I wanted to make these changes, see this Twitter thread: https://twitter.com/mislav/status/1270388510684598272"
|
14
14
|
spec.homepage = "https://gitlab.com/apfritts/gitlab-branch-rename"
|
15
15
|
spec.license = "MIT"
|
@@ -26,6 +26,8 @@ Gem::Specification.new do |spec|
|
|
26
26
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
27
|
end
|
28
28
|
spec.bindir = "exe"
|
29
|
-
|
29
|
+
executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
puts executables
|
31
|
+
spec.executables = executables
|
30
32
|
spec.require_paths = ["lib"]
|
31
33
|
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require "optparse"
|
2
|
+
|
3
|
+
module GitlabBranchRename
|
4
|
+
class Configuration
|
5
|
+
attr_accessor :token, :branch_to_rename, :new_branch_name,
|
6
|
+
:endpoint, :is_default_branch, :has_visibility, :minimum_access_level,
|
7
|
+
:skip_confirm, :pretend, :logfile
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
# Required parameters
|
11
|
+
self.branch_to_rename = nil
|
12
|
+
self.new_branch_name = nil
|
13
|
+
|
14
|
+
# Criteria parameters with defaults
|
15
|
+
self.endpoint = "https://gitlab.com/api/v4"
|
16
|
+
self.token = ENV["GITLAB_ACCESS_TOKEN"]
|
17
|
+
self.is_default_branch = false
|
18
|
+
self.has_visibility = %w[private internal public]
|
19
|
+
self.minimum_access_level = 40
|
20
|
+
|
21
|
+
# Optional parameters
|
22
|
+
self.skip_confirm = false
|
23
|
+
self.pretend = false
|
24
|
+
self.logfile = STDOUT
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.configure
|
28
|
+
configuration = Configuration.new
|
29
|
+
optparse = OptionParser.new do |opts|
|
30
|
+
opts.banner = "Usage: gitlab-branch-rename --from <branch to rename> --to <new branch name> [options]"
|
31
|
+
opts.separator ""
|
32
|
+
opts.separator "Specific options:"
|
33
|
+
opts.on("--from OLD_BRANCH", "Branch to rename") do |from|
|
34
|
+
configuration.branch_to_rename = from
|
35
|
+
end
|
36
|
+
opts.on("--to NEW_BRANCH", "New branch name") do |to|
|
37
|
+
configuration.new_branch_name = to
|
38
|
+
end
|
39
|
+
opts.on("--endpoint ENDPOINT", "Gitlab server endpoint. default: Gitlab cloud") do |endpoint|
|
40
|
+
configuration.endpoint = endpoint
|
41
|
+
end
|
42
|
+
opts.on("--token-env-var ENV_VAR_NAME", "Environment variable to pull token out of") do |token_env_var|
|
43
|
+
configuration.token = ENV[token_env_var]
|
44
|
+
end
|
45
|
+
opts.on("--only-if-default", "Rename only if it is the default branch") do
|
46
|
+
configuration.is_default_branch = true
|
47
|
+
end
|
48
|
+
opts.on("--visibility VISIBILITIES", "Comma-separated list of project visibilities default: public,internal,private") do |visibility|
|
49
|
+
configuration.has_visibility = visibility.split ","
|
50
|
+
end
|
51
|
+
opts.on("--skip-confirm", "Skip confirmation before executing rename") do
|
52
|
+
configuration.skip_confirm = true
|
53
|
+
end
|
54
|
+
opts.on("--pretend", "Pretend to perform actions. Overrides confirm option") do
|
55
|
+
configuration.pretend = true
|
56
|
+
end
|
57
|
+
opts.on("--logfile LOGFILE", "Logfile destination. Use - for STDOUT") do |logfile|
|
58
|
+
if logfile == "-"
|
59
|
+
configuration.logfile = STDOUT
|
60
|
+
else
|
61
|
+
configuration.logfile = logfile
|
62
|
+
end
|
63
|
+
end
|
64
|
+
opts.separator ""
|
65
|
+
opts.separator "Visit https://gitlab.com/apfritts/gitlab-branch-rename for more details."
|
66
|
+
end
|
67
|
+
|
68
|
+
begin
|
69
|
+
optparse.parse!
|
70
|
+
raise OptionParser::MissingArgument.new('old branch name') if configuration.branch_to_rename.nil?
|
71
|
+
raise OptionParser::MissingArgument.new('new branch name') if configuration.new_branch_name.nil?
|
72
|
+
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
|
73
|
+
puts $!.to_s
|
74
|
+
puts optparse
|
75
|
+
exit 1
|
76
|
+
end
|
77
|
+
|
78
|
+
configuration
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "gitlab"
|
2
|
+
|
3
|
+
module GitlabBranchRename
|
4
|
+
class GitlabConfigure
|
5
|
+
def self.configure(configuration)
|
6
|
+
Gitlab.endpoint = configuration.endpoint
|
7
|
+
Gitlab.private_token = configuration.token
|
8
|
+
|
9
|
+
#Gitlab.http_proxy("proxyhost", 8888)
|
10
|
+
#Gitlab.http_proxy("proxyhost", 8888, "proxyuser", "strongpasswordhere")
|
11
|
+
#ENV["GITLAB_API_HTTPARTY_OPTIONS"] = "{read_timeout: 60}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module GitlabBranchRename
|
2
|
+
class LoggerConfigure
|
3
|
+
def self.configure(configuration)
|
4
|
+
logger = Logger.new(STDOUT)
|
5
|
+
logger.formatter = proc do |severity, datetime, progname, msg|
|
6
|
+
date_format = datetime.to_datetime.strftime("%Q")
|
7
|
+
if severity == "INFO" or severity == "WARN"
|
8
|
+
"#{severity} #{date_format} #{msg}\n"
|
9
|
+
else
|
10
|
+
"#{severity} #{date_format} #{msg}\n"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
logger
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "gitlab_branch_rename/configuration"
|
2
|
+
require "gitlab_branch_rename/gitlab_configure"
|
3
|
+
require "gitlab_branch_rename/logger_configure"
|
4
|
+
require "gitlab_branch_rename/operation"
|
5
|
+
require "gitlab_branch_rename/version"
|
6
|
+
|
7
|
+
module GitlabBranchRename
|
8
|
+
def self.launch
|
9
|
+
c = Configuration.configure
|
10
|
+
logger = LoggerConfigure.configure c
|
11
|
+
logger.debug("Gitlab instance configuration:")
|
12
|
+
logger.debug("Endpoint: #{c.endpoint}")
|
13
|
+
logger.debug("Token: #{c.token.nil? ? "set" : "not set"}")
|
14
|
+
logger.debug("Project filter criteria:")
|
15
|
+
logger.debug("Visibility: #{c.has_visibility.join(', ')}")
|
16
|
+
logger.debug("Operation options:")
|
17
|
+
logger.debug("Old branch: #{c.branch_to_rename}")
|
18
|
+
logger.debug("New branch: #{c.new_branch_name}")
|
19
|
+
logger.debug("Skip confirm: #{c.skip_confirm.nil? ? "yes" : "no"}")
|
20
|
+
logger.debug("Pretend: #{c.pretend.nil? ? "yes" : "no"}")
|
21
|
+
logger.debug("Log file: #{c.logfile == STDOUT ? "-" : c.logfile}")
|
22
|
+
GitlabConfigure.configure c
|
23
|
+
operation = Operation.new c, logger
|
24
|
+
operation.execute
|
25
|
+
logger.info("For any local repositories, use these commands to update:")
|
26
|
+
logger.info(" git fetch `git remote` --prune")
|
27
|
+
logger.info(" git checkout #{c.new_branch_name}")
|
28
|
+
logger.info(" git remote set-head `git remote` #{c.new_branch_name}")
|
29
|
+
logger.info(" git branch -d #{c.branch_to_rename}")
|
30
|
+
logger.close
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require "gitlab"
|
2
|
+
|
3
|
+
module GitlabBranchRename
|
4
|
+
class Operation
|
5
|
+
def initialize(configuration, logger)
|
6
|
+
@configuration = configuration
|
7
|
+
@logger = logger
|
8
|
+
end
|
9
|
+
|
10
|
+
def execute
|
11
|
+
projects = Gitlab.projects(min_access_level: @configuration.minimum_access_level,
|
12
|
+
owned: true,
|
13
|
+
per_page: 5)
|
14
|
+
projects.auto_paginate do |project|
|
15
|
+
@logger.info("Evaluating #{project.path_with_namespace}")
|
16
|
+
|
17
|
+
# Verify this project meets criteria for renaming
|
18
|
+
unless @configuration.has_visibility.include?(project.visibility)
|
19
|
+
@logger.debug("Project has visibility #{project.visibility}, skipping")
|
20
|
+
next
|
21
|
+
end
|
22
|
+
if project.archived
|
23
|
+
@logger.debug("Project is archived")
|
24
|
+
next
|
25
|
+
end
|
26
|
+
|
27
|
+
# Feature: Skip forks?
|
28
|
+
|
29
|
+
# Skip projects you don"t have proper permission to change
|
30
|
+
# Verify it meets your criteria for renaming default branch
|
31
|
+
if @configuration.is_default_branch and project.default_branch != @configuration.branch_to_rename
|
32
|
+
@logger.debug("Project default branch does not match is_default_branch criteria")
|
33
|
+
next
|
34
|
+
end
|
35
|
+
|
36
|
+
# Feature: verify new branch doesn't already exist
|
37
|
+
|
38
|
+
# Verify new branch doesn't exist already
|
39
|
+
begin
|
40
|
+
Gitlab.branch(project.id, @configuration.new_branch_name)
|
41
|
+
@logger.debug("Destination branch #{@configuration.new_branch_name} already exists")
|
42
|
+
next
|
43
|
+
rescue Gitlab::Error::NotFound => e
|
44
|
+
@logger.debug("Destination branch #{@configuration.new_branch_name} does not exist yet")
|
45
|
+
end
|
46
|
+
|
47
|
+
# Save branch protection parameters
|
48
|
+
old_branch = begin
|
49
|
+
Gitlab.branch(project.id, @configuration.branch_to_rename)
|
50
|
+
rescue Gitlab::Error::NotFound => e
|
51
|
+
@logger.warn("Project #{project.path_with_namespace} doesn't have branch #{@configuration.branch_to_rename}")
|
52
|
+
next
|
53
|
+
end
|
54
|
+
branch_protection_parameters = nil
|
55
|
+
if old_branch.protected
|
56
|
+
# Save branch protection information
|
57
|
+
bpp_raw = Gitlab.protected_branch(project.id, old_branch.name)
|
58
|
+
bpp_hash = bpp_raw.to_hash
|
59
|
+
branch_protection_parameters = bpp_hash.slice("push_access_levels", "merge_access_levels", "unprotect_access_levels", "code_owner_approval_required")
|
60
|
+
@logger.debug("Will transfer branch protection parameters: #{branch_protection_parameters}")
|
61
|
+
end
|
62
|
+
|
63
|
+
# Confirm or pretend
|
64
|
+
msg = "Rename #{project.path_with_namespace} branch #{@configuration.branch_to_rename} to #{@configuration.new_branch_name} " +
|
65
|
+
(branch_protection_parameters ? "with" : "without") + " protection"
|
66
|
+
if @configuration.pretend
|
67
|
+
puts(msg)
|
68
|
+
@logger.info(msg)
|
69
|
+
next
|
70
|
+
end
|
71
|
+
unless @configuration.skip_confirm
|
72
|
+
puts(msg)
|
73
|
+
print("\tContinue with rename and delete? (y/N) ")
|
74
|
+
input = gets.chomp
|
75
|
+
if input != "y"
|
76
|
+
msg = "Skipping branch rename for #{project.path_with_namespace} based on user input #{input}"
|
77
|
+
puts(msg)
|
78
|
+
@logger.info(msg)
|
79
|
+
next
|
80
|
+
end
|
81
|
+
@logger.info("User confirmed branch rename for #{project.path_with_namespace}")
|
82
|
+
end
|
83
|
+
|
84
|
+
# Create new branch
|
85
|
+
begin
|
86
|
+
Gitlab.create_branch(project.id, @configuration.new_branch_name, old_branch.name)
|
87
|
+
@logger.debug("Created new branch with name #{@configuration.new_branch_name}")
|
88
|
+
rescue StandardError => e
|
89
|
+
@logger.error(e)
|
90
|
+
next
|
91
|
+
end
|
92
|
+
|
93
|
+
# Protect branch
|
94
|
+
unless branch_protection_parameters
|
95
|
+
begin
|
96
|
+
Gitlab.protect_branch(project.id, @configuration.new_branch_name, branch_protection_parameters)
|
97
|
+
@logger.debug("Protected branch with parameters #{branch_protection_parameters}")
|
98
|
+
rescue StandardError => e
|
99
|
+
@logger.error(e)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# Feature: Update PRs? Issues?
|
104
|
+
|
105
|
+
# Change default branch
|
106
|
+
begin
|
107
|
+
Gitlab.edit_project(project.id, {default_branch: @configuration.new_branch_name})
|
108
|
+
@logger.debug("Changed default branch")
|
109
|
+
rescue StandardError => e
|
110
|
+
@logger.error(e)
|
111
|
+
end
|
112
|
+
|
113
|
+
# Unprotect protected branches
|
114
|
+
unless branch_protection_parameters
|
115
|
+
begin
|
116
|
+
Gitlab.unprotect_branch(project.id, old_branch.name)
|
117
|
+
@logger.debug("Unprotected old branch")
|
118
|
+
rescue StandardError => e
|
119
|
+
@logger.error(e)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# Delete master branch
|
124
|
+
begin
|
125
|
+
Gitlab.delete_branch(project.id, old_branch.name)
|
126
|
+
@logger.debug("Deleted old branch")
|
127
|
+
rescue StandardError => e
|
128
|
+
@logger.error(e)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gitlab-branch-rename
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- AP Fritts
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: gitlab
|
@@ -25,9 +25,9 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '4.16'
|
27
27
|
description: 'To support Black Lives Matter and try to create safer spaces, I wanted
|
28
|
-
to
|
28
|
+
to rename all of my own default branches in Gitlab. I wasn''t able to find any tools
|
29
29
|
to do that, so I wrote this one. If you''re unsure why this is important or want
|
30
|
-
to better understand my personal
|
30
|
+
to better understand my personal understanding of why I wanted to make these changes,
|
31
31
|
see this Twitter thread: https://twitter.com/mislav/status/1270388510684598272'
|
32
32
|
email:
|
33
33
|
- ap@apfritts.com
|
@@ -44,15 +44,20 @@ files:
|
|
44
44
|
- Gemfile
|
45
45
|
- Gemfile.lock
|
46
46
|
- LICENSE.txt
|
47
|
+
- Makefile
|
47
48
|
- README.md
|
48
49
|
- Rakefile
|
49
50
|
- bin/console
|
50
51
|
- bin/setup
|
51
52
|
- exe/gitlab-branch-rename
|
52
53
|
- gitlab-branch-rename.gemspec
|
53
|
-
-
|
54
|
-
- lib/
|
55
|
-
-
|
54
|
+
- gitlab-branch-rename.gemspec.lock
|
55
|
+
- lib/gitlab_branch_rename/configuration.rb
|
56
|
+
- lib/gitlab_branch_rename/gitlab_configure.rb
|
57
|
+
- lib/gitlab_branch_rename/logger_configure.rb
|
58
|
+
- lib/gitlab_branch_rename/main.rb
|
59
|
+
- lib/gitlab_branch_rename/operation.rb
|
60
|
+
- lib/gitlab_branch_rename/version.rb
|
56
61
|
homepage: https://gitlab.com/apfritts/gitlab-branch-rename
|
57
62
|
licenses:
|
58
63
|
- MIT
|
data/lib/rename.rb
DELETED
data/rename.rb
DELETED
@@ -1,153 +0,0 @@
|
|
1
|
-
require "gitlab"
|
2
|
-
|
3
|
-
ACCESS_LEVEL_MAINTAINER = 40
|
4
|
-
|
5
|
-
puts("Create a Gitlab API key at https://gitlab.com/profile/personal_access_tokens")
|
6
|
-
|
7
|
-
default_endpoint = "https://gitlab.com/api/v4"
|
8
|
-
options = {
|
9
|
-
# Required parameters
|
10
|
-
endpoint: default_endpoint,
|
11
|
-
token: ENV["GITLAB_ACCESS_TOKEN"],
|
12
|
-
branch_to_rename: "master",
|
13
|
-
new_branch_name: "trunk",
|
14
|
-
|
15
|
-
# Criteria parameters with defaults
|
16
|
-
is_default_branch: true,
|
17
|
-
has_visibility: %w[private internal public],
|
18
|
-
|
19
|
-
# Optional parameters
|
20
|
-
confirm: true,
|
21
|
-
pretend: false,
|
22
|
-
}
|
23
|
-
|
24
|
-
logfile = "/tmp/gitlab-branch-rename.log"
|
25
|
-
|
26
|
-
logger = Logger.new(STDOUT)
|
27
|
-
logger.formatter = proc do |severity, datetime, progname, msg|
|
28
|
-
date_format = datetime.to_datetime.strftime("%Q")
|
29
|
-
if severity == "INFO" or severity == "WARN"
|
30
|
-
"#{severity} #{date_format} #{msg}\n"
|
31
|
-
else
|
32
|
-
"#{severity} #{date_format} #{msg}\n"
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
Gitlab.endpoint = options[:endpoint]
|
37
|
-
Gitlab.private_token = options[:token]
|
38
|
-
|
39
|
-
#Gitlab.http_proxy("proxyhost", 8888)
|
40
|
-
#Gitlab.http_proxy("proxyhost", 8888, "proxyuser", "strongpasswordhere")
|
41
|
-
#ENV["GITLAB_API_HTTPARTY_OPTIONS"] = "{read_timeout: 60}"
|
42
|
-
|
43
|
-
logger.info("Starting run with options: #{options}")
|
44
|
-
|
45
|
-
# loop through all projects
|
46
|
-
projects = Gitlab.projects(min_access_level: ACCESS_LEVEL_MAINTAINER,
|
47
|
-
owned: true,
|
48
|
-
per_page: 5)
|
49
|
-
projects.auto_paginate do |project|
|
50
|
-
logger.info("Evaluating #{project.path_with_namespace}")
|
51
|
-
# Verify this project meets criteria for renaming
|
52
|
-
unless options[:has_visibility].include?(project.visibility)
|
53
|
-
logger.debug("Project has visibility #{project.visibility}, skipping")
|
54
|
-
next
|
55
|
-
end
|
56
|
-
if project.archived
|
57
|
-
logger.debug("Project is archived")
|
58
|
-
next
|
59
|
-
end
|
60
|
-
# Feature: Skip forks?
|
61
|
-
# Skip projects you don"t have proper permission to change
|
62
|
-
# Verify it meets your criteria for renaming default branch
|
63
|
-
if options[:is_default_branch] and project.default_branch != options[:branch_to_rename]
|
64
|
-
logger.debug("Project default branch does not match is_default_branch criteria")
|
65
|
-
next
|
66
|
-
end
|
67
|
-
# Feature: verify new branch doesn't already exist
|
68
|
-
begin
|
69
|
-
Gitlab.branch(project.id, options[:new_branch_name])
|
70
|
-
logger.debug("Destination branch #{options[:new_branch_name]} already exists")
|
71
|
-
next
|
72
|
-
rescue Gitlab::Error::NotFound => e
|
73
|
-
logger.debug("Destination branch #{options[:new_branch_name]} does not exist yet")
|
74
|
-
end
|
75
|
-
# Save branch protection parameters
|
76
|
-
old_branch = Gitlab.branch(project.id, options[:branch_to_rename])
|
77
|
-
branch_protection_parameters = nil
|
78
|
-
if old_branch.protected
|
79
|
-
# Save branch protection information
|
80
|
-
bpp_raw = Gitlab.protected_branch(project.id, old_branch.name)
|
81
|
-
bpp_hash = bpp_raw.to_hash
|
82
|
-
branch_protection_parameters = bpp_hash.slice("push_access_levels", "merge_access_levels", "unprotect_access_levels", "code_owner_approval_required")
|
83
|
-
logger.debug("Will transfer branch protection parameters: #{branch_protection_parameters}")
|
84
|
-
end
|
85
|
-
msg = "Rename #{project.path_with_namespace} branch #{options[:branch_to_rename]} to #{options[:new_branch_name]} " +
|
86
|
-
(branch_protection_parameters ? "with" : "without") + " protection"
|
87
|
-
if options[:pretend]
|
88
|
-
puts(msg)
|
89
|
-
logger.info(msg)
|
90
|
-
next
|
91
|
-
elsif options[:confirm]
|
92
|
-
puts(msg)
|
93
|
-
print("\tContinue with branch? (y/N) ")
|
94
|
-
input = gets.chomp
|
95
|
-
if input != "y"
|
96
|
-
msg = "Skipping branch rename for #{project.path_with_namespace} based on user input #{input}"
|
97
|
-
puts(msg)
|
98
|
-
logger.info(msg)
|
99
|
-
next
|
100
|
-
end
|
101
|
-
logger.info("User confirmed branch rename for #{project.path_with_namespace}")
|
102
|
-
end
|
103
|
-
# Create new branch
|
104
|
-
begin
|
105
|
-
new_branch = Gitlab.create_branch(project.id, options[:new_branch_name], old_branch.name)
|
106
|
-
logger.debug("Created new branch with name options[:new_branch_name]")
|
107
|
-
rescue StandardError => e
|
108
|
-
logger.error(e)
|
109
|
-
next
|
110
|
-
end
|
111
|
-
# Protect branch
|
112
|
-
unless branch_protection_parameters
|
113
|
-
begin
|
114
|
-
Gitlab.protect_branch(project.id, options[:new_branch_name], branch_protection_parameters)
|
115
|
-
logger.debug("Protected branch with parameters #{branch_protection_parameters}")
|
116
|
-
rescue StandardError => e
|
117
|
-
logger.error(e)
|
118
|
-
end
|
119
|
-
end
|
120
|
-
# Feature: Update PRs? Issues?
|
121
|
-
# Change default branch
|
122
|
-
begin
|
123
|
-
Gitlab.edit_project(project.id, {default_branch: options[:new_branch_name]})
|
124
|
-
logger.debug("Changed default branch")
|
125
|
-
rescue StandardError => e
|
126
|
-
logger.error(e)
|
127
|
-
end
|
128
|
-
|
129
|
-
# Unprotect protected branches
|
130
|
-
unless branch_protection_parameters
|
131
|
-
begin
|
132
|
-
Gitlab.unprotect_branch(project.id, old_branch.name)
|
133
|
-
logger.debug("Unprotected old branch")
|
134
|
-
rescue StandardError => e
|
135
|
-
logger.error(e)
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
# Delete master branch
|
140
|
-
begin
|
141
|
-
Gitlab.delete_branch(project.id, old_branch.name)
|
142
|
-
logger.debug("Deleted old branch")
|
143
|
-
rescue StandardError => e
|
144
|
-
logger.error(e)
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
puts "For any local repositories, use these commands to update:"
|
149
|
-
puts " git fetch `git remote` --prune"
|
150
|
-
puts " git checkout #{options[:new_branch_name]}"
|
151
|
-
puts " git remote set-head `git remote` #{options[:new_branch_name]}"
|
152
|
-
puts " git branch -d #{options[:branch_to_rename]}"
|
153
|
-
logger.close
|