parse URLs to find the review and patchset

Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2018-04-26 15:26:18 -04:00
parent eb61124102
commit a1a40815a6
4 changed files with 118 additions and 0 deletions

View File

@ -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()

View File

@ -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),
)

View File

@ -1 +1,2 @@
requests>=1.1
six

View File

@ -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