ci_against 0.1.0

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: 5cdc3e877f07ce26e616f5e198cd59ac0d15e77ad4dff9dd7f24fa62c3996b54
4
+ data.tar.gz: 56467df4050ae3c55950e1e2c11c1c2a2efe572d2576ca08e1c2f3cd73da64b4
5
+ SHA512:
6
+ metadata.gz: e72531c0f800fe288d16e3c180cb1d2147c2a09b690f9519d57c69d142ea2f8c206d34b1f140bffe01c6dc3f5c2ec4727fc3aa32acac9831b7fd7aeef4a6fba9
7
+ data.tar.gz: c7f77ce65571ceef5176970298f81cbed36003ce0715ec31f28a30ec67b71bc9abd5e3625f1a896423c0d2d716e07ed01863b375f05b0152021cf4ecc9f7f0d6
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.4.5
5
+ - 2.5.3
6
+ - 2.6.0
7
+ - ruby-head
8
+
9
+ cache: bundler
10
+
11
+ before_install: gem install bundler
12
+
13
+ matrix:
14
+ allow_failures:
15
+ - rvm: ruby-head
16
+ fast_finish: true
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ci_against.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2018 Masataka Pocke Kuwabara
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # CI Against
2
+
3
+ A CLI tool to Bump Ruby versions in Travis CI automatically.
4
+
5
+ CI Against opens a pull request to bump Ruby version in Travis CI by one command.
6
+
7
+ ## Installation
8
+
9
+ ### Requirements
10
+
11
+ * Ruby 2.4 or higher
12
+
13
+ ### How to install
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'ci_against'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install ci_against
28
+
29
+ ## Usage
30
+
31
+ It needs GitHub's personal access token. First, please get the token from here https://github.com/settings/tokens/new
32
+
33
+ ### Dry Run
34
+
35
+ CI Against has dry-run feature, so I recommend to confirm the content of the pull request before it opens the pull request.
36
+
37
+ ```bash
38
+ $ export GITHUB_ACCESS_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
39
+ $ ci_against --dry-run your_github_name/your_repo_name
40
+ CI against Ruby 2.5.3 and 2.6.0
41
+
42
+ ## Added
43
+
44
+ * 2.5.3
45
+ * 2.6.0
46
+
47
+
48
+ ## Changed
49
+
50
+ * 2.3.4 => 2.3.8
51
+ * 2.4.1 => 2.4.5
52
+ diff --git a/tmp/ci-against20181228-2141-qio4i0 b/tmp/ci-against20181228-2141-1hmqgc4
53
+ index 14298b9..5dd84a7 100644
54
+ --- a/tmp/ci-against20181228-2141-qio4i0
55
+ +++ b/tmp/ci-against20181228-2141-1hmqgc4
56
+ @@ -1,4 +1,6 @@
57
+ language: ruby
58
+ rvm:
59
+ - - "2.3.4"
60
+ - - "2.4.1"
61
+ + - "2.3.8"
62
+ + - "2.4.5"
63
+ + - "2.5.3"
64
+ + - "2.6.0"
65
+ ```
66
+
67
+ If you'd like to update multiple repositories, you can specify them.
68
+
69
+ ```bash
70
+ $ export GITHUB_ACCESS_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
71
+ $ ci_against --dry-run your_github_name/your_repo_name your_github_name/your_cool_repo_name your_github_name/your_awesome_repo_name
72
+ ```
73
+
74
+ ### Apply
75
+
76
+ To apply it, just remove `--dry-run` option.
77
+
78
+ ```bash
79
+ $ export GITHUB_ACCESS_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
80
+ $ ci_against your_github_name/your_repo_name
81
+ PR created: https://github.com/your_github_name/your_repo_name/pull/42
82
+ ```
83
+
84
+
85
+ ## Development
86
+
87
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
88
+
89
+ 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).
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pocke/ci_against.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+ task :default => :test
4
+
5
+ Rake::TestTask.new do |test|
6
+ test.libs << 'test'
7
+ test.test_files = Dir['test/**/test_*.rb']
8
+ test.verbose = true
9
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ci_against"
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 ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,42 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ci_against/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ci_against"
8
+ spec.version = CIAgainst::VERSION
9
+ spec.authors = ["Masataka Pocke Kuwabara"]
10
+ spec.email = ["kuwabara@pocke.me"]
11
+
12
+ spec.summary = %q{}
13
+ spec.description = %q{}
14
+ spec.homepage = "https://github.com/pocke/ci_against"
15
+
16
+ spec.required_ruby_version = '>= 2.4.0'
17
+
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
21
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against " \
24
+ "public gem pushes."
25
+ end
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_development_dependency "bundler", "~> 1.17"
37
+ spec.add_development_dependency "rake", ">= 10.0"
38
+ spec.add_development_dependency "minitest", "~> 5.11.0"
39
+
40
+ spec.add_runtime_dependency 'octokit', '>= 4', '< 5'
41
+ spec.add_runtime_dependency 'psych', '>= 3'
42
+ end
data/exe/ci_against ADDED
@@ -0,0 +1,4 @@
1
+ #!ruby
2
+
3
+ require 'ci_against'
4
+ exit CIAgainst::CLI.new(ARGV).run
data/lib/ci_against.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'optparse'
2
+ require 'psych'
3
+ require 'octokit'
4
+ require 'tempfile'
5
+ require 'securerandom'
6
+
7
+ require "ci_against/version"
8
+ require 'ci_against/converter'
9
+ require 'ci_against/travis_yml'
10
+ require 'ci_against/cli'
11
+ require 'ci_against/runner'
12
+
13
+ module CIAgainst
14
+ end
@@ -0,0 +1,33 @@
1
+ module CIAgainst
2
+ class CLI
3
+ def initialize(argv)
4
+ @argv = argv
5
+ @params = {}
6
+ @args = nil
7
+ end
8
+
9
+ def run
10
+ parse_option
11
+ if @params[:all]
12
+ raise NotImplementedError
13
+ else
14
+ @args.each do |repo|
15
+ Runner.new(repo, github_access_token: ENV.fetch('GITHUB_ACCESS_TOKEN')).run(dry_run: @params[:'dry-run'])
16
+ end
17
+ end
18
+ return 0
19
+ end
20
+
21
+ private
22
+
23
+ def parse_option
24
+ opt = OptionParser.new
25
+ opt.on('-a', '--all')
26
+ opt.on('--dry-run')
27
+
28
+ @args = opt.parse(@argv, into: @params)
29
+ raise "#{@args} is meaningless when --all option is passed" if @params[:all] && !@args.empty?
30
+ raise "Please specify repository names or --all option" if !@params[:all] && @args.empty?
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,101 @@
1
+ module CIAgainst
2
+ class Converter
3
+ LATEST_RUBIES = %w[
4
+ 2.6.0
5
+ 2.5.3
6
+ 2.4.5
7
+ 2.3.8
8
+ 2.2.10
9
+ 2.1.10
10
+ ]
11
+ VERSION_REGEXP = /\d+\.\d+\.\d+/
12
+
13
+ attr_reader :log
14
+
15
+ def initialize(yml_string)
16
+ @yml = TravisYML.new(yml_string)
17
+ @log = {
18
+ changed: [],
19
+ added: [],
20
+ }
21
+ end
22
+
23
+ def convert
24
+ update_to_latest_rubies
25
+ insert_new_rubies
26
+ @yml.code
27
+ end
28
+
29
+ private
30
+
31
+ attr_reader :yml
32
+
33
+ def update_to_latest_rubies
34
+ rvm = find_rvm(yml.tree)
35
+ case rvm
36
+ when Psych::Nodes::Sequence
37
+ return unless rvm.style == Psych::Nodes::Sequence::BLOCK
38
+ rvm.children.each do |child|
39
+ update_to_latest_ruby(child) if child.is_a?(Psych::Nodes::Scalar)
40
+ end
41
+ when Psych::Nodes::Scalar
42
+ update_to_latest_ruby(rvm)
43
+ end
44
+ end
45
+
46
+ def update_to_latest_ruby(scalar)
47
+ version = scalar.value
48
+ return unless version.match?(VERSION_REGEXP)
49
+ latest_version = LATEST_RUBIES.find{|v| v.start_with?(minor_version(version))}
50
+ return unless latest_version
51
+ return if latest_version == version
52
+ return unless scalar.start_line == scalar.end_line
53
+
54
+ yml.replace_line(yml.lines[scalar.start_line].sub(version, latest_version), scalar.start_line)
55
+ log[:changed] << {from: version, to: latest_version}
56
+ end
57
+
58
+ def insert_new_rubies
59
+ rvm = find_rvm(yml.tree)
60
+ case rvm
61
+ when Psych::Nodes::Sequence
62
+ return unless rvm.style == Psych::Nodes::Sequence::BLOCK
63
+ biggest_version_node = rvm
64
+ .children
65
+ .select{|node| node.is_a?(Psych::Nodes::Scalar) && node.value.match?(VERSION_REGEXP)}
66
+ .max_by{|scalar| Gem::Version.new(scalar.value)}
67
+ return unless biggest_version_node
68
+ minor_version = Gem::Version.new(minor_version(biggest_version_node.value))
69
+
70
+ line_base = yml.lines[biggest_version_node.start_line]
71
+ LATEST_RUBIES
72
+ .select{|v| Gem::Version.new(minor_version(v)) > minor_version}
73
+ .reverse
74
+ .each do |v|
75
+ yml.insert_line(line_base.sub(VERSION_REGEXP, v), biggest_version_node.start_line + 1)
76
+ log[:added] << v
77
+ end
78
+ else
79
+ # Do nothing, because if other than sequence is specified as rvm, it cannot insert new version.
80
+ end
81
+ end
82
+
83
+ def find_rvm(stream)
84
+ return unless stream.is_a?(Psych::Nodes::Stream)
85
+ doc = stream.children.first
86
+ return unless doc.is_a?(Psych::Nodes::Document)
87
+ mapping = doc.children.first
88
+ return unless mapping.is_a?(Psych::Nodes::Mapping)
89
+
90
+ mapping.children.each_slice(2) do |key, value|
91
+ return value if key.value == 'rvm'
92
+ end
93
+ return nil
94
+ end
95
+
96
+
97
+ def minor_version(version)
98
+ version.split('.')[0..1].join('.')
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,117 @@
1
+ module CIAgainst
2
+ class Runner
3
+ # repo: owner/repo
4
+ def initialize(repo, github_access_token:)
5
+ @repo = repo
6
+ @octokit = Octokit::Client.new(access_token: github_access_token)
7
+ end
8
+
9
+ def run(dry_run:)
10
+ begin
11
+ content = Base64.decode64(octokit.contents(@repo, path: '.travis.yml').content)
12
+ rescue Octokit::NotFound
13
+ log ".travis.yml does not exist in #{@repo}"
14
+ return
15
+ end
16
+ if exist_file?('.ruby-version')
17
+ log ".ruby-version exists in #{@repo}. So skipped."
18
+ return
19
+ end
20
+ converter = Converter.new(content)
21
+ new_content = converter.convert
22
+ if new_content == content
23
+ log "No difference. Skipped"
24
+ return
25
+ end
26
+
27
+ if dry_run
28
+ display_diff(content, new_content, log: converter.log)
29
+ else
30
+ create_pull_request(new_content, log: converter.log)
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ attr_reader :octokit
37
+
38
+ def display_diff(content, new_content, log:)
39
+ puts title(log)
40
+ puts
41
+ puts description(log)
42
+ # TODO: Do not use git-diff
43
+ Tempfile.open('ci-against') do |f1|
44
+ Tempfile.open('ci-against') do |f2|
45
+ f1.write(content)
46
+ f1.flush
47
+ f2.write(new_content)
48
+ f2.flush
49
+ system('git', 'diff', '--no-index', f1.path, f2.path)
50
+ end
51
+ end
52
+ end
53
+
54
+ def create_pull_request(new_content, log:)
55
+ repo = octokit.repository(@repo)
56
+ base_branch = octokit.branch(@repo, repo.default_branch)
57
+
58
+ blob = octokit.create_blob(@repo, new_content)
59
+ tree = octokit.create_tree(@repo, [{
60
+ path: '.travis.yml',
61
+ mode: '100644',
62
+ type: 'blob',
63
+ sha: blob,
64
+ }], base_tree: base_branch.commit.commit.tree.sha)
65
+ commit = octokit.create_commit(@repo, title(log) + "\n\n" + description(log), tree.sha, base_branch.commit.sha)
66
+
67
+ head_branch_name = "ci-against-#{SecureRandom.hex(6)}"
68
+ octokit.create_ref(@repo, "refs/heads/#{head_branch_name}", commit.sha)
69
+
70
+ pr = octokit.create_pull_request(@repo, base_branch.name, head_branch_name, title(log), description(log) + "\n----\n\n<sub>Opend by [CI Against](https://github.com/pocke/ci_against)</sub>")
71
+ log "PR created: #{pr.html_url}"
72
+ end
73
+
74
+ def title(log)
75
+ if log[:added].empty?
76
+ ruby = log[:changed].size == 1 ? 'Ruby' : 'Rubies'
77
+ "CI against new #{ruby}"
78
+ else
79
+ "CI against Ruby #{log[:added].join(" and ")}"
80
+ end
81
+ end
82
+
83
+ def description(log)
84
+ res = +''
85
+ unless log[:added].empty?
86
+ res << <<~END
87
+ ## Added
88
+
89
+ #{log[:added].map{|x| "* #{x}"}.join("\n")}
90
+ END
91
+ end
92
+
93
+ unless log[:changed].empty?
94
+ res << <<~END
95
+
96
+
97
+ ## Changed
98
+
99
+ #{log[:changed].map{|x| "* #{x[:from]} => #{x[:to]}"}.join("\n")}
100
+ END
101
+ end
102
+ res
103
+ end
104
+
105
+ def exist_file?(path)
106
+ octokit.contents(@repo, path: path)
107
+ true
108
+ rescue Octokit::NotFound
109
+ false
110
+ end
111
+
112
+ # TODO: use logger
113
+ def log(msg)
114
+ puts msg
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,36 @@
1
+ module CIAgainst
2
+ class TravisYML
3
+ def initialize(yaml_string)
4
+ @nleol = yaml_string[-1] == "\n"
5
+ @tree = parse(yaml_string)
6
+ @lines = yaml_string.lines(chomp: true)
7
+ @line_offest = @lines.map{0} << 0
8
+ end
9
+
10
+ attr_reader :tree, :lines
11
+
12
+ def insert_line(line, lineno)
13
+ offset = @line_offest[0..lineno].sum
14
+ @lines = [*lines[0...(lineno+offset)], line, *lines[(lineno+offset)..-1]]
15
+ @line_offest[lineno] += 1
16
+ end
17
+
18
+ def replace_line(line, lineno)
19
+ @lines[lineno] = line
20
+ end
21
+
22
+ def code
23
+ res = @lines.join("\n")
24
+ if @nleol
25
+ res << "\n"
26
+ end
27
+ res
28
+ end
29
+
30
+ private
31
+
32
+ def parse(yaml_string)
33
+ Psych::Parser.new(Psych::TreeBuilder.new).parse(yaml_string).handler.root
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module CIAgainst
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ci_against
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masataka Pocke Kuwabara
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.11.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.11.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: octokit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '4'
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '5'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '4'
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '5'
75
+ - !ruby/object:Gem::Dependency
76
+ name: psych
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '3'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '3'
89
+ description: ''
90
+ email:
91
+ - kuwabara@pocke.me
92
+ executables:
93
+ - ci_against
94
+ extensions: []
95
+ extra_rdoc_files: []
96
+ files:
97
+ - ".gitignore"
98
+ - ".travis.yml"
99
+ - Gemfile
100
+ - LICENSE
101
+ - README.md
102
+ - Rakefile
103
+ - bin/console
104
+ - bin/setup
105
+ - ci_against.gemspec
106
+ - exe/ci_against
107
+ - lib/ci_against.rb
108
+ - lib/ci_against/cli.rb
109
+ - lib/ci_against/converter.rb
110
+ - lib/ci_against/runner.rb
111
+ - lib/ci_against/travis_yml.rb
112
+ - lib/ci_against/version.rb
113
+ homepage: https://github.com/pocke/ci_against
114
+ licenses: []
115
+ metadata:
116
+ homepage_uri: https://github.com/pocke/ci_against
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: 2.4.0
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubygems_version: 3.0.1
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: ''
136
+ test_files: []