Created
October 22, 2018 15:35
-
-
Save csexton/617bfc207be9cbb333fd08dc1895cc66 to your computer and use it in GitHub Desktop.
Get the Pull Request number from a git branch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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