Skip to content

Instantly share code, notes, and snippets.

@a-y-khan
Last active February 21, 2018 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-y-khan/aa2474cd0351235e80d8a6343acf3ee1 to your computer and use it in GitHub Desktop.
Save a-y-khan/aa2474cd0351235e80d8a6343acf3ee1 to your computer and use it in GitHub Desktop.
TeamCity Python build step to query GitHub GraphQL endpoint
import json
import requests
import subprocess
def query_github_graphql(url: str, api_token: str, owner: str, service: str, pull_request: int):
headers = {'Authorization': 'bearer {}'.format(api_token), 'Content-Type': 'application/json'}
payload = {'query': '{repository(owner:\"%s\", name: \"%s\"){pullRequest(number: %d){headRefName,baseRefName}}}' % (owner, service, pull_request)}
response = requests.post(url=url, json=payload, headers=headers)
print(response.text)
response_dict = json.loads(response.text)
target_branch = response_dict['data']['repository']['pullRequest']['baseRefName']
pull_request_branch = response_dict['data']['repository']['pullRequest']['headRefName']
subprocess.call(['echo', '##teamcity[setParameter name=\'targetBranch\' value=\'%s\']' % target_branch])
subprocess.call(['echo', '##teamcity[setParameter name=\'pullRequestBranch\' value=\'%s\']' % pull_request_branch])
if '__main__' == __name__:
query_github_graphql('%endpoint_url%', '%token%', '%owner%', '%repo%', %pull_request_number%)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment