Add reviewers via the commit message
Asked Answered
E

6

11

Is it possible to add reviewers in Gerrit via a commit message? Consider this commit message:

component: make foo more bar

Foo was not bar enough, this change adds more bar to make foo fit better
in baz.

Change-Id: I4724e283214e0bfbb85a8e3d8db4971618e2609a
Cc: [email protected]
Cc: [email protected]

Here, [email protected] and [email protected] must be added as reviewers when pushing to gerrit.

I am aware of a special branch specifier to add reviewers, but would like to have something more automated as I create commits. The changes are independent, though it would be nice if I could group them on a topic branch because they are related.

Esqueda answered 4/7, 2014 at 14:52 Comment(3)
i think you can create a pre-push hook for this to create the special branch specifier from the commit msgGutter
@jthill the git tag was added since commit hooks may also work here which requires git knowledge. I'll rollback if you don't mind?Esqueda
I don't mind at all. I said my piece, you didn't even have to say yours, it's your post. I could easily be wrong to think a large number of gerrit users here also know git well enough to answer, enough that bothering the much larger number of git-only users following that tag but don't know gerrit was not worth it.Elegit
E
11

It is not possible to set per-commit reviewers, these are applied per push (see gerrit's git-receive-pack manual). Instead of executing git push origin HEAD or git review (assuming origin to be the gerrit remote, and HEAD the branch you want to push), you can run the following to add two reviewers for all new commits:

git push origin HEAD:refs/for/master%[email protected],[email protected]

That gets applied to all commits which is not what you want. Due to the above limitations, let's change the workflow to push changes first and then set some reviewers.


Since Gerrit distinguishes between Cc (just send a mail notification) and reviewers (send a mail, but also mark the user as reviewer), I'll modify the commit message as follows:

component: make foo more bar

Foo was not bar enough, this change adds more bar to make foo fit better
in baz.

[email protected]
[email protected]

Change-Id: I4724e283214e0bfbb85a8e3d8db4971618e2609a

Given a number of commits, one can follow the following steps to add separate reviewers for each commit:

  1. Gather a list of commits IDs (or Change-Ids). Example that assumes the master branch as base: git rev-list --reverse origin/master..
  2. For each commit ID, scan for R=... (reviewers) in the commit message. The commit message for a given commit can be found with git show --no-patch --format=%b COMMIT_ID
  3. If reviewers exist for a commit message, add them with the command ssh -p 29418 user@host 'gerrit set-reviewers -a [email protected] COMMIT_ID' (instead of COMMIT_ID, you can also use the Change-Id which is I4724e283214e0bfbb85a8e3d8db4971618e2609a for the example).

To perform the above steps (together with auto-detecting the user, host and port settings), I wrote a Bash script: https://git.lekensteyn.nl/scripts/tree/git/gerrit-add-reviewers

It is recommended to have a .gitreview file in your repo with a remote that points to a Gerrit instance. Then execute ~/scripts/gerrit-add-reviews origin/master.. from within that git repo to scan commit messages and add reviewers.

Esqueda answered 5/7, 2014 at 13:39 Comment(0)
A
2

I put together a script where you can pass the email addresses of the reviewers you want to add and it will take care of the rest for you. You can even fuzzy-find in existing committers (email and name), so that you don't have to type a lot.

https://gist.github.com/andersonvom/924fdc5f92aefa5eca9c

Just call it using:

$ greview [<rev1> [<rev2> [...]]]
Alvord answered 9/10, 2015 at 23:39 Comment(1)
That script uses a neat trick to set reviewers after push: git push --receivepack="git-receive-pack --reviewer [email protected] --reviewer [email protected]" origin HEAD:refs/for/master/topicEsqueda
F
2

Send a review for a change on the master branch to [email protected]:

git push ssh://review.example.com:29418/project HEAD:refs/for/master%[email protected]

Send reviews, but tagging them with the topic name 'bug42':

git push ssh://review.example.com:29418/project HEAD:refs/for/master%[email protected],topic=bug42

Also CC two other parties:

git push ssh://review.example.com:29418/project HEAD:refs/for/master%[email protected],[email protected],[email protected]

Configure a push macro to perform the last action:

git config remote.charlie.url ssh://review.example.com:29418/project
git config remote.charlie.push HEAD:refs/for/master%[email protected],[email protected],[email protected]

afterwards .git/config contains the following:

[remote "charlie"]
  url = ssh://review.example.com:29418/project
  push = HEAD:refs/for/master%[email protected],[email protected],[email protected]

and now sending a new change for review to charlie, CC’ing both alice and bob is much easier:

git push charlie
Footer answered 9/1, 2018 at 9:42 Comment(0)
P
1

There is very nice Git-review tool for simplifying your life with Gerrit. Apart of other syntax sugar advantages, it allows you to add reviewers via command-line like:

git review --reviewers [email protected] [email protected]

And of course you can create git aliases out of it to make it simpler like:

#git review --reviewers [email protected] [email protected]
git publish

It's not directly answering your question "how to add reviewers from commit message", but it can be used, for example, to create custom pre-commit hook and extract emails first and push them to gerrit second

Portentous answered 26/3, 2018 at 12:8 Comment(0)
H
0

Combine gerrit stream-events and set-reviewers, you can do this.

Actually, you first need get the CL's status (create, update & ...), then execute corresponding action, such as add reviewers, even submit CL.

  1. get gerrit events from "gerrit stream-events" command.

  2. parse the JSON format event as each line. In the JSON event, we can get project, branch, Change-Id, CL-number, subject of Commit(not entire commit), and a lot information.

if you really need entire commit message, "gerrit query" is useful.

filter the "patchset-created", patchSet's number is 1(if you only like do this for new patchSet), parse the subject of commit, and get the reviewers as you predefined.

  1. use "gerrit set-reviewers" command, to add reviewer.

as my practice, use python, all this is easy.

Its flow is like a tiny Jenkins.

Huntington answered 30/3, 2015 at 3:32 Comment(0)
S
0

Below solution works

ssh -p gerrit set-reviewers [--project (PROJECT) | -p (PROJECT)] [--add (REVIEWER) … | -a (REVIEWER) …] [--] {COMMIT | CHANGE-ID}

Example: ssh -p 29418 gerrit.example.com gerrit set-reviewers -a [email protected] Iac6b2ac2

Soy answered 27/2, 2018 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.