Gerrit notifications via Rocket.Chat

Case

Gerrit often sends a lot of emails, especially if you take part in many projects. For a while we felt that sometimes it’s hard to notice the most important ones, like reviewers’ and Sputnik‘s comments on our changes. We use Rocket.Chat for text communication inside the company and most of us are connected throughout the day, so we thought it might be useful to get chat message notification every time a comment is added on one’s changes.

Gerrit hooks

Gerrit has a built-in mechanism for running hooks – scripts that are called whenever a specific event occurs. The script must be named the same as the hook. We created a bash script named comment-added, which is run every time someone adds a comment. Gerrit provides it with a lot of useful parameters, like project name, comment author, score, change owner, etc. Full documentation can be found here. After parsing those parameters, we can send a message to change owner on Rocket.Chat.

Gerrit hooks script have to be placed in a certain location. To avoid manually updating the files there, we set up a repository for hook scripts. They are periodically pulled to the correct location which simplifies the process of making changes to the scripts.

Integration with Rocket.Chat

Rocket.Chat has a pretty versatile REST API that allows us to send messages by calling curl from the comment-added script. Full documentation for the API can be found here. Currently, the API is in beta version, but so far we haven’t had any issues with it.

We previously set up a user that sends Jenkins automatic messages to our team chat and we reuse this user here. Firstly, we have to log in by calling api/v1/login endpoint:

curl https://<Rocket.Chat server address>/api/v1/login -d "username=<username>&password=<password>"

In response, we get a JSON with the logged user’s id and authorization token:

{
  "status": "success",
  "data": {
    "authToken": "<authorization token>",
    "userId": "<user id>"
  }
}

Next, we send a direct message to user by their username (in our case, we can get the username from change owner’s email), calling api/v1/chat.postMessage endpoint. This sends a direct message to the user, even if there was no previous conversation between the users – no need to set up a room or open chat. Example:

curl -H "X-Auth-Token: <authorization token>" \
  -H "X-User-Id: <user id>" \
  -H "Content-Type: application/json" \
  -d "{\"channel\": \"@<recipient username>\", \"text\": \"<message>\"}" \
  https://<Rocket.Chat server address>/api/v1/chat.postMessage

Summary

We created a simple script to solve the issue of getting notified when we get comments on our changes. So far, the team seems pleased with how this works and finds it quite useful. We hope that it would be useful for you as well – full code can be found here.

You May Also Like

Complex flows with Apache Camel

At work, we're mainly integrating services and systems, and since we're on a constant lookout for new, better technologies, ways to do things easier, make them more sustainable, we're trying to Usually we use Apache Camel for this task, which is a Swis...At work, we're mainly integrating services and systems, and since we're on a constant lookout for new, better technologies, ways to do things easier, make them more sustainable, we're trying to Usually we use Apache Camel for this task, which is a Swis...

Inconsistent Dependency Injection to domains with Grails

I've encountered strange behavior with a domain class in my project: services that should be injected were null. I've became suspicious as why is that? Services are injected properly in other domain classes so why this one is different?

Constructors experiment

I've created an experiment. I've created empty LibraryService that should be injected and Book domain class like this:

class Book {
def libraryService

String author
String title
int pageCount

Book() {
println("Finished constructor Book()")
}

Book(String author) {
this()
this.@author = author
println("Finished constructor Book(String author)")
}

Book(String author, String title) {
super()
this.@author = author
this.@title = title
println("Finished constructor Book(String author, String title)")
}

Book(String author, String title, int pageCount) {
this.@author = author
this.@title = title
this.@pageCount = pageCount
println("Finished constructor Book(String author, String title, int pageCount)")
}

void logInjectedService() {
println(" Service libraryService is injected? -> $libraryService")
}
}
class LibraryService {
def serviceMethod() {
}
}

Book has 4 explicit constructors. I want to check which constructor is injecting dependecies. This is my method that constructs Book objects and I called it in controller:

class BookController {
def index() {
constructAndExamineBooks()
}

static constructAndExamineBooks() {
println("Started constructAndExamineBooks")
Book book1 = new Book().logInjectedService()
Book book2 = new Book("foo").logInjectedService()
Book book3 = new Book("foo", 'bar').logInjectedService()
Book book4 = new Book("foo", 'bar', 100).logInjectedService()
Book book5 = new Book(author: "foo", title: 'bar')
println("Finished constructor Book(Map params)")
book5.logInjectedService()
}
}

Analysis

Output looks like this:

Started constructAndExamineBooks
Finished constructor Book()
Service libraryService is injected? -> eu.spoonman.refaktor.LibraryService@2affcce2
Finished constructor Book()
Finished constructor Book(String author)
Service libraryService is injected? -> eu.spoonman.refaktor.LibraryService@2affcce2
Finished constructor Book(String author, String title)
Service libraryService is injected? -> null
Finished constructor Book(String author, String title, int pageCount)
Service libraryService is injected? -> null
Finished constructor Book()
Finished constructor Book(Map params)
Service libraryService is injected? -> eu.spoonman.refaktor.LibraryService@2affcce2

What do we see?

  1. Empty constructor injects dependencies.
  2. Constructor that invokes empty constructor explicitly injects dependencies.
  3. Constructor that invokes parent's constructor explicitly does not inject dependencies.
  4. Constructor without any explicit call declared does not call empty constructor thus it does not inject dependencies.
  5. Constructor provied by Grails with a map as a parameter invokes empty constructor and injects dependencies.

Conclusion

Always explicitily invoke empty constructor in your Grail domain classes to ensure Dependency Injection! I didn't know until today either!