From a1a40815a6e4dec41a5ee1b74748931ac0eac2ae Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Thu, 26 Apr 2018 15:26:18 -0400 Subject: [PATCH] parse URLs to find the review and patchset Signed-off-by: Doug Hellmann --- git_nit/cmd.py | 44 ++++++++++++++++ git_nit/tests/test_parse_review_id.py | 72 +++++++++++++++++++++++++++ requirements.txt | 1 + test-requirements.txt | 1 + 4 files changed, 118 insertions(+) create mode 100644 git_nit/tests/test_parse_review_id.py diff --git a/git_nit/cmd.py b/git_nit/cmd.py index 6fe8f57..d56e14b 100644 --- a/git_nit/cmd.py +++ b/git_nit/cmd.py @@ -15,13 +15,44 @@ from __future__ import print_function import argparse +import os + import pkg_resources +from six.moves import urllib + + def get_version(): requirement = pkg_resources.Requirement.parse('git-nit') provider = pkg_resources.get_provider(requirement) return provider.version +def parse_review_id(review_id): + "Given a review URL or ID return the review number and PS number, if any." + parsed = urllib.parse.urlparse(review_id) + if parsed.fragment: + # https://review.openstack.org/#/c/564559/ style + parts = [ + p + for p in parsed.fragment.split('/') + if p and p != 'c' + ] + else: + # https://review.openstack.org/564559/ style + parts = [ + p + for p in parsed.path.split('/') + if p + ] + if not parts: + raise ValueError('Could not parse review ID {!r}'.format(review_id)) + review = parts[0] + if len(parts) > 1: + patchset = parts[1] + else: + patchset = None + return (review, patchset) + def main(): parser = argparse.ArgumentParser() @@ -30,8 +61,21 @@ def main(): action='version', version=get_version(), ) + parser.add_argument( + '--project-dir', + default=os.environ.get('PROJECT_DIR', '.'), + help=( + 'parent directory for creating a new project, ' + 'defaults to $PROJECT_DIR or "."'), + ) + parser.add_argument( + 'review', + help='the URL for the review', + ) args = parser.parse_args() + review, patchset = parse_review_id(args.review) + print(review, patchset) if __name__ == '__main__': main() diff --git a/git_nit/tests/test_parse_review_id.py b/git_nit/tests/test_parse_review_id.py new file mode 100644 index 0000000..f717da6 --- /dev/null +++ b/git_nit/tests/test_parse_review_id.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from git_nit import cmd + +import testscenarios.testcase +import testtools + + +class ParseReviewIDTest(testscenarios.testcase.WithScenarios, + testtools.TestCase): + + scenarios = [ + ('fragment with patchset', { + 'url': 'https://review.openstack.org/#/c/564559/5/', + 'review': '564559', + 'patchset': '5', + }), + ('fragment with patchset, no trailing slash', { + 'url': 'https://review.openstack.org/#/c/564559/5', + 'review': '564559', + 'patchset': '5', + }), + ('fragment without patchset', { + 'url': 'https://review.openstack.org/#/c/564559/', + 'review': '564559', + 'patchset': None, + }), + ('fragment without patchset, no trailing slash', { + 'url': 'https://review.openstack.org/#/c/564559', + 'review': '564559', + 'patchset': None, + }), + ('path with patchset', { + 'url': 'https://review.openstack.org/564559/5/', + 'review': '564559', + 'patchset': '5', + }), + ('path with patchset, no trailing slash', { + 'url': 'https://review.openstack.org/564559/5', + 'review': '564559', + 'patchset': '5', + }), + ('path without patchset', { + 'url': 'https://review.openstack.org/564559/', + 'review': '564559', + 'patchset': None, + }), + ('path without patchset, no trailing slash', { + 'url': 'https://review.openstack.org/564559', + 'review': '564559', + 'patchset': None, + }), + ] + + def test(self): + review, patchset = cmd.parse_review_id(self.url) + self.assertEqual( + (self.review, self.patchset), + (review, patchset), + ) diff --git a/requirements.txt b/requirements.txt index 944892c..289b82a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ requests>=1.1 +six diff --git a/test-requirements.txt b/test-requirements.txt index 5976a1c..b724adf 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -6,3 +6,4 @@ sphinx>=1.1.2,!=1.2.0 coverage python-subunit>=1.0.0 # Apache-2.0/BSD stestr>=1.0.0 # Apache-2.0 +testscenarios