Skip to content

Instantly share code, notes, and snippets.

@kvld
Last active May 31, 2020 12:31
Show Gist options
  • Save kvld/a15b9f9159a6044628dffb565a48ab4a to your computer and use it in GitHub Desktop.
Save kvld/a15b9f9159a6044628dffb565a48ab4a to your computer and use it in GitHub Desktop.
import sys
import requests
import os
import json
def upate_comment(url, message, token):
headers = {'Authorization': 'token ' + token}
payload = {'body': message}
req = requests.patch(url, json=payload, headers=headers)
def run_bitrise(token, app_slug, branch_repo_owner, pull_request_id, envs):
headers = {'Authorization': token}
mapped_envs = [{'mapped_to': c, 'value': envs[c], 'is_expand': True} for c in envs]
payload = {
'hook_info': {'type': 'bitrise'},
'build_params': {
'branch_repo_owner': branch_repo_owner,
'branch_dest_repo_owner': branch_repo_owner,
'pull_request_id': pull_request_id
},
'environments': mapped_envs
}
req = requests.post('https://api.bitrise.io/v0.1/apps/' + app_slug + '/builds', json=payload, headers=headers)
def _testflight_handler(args):
envs = {'GITHUB_COMMENT_TO_APPEND': comment_url}
run_bitrise(token=bitrise_token, app_slug=bitrise_app_slug, branch_repo_owner=repo_owner, pull_request_id=pull_request_id, envs=envs)
def _handle(command, args):
handlers = {
'testflight': _testflight_handler
}
if command in handlers:
handlers[command](args)
if __name__ == '__main__':
event_path = os.environ['GITHUB_EVENT_PATH']
with open(event_path, 'r') as event_file:
event = json.load(event_file)
token = os.environ['TOKEN']
bitrise_token = os.environ['BITRISE_TOKEN']
bitrise_app_slug = os.environ['BITRISE_APP_SLUG']
comment = event['comment']['body']
comment_url = event['comment']['url']
repo_owner = event['repository']['full_name'].split('/')[0]
pull_request_id = event['issue']['number']
if comment[0] != '/':
sys.exit(0)
parts = comment.split(' ')
command = parts[0][1:]
args = {}
if len(parts) > 1:
for arg in ' '.join(parts[1:]).split(','):
arg_parts = arg.split('=')
if len(arg_parts) != 2:
continue
key = arg_parts.strip()
value = arg_parts.strip().strip('"').strip("'")
args[key] = value
_handle(command, args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment