Last active
February 21, 2018 06:02
-
-
Save a-y-khan/aa2474cd0351235e80d8a6343acf3ee1 to your computer and use it in GitHub Desktop.
TeamCity Python build step to query GitHub GraphQL endpoint
This file contains 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
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