#!/usr/bin/env python

# This file is used to generate a custom command for our pull request bot at C2FO
# It's very hard coded for me but it works to generate a command like:
#
# @CIBot pr me SUPP-53 C2FO c2fo SUPP-53-cool-stuff develop @AndrewHeuermann @WesCravens @JasonCoble @RichardMoorhead
#
# It will print out the command you should give to the CIBot.
# It also copies this command to your clipboard (if you are using mac)

from subprocess import check_output
from subprocess import Popen
from subprocess import PIPE
import sys


def join_teams(*args):
    return ' '.join(args)

# Get the current branch name
branch_name = check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode('UTF-8')

# Gets the current repository information
# Looks like:
#   origin  git@github.com:C2FO/c2fo.git (fetch)
#   origin  git@github.com:C2FO/c2fo.git (push)
#
remote_repo_info = check_output(['git', 'remote', '-v']).decode('UTF-8')

# Get the organization and repo name (e.g. ['C2FO', 'c2fo.git (fetch)'])
org_repo_names = remote_repo_info.split('\n')[0].split(':')[1].split('/')
org_name = org_repo_names[0]
repository_name = org_repo_names[1].split('.git')[0].split(' ')[0]

# Push the branch to the matching remote
remote_name = check_output(['git', 'remote']).decode('UTF-8').replace('\n', '')
branch_name_no_newline = branch_name.replace('\n', '')
# Setup remote tracking
check_output(['git', 'branch','-u', remote_name + '/' + branch_name_no_newline, '--quiet'])
# Push the brach
check_output(['git', 'push', '-q', remote_name, branch_name_no_newline])

# Standard branch should be develop, update if needed
branch_target = 'develop'


if len(sys.argv) > 1:
    branch_target = sys.argv[1]

# This is my current team the "tpf team".
tpf_team = '<@andy.moon> <@chrisgurba> <@scottdillender> <@jeetdas>'
devops = '<@chrisstein> <@thomas.carroll> <@jeffstephens> <@ryan.baker> <@evan.gibler> <@matthew.hembree>'
atlas = '<@matthoag> <@anthonyshipman> <@michaelprittie> <@hoale>'
web_team = '<@nancy.holder> <@dylanstanfield> <@josh.herder> <@jordan.vanwinkle> <@dustinsmith> <@michael.westblade> <@rhia.dixon> <@eric.kemmis>'

team = '<@eric.kemmis> <@imon.stevenson>'
devops_team = '<@chrisstein> <@ryan.baker>'

whole_team = join_teams(team, devops, tpf_team)
app_team = join_teams(team, tpf_team)

used_team = team
if len(sys.argv) > 2:
    arg = sys.argv[2]
    if arg == 'whole_team':
        used_team = whole_team
    elif arg == 'devops':
        used_team = devops
    elif arg == 'tpf_team':
        used_team = tpf_team
    elif arg == 'app_team':
        used_team = app_team
    elif arg == 'devops_and_team':
        used_team = '{devops_team} {team}'.format(devops_team=devops_team, team=team)
    elif arg == 'atlas':
        used_team = atlas
    elif arg == 'web_team':
        used_team = web_team
    else:
        used_team = team

# Get JIRA number from branch name
jira_number = '-'.join(branch_name.split('-')[:2])

# Create the command that actually needs to be sent to the robot
command_text = "<@cibot> pr me {jira_number} {org_name} {repository_name} {branch_name} {branch_target} {team}".format(
    jira_number=jira_number,
    branch_name=branch_name,
    org_name=org_name,
    repository_name=repository_name,
    branch_target=branch_target,
    team=used_team
).replace('\n', '')

sys.stdout.write(command_text + '\n')

# Finally copy the command to the clipboard
# Thank you stack overflow: http://stackoverflow.com/a/25802742
def write_to_clipboard(output):
    process = Popen(
        'pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=PIPE)
    process.communicate(output.encode('utf-8'))

write_to_clipboard(command_text)
