summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Hellmann <doug@doughellmann.com>2018-04-26 15:26:18 -0400
committerDoug Hellmann <doug@doughellmann.com>2018-04-26 15:26:18 -0400
commita1a40815a6e4dec41a5ee1b74748931ac0eac2ae (patch)
treefb554f04950fb68a4a73319cada127acfbee7fae
parenteb6112410218ddcbaa39e44f6328d5f92850d767 (diff)
parse URLs to find the review and patchset
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
-rw-r--r--git_nit/cmd.py44
-rw-r--r--git_nit/tests/test_parse_review_id.py72
-rw-r--r--requirements.txt1
-rw-r--r--test-requirements.txt1
4 files changed, 118 insertions, 0 deletions
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 @@
15from __future__ import print_function 15from __future__ import print_function
16 16
17import argparse 17import argparse
18import os
19
18import pkg_resources 20import pkg_resources
21from six.moves import urllib
22
23
19def get_version(): 24def get_version():
20 requirement = pkg_resources.Requirement.parse('git-nit') 25 requirement = pkg_resources.Requirement.parse('git-nit')
21 provider = pkg_resources.get_provider(requirement) 26 provider = pkg_resources.get_provider(requirement)
22 return provider.version 27 return provider.version
23 28
24 29
30def parse_review_id(review_id):
31 "Given a review URL or ID return the review number and PS number, if any."
32 parsed = urllib.parse.urlparse(review_id)
33 if parsed.fragment:
34 # https://review.openstack.org/#/c/564559/ style
35 parts = [
36 p
37 for p in parsed.fragment.split('/')
38 if p and p != 'c'
39 ]
40 else:
41 # https://review.openstack.org/564559/ style
42 parts = [
43 p
44 for p in parsed.path.split('/')
45 if p
46 ]
47 if not parts:
48 raise ValueError('Could not parse review ID {!r}'.format(review_id))
49 review = parts[0]
50 if len(parts) > 1:
51 patchset = parts[1]
52 else:
53 patchset = None
54 return (review, patchset)
55
25 56
26def main(): 57def main():
27 parser = argparse.ArgumentParser() 58 parser = argparse.ArgumentParser()
@@ -30,8 +61,21 @@ def main():
30 action='version', 61 action='version',
31 version=get_version(), 62 version=get_version(),
32 ) 63 )
64 parser.add_argument(
65 '--project-dir',
66 default=os.environ.get('PROJECT_DIR', '.'),
67 help=(
68 'parent directory for creating a new project, '
69 'defaults to $PROJECT_DIR or "."'),
70 )
71 parser.add_argument(
72 'review',
73 help='the URL for the review',
74 )
33 args = parser.parse_args() 75 args = parser.parse_args()
34 76
77 review, patchset = parse_review_id(args.review)
78 print(review, patchset)
35 79
36if __name__ == '__main__': 80if __name__ == '__main__':
37 main() 81 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 @@
1#!/usr/bin/env python
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from git_nit import cmd
16
17import testscenarios.testcase
18import testtools
19
20
21class ParseReviewIDTest(testscenarios.testcase.WithScenarios,
22 testtools.TestCase):
23
24 scenarios = [
25 ('fragment with patchset', {
26 'url': 'https://review.openstack.org/#/c/564559/5/',
27 'review': '564559',
28 'patchset': '5',
29 }),
30 ('fragment with patchset, no trailing slash', {
31 'url': 'https://review.openstack.org/#/c/564559/5',
32 'review': '564559',
33 'patchset': '5',
34 }),
35 ('fragment without patchset', {
36 'url': 'https://review.openstack.org/#/c/564559/',
37 'review': '564559',
38 'patchset': None,
39 }),
40 ('fragment without patchset, no trailing slash', {
41 'url': 'https://review.openstack.org/#/c/564559',
42 'review': '564559',
43 'patchset': None,
44 }),
45 ('path with patchset', {
46 'url': 'https://review.openstack.org/564559/5/',
47 'review': '564559',
48 'patchset': '5',
49 }),
50 ('path with patchset, no trailing slash', {
51 'url': 'https://review.openstack.org/564559/5',
52 'review': '564559',
53 'patchset': '5',
54 }),
55 ('path without patchset', {
56 'url': 'https://review.openstack.org/564559/',
57 'review': '564559',
58 'patchset': None,
59 }),
60 ('path without patchset, no trailing slash', {
61 'url': 'https://review.openstack.org/564559',
62 'review': '564559',
63 'patchset': None,
64 }),
65 ]
66
67 def test(self):
68 review, patchset = cmd.parse_review_id(self.url)
69 self.assertEqual(
70 (self.review, self.patchset),
71 (review, patchset),
72 )
diff --git a/requirements.txt b/requirements.txt
index 944892c..289b82a 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1 +1,2 @@
1requests>=1.1 1requests>=1.1
2six
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
6coverage 6coverage
7python-subunit>=1.0.0 # Apache-2.0/BSD 7python-subunit>=1.0.0 # Apache-2.0/BSD
8stestr>=1.0.0 # Apache-2.0 8stestr>=1.0.0 # Apache-2.0
9testscenarios