#!/usr/bin/python

"""
Propose a merge in Launchpad with one command.

Copyright (C) 2013 Brendan Donegan

Authors
  Brendan Donegan <brendan.j.donegan@gmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3,
as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import os
import sys

from argparse import ArgumentParser
from launchpadlib.launchpad import Launchpad
from subprocess import check_call, check_output, CalledProcessError
from tempfile import NamedTemporaryFile


def main():
    parser = ArgumentParser("A script for proposing merges in Launchpad.")
    parser.add_argument("--target", "-t",
                        help="The target to merge into.")
    parser.add_argument("--withhold","-w",
                        action='store_true',
                        help="Don't ask for a review now, just create the MR")
    args = parser.parse_args()

    lp = Launchpad.login_with(sys.argv[0], 'production')

    try:
        lp_user = check_output(['bzr','launchpad-login']).strip()
    except CalledProcessError:
        return 1

    try:
        parent_branch = check_output('bzr info | grep "parent branch"',
                                     shell=True)
    except CalledProcessError:
        return 1

    target = args.target
    if not args.target:
        target = parent_branch.strip().split('bazaar.launchpad.net/')[-1]

    lp_project = target.split('/')[1]

    if '+branch' in target:
        target = target.split('/')[1]

    lp_nick = check_output(['bzr','nick']).strip()

    branch_url = 'lp:~' + '/'.join([lp_user, lp_project, lp_nick])

    try:
        check_call(['bzr','push',branch_url])
    except:
        print("Problem pushing branch.")

    merge_branch = lp.branches.getByUrl(url=branch_url)

    target_branch = lp.branches.getByUrl(url='lp:' + target)

    editor = os.environ.get('EDITOR','nano')
    tempfile = NamedTemporaryFile(delete=False)
    try:
        check_call([editor,tempfile.name])
    except OSError:
        print("Failed to open text editor")

    description = tempfile.read()
    tempfile.close()

    merge_request = merge_branch.createMergeProposal(
        target_branch=target_branch,
        initial_comment=description,
        needs_review=not(args.withhold))
    print(merge_request.web_link)

    return 0

if __name__ == "__main__":
    sys.exit(main())
