view bzgit.py @ 7:b951b70aa952 default tip

cleanin up round 1
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 17 Dec 2012 12:28:16 -0800
parents 7138a453ecf7
children
line wrap: on
line source

#!/usr/bin/env python

"""
bzgit
github <-> bugzilla.m.o bridge
"""

import bzconsole
import optparse
import sys
import urlparse
from pygithub3 import Github
from bzconsole.command import read_dotfile # XXX to be moved

def path_segments(path):
    """return path segments"""
    segments = path.strip('/').split('/')
    if segments == ['']:
        return []
    return segments

def add_options(parser):
    """add bzgit options to an OptionParser instance"""


def main(args=sys.argv[1:]):

    # parse command line arguments
    usage = '%prog [options] http://github.com/url/of/pull/request'
    class PlainDescriptionFormatter(optparse.IndentedHelpFormatter):
        """description formatter"""
        def format_description(self, description):
            if description:
                return description.strip() + '\n'
            else:
                return ''
    parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter())
    add_options(parser)
    options, args = parser.parse_args(args)
    if not args:
        parser.print_usage()
        parser.exit()

    # parse resource
    assert len(args) == 1 # for now
    resource = args[0]
    scheme, hostspec, path, query, anchor = urlparse.urlsplit(resource)
    segments = path_segments(path)
    assert len(segments) > 2 # for now
    github_user = segments[0]
    github_project = segments[1]

    assert len(segments) == 4 # for now
    assert segments[2] == 'pull'
    pull_request = int(segments[3])

    # connect to gh
    gh = Github() # TODO: auth
    pull = gh.pull_requests.get(pull_request, github_user, github_project)
    title = pull.title
    diff = pull.diff_url

    # move all the things
    bz_options = read_dotfile()
    assert set(['username', 'password']).issubset(bz_options)
    bz = bzconsole.BZapi(**bz_options)

    description = """From %s:

%s
""" % (resource, title)

    # TODO: add CC of github user if possible
    cc = [':wlach']
    new = bz.new('Mozbase', title=title, description=description, cc=cc)
    bug = int(new.strip().rsplit('/')[-1])
    print 'https://bugzilla.mozilla.org/show_bug.cgi?id=%s' % bug
    attachment = bz.attach(bug, diff, reviewer=':wlach')

    # comment on pull request wrt bugzilla.m.o issue location
    # TODO

    # close pull request
    # TODO

if __name__ == '__main__':
    main()