tcie_migrate_to_apps 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bd0b653bee43e2b70c8fd2351ce7e170367e52e0361ca23c8f0683b7833bdaf6
4
+ data.tar.gz: 654516acb0dbd500c3766b795e4a3605d11982712ca3e858822cd01085696e53
5
+ SHA512:
6
+ metadata.gz: '071843f2b51a9326a90416eda0799e6427470274e598279299c4044f2e6c160c24ff3fb1f9548e60a2baaa75da88c1c4fff15686cae72d0289528575e04c2900'
7
+ data.tar.gz: a4e58b0c6654f21531b0734b6ebadbd3e0dcf0089bb11a1ac9d0f02fb68eb884bb23dae30f535d80711be4a87d92613e19f433d758881676ba005f3c8363b47e
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'rspec'
7
+ gem 'webmock'
8
+ end
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ # MIT LICENSE
2
+
3
+ Copyright (c) 2018 Travis CI <support@travis-ci.com>
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.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Migrate your GitHub organizations to use the Travis CI GitHub App integration
2
+
3
+ This gem will help you migrate the repositories you have on [https://travis-ci.com](travis-ci.com) from legacy [GitHub Services](https://developer.github.com/v3/guides/replacing-github-services/) integration to the new [GitHub Apps](https://developer.github.com/apps/) integration.
4
+
5
+ The main difference is that you will now give Travis CI access to your repositories on a repository basis instead of giving it access to all of them.
6
+
7
+ Hence, we have developped this gem to help you migrate your repositories that are currently active on Travis CI in one sweep rather than having to add them manually one by one in the GitHub UI.
8
+
9
+ Here are the steps:
10
+
11
+ ## 1. Install the gem
12
+ ```
13
+ gem install tcie_migrate_to_apps
14
+ ```
15
+
16
+ ## 2. Generate a GitHub personal access token with repo scope
17
+
18
+ You can generate a new GitHub token [here](https://github.com/settings/tokens/new).
19
+
20
+ Choose the name of your liking and ensure to select the whole `repo` scope as shown below:
21
+
22
+ ![GitHub new token page](https://github.com/travis-ci/travis_migrate_to_apps/blob/assets/github-token-new.png)
23
+
24
+ Then click the "Generate token" button at the bottom to generate the token.
25
+
26
+ You'll then be back on the GitHub token page:
27
+
28
+ ![GitHub token page](https://github.com/travis-ci/travis_migrate_to_apps/blob/assets/github-token-added.png)
29
+
30
+ Take care of copying the newly generated token and save it for later usage.
31
+
32
+ **Note: if you are migrating an organization, the token must be generated by [an owner of the GitHub organization](https://help.github.com/articles/permission-levels-for-an-organization/).**
33
+
34
+ ## 3. Activate the Travis CI GitHub Apps integration with 1 repo
35
+
36
+ Go to your profile page on Travis CI: https://travis-ci.com/profile
37
+
38
+ Click the "Activate GitHub Apps Integration" button highlighted below:
39
+
40
+ ![Activate GitHub Apps Integration button](https://github.com/travis-ci/travis_migrate_to_apps/blob/assets/github-apps-button-on-profile-page.png)
41
+
42
+ You'll directed to the GitHub Apps page for the Travis CI app:
43
+
44
+ ![GitHub Apps page](https://github.com/travis-ci/travis_migrate_to_apps/blob/assets/travis-ci-github-app.png)
45
+
46
+ Choose at least one repository and click the "Approve & Install" button.
47
+
48
+ You'll then be redirected to your profile page on Travis CI and the newly added repository should appear under "GitHub Apps Integration":
49
+
50
+ ![Travis CI profile page](https://github.com/travis-ci/travis_migrate_to_apps/blob/assets/travis-ci-profile-with-github-apps-integration.png)
51
+
52
+ ## 4. Get your Travis CI API token
53
+
54
+ Here are the two ways you can get this token:
55
+
56
+ 1. On your profile page: https://travis-ci.com/profile
57
+
58
+ ![Travis CI token on profile page](https://github.com/travis-ci/travis_migrate_to_apps/blob/assets/travis-ci-token-profile-page.png)
59
+
60
+ 2. Via the [Travis CI client](https://github.com/travis-ci/travis.rb) by running: `travis token --pro`
61
+
62
+ ## 5. Run the gem
63
+
64
+ ```
65
+ tcie_migrate_to_apps [owner_name] [travis_access_token] [github_access_token] [travis api endpoint | https://api.travis-ci.com] [github api endpoint | https://api.github.com]
66
+ ```
67
+
68
+ where
69
+
70
+ - `[owner_name]` is the GitHub account (user or organization) where the repositories you want to migrate are located
71
+ - `[travis_access_token]` is the Travis CI token obtained in step #4 above
72
+ - `[github_access_token]` is the GitHub token obtained in step #2 above
73
+ - `[travis api endpoint]` - Travis CI API endpoint - Enterprise only
74
+ - `[github api endpoint]` - GitHub API endpoint - Enterprise only
75
+
76
+ Happy migration!
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'tcie_migrate_to_apps'
4
+
5
+ TcieMigrateToApps::Cli.new(*ARGV).run
data/lib/colors.rb ADDED
@@ -0,0 +1,15 @@
1
+ module Colors
2
+ COLORS = {
3
+ red: "\e[31m",
4
+ green: "\e[32m",
5
+ yellow: "\e[33m",
6
+ blue: "\e[34m",
7
+ gray: "\e[37m",
8
+ reset: "\e[0m"
9
+ }
10
+
11
+ def colored(color, str)
12
+ return str if color == :none
13
+ [COLORS[color], str, COLORS[:reset]].join
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+ require 'tcie_migrate_to_apps/cli'
@@ -0,0 +1,158 @@
1
+ require 'net/https'
2
+ require 'json'
3
+ require 'colors'
4
+
5
+ module TcieMigrateToApps
6
+ class Cli < Struct.new(:owner_name, :travis_access_token, :github_access_token, :api_endpoint, :github_api_endpoint)
7
+ include Colors
8
+
9
+ USAGE = 'Usage: tcie_migrate_to_apps [owner_name] [travis_access_token] [github_access_token] [travis api endpoint | https://api.travis-ci.com] [github api endpoint | https://api.github.com]'
10
+
11
+ MSGS = {
12
+ start: 'Starting to migrate the account %s to use the Travis CI GitHub App integration.',
13
+ fetch_installation: "Looking up %s's GitHub App installation.",
14
+ fetch_repos: "Looking up %s's active repositories.",
15
+ migrate_repos: 'Starting to migrate %i repositories.',
16
+ migrating_repo: 'Migrating repository %s ... ',
17
+ migrated_repo: 'done.',
18
+ done: 'Done.',
19
+ missing_installation: 'Sorry, we could not find an active installation for %s.',
20
+ missing_repos: 'Sorry, we could not find any repositories to migrate.',
21
+ request_failed: "Sorry, a %s request to %s failed, please check your auth token. (%i: %s)",
22
+ }
23
+
24
+ URIS = {
25
+ travis: {
26
+ installation: '%s/owner/%s?include=owner.installation',
27
+ repositories: '%s/owner/%s/repos?repository.active=true&repository.managed_by_installation=false&limit=%i&offset=%i'
28
+ },
29
+ github: {
30
+ installation_repos: '%s/user/installations/%i/repositories/%i'
31
+ }
32
+ }
33
+
34
+ HEADERS = {
35
+ travis: {
36
+ 'Travis-API-Version' => '3',
37
+ 'User-Agent' => 'Travis GitHub App Migration Tool',
38
+ 'Authorization' => 'token %{token}'
39
+ },
40
+ github: {
41
+ 'Accept' => 'application/vnd.github.machine-man-preview+json',
42
+ 'Authorization' => 'token %{token}'
43
+ }
44
+ }
45
+
46
+ PER_PAGE = 20
47
+
48
+ attr_reader :installation
49
+ attr_reader :api_endpoint
50
+ attr_reader :github_api_endpoint
51
+
52
+ def initialize(*)
53
+ super
54
+ to_h.keys.each do |key|
55
+ if key == :api_endpoint
56
+ @api_endpoint = to_h[key] ? to_h[key] : 'https://api.travis-ci.com'
57
+ elsif key == :github_api_endpoint
58
+ @github_api_endpoint = to_h[key] ? to_h[key] : 'https://api.github.com'
59
+ else
60
+ missing_arg(key) unless send(key)
61
+ end
62
+ end
63
+ end
64
+
65
+ def run
66
+ msg :start, owner_name, color: :yellow
67
+ validate
68
+ migrate_repos
69
+ msg :done, color: :green
70
+ end
71
+
72
+ private
73
+
74
+ def installation
75
+ @installation ||= fetch_installation
76
+ end
77
+
78
+ def repos
79
+ @repos ||= begin
80
+ msg :fetch_repos, owner_name
81
+ fetch_repos
82
+ end
83
+ end
84
+
85
+ def validate
86
+ error :missing_installation, owner_name unless installation
87
+ error :missing_repos unless repos.any?
88
+ end
89
+
90
+ def migrate_repos
91
+ msg :migrate_repos, repos.count
92
+ repos.each { |repo| migrate_repo(repo) }
93
+ end
94
+
95
+ def migrate_repo(repo)
96
+ msg :migrating_repo, repo['name'], nl: false
97
+ uri = uri(:github, :installation_repos, github_api_endpoint, installation['github_id'], repo['github_id'])
98
+ request(:put, uri, headers(:github))
99
+ msg :migrated_repo, repo['name']
100
+ end
101
+
102
+ def fetch_installation
103
+ msg :fetch_installation, owner_name
104
+ uri = uri(:travis, :installation, api_endpoint, owner_name)
105
+ data = request(:get, uri, headers(:travis))
106
+ data['installation']
107
+ end
108
+
109
+ def fetch_repos(repos = [], page = 1)
110
+ offset = (page - 1) * PER_PAGE
111
+ uri = uri(:travis, :repositories, api_endpoint, owner_name, PER_PAGE, offset)
112
+ data = request(:get, uri, headers(:travis))
113
+ repos += data['repositories'].map { |repo| only(repo, 'name', 'github_id') }
114
+ repos = fetch_repos(repos, page + 1) unless data['@pagination']['is_last']
115
+ repos
116
+ end
117
+
118
+ def uri(target, resource, *args)
119
+ URI(URIS[target][resource] % args)
120
+ end
121
+
122
+ def headers(target)
123
+ args = { token: send(:"#{target}_access_token") }
124
+ HEADERS[target].map { |key, value| [key, value % args] }.to_h
125
+ end
126
+
127
+ def request(method, uri, headers)
128
+ req = Net::HTTP.const_get(method.to_s.capitalize).new(uri)
129
+ headers.each { |key, value| req[key] = value }
130
+ http = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true)
131
+ res = http.request(req)
132
+ error :request_failed, method, uri, res.code, res.body unless res.is_a?(Net::HTTPSuccess)
133
+ JSON.parse(res.body) if method == :get
134
+ end
135
+
136
+ def error(key, *args)
137
+ abort colored(:red, MSGS[key] % args)
138
+ end
139
+
140
+ def msg(key, *args)
141
+ opts = args.last.is_a?(Hash) ? args.pop : {}
142
+ msg = MSGS[key] % args
143
+ msg = colored(opts[:color], msg) if opts[:color]
144
+ method = opts[:nl].is_a?(FalseClass) ? :print : :puts
145
+ send(method, msg)
146
+ end
147
+
148
+ def missing_arg(key)
149
+ puts colored(:red, "No #{key} given")
150
+ puts USAGE
151
+ abort
152
+ end
153
+
154
+ def only(hash, *keys)
155
+ hash.select { |key, _| keys.include?(key) }
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,3 @@
1
+ module TcieMigrateToApps
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tcie_migrate_to_apps
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Travis CI
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Migrate your GitHub organizations to use the Travis CI GitHub App integration.
14
+ email:
15
+ - support@travis-ci.com
16
+ executables:
17
+ - tcie_migrate_to_apps
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - Gemfile
22
+ - LICENSE.md
23
+ - README.md
24
+ - bin/tcie_migrate_to_apps
25
+ - lib/colors.rb
26
+ - lib/tcie_migrate_to_apps.rb
27
+ - lib/tcie_migrate_to_apps/cli.rb
28
+ - lib/tcie_migrate_to_apps/version.rb
29
+ homepage: https://github.com/travis-ci/travis_migrate_to_apps
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 3.1.2
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Migrate your GitHub organizations to use the Travis CI GitHub App integration
52
+ test_files: []