LoginSignup
1
0

More than 5 years have passed since last update.

バージョンを+1するpull requestを作る

Posted at

バージョンを+1するだけのpull requestを毎回作っていたが、GitHub APIで自動化できた。

(GitHub API v3)
https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review

Xcodeのプロジェクトでxcconfigを使っているが、他の環境でも同じようなものではあると思う。

Rubyのスクリプトで、以下のようなことをする。

  • 書き換えるファイルはApp.xcconfig (すでにgit管理下にあること)
  • 書き換える内容はAPP_VERSIONをx.x.xにする
  • base_branch(master)からhead_branch(feature/...)を作り、pull request化する
  • 環境変数MY_GITHUB_TOKENにGitHub tokenを設定しておく
  • WindowsなどHTTPS通信に失敗する場合はhttp.verify_mode = OpenSSL::SSL::VERIFY_NONEをコメントアウト
bump_version.rb
#!/usr/bin/env ruby

require 'net/https'
require 'uri'
require 'json'

version = ARGV[0]
if !version || version.empty?
  puts "Please specify version"
  exit
end
unless /^(\d+\.)*\d+$/ =~ version
  puts "Invalid version format"
  exit
end

base_branch = 'master'
head_branch = "feature/bump-version-to-#{version}"
repository = 'username/repository'
version_filename = 'App.xcconfig'
system("git checkout #{base_branch}")
system("git checkout #{version_filename}")
content = open(version_filename, 'r').read.gsub(/APP_VERSION=(.*)/, "APP_VERSION=#{version}")
File.open(version_filename, 'w') { |f| f.write(content) }
diff = `git diff #{version_filename}`
if diff.empty?
  puts "No diffs"
  exit
end
system("git checkout -b #{head_branch}")
system("git add #{version_filename}")
system("git commit -m 'Bump version to #{version}'")
system("git push origin #{head_branch}")

github_token = ENV['MY_GITHUB_TOKEN']
endpoint = "https://api.github.com/repos/#{repository}/pulls"
uri = URI.parse(endpoint)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
#http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req = Net::HTTP::Post.new(uri.request_uri)
req["Authorization"] = "token #{github_token}"
req.body = {'title' => "Bump version to #{version}", 'body' => '', 'head' => head_branch, 'base' => base_branch}.to_json
res = http.request(req)
if res.code.to_i >= 300
  puts "Faied: #{res.code}, #{res.body}"
  exit
end
json = JSON.parse(res.body)
puts "SUCCESS: #{json['url']}"
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0