[wishlist] add function to extract project name from parent URL of LP-hosted branches

Bug #744992 reported by Alexander Belchenko
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Bazaar
Confirmed
Wishlist
Unassigned

Bug Description

In QBzr we have the code to create suggestion for new push URL for feature branches. To create a suggestion we analyze the parent branch of the parent branch of the current branch which is about to push the first time (without remembered push location). We have a generic code for remote branches (just replace the last part of parent URL with new branch directory name). Also we have a specific support for LP-hosted branches because we want to suggest new push location as lp:~user-id/project/new-branch. That's worked well in the past but last months LP has switched to use +branch URL aliases, and it breaks the QBzr code.

Thanks to John Meinel and Max Bowsher I've fixed the code to extract the project name from URL, but I wonder if such code would be better keep in the launchpad plugin bundled with bzr itself, so this logic will be updated every time the LP support changed? Although the only one client for that code today is QBzr. I'm not sure how to ensure this code will be changed when lp_directory.py will be changed?

Below is my code and test:

def launchpad_project_from_url(url):
    """If url is a Launchpad code URL, get the project name.

    @return: project name or None
    """
    # The format ought to be one of the following:
    # scheme://host/~user-id/project-name/branch-name
    # scheme://host/+branch/project-name
    # scheme://host/+branch/project-name/series-name
    # there could be distro branches, they are very complex,
    # so we only support upstream branches based on source package
    # scheme://host/+branch/DISTRO/SOURCEPACKAGE
    # scheme://host/+branch/DISTRO/SERIES/SOURCEPACKAGE
    # scheme://host/+branch/DISTRO/POCKET/SOURCEPACKAGE
    # scheme://host/~USER/DISTRO/SERIES/SOURCEPACKAGE/BRANCHNAME
    DISTROS = ('debian', 'ubuntu')
    from urlparse import urlsplit
    scheme, host, path = urlsplit(url)[:3]
    # Sanity check the host
    if (host in ('bazaar.launchpad.net',
                 'bazaar.launchpad.dev',
                 'bazaar.qastaging.launchpad.net',
                 'bazaar.staging.launchpad.net')):
        parts = path.strip('/').split('/')
        if parts[0].startswith('~'):
            if len(parts) == 3 and parts[1] not in DISTROS:
                # scheme://host/~user-id/project-name/branch-name/
                return parts[1]
            elif len(parts) == 5 and parts[1] in DISTROS:
                # scheme://host/~USER/DISTRO/SERIES/SOURCEPACKAGE/BRANCHNAME
                return parts[-2]
        elif parts[0] in ('%2Bbranch', '+branch'):
            n = len(parts)
            if n >= 2:
                part1 = parts[1]
                if n in (2,3) and part1 not in DISTROS:
                    # scheme://host/+branch/project-name
                    # scheme://host/+branch/project-name/series-name
                    return part1
                elif n in (3,4) and part1 in DISTROS:
                    # scheme://host/+branch/DISTRO/SOURCEPACKAGE
                    # scheme://host/+branch/DISTRO/SERIES/SOURCEPACKAGE
                    # scheme://host/+branch/DISTRO/POCKET/SOURCEPACKAGE
                    return parts[-1]
    return None

    def test_launchpad_project_from_url(self):
        fut = util.launchpad_project_from_url # fut = function under test
        # classic
        self.assertEquals('qbzr', fut('bzr+ssh://bazaar.launchpad.net/~qbzr-dev/qbzr/trunk'))
        # lp:qbzr
        self.assertEquals('qbzr', fut('bzr+ssh://bazaar.launchpad.net/+branch/qbzr'))
        self.assertEquals('qbzr', fut('bzr+ssh://bazaar.launchpad.net/%2Bbranch/qbzr'))
        # lp:qbzr/0.20
        self.assertEquals('qbzr', fut('bzr+ssh://bazaar.launchpad.net/%2Bbranch/qbzr/0.20'))
        # lp:ubuntu/qbzr
        self.assertEquals('qbzr', fut('bzr+ssh://bazaar.launchpad.net/%2Bbranch/ubuntu/qbzr'))
        # lp:ubuntu/natty/qbzr
        self.assertEquals('qbzr', fut('bzr+ssh://bazaar.launchpad.net/%2Bbranch/ubuntu/natty/qbzr'))
        # lp:ubuntu/natty-proposed/qbzr
        self.assertEquals('qbzr', fut('bzr+ssh://bazaar.launchpad.net/%2Bbranch/ubuntu/natty-proposed/qbzr'))
        # lp:~someone/ubuntu/maverick/qbzr/sru
        self.assertEquals('qbzr', fut('bzr+ssh://bazaar.launchpad.net/~someone/ubuntu/maverick/qbzr/sru'))

Revision history for this message
Max Bowsher (maxb) wrote :

If it were to be put somewhere more general, like the bzr launchpad plugin, it would first need to support package branches better.

Jelmer Vernooij (jelmer)
Changed in bzr:
status: New → Confirmed
importance: Undecided → Wishlist
Revision history for this message
Martin Pool (mbp) wrote : Re: [Bug 744992] Re: [wishlist] add function to extract project name from parent URL of LP-hosted branches

Rather than parsing the url, you could alternatively do an api call to
Launchpad to get the branch object, and then traverse from there to
the owning project.

Jelmer Vernooij (jelmer)
tags: added: check-for-breezy
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.