travis_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
+ SHA1:
3
+ metadata.gz: f62ca1e281bce57365c17ea8826871ba7cbf80fa
4
+ data.tar.gz: bf406955dd183c6e2ea7e3b5758a31af43964040
5
+ SHA512:
6
+ metadata.gz: e1bda52e5041df03d4a7885cdd1ab1e0e25afe9c439293f356a15e03bd4c1c12030e3e0cc4ecf6fdcf3c7511676307037fb885c8d7ff7ebbc22ad40f261a53bd
7
+ data.tar.gz: '09364eab42385c7bb39e772fa6a2ce468c787aee39eaeffd1cfbe5282bc0fbf6e37edf3f33c102569deb79da0f55d520f1baa02fc1db09b11e1ad5ed203b8720'
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/Gemfile.lock ADDED
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ travis_migrate_to_apps (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.5.2)
10
+ public_suffix (>= 2.0.2, < 4.0)
11
+ crack (0.4.3)
12
+ safe_yaml (~> 1.0.0)
13
+ diff-lcs (1.3)
14
+ hashdiff (0.3.7)
15
+ public_suffix (3.0.2)
16
+ rspec (3.7.0)
17
+ rspec-core (~> 3.7.0)
18
+ rspec-expectations (~> 3.7.0)
19
+ rspec-mocks (~> 3.7.0)
20
+ rspec-core (3.7.1)
21
+ rspec-support (~> 3.7.0)
22
+ rspec-expectations (3.7.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.7.0)
25
+ rspec-mocks (3.7.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.7.0)
28
+ rspec-support (3.7.1)
29
+ safe_yaml (1.0.4)
30
+ webmock (3.3.0)
31
+ addressable (>= 2.3.6)
32
+ crack (>= 0.3.2)
33
+ hashdiff
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ rspec
40
+ travis_migrate_to_apps!
41
+ webmock
42
+
43
+ BUNDLED WITH
44
+ 1.16.1
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.
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'travis_migrate_to_apps'
4
+
5
+ TravisMigrateToApps.new(*ARGV)
@@ -0,0 +1,3 @@
1
+ module TravisMigrateToApps
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,127 @@
1
+ require 'net/https'
2
+ require 'json'
3
+
4
+ class TravisMigrateToApps < Struct.new(:owner_name, :travis_access_token, :github_access_token)
5
+ USAGE = 'Usage: travis_migrate_to_apps [owner_name] [travis_access_token] [github_access_token]'
6
+
7
+ MSGS = {
8
+ start: 'Starting to migrate %s to use the Travis CI GitHub App integration',
9
+ migrate_repos: 'Starting to migrate %i repositories',
10
+ migrated_repo: 'Migrated repository %s',
11
+ done: 'Done.',
12
+ missing_installation: 'Sorry but we could not find an active installation for %s',
13
+ missing_repos: 'Sorry but we could not find any repositories to migrate',
14
+ request_failed: "Sorry but a request %s failed, please check your auth token. (%i: %s)",
15
+ }
16
+
17
+ URIS = {
18
+ travis: {
19
+ installation: 'https://api.travis-ci.com/owner/%s?include=owner.installation',
20
+ repositories: 'https://api.travis-ci.com/owner/%s/repos?repository.active=true&repository.managed_by_installation=false&limit=%i&offset=%i'
21
+ },
22
+ github: {
23
+ installation_repos: 'https://api.github.com/user/installations/%i/repositories/%i'
24
+ }
25
+ }
26
+
27
+ HEADERS = {
28
+ travis: {
29
+ 'Travis-API-Version' => '3',
30
+ 'User-Agent' => 'Travis GitHub App Migration Tool',
31
+ 'Authorization' => 'token %{token}'
32
+ },
33
+ github: {
34
+ 'Accept' => 'application/vnd.github.machine-man-preview+json',
35
+ 'Authorization' => 'token %{token}'
36
+ }
37
+ }
38
+
39
+ PER_PAGE = 20
40
+
41
+ attr_reader :installation
42
+
43
+ def initialize(*)
44
+ super
45
+ to_h.keys.each do |key|
46
+ abort "#{USAGE}\nNo #{key} given" unless send(key)
47
+ end
48
+ end
49
+
50
+ def run
51
+ msg :start, owner_name
52
+ validate
53
+ migrate_repos
54
+ msg :done
55
+ end
56
+
57
+ private
58
+
59
+ def installation
60
+ @installation ||= fetch_installation
61
+ end
62
+
63
+ def repos
64
+ @repos ||= fetch_repos
65
+ end
66
+
67
+ def validate
68
+ error :missing_installation, owner_name unless installation
69
+ error :missing_repos unless repos.any?
70
+ end
71
+
72
+ def migrate_repos
73
+ msg :migrate_repos, repos.count
74
+ repos.each { |repo| migrate_repo(repo) }
75
+ end
76
+
77
+ def migrate_repo(repo)
78
+ uri = uri(:github, :installation_repos, repo['github_id'], installation['github_id'])
79
+ request(:put, uri, headers(:github))
80
+ msg :migrated_repo, repo['name']
81
+ end
82
+
83
+ def fetch_installation
84
+ uri = uri(:travis, :installation, owner_name)
85
+ data = request(:get, uri, headers(:travis))
86
+ data['installation']
87
+ end
88
+
89
+ def fetch_repos(repos = [], page = 1)
90
+ offset = (page - 1) * PER_PAGE
91
+ uri = uri(:travis, :repositories, owner_name, PER_PAGE, offset)
92
+ data = request(:get, uri, headers(:travis))
93
+ repos += data['repositories'].map { |repo| only(repo, 'name', 'github_id') }
94
+ fetch_repos(repos, page + 1) unless data['@pagination']['is_last']
95
+ repos
96
+ end
97
+
98
+ def uri(target, resource, *args)
99
+ URI(URIS[target][resource] % args)
100
+ end
101
+
102
+ def headers(target)
103
+ args = { token: send(:"#{target}_access_token") }
104
+ HEADERS[target].map { |key, value| [key, value % args] }.to_h
105
+ end
106
+
107
+ def request(method, uri, headers)
108
+ req = Net::HTTP.const_get(method.to_s.capitalize).new(uri)
109
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
110
+ http.request(req)
111
+ end
112
+ error :request_failed, uri, res.code, res.body unless res.is_a?(Net::HTTPSuccess)
113
+ JSON.parse(res.body) if method == :get
114
+ end
115
+
116
+ def error(key, *args)
117
+ abort MSGS[key] % args
118
+ end
119
+
120
+ def msg(key, *args)
121
+ puts MSGS[key] % args
122
+ end
123
+
124
+ def only(hash, *keys)
125
+ hash.select { |key, _| keys.include?(key) }
126
+ end
127
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: travis_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: 2018-04-28 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
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Gemfile
21
+ - Gemfile.lock
22
+ - LICENSE.md
23
+ - bin/travis_migrate_to_apps
24
+ - lib/travis_migrate_to_apps.rb
25
+ - lib/travis_migrate_to_apps/version.rb
26
+ homepage: https://github.com/travis-ci/travis_migrate_to_apps
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.6.13
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Migrate your GitHub organizations to use the Travis CI GitHub App integration
50
+ test_files: []