Class: Tachikoma::Application

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/tachikoma/application.rb

Overview

Main logic of Tachikoma

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(strategy) ⇒ Object



12
13
14
# File 'lib/tachikoma/application.rb', line 12

def self.run(strategy)
  new.run(strategy)
end

Instance Method Details

#authorized_base_url_with_type(fetch_url, type, github_token, github_account) ⇒ Object



178
179
180
181
182
183
184
185
186
# File 'lib/tachikoma/application.rb', line 178

def authorized_base_url_with_type(fetch_url, type, github_token, )
  uri = URI.parse(fetch_url)
  case type
  when 'fork', 'shared'
    "#{uri.scheme}://#{github_token}:x-oauth-basic@#{uri.host}#{uri.path}"
  else
    fail InvalidType, "Invalid type #{type}"
  end
end

#authorized_compare_url_with_type(fetch_url, type, github_token, github_account) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/tachikoma/application.rb', line 166

def authorized_compare_url_with_type(fetch_url, type, github_token, )
  uri = URI.parse(fetch_url)
  case type
  when 'fork'
    %Q(#{uri.scheme}://#{github_token}:x-oauth-basic@#{uri.host}#{path_for_fork(uri.path, )})
  when 'shared'
    "#{uri.scheme}://#{github_token}:x-oauth-basic@#{uri.host}#{uri.path}"
  else
    fail InvalidType, "Invalid type #{type}"
  end
end

#bundleObject



86
87
88
89
# File 'lib/tachikoma/application.rb', line 86

def bundle
  warn '[DEPRECATION] `bundle` is deprecated. Please use `bundler` instead.'
  bundler
end

#bundlerObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/tachikoma/application.rb', line 70

def bundler
  Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
    Bundler.with_clean_env do
      sh %Q(ruby -i -pe '$_.gsub! /^ruby/, "#ruby"' Gemfile)
      sh "git config user.name #{@commiter_name}"
      sh "git config user.email #{@commiter_email}"
      sh "git checkout -b tachikoma/update-#{@readable_time} #{@base_remote_branch}"
      sh "bundle --gemfile Gemfile --no-deployment --without nothing --path vendor/bundle #{@parallel_option}"
      sh 'bundle update'
      sh 'git add Gemfile.lock'
      sh %Q(git commit -m "Bundle update #{@readable_time}") do; end # ignore exitstatus
      sh "git push #{@authorized_compare_url} tachikoma/update-#{@readable_time}"
    end
  end
end

#bundler_parallel_option(bundler_version, parallel_number) ⇒ Object



209
210
211
212
213
214
# File 'lib/tachikoma/application.rb', line 209

def bundler_parallel_option(bundler_version, parallel_number)
  # bundler 1.4.0.pre.1 gets parallel number option
  if Gem::Version.create(bundler_version) >= Gem::Version.create('1.4.0.pre.1') && parallel_number > 1
    "--jobs=#{parallel_number}"
  end
end

#cartonObject



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/tachikoma/application.rb', line 91

def carton
  Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
    sh "git config user.name #{@commiter_name}"
    sh "git config user.email #{@commiter_email}"
    sh "git checkout -b tachikoma/update-#{@readable_time} #{@base_remote_branch}"
    sh 'carton install'
    sh 'carton update'
    sh 'git add carton.lock' if File.exist?('carton.lock')
    sh 'git add cpanfile.snapshot' if File.exist?('cpanfile.snapshot')
    sh %Q(git commit -m "Carton update #{@readable_time}") do; end # ignore exitstatus
    sh "git push #{@authorized_compare_url} tachikoma/update-#{@readable_time}"
  end
end

#cleanObject



60
61
62
63
# File 'lib/tachikoma/application.rb', line 60

def clean
  mkdir_p(Tachikoma.repos_path)
  rm_rf(Dir.glob(File.join(Tachikoma.repos_path, '*')))
end

#cocoapodsObject



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/tachikoma/application.rb', line 142

def cocoapods
  Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
    sh "git config user.name #{@commiter_name}"
    sh "git config user.email #{@commiter_email}"
    sh "git checkout -b tachikoma/update-#{@readable_time} #{@base_remote_branch}"
    sh 'pods install'
    sh 'pods update'
    sh 'git add Podfile.lock'
    sh %Q(git commit -m "Cocoapods update #{@readable_time}") do; end # ignore exitstatus
    sh "git push #{@authorized_compare_url} tachikoma/update-#{@readable_time}"
  end
end

#composerObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/tachikoma/application.rb', line 127

def composer
  Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
    sh "git config user.name #{@commiter_name}"
    sh "git config user.email #{@commiter_email}"
    sh "git checkout -b tachikoma/update-#{@readable_time} #{@base_remote_branch}"
    # FIXME: Use Octokit.api_endpoint for GitHub Enterprise
    sh "composer config github-oauth.github.com #{@github_token}"
    sh 'composer install --no-interaction'
    sh 'composer update --no-interaction'
    sh 'git add composer.lock'
    sh %Q(git commit -m "Composer update #{@readable_time}") do; end # ignore exitstatus
    sh "git push #{@authorized_compare_url} tachikoma/update-#{@readable_time}"
  end
end

#davidObject



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/tachikoma/application.rb', line 115

def david
  Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
    sh "git config user.name #{@commiter_name}"
    sh "git config user.email #{@commiter_email}"
    sh "git checkout -b tachikoma/update-#{@readable_time} #{@base_remote_branch}"
    sh 'david update --warn404'
    sh 'git add package.json'
    sh %Q(git commit -m "David update #{@readable_time}") do; end # ignore exitstatus
    sh "git push #{@authorized_compare_url} tachikoma/update-#{@readable_time}"
  end
end

#fetchObject



65
66
67
68
# File 'lib/tachikoma/application.rb', line 65

def fetch
  clean
  sh "git clone #{@authorized_base_url} #{Tachikoma.repos_path}/#{@build_for}"
end

#github_token_key(build_for) ⇒ Object

build_for = fenix-knight, github_token_key = TOKEN_FENIX_KNIGHT



162
163
164
# File 'lib/tachikoma/application.rb', line 162

def github_token_key(build_for)
  "TOKEN_#{build_for}".gsub(/-/, '_').upcase
end

#loadObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tachikoma/application.rb', line 23

def load
  @build_for = ENV['BUILD_FOR']
  @github_token = ENV[github_token_key(@build_for)]

  base_config_path = File.join(Tachikoma.original_data_path, 'default.yaml')
  base_config = YAML.safe_load_file(base_config_path) || {}
  user_config_path = File.join(Tachikoma.data_path, '__user_config__.yaml')
  user_config = YAML.safe_load_file(user_config_path) if File.exist?(user_config_path)
  user_config ||= {}
  each_config_path = File.join(Tachikoma.data_path, "#{@build_for}.yaml")
  each_config = YAML.safe_load_file(each_config_path) if File.exist?(each_config_path)
  unless each_config
    fail %Q(Something wrong, BUILD_FOR: #{@build_for}, your config_path: #{each_config_path})
  end

  @configure = base_config.merge(user_config).merge(each_config)

  @commiter_name = @configure['commiter_name']
  @commiter_email = @configure['commiter_email']
  @github_account = @configure['github_account']
  @url = @configure['url']
  @type = @configure['type']
  @base_remote_branch = @configure['base_remote_branch']
  @authorized_compare_url = authorized_compare_url_with_type(@url, @type, @github_token, @github_account)
  @authorized_base_url = authorized_base_url_with_type(@url, @type, @github_token, @github_account)
  @timestamp_format = @configure['timestamp_format']
  @readable_time = Time.now.utc.strftime(@timestamp_format)
  @parallel_option = bundler_parallel_option(Bundler::VERSION, @configure['bundler_parallel_number'])

  @target_head = target_repository_user(@type, @url, @github_account)
  @pull_request_url = repository_identity(@url)
  @pull_request_body = @configure['pull_request_body']
  @pull_request_base = @configure['pull_request_base']
  @pull_request_head = "#{@target_head}:tachikoma/update-#{@readable_time}"
  @pull_request_title = "Exec tachikoma update #{@readable_time}"
end

#noneObject



105
106
107
108
109
110
111
112
113
# File 'lib/tachikoma/application.rb', line 105

def none
  Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
    sh "git config user.name #{@commiter_name}"
    sh "git config user.email #{@commiter_email}"
    sh "git checkout -b tachikoma/update-#{@readable_time} #{@base_remote_branch}"
    sh %Q(git commit --allow-empty -m "None update #{@readable_time}") do; end # ignore exitstatus
    sh "git push #{@authorized_compare_url} tachikoma/update-#{@readable_time}"
  end
end

#path_for_fork(path, github_account) ⇒ Object



188
189
190
# File 'lib/tachikoma/application.rb', line 188

def path_for_fork(path, )
  path.sub(%r{^/[^/]+}) { '/' +  }
end

#pull_requestObject



155
156
157
158
159
# File 'lib/tachikoma/application.rb', line 155

def pull_request
  @client = Octokit::Client.new access_token: @github_token
  @client.create_pull_request(@pull_request_url, @pull_request_base, @pull_request_head, @pull_request_title, @pull_request_body)
rescue Octokit::UnprocessableEntity
end

#repository_identity(url) ⇒ Object



203
204
205
206
207
# File 'lib/tachikoma/application.rb', line 203

def repository_identity(url)
  project_name, user_name, _ = url.split('/').reverse
  project_name_identity = project_name.split('.git').first
  user_name + '/' + project_name_identity
end

#run(strategy) ⇒ Object



16
17
18
19
20
21
# File 'lib/tachikoma/application.rb', line 16

def run(strategy)
  load
  fetch
  send(strategy) if respond_to?(strategy)
  pull_request
end

#target_repository_user(type, fetch_url, github_account) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/tachikoma/application.rb', line 192

def target_repository_user(type, fetch_url, )
  case type
  when 'fork'
    
  when 'shared'
    URI.parse(fetch_url).path.split('/', 3)[1]
  else
    fail InvalidType, "Invalid type: #{type}"
  end
end