Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

バージョンを+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']}"
hotpepsi
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away