ruboty-bundler 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 291a77afd6dfc021d4027d0a21172d8af03c476c
4
+ data.tar.gz: 719314d2150d0ab3e52f144a9928cdcf79f8fea3
5
+ SHA512:
6
+ metadata.gz: bc3bca104b808d49addfcdbc8ef21eaf1fba2991849b3023ae2ba08e1c5664fbbbc20ceb6b588f9b54bf1f89620669d4f7a3d0573a7ba61297467ecb390ee271
7
+ data.tar.gz: 72fef39cb97984922af7572361d7cbaad6566fdcf17d2e033281094b68bcbf891a47245dfdf55f9b106522df2de3d897b663fdf58f744dce7ac35ac4e5967cb4
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruboty-bundler.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ryo Nakamura
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,21 @@
1
+ # Ruboty::Bundler
2
+ [Ruboty](https://github.com/r7kamura/ruboty) plug-in to manage Gemfile on GitHub repository.
3
+
4
+ ## Installation
5
+ ```rb
6
+ gem "ruboty-bundler"
7
+ ```
8
+
9
+ ## Example
10
+ ```
11
+ @ruboty add gem ruboty-cron
12
+ @ruboty add gem ruboty-cron >= 0.0.2
13
+ @ruboty delete gem ruboty-cron
14
+ ```
15
+
16
+ See [ruboty-github](https://github.com/r7kamura/ruboty-github) other commands.
17
+
18
+ ## ENV
19
+ ```
20
+ RUBOTY_BUNDLER_REPOSITORY - Target repository name (e.g. r7kamura/kokodeikku_bot)
21
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruboty/bundler"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ module Ruboty
2
+ module Bundler
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require "ruboty/bundler/version"
2
+ require "ruboty/handlers/bundler"
@@ -0,0 +1,213 @@
1
+ require "base64"
2
+ require "ruboty"
3
+ require "ruboty/github"
4
+ require "tempfile"
5
+ require "tmpdir"
6
+
7
+ module Ruboty
8
+ module Handlers
9
+ class Bundler < Base
10
+ DEFAULT_GIT_REPOSITORY_PATH = "/tmp/app"
11
+ DEFAULT_GITHUB_HOST = "github.com"
12
+ NAMESPACE = Ruboty::Github::Actions::Base::NAMESPACE
13
+
14
+ env :RUBOTY_BUNDLER_REPOSITORY, "Target repository name (e.g. r7kamura/kokodeikku_bot)"
15
+
16
+ on(
17
+ /add gem (?<gem_name>\S+)(?: (?<version>.+))?/,
18
+ description: "Add gem",
19
+ name: :add,
20
+ )
21
+
22
+ on(
23
+ /delete gem (?<gem_name>.+)/,
24
+ description: "Delete gem",
25
+ name: :delete,
26
+ )
27
+
28
+ def add(message)
29
+ if client = client_for(message.from_name)
30
+ message.reply("Bundler started")
31
+ response = client.contents(repository, path: "Gemfile")
32
+ gemfile_content = Base64.decode64(response[:content])
33
+ gemfile_lock_content = Base64.decode64(client.contents(repository, path: "Gemfile.lock")[:content])
34
+ gems = Gems.new(gemfile: gemfile_content, gemfile_lock: gemfile_lock_content)
35
+ gems.add(message[:gem_name], version: message[:version])
36
+ gemfile_content = gems.to_s
37
+ gemfile_sha = response[:sha]
38
+ gemfile_lock_content = Install.new(gemfile_content).call
39
+ gemfile_lock_sha = client.contents(repository, path: "Gemfile.lock")[:sha]
40
+ commit_message = "Add #{message[:gem_name]} gem"
41
+ client.update_contents(
42
+ repository,
43
+ "Gemfile",
44
+ commit_message,
45
+ gemfile_sha,
46
+ gemfile_content,
47
+ )
48
+ client.update_contents(
49
+ repository,
50
+ "Gemfile.lock",
51
+ commit_message,
52
+ gemfile_lock_sha,
53
+ gemfile_lock_content,
54
+ )
55
+ message.reply("Bundler finished")
56
+ else
57
+ message.reply("I don't know your GitHub access token")
58
+ end
59
+ rescue Errno::ENOENT
60
+ message.reply("Failed to add gem")
61
+ end
62
+
63
+ # TODO
64
+ def delete(message)
65
+ if client = client_for(message.from_name)
66
+ else
67
+ message.reply("I don't know your GitHub access token")
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ # @return [Hash]
74
+ def access_tokens_table
75
+ robot.brain.data[NAMESPACE] ||= {}
76
+ end
77
+
78
+ def api_endpoint
79
+ "https://#{github_host}/api/v3" if github_host
80
+ end
81
+
82
+ # @param username [String, nil]
83
+ # @return [Octokit::Client, nil]
84
+ def client_for(username)
85
+ if access_token = access_tokens_table[username]
86
+ Octokit::Client.new(
87
+ {
88
+ access_token: access_token,
89
+ api_endpoint: api_endpoint,
90
+ web_endpoint: web_endpoint,
91
+ }.reject do |key, value|
92
+ value.nil?
93
+ end
94
+ )
95
+ end
96
+ end
97
+
98
+ def git_repository_path
99
+ DEFAULT_GIT_REPOSITORY_PATH
100
+ end
101
+
102
+ def git_repository_url
103
+ web_endpoint + ENV["RUBOTY_BUNDLER_REPOSITORY"]
104
+ end
105
+
106
+ def github_host
107
+ ENV["GITHUB_HOST"]
108
+ end
109
+
110
+ def repository
111
+ ENV["RUBOTY_BUNDLER_REPOSITORY"]
112
+ end
113
+
114
+ def web_endpoint
115
+ "https://#{github_host || DEFAULT_GITHUB_HOST}/"
116
+ end
117
+
118
+ class DependencyView
119
+ def initialize(dependency)
120
+ @dependency = dependency
121
+ end
122
+
123
+ def to_s
124
+ str = "gem #{arguments.join(', ')}"
125
+ end
126
+
127
+ private
128
+
129
+ def arguments
130
+ [gem_name, requirement].compact.map(&:inspect)
131
+ end
132
+
133
+ def gem_name
134
+ @dependency.name
135
+ end
136
+
137
+ def requirement
138
+ if @dependency.requirement.to_s != ">= 0"
139
+ @dependency.requirement.to_s
140
+ end
141
+ end
142
+ end
143
+
144
+ class Gems
145
+ # @param gemfile [String] Content of Gemfile
146
+ # @param gemfile_lcok [String] Content of Gemfile.lock
147
+ def initialize(gemfile: nil, gemfile_lock: nil)
148
+ @gemfile = gemfile
149
+ @gemfile_lock = gemfile_lock
150
+ end
151
+
152
+ def add(gem_name, version: nil)
153
+ dependencies.reject! do |dependency|
154
+ dependency.name == gem_name
155
+ end
156
+ dependencies << ::Bundler::Dependency.new(gem_name, version || ">= 0")
157
+ end
158
+
159
+ # @return [String] Valid Gemfile content
160
+ def to_s
161
+ %<source "https://rubygems.org"\n\n> +
162
+ dependencies.map do |dependency|
163
+ DependencyView.new(dependency).to_s
164
+ end.join("\n")
165
+ end
166
+
167
+ private
168
+
169
+ def dependencies
170
+ @dependencies ||= ::Bundler::Dsl.evaluate(
171
+ gemfile_file.path,
172
+ gemfile_lock_file.path,
173
+ {},
174
+ ).dependencies
175
+ end
176
+
177
+ def gemfile_file
178
+ @gemfile_file ||= Tempfile.new("Gemfile").tap do |file|
179
+ file.write(@gemfile)
180
+ file.flush
181
+ end
182
+ end
183
+
184
+ def gemfile_lock_file
185
+ @gemfile_lock_file ||= Tempfile.new("Gemfile.lock").tap do |file|
186
+ file.write(@gemfile_lock)
187
+ file.flush
188
+ end
189
+ end
190
+ end
191
+
192
+ class Install
193
+ # @param gemfile_content [String] Content of Gemfile.
194
+ def initialize(gemfile_content)
195
+ @gemfile_content = gemfile_content
196
+ end
197
+
198
+ # @return [String] Content of Gemfile.lock.
199
+ def call
200
+ Dir.mktmpdir do |dir|
201
+ Dir.chdir(dir) do
202
+ File.write("Gemfile", @gemfile_content)
203
+ ::Bundler.with_clean_env do
204
+ Ruboty.logger.debug(`bundle install`)
205
+ end
206
+ File.read("Gemfile.lock")
207
+ end
208
+ end
209
+ end
210
+ end
211
+ end
212
+ end
213
+ end
@@ -0,0 +1,20 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "ruboty/bundler/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ruboty-bundler"
7
+ spec.version = Ruboty::Bundler::VERSION
8
+ spec.authors = ["Ryo Nakamura"]
9
+ spec.email = ["r7kamura@gmail.com"]
10
+ spec.summary = "Ruboty plug-in to manage Gemfile on GitHub repository"
11
+ spec.homepage = "https://github.com/r7kamura/ruboty"
12
+ spec.license = "MIT"
13
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
14
+ spec.require_paths = ["lib"]
15
+
16
+ spec.add_dependency "ruboty", ">= 1.2.1"
17
+ spec.add_dependency "ruboty-github"
18
+ spec.add_development_dependency "bundler", "~> 1.8"
19
+ spec.add_development_dependency "rake", "~> 10.0"
20
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruboty-bundler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryo Nakamura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruboty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruboty-github
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description:
70
+ email:
71
+ - r7kamura@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - lib/ruboty/bundler.rb
84
+ - lib/ruboty/bundler/version.rb
85
+ - lib/ruboty/handlers/bundler.rb
86
+ - ruboty-bundler.gemspec
87
+ homepage: https://github.com/r7kamura/ruboty
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.4.5
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Ruboty plug-in to manage Gemfile on GitHub repository
111
+ test_files: []
112
+ has_rdoc: