Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Octokit でプルリクエストを作成する

alt

Octokit でプルリクエストを作成する。
※この記事は個人メモです。既出情報のため、元から知っている人は特に得るものがありません

サンプル

環境変数の取得に dotenv を使っています。
dotenv については以下を参照ください。

qiita.com

Gemfile

source "https://rubygems.org"

gem "dotenv"
gem "octokit"
$ bundle install

コード

OctokitのIFは以下のようになっています。

- (Sawyer::Resource) create_pull_request(repo, base, head, title, body = nil, options = {})

require 'octokit'
require 'dotenv'

Dotenv.load
client = Octokit::Client.new(:access_token => ENV['GITHUB_TOKEN'])
client.create_pull_request("owner/repo", "master", "hoge", "Pull Request title", "Pull Request body")
  • すでに Pull Request を作成済みの場合

以下のようなエラーがでます

422 - Validation Failed (Octokit::UnprocessableEntity)
Error summary:
  resource: PullRequest
  code: custom
  message: A pull request already exists for owner:repo. // See: https://developer.github.com/v3/pulls/#create-a-pull-request
  # 以下略
404 - Not Found // See: https://developer.github.com/v3 (Octokit::NotFound)
  • 存在しないブランチに対して Pull Request をした場合
422 - Validation Failed (Octokit::UnprocessableEntity)
Error summary:
  resource: PullRequest
  field: head
  code: invalid // See: https://developer.github.com/v3/pulls/#create-a-pull-request
  # 以下略

外部資料