gitlab-branch-rename 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f33aa63fa0d74d53308136f89910a11d861daa231ba279f670188e4e832981f3
4
+ data.tar.gz: 8661fecb4807479d6df798baa2ea01f58173a6492dd3c8ffee60df42f953e9fd
5
+ SHA512:
6
+ metadata.gz: 486bd423917b401e3d0a98485a621a0c3a3b7b63606721d1a1c47bf366014eb6e5b5b1079377633d5d91d824a97e985325ca89547c200b44d9f6b0006e628534
7
+ data.tar.gz: 220b75c80baa11007d68b7c3504438b7472ee937fac7b333225b0d51c570a6c067f3c7fcaab2caa0b9a43bebe4307a474ec35516b2d2653a2e4ecbbbac27fb74
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ gitlab-branch-rename-*.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ 2.6@gitlab-branch-rename
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
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.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
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
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
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
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
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.
39
+
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.
45
+
46
+ ## Scope
47
+
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.
54
+
55
+ ## Enforcement
56
+
57
+ 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.
63
+
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.
67
+
68
+ ## Attribution
69
+
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]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+ ruby "~> 2.6"
3
+
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,51 @@
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
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 AP Fritts
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ # Gitlab::Branch::Rename
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.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'gitlab-branch-rename'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install gitlab-branch-rename
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
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.
30
+
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
+
33
+ ## Contributing
34
+
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).
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Code of Conduct
43
+
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).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
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__)
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rename"
4
+
5
+ GitlabBranchRename.new.main
@@ -0,0 +1,31 @@
1
+ require_relative 'lib/version'
2
+
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"]
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"
16
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://gitlab.com/apfritts/gitlab-branch-rename"
20
+
21
+ spec.add_runtime_dependency "gitlab", "~> 4.16"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # 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)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+ end
@@ -0,0 +1,9 @@
1
+ require "version"
2
+
3
+ module GitlabBranchRename
4
+ class Error < StandardError; end
5
+
6
+ def main
7
+ puts "hi"
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module GitlabBranchRename
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,153 @@
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
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitlab-branch-rename
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - AP Fritts
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gitlab
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.16'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ 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'
32
+ email:
33
+ - ap@apfritts.com
34
+ executables:
35
+ - gitlab-branch-rename
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - ".gitignore"
40
+ - ".rspec"
41
+ - ".ruby-version"
42
+ - ".travis.yml"
43
+ - CODE_OF_CONDUCT.md
44
+ - Gemfile
45
+ - Gemfile.lock
46
+ - LICENSE.txt
47
+ - README.md
48
+ - Rakefile
49
+ - bin/console
50
+ - bin/setup
51
+ - exe/gitlab-branch-rename
52
+ - gitlab-branch-rename.gemspec
53
+ - lib/rename.rb
54
+ - lib/version.rb
55
+ - rename.rb
56
+ homepage: https://gitlab.com/apfritts/gitlab-branch-rename
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ homepage_uri: https://gitlab.com/apfritts/gitlab-branch-rename
61
+ source_code_uri: https://gitlab.com/apfritts/gitlab-branch-rename
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.6.0
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 3.0.8
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Tool to rename your Gitlab project branches en-mass
81
+ test_files: []