Browse Source

parse URLs to find the review and patchset

Signed-off-by: Doug Hellmann <doug@doughellmann.com>
tags/1.0.0
Doug Hellmann 1 year ago
parent
commit
a1a40815a6
4 changed files with 118 additions and 0 deletions
  1. 44
    0
      git_nit/cmd.py
  2. 72
    0
      git_nit/tests/test_parse_review_id.py
  3. 1
    0
      requirements.txt
  4. 1
    0
      test-requirements.txt

+ 44
- 0
git_nit/cmd.py View File

@@ -15,13 +15,44 @@
15 15
 from __future__ import print_function
16 16
 
17 17
 import argparse
18
+import os
19
+
18 20
 import pkg_resources
21
+from six.moves import urllib
22
+
23
+
19 24
 def get_version():
20 25
     requirement = pkg_resources.Requirement.parse('git-nit')
21 26
     provider = pkg_resources.get_provider(requirement)
22 27
     return provider.version
23 28
 
24 29
 
30
+def 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
 
26 57
 def main():
27 58
     parser = argparse.ArgumentParser()
@@ -30,8 +61,21 @@ def main():
30 61
         action='version',
31 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 75
     args = parser.parse_args()
34 76
 
77
+    review, patchset = parse_review_id(args.review)
78
+    print(review, patchset)
35 79
 
36 80
 if __name__ == '__main__':
37 81
     main()

+ 72
- 0
git_nit/tests/test_parse_review_id.py View File

@@ -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
+
15
+from git_nit import cmd
16
+
17
+import testscenarios.testcase
18
+import testtools
19
+
20
+
21
+class 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
+        )

+ 1
- 0
requirements.txt View File

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

+ 1
- 0
test-requirements.txt View File

@@ -6,3 +6,4 @@ sphinx>=1.1.2,!=1.2.0
6 6
 coverage
7 7
 python-subunit>=1.0.0 # Apache-2.0/BSD
8 8
 stestr>=1.0.0 # Apache-2.0
9
+testscenarios

Loading…
Cancel
Save