Skip to content

Instantly share code, notes, and snippets.

@raggleton
Created November 12, 2018 15:51
Show Gist options
  • Save raggleton/108dbfe1dae4ed88ac6a1298aeb143f0 to your computer and use it in GitHub Desktop.
Save raggleton/108dbfe1dae4ed88ac6a1298aeb143f0 to your computer and use it in GitHub Desktop.
How to get github PR unique ID using python2/3
#!/usr/bin/env python
"""
Get the Github Pull Request unique ID.
Needed for posting comments, etc
"""
from __future__ import print_function
import json
# python2/3 compatibile code
try:
# python3
from urllib.request import Request
from urllib.request import urlopen
except ImportError:
# python2
from urllib2 import Request
from urllib2 import urlopen
query = """
{
repository(
owner: "raggleton",
name: "CMSSW-CI"
) {
pullRequest(number: 1) {
id
}
}
}
"""
headers = {'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization' : 'bearer SECRET'}
# if using requests library
# result = requests.post("https://api.github.com/graphql",
# json={'query': query},
# headers=headers)
# result_dict = result.json()
# print(result_dict)
# print(type(result_dict))
# if using urllib(2)
blob = json.dumps(dict(query = query.strip())).encode('utf-8')
this_request = Request("https://api.github.com/graphql", blob, headers)
response = urlopen(this_request)
data = json.loads(response.read().decode('utf-8'))
print(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment