Skip to content

Instantly share code, notes, and snippets.

@csexton
Created October 22, 2018 15:35
Show Gist options
  • Save csexton/617bfc207be9cbb333fd08dc1895cc66 to your computer and use it in GitHub Desktop.
Save csexton/617bfc207be9cbb333fd08dc1895cc66 to your computer and use it in GitHub Desktop.
Get the Pull Request number from a git branch
# frozen_string_literal: true
require "net/https"
require "uri"
require "json"
def find_pr(org, repo, branch)
uri = URI("https://api.github.com/repos/#{org}/#{repo}/pulls?head=#{org}:#{branch}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
request['Authorization'] = "token #{ENV['GITHUB_ACCESS_TOKEN']}"
response = http.request(request)
json = JSON.parse(response.body)
if json.count == 1
json.first["number"]
else
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment