gitlab-branch-rename 0.1.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f33aa63fa0d74d53308136f89910a11d861daa231ba279f670188e4e832981f3
4
- data.tar.gz: 8661fecb4807479d6df798baa2ea01f58173a6492dd3c8ffee60df42f953e9fd
3
+ metadata.gz: 9f2589ca09aff738a566db1d7a72d15f8dcb29572fc761e33e2821223af3d2ab
4
+ data.tar.gz: 602a4c2746dc40c862447d50c2f53eded008d37303e89f5d27e395ef3c1f11d7
5
5
  SHA512:
6
- metadata.gz: 486bd423917b401e3d0a98485a621a0c3a3b7b63606721d1a1c47bf366014eb6e5b5b1079377633d5d91d824a97e985325ca89547c200b44d9f6b0006e628534
7
- data.tar.gz: 220b75c80baa11007d68b7c3504438b7472ee937fac7b333225b0d51c570a6c067f3c7fcaab2caa0b9a43bebe4307a474ec35516b2d2653a2e4ecbbbac27fb74
6
+ metadata.gz: 4c84b600439182dd10729fd2ee47c05998ad2f8df2ae2c93d5c63c5bbe777ca8dfa5d72c9c9018349fb89fff2b82d2bd50822c301d33b30eaa120573eebfe3f3
7
+ data.tar.gz: b03da6446520c0e4fa3b81c5353c228d43a488757992c919f5fbc7b090508a8e9eca6f85b22932da0c3175afa39c5595ca84133d5a93c225581a87e8d3e0afc5
data/.gitignore CHANGED
@@ -10,4 +10,5 @@
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
12
 
13
+ Gemfile.lock
13
14
  gitlab-branch-rename-*.gem
@@ -2,73 +2,128 @@
2
2
 
3
3
  ## Our Pledge
4
4
 
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
10
- orientation.
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, religion, or sexual identity
10
+ and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
11
14
 
12
15
  ## Our Standards
13
16
 
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
16
19
 
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the
26
+ overall community
22
27
 
23
- Examples of unacceptable behavior by participants include:
28
+ Examples of unacceptable behavior include:
24
29
 
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
30
+ * The use of sexualized language or imagery, and sexual attention or
31
+ advances of any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
28
33
  * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
34
+ * Publishing others' private information, such as a physical or email
35
+ address, without their explicit permission
31
36
  * Other conduct which could reasonably be considered inappropriate in a
32
37
  professional setting
33
38
 
34
- ## Our Responsibilities
39
+ ## Enforcement Responsibilities
35
40
 
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
39
45
 
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
45
50
 
46
51
  ## Scope
47
52
 
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official e-mail address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
54
58
 
55
59
  ## Enforcement
56
60
 
57
61
  Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at ap@apfritts.com. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
62
+ reported to the community leaders responsible for enforcement via
63
+ [Twitter] or email `ap` at `apfritts` dot `com`.
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series
86
+ of actions.
63
87
 
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or
93
+ permanent ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within
113
+ the community.
67
114
 
68
115
  ## Attribution
69
116
 
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at [https://contributor-covenant.org/version/1/4][version]
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.0, available at
119
+ https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
120
+
121
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct
122
+ enforcement ladder](https://github.com/mozilla/diversity).
123
+
124
+ [homepage]: https://www.contributor-covenant.org
125
+ [Twitter]: https://twitter.com/apfritts
72
126
 
73
- [homepage]: https://contributor-covenant.org
74
- [version]: https://contributor-covenant.org/version/1/4/
127
+ For answers to common questions about this code of conduct, see the FAQ at
128
+ https://www.contributor-covenant.org/faq. Translations are available at
129
+ https://www.contributor-covenant.org/translations.
data/Gemfile CHANGED
@@ -2,6 +2,3 @@ source "https://rubygems.org"
2
2
  ruby "~> 2.6"
3
3
 
4
4
  gemspec
5
-
6
- gem "rake", "~> 12.0"
7
- gem "rspec", "~> 3.0"
@@ -0,0 +1,12 @@
1
+ build:
2
+ gem build ./gitlab-branch-rename.gemspec
3
+
4
+ clean:
5
+ -rm gitlab-branch-rename-*.gem
6
+
7
+ install: build
8
+ gem install ./gitlab-branch-rename-*.gem
9
+
10
+ push: clean build
11
+ gem push gitlab-branch-rename-*.gem
12
+ open https://rubygems.org/gems/gitlab-branch-rename
data/README.md CHANGED
@@ -1,44 +1,73 @@
1
- # Gitlab::Branch::Rename
1
+ # Gitlab Branch Rename
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/gitlab/branch/rename`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ This is a simple tool to rename your Gitlab project branches en masse.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ Other steps you'll need to take on your own:
6
6
 
7
- ## Installation
7
+ * Update your CI configuration that point to old branches
8
+ * Update links in your documentation that point to old branches
8
9
 
9
- Add this line to your application's Gemfile:
10
+ ## Motivation
10
11
 
11
- ```ruby
12
- gem 'gitlab-branch-rename'
13
- ```
12
+ To support Black Lives Matter and create safer spaces, I wanted to rename all of the default branches in my Gitlab
13
+ account. I wasn't able to find any tools to do that, so I wrote this one. If you're unsure why this is important or
14
+ want to better understand why I wanted to make these changes, see this great [Twitter thread].
15
+
16
+ ## Installation
17
+
18
+ This gem is designed to be used from the command line and not currently incorporated into your own projects. If you
19
+ wish to do that, happy to help make that happen.
14
20
 
15
- And then execute:
21
+ Install this with RVM:
16
22
 
17
- $ bundle install
23
+ $ rvm @global do gem install gitlab-branch-rename
18
24
 
19
- Or install it yourself as:
25
+ or install to your system ruby installation:
20
26
 
21
- $ gem install gitlab-branch-rename
27
+ $ sudo gem install gitlab-branch-rename
22
28
 
23
29
  ## Usage
24
30
 
25
- TODO: Write usage instructions here
31
+ ```
32
+ Usage: gitlab-branch-rename --from <branch to rename> --to <new branch name> [options]
33
+
34
+ Specific options:
35
+ --from OLD_BRANCH Branch to rename
36
+ --to NEW_BRANCH New branch name
37
+ --endpoint ENDPOINT Gitlab server endpoint. default: Gitlab cloud
38
+ --token-env-var ENV_VAR_NAME Environment variable to pull token out of
39
+ --only-if-default Rename only if it is the default branch
40
+ --visibility VISIBILITIES Comma-separated list of project visibilities default: public,internal,private
41
+ --skip-confirm Skip confirmation before executing rename
42
+ --pretend Pretend to perform actions. Overrides confirm option
43
+ --logfile LOGFILE Logfile destination. Use - for STDOUT
44
+ ```
26
45
 
27
46
  ## Development
28
47
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
+ To setup your development environment, run `bundle install` and `make` to run tests.
30
49
 
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).
50
+ To release a new version, update the version number in `lib/gitlab_branch_rename/version.rb`, and then run `make push`.
32
51
 
33
- ## Contributing
52
+ During development, I used these references:
53
+ * https://docs.gitlab.com/ce/api/projects.html#edit-project
54
+ * https://www.rubydoc.info/gems/gitlab/Gitlab/Client/Projects
34
55
 
35
- 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).
56
+ ## Contributing
36
57
 
58
+ Bug reports and pull requests are welcome on [Gitlab]. This project is intended to be a safe, welcoming space for
59
+ collaboration, and contributors are expected to adhere to the [code of conduct].
37
60
 
38
61
  ## License
39
62
 
40
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
63
+ The gem is available as open source under the terms of the [MIT License].
41
64
 
42
65
  ## Code of Conduct
43
66
 
44
- Everyone interacting in the Gitlab::Branch::Rename project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/gitlab-branch-rename/blob/master/CODE_OF_CONDUCT.md).
67
+ Everyone interacting in the Gitlab::Branch::Rename project's codebases, issue trackers, chat rooms and mailing lists
68
+ is expected to follow the [code of conduct].
69
+
70
+ [code of conduct]: https://gitlab.com/apfritts/gitlab-branch-rename/blob/trunk/CODE_OF_CONDUCT.md
71
+ [Gitlab]: https://gitlab.com/apfritts/gitlab-branch-rename
72
+ [MIT License]: https://opensource.org/licenses/MIT
73
+ [Twitter thread]: https://twitter.com/mislav/status/1270388510684598272
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "rename"
3
+ require "gitlab_branch_rename/main"
4
4
 
5
- GitlabBranchRename.new.main
5
+ GitlabBranchRename.launch
@@ -1,31 +1,32 @@
1
- require_relative 'lib/version'
1
+ require_relative 'lib/gitlab_branch_rename/version'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
- spec.name = "gitlab-branch-rename"
5
- spec.version = GitlabBranchRename::VERSION
6
- spec.authors = ["AP Fritts"]
7
- spec.email = ["ap@apfritts.com"]
4
+ spec.name = "gitlab-branch-rename"
5
+ spec.version = GitlabBranchRename::VERSION
6
+ spec.authors = ["AP Fritts"]
7
+ spec.email = ["ap@apfritts.com"]
8
8
 
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
- "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" +
13
- "of why I wanted to make these changes, see this Twitter thread: https://twitter.com/mislav/status/1270388510684598272"
14
- spec.homepage = "https://gitlab.com/apfritts/gitlab-branch-rename"
15
- spec.license = "MIT"
9
+ spec.summary = "Tool to rename your Gitlab project branches en masse"
10
+ spec.description = "To support Black Lives Matter and create safer spaces, I wanted to rename all of the default " +
11
+ "branches in my Gitlab account. I wasn't able to find any tools to do that, so I wrote this one. If you're " +
12
+ "unsure why this is important or want to better understand why I wanted to make these changes, see this " +
13
+ "great {Twitter thread}[https://twitter.com/mislav/status/1270388510684598272]."
14
+ spec.homepage = "https://gitlab.com/apfritts/gitlab-branch-rename"
15
+ spec.license = "MIT"
16
16
  spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
17
17
 
18
18
  spec.metadata["homepage_uri"] = spec.homepage
19
19
  spec.metadata["source_code_uri"] = "https://gitlab.com/apfritts/gitlab-branch-rename"
20
20
 
21
+ spec.add_runtime_dependency "colorize", "~> 0.8.1"
21
22
  spec.add_runtime_dependency "gitlab", "~> 4.16"
22
23
 
23
24
  # Specify which files should be added to the gem when it is released.
24
25
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
26
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
27
+ `git ls-files -z`.split("\x0")
27
28
  end
28
- spec.bindir = "exe"
29
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.bindir = "exe"
30
+ spec.executables = "gitlab-branch-rename"
30
31
  spec.require_paths = ["lib"]
31
32
  end
@@ -0,0 +1,90 @@
1
+ require "optparse"
2
+ require "gitlab_branch_rename/version"
3
+
4
+ module GitlabBranchRename
5
+ class Configuration
6
+ attr_accessor :token, :branch_to_rename, :new_branch_name,
7
+ :endpoint, :is_default_branch, :has_visibility, :minimum_access_level,
8
+ :skip_confirm, :pretend, :disable_color, :logfile
9
+
10
+ def initialize
11
+ # Required parameters
12
+ self.branch_to_rename = nil
13
+ self.new_branch_name = nil
14
+
15
+ # Criteria parameters with defaults
16
+ self.endpoint = "https://gitlab.com/api/v4"
17
+ self.token = ENV["GITLAB_ACCESS_TOKEN"]
18
+ self.is_default_branch = false
19
+ self.has_visibility = %w[private internal public]
20
+ self.minimum_access_level = 40
21
+
22
+ # Optional parameters
23
+ self.skip_confirm = false
24
+ self.pretend = false
25
+ self.disable_color = false
26
+ self.logfile = STDOUT
27
+ end
28
+
29
+ def self.configure
30
+ configuration = Configuration.new
31
+ optparse = OptionParser.new do |opts|
32
+ opts.banner = "Usage: gitlab-branch-rename --from <branch to rename> --to <new branch name> [options]"
33
+ opts.separator ""
34
+ opts.separator "Specific options:"
35
+ opts.on("--from OLD_BRANCH", "Branch to rename (required)") do |from|
36
+ configuration.branch_to_rename = from
37
+ end
38
+ opts.on("--to NEW_BRANCH", "New branch name (required)") do |to|
39
+ configuration.new_branch_name = to
40
+ end
41
+ opts.on("--endpoint ENDPOINT", "Gitlab server endpoint, default: https://gitlab.com/api/v4") do |endpoint|
42
+ configuration.endpoint = endpoint
43
+ end
44
+ opts.on("--token-env-var ENV_VAR_NAME", "Environment variable to use for Gitlab API token, default: GITLAB_ACCESS_TOKEN") do |token_env_var|
45
+ configuration.token = ENV[token_env_var]
46
+ end
47
+ opts.on("--only-if-default", "Rename only if the old branch is currently the default project branch") do
48
+ configuration.is_default_branch = true
49
+ end
50
+ opts.on("--visibility VISIBILITIES", "Comma-separated list of project visibilities, default: public,internal,private") do |visibility|
51
+ configuration.has_visibility = visibility.split ","
52
+ end
53
+ opts.on("--skip-confirm", "Skip confirmation before executing rename") do
54
+ configuration.skip_confirm = true
55
+ end
56
+ opts.on("--pretend", "Pretend to perform actions. Overrides --skip-confirm option") do
57
+ configuration.pretend = true
58
+ end
59
+ opts.on("--disable-color", "Disable output colors") do
60
+ configuration.disable_color = true
61
+ end
62
+ opts.on("--logfile LOGFILE", "Logfile destination. Use - for STDOUT") do |logfile|
63
+ if logfile == "-"
64
+ configuration.logfile = STDOUT
65
+ else
66
+ configuration.logfile = logfile
67
+ end
68
+ end
69
+ opts.on("--version", "Print version and exit") do
70
+ puts(GitlabBranchRename::VERSION)
71
+ exit 0
72
+ end
73
+ opts.separator ""
74
+ opts.separator "Visit https://gitlab.com/apfritts/gitlab-branch-rename for more details."
75
+ end
76
+
77
+ begin
78
+ optparse.parse!
79
+ raise OptionParser::MissingArgument.new("old branch name") if configuration.branch_to_rename.nil?
80
+ raise OptionParser::MissingArgument.new("new branch name") if configuration.new_branch_name.nil?
81
+ rescue OptionParser::InvalidOption, OptionParser::MissingArgument
82
+ puts $!.to_s
83
+ puts optparse
84
+ exit 1
85
+ end
86
+
87
+ configuration
88
+ end
89
+ end
90
+ 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,36 @@
1
+ require "colorize"
2
+
3
+ module GitlabBranchRename
4
+ class LoggerConfigure
5
+ def self.configure(configuration)
6
+ logger = Logger.new(STDOUT)
7
+ logger.formatter = proc do |severity, datetime, progname, msg|
8
+ date_format = datetime.to_datetime.strftime("%Q")
9
+ output = nil
10
+ color = :black
11
+ case severity
12
+ when "DEBUG"
13
+ output = "D #{date_format} #{msg}\n"
14
+ color = :light_black
15
+ when "INFO"
16
+ output = "I #{date_format} #{msg}\n"
17
+ color = :black
18
+ when "WARN"
19
+ output = "W #{date_format} #{msg}\n"
20
+ color = :yellow
21
+ when "ERROR"
22
+ output = "E #{date_format} #{msg}\n"
23
+ color = :red
24
+ else
25
+ output = "? #{date_format} #{msg}\n"
26
+ end
27
+ if configuration.disable_color
28
+ output
29
+ else
30
+ output.colorize(color)
31
+ end
32
+ end
33
+ logger
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,35 @@
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.info("")
31
+ logger.info("Or all as one line:")
32
+ logger.info(" git fetch `git remote` --prune && git checkout #{c.new_branch_name} && git remote set-head `git remote` #{c.new_branch_name} && git branch -d #{c.branch_to_rename}")
33
+ logger.close
34
+ end
35
+ 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.nil? ? "without" : "with") + " 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("Error creating new branch: #{e}")
90
+ next
91
+ end
92
+
93
+ # Protect branch
94
+ unless branch_protection_parameters.nil?
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("Error protecting new branch: #{e} with #{branch_protection_parameters}")
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("Error changing default branch: #{e}")
111
+ end
112
+
113
+ # Unprotect protected branches
114
+ if 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("Error unprotecting old branch: #{e}")
120
+ end
121
+ end
122
+
123
+ # Delete old 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("Error deleting old branch: #{e}")
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -1,3 +1,3 @@
1
1
  module GitlabBranchRename
2
- VERSION = "0.1.0"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-branch-rename
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.6.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-25 00:00:00.000000000 Z
11
+ date: 2020-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.1
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: gitlab
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -24,11 +38,11 @@ dependencies:
24
38
  - - "~>"
25
39
  - !ruby/object:Gem::Version
26
40
  version: '4.16'
27
- description: 'To support Black Lives Matter and try to create safer spaces, I wanted
28
- to renameall of my own default branches in Gitlab. I wasn''t able to find any tools
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 understandingof why I wanted to make these changes,
31
- see this Twitter thread: https://twitter.com/mislav/status/1270388510684598272'
41
+ description: To support Black Lives Matter and create safer spaces, I wanted to rename
42
+ all of the default branches in my Gitlab account. I wasn't able to find any tools
43
+ to do that, so I wrote this one. If you're unsure why this is important or want
44
+ to better understand why I wanted to make these changes, see this great {Twitter
45
+ thread}[https://twitter.com/mislav/status/1270388510684598272].
32
46
  email:
33
47
  - ap@apfritts.com
34
48
  executables:
@@ -37,22 +51,20 @@ extensions: []
37
51
  extra_rdoc_files: []
38
52
  files:
39
53
  - ".gitignore"
40
- - ".rspec"
41
54
  - ".ruby-version"
42
- - ".travis.yml"
43
55
  - CODE_OF_CONDUCT.md
44
56
  - Gemfile
45
- - Gemfile.lock
46
57
  - LICENSE.txt
58
+ - Makefile
47
59
  - README.md
48
- - Rakefile
49
- - bin/console
50
- - bin/setup
51
60
  - exe/gitlab-branch-rename
52
61
  - gitlab-branch-rename.gemspec
53
- - lib/rename.rb
54
- - lib/version.rb
55
- - rename.rb
62
+ - lib/gitlab_branch_rename/configuration.rb
63
+ - lib/gitlab_branch_rename/gitlab_configure.rb
64
+ - lib/gitlab_branch_rename/logger_configure.rb
65
+ - lib/gitlab_branch_rename/main.rb
66
+ - lib/gitlab_branch_rename/operation.rb
67
+ - lib/gitlab_branch_rename/version.rb
56
68
  homepage: https://gitlab.com/apfritts/gitlab-branch-rename
57
69
  licenses:
58
70
  - MIT
@@ -77,5 +89,5 @@ requirements: []
77
89
  rubygems_version: 3.0.8
78
90
  signing_key:
79
91
  specification_version: 4
80
- summary: Tool to rename your Gitlab project branches en-mass
92
+ summary: Tool to rename your Gitlab project branches en masse
81
93
  test_files: []
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
@@ -1,6 +0,0 @@
1
- ---
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.7.0
6
- before_install: gem install bundler -v 2.1.2
@@ -1,51 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- gitlab-branch-rename (0.1.0)
5
- gitlab (~> 4.16)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- diff-lcs (1.4.4)
11
- gitlab (4.16.1)
12
- httparty (~> 0.14, >= 0.14.0)
13
- terminal-table (~> 1.5, >= 1.5.1)
14
- httparty (0.18.1)
15
- mime-types (~> 3.0)
16
- multi_xml (>= 0.5.2)
17
- mime-types (3.3.1)
18
- mime-types-data (~> 3.2015)
19
- mime-types-data (3.2020.0512)
20
- multi_xml (0.6.0)
21
- rake (12.3.3)
22
- rspec (3.9.0)
23
- rspec-core (~> 3.9.0)
24
- rspec-expectations (~> 3.9.0)
25
- rspec-mocks (~> 3.9.0)
26
- rspec-core (3.9.2)
27
- rspec-support (~> 3.9.3)
28
- rspec-expectations (3.9.2)
29
- diff-lcs (>= 1.2.0, < 2.0)
30
- rspec-support (~> 3.9.0)
31
- rspec-mocks (3.9.1)
32
- diff-lcs (>= 1.2.0, < 2.0)
33
- rspec-support (~> 3.9.0)
34
- rspec-support (3.9.3)
35
- terminal-table (1.8.0)
36
- unicode-display_width (~> 1.1, >= 1.1.1)
37
- unicode-display_width (1.7.0)
38
-
39
- PLATFORMS
40
- ruby
41
-
42
- DEPENDENCIES
43
- gitlab-branch-rename!
44
- rake (~> 12.0)
45
- rspec (~> 3.0)
46
-
47
- RUBY VERSION
48
- ruby 2.7.0p0
49
-
50
- BUNDLED WITH
51
- 2.1.4
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "rename"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,6 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
@@ -1,9 +0,0 @@
1
- require "version"
2
-
3
- module GitlabBranchRename
4
- class Error < StandardError; end
5
-
6
- def main
7
- puts "hi"
8
- end
9
- end
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