Browse Source

Add reviewers on upload

Add the --reviewers argument which allows specifying reviewers to be
added to each patch uploaded.  This simplifies the upload process by not
requiring an additional step of adding reviewers to changes.

Gerrit supports specifying reviewers to be added to changes by appending
a list of r='email' options to the refspec being pushed to.  For
example, from the Gerrit 'Uploading Changes' documentation:

git push tr:kernel/common HEAD:refs/for/experimental%r=a@a.com,cc=b@o.com

The --reviewers argument can be passed multiple reviewers to add to the
patch set.  Reviewers containing whitespace are rejected, as whitespace
cannot be added to the refspec.

Change-Id: I8c2f9453a90dd78aa47f7996f2502f9f6cf2d66d
Michael Pratt 4 years ago
parent
commit
bb7395009d
4 changed files with 62 additions and 0 deletions
  1. 4
    0
      doc/source/usage.rst
  2. 5
    0
      git-review.1
  3. 21
    0
      git_review/cmd.py
  4. 32
    0
      git_review/tests/test_git_review.py

+ 4
- 0
doc/source/usage.rst View File

@@ -18,6 +18,10 @@ If you want to supply a review topic::
18 18
 
19 19
     git review -t topic/awesome-feature
20 20
 
21
+If you want to subscribe some reviewers::
22
+
23
+    git review --reviewers a@example.com b@example.com
24
+
21 25
 If you want to disable autogenerated topic::
22 26
 
23 27
     git review -T

+ 5
- 0
git-review.1 View File

@@ -41,6 +41,7 @@
41 41
 .Op Fl fnuvDRT
42 42
 .Op Fl r Ar remote
43 43
 .Op Fl t Ar topic
44
+.Op Fl \-reviewers Ar reviewer ...
44 45
 .Op Ar branch
45 46
 .Nm
46 47
 .Fl \-version
@@ -140,6 +141,8 @@ Sets the target topic for this change on the gerrit server.
140 141
 If not specified, a bug number from the commit summary will be used. Alternatively, the local branch name will be used if different from remote branch.
141 142
 .It Fl T , Fl \-no\-topic
142 143
 Submit review without topic.
144
+.It Fl \-reviewers Ar reviewer ...
145
+Subscribe one or more reviewers to the uploaded patch sets.  Reviewers should be identifiable by Gerrit (usually use their Gerrit username or email address).
143 146
 .It Fl u , Fl \-update
144 147
 Skip cached local copies and force updates from network resources.
145 148
 .It Fl l , Fl \-list
@@ -337,6 +340,8 @@ specific error codes are available:
337 340
 Gerrit
338 341
 .Ar commit\-msg
339 342
 hook could not be successfully installed.
343
+.It 3
344
+Could not parse malformed argument value or user input.
340 345
 .It 32
341 346
 Cannot fetch list of open changesets from Gerrit.
342 347
 .It 33

+ 21
- 0
git_review/cmd.py View File

@@ -1217,6 +1217,20 @@ def convert_bool(one_or_zero):
1217 1217
     return str(one_or_zero) in ["1", "true", "True"]
1218 1218
 
1219 1219
 
1220
+class MalformedInput(GitReviewException):
1221
+    EXIT_CODE = 3
1222
+
1223
+
1224
+def assert_valid_reviewers(reviewers):
1225
+    """Ensure no whitespace is found in reviewer names, as it will result
1226
+    in an invalid refspec.
1227
+    """
1228
+    for reviewer in reviewers:
1229
+        if re.search(r'\s', reviewer):
1230
+            raise MalformedInput(
1231
+                "Whitespace not allowed in reviewer: '%s'" % reviewer)
1232
+
1233
+
1220 1234
 def _main():
1221 1235
     usage = "git review [OPTIONS] ... [BRANCH]"
1222 1236
 
@@ -1237,6 +1251,8 @@ def _main():
1237 1251
                                  action="store_true",
1238 1252
                                  help="No topic except if explicitly provided")
1239 1253
 
1254
+    parser.add_argument("--reviewers", nargs="+",
1255
+                        help="Add reviewers to uploaded patch sets.")
1240 1256
     parser.add_argument("-D", "--draft", dest="draft", action="store_true",
1241 1257
                         help="Submit review as a draft")
1242 1258
     parser.add_argument("-c", "--compatible", dest="compatible",
@@ -1448,6 +1464,11 @@ def _main():
1448 1464
         topic = None if options.notopic else get_topic(branch)
1449 1465
     if topic and topic != branch:
1450 1466
         cmd += "/%s" % topic
1467
+
1468
+    if options.reviewers:
1469
+        assert_valid_reviewers(options.reviewers)
1470
+        cmd += "%" + ",".join("r=%s" % r for r in options.reviewers)
1471
+
1451 1472
     if options.regenerate:
1452 1473
         print("Amending the commit to regenerate the change id\n")
1453 1474
         regenerate_cmd = "git commit --amend"

+ 32
- 0
git_review/tests/test_git_review.py View File

@@ -15,6 +15,7 @@
15 15
 # See the License for the specific language governing permissions and
16 16
 # limitations under the License.
17 17
 
18
+import json
18 19
 import os
19 20
 import shutil
20 21
 
@@ -128,6 +129,37 @@ class GitReviewTestCase(tests.BaseGitReviewTestCase):
128 129
         review_res = self._run_git_review('-y')
129 130
         self.assertIn("Processing changes: new: 2", review_res)
130 131
 
132
+    def test_git_review_re(self):
133
+        """Test git-review adding reviewers to changes."""
134
+        self._run_git_review('-s')
135
+
136
+        # Create users to add as reviewers
137
+        self._run_gerrit_cli('create-account', '--email',
138
+                             'reviewer1@example.com', 'reviewer1')
139
+        self._run_gerrit_cli('create-account', '--email',
140
+                             'reviewer2@example.com', 'reviewer2')
141
+
142
+        self._simple_change('test file', 'test commit message')
143
+
144
+        review_res = self._run_git_review('--reviewers', 'reviewer1',
145
+                                          'reviewer2')
146
+        self.assertIn("Processing changes: new: 1", review_res)
147
+
148
+        # verify both reviewers are on patch set
149
+        head = self._run_git('rev-parse', 'HEAD')
150
+        change = self._run_gerrit_cli('query', '--format=JSON',
151
+                                      '--all-reviewers', head)
152
+        # The first result should be the one we want
153
+        change = json.loads(change.split('\n')[0])
154
+
155
+        self.assertEqual(2, len(change['allReviewers']))
156
+
157
+        reviewers = set()
158
+        for reviewer in change['allReviewers']:
159
+            reviewers.add(reviewer['username'])
160
+
161
+        self.assertEqual(set(['reviewer1', 'reviewer2']), reviewers)
162
+
131 163
     def test_rebase_no_remote_branch_msg(self):
132 164
         """Test message displayed where no remote branch exists."""
133 165
         self._run_git_review('-s')

Loading…
Cancel
Save