Gerrit Code Review: how to script a plugin with Scala and Groovy

Luca Milanesio
Luca MilanesioDirector at GerritForge Ltd
Luca Milanesio
GerritForge
Luca@gerritforge.com
http://www.gerritforge.com
Twitter: @gitenterprise
How to script a Plugin
2
About Luca
• Luca Milanesio
Co-founder of GerritForge
• over 20 years of experience
in Agile Development
SCM, CI and ALM worldwide
• Contributor to Jenkins
since 2007 (and previously Hudson)
• Git SCM mentor
for the Enterprise since 2009
• Contributor to Gerrit Code Review community since 2011
3
About GerritForge
Founded in 2009 in London UK
Mission: Agile Enterprise
Products:
4
Agenda
 Where we come from ?
 2 years ago: Gerrit plugins
 We want more plugins
 Create a plugin in 60 seconds
 What, how and when are coming?
 Plugins showcase
5
WHO REMEMBERS
THIS ?
GitTogether 2011
Gerrit 2.2.x
6
Why don’t we
introduce plugins ?
They have been so
successful with
Jenkins
7
Gerrit Hackathon
7th of May 2012
The first “helloworld”
plugin was born
8
WHO REMEMBERS THIS ?
Gerrit Summit 2012
Ver. 2.5.x
9
Gerrit Plugins
Growth
in 2 years
50
10
Can we do more ?
Let's ask Jenkins 
11
PLUGINS SUCCESS
=
COMMUNITY
ENGAGEMENT
12
COMMUNITY
=
BRIDGING
DIFFERENCES
13
That would be very useful for
Gerrit Administrators to be
able to write quick automation
scripts for their daily tasks. I
would prioritize groovy plugin
more for the popularity of
groovy scripting.
I think adding a "scripting
language plugin" to support
plugins written in scripting
languages is a very good idea
Hi all,
I was thinking about extending
Gerrit capabilities to load
plugins / extensions by
providing "wrapper" for other
JVM languages.
So … we asked the Community
I would prefer scala, because
I've already written my plugins
with it. imho the builld process is
the main impediment.
14
CHALLENGE
for building a
Gerrit Plugin
15
NO STEPS, NO BUILD … just do it !
$ cat - > ~/gerrit/plugins/hello-1.0.groovy
import com.google.gerrit.sshd.*
import com.google.gerrit.extensions.annotations.*
@Export("groovy")
class GroovyCommand extends SshCommand {
public void run() { stdout.println "Hi from Groovy" }
}
^D
This sample has 190 chars: you need to type
at least 3.2 chars/sec to complete the sample
in only one minute 
https://gist.github.com/lucamilanesio/9687053
16
IS THAT TRUE ? Let’s check on Gerrit
$ ssh –p 29418 user@localhost gerrit plugin ls
Name Version Status File
----------------------------------------------------------------------
hello 1.0 ENABLED hello-1.0.groovy
Gerrit auto-detects the new Groovy file under
$GERRIT_SITE/plugins, invoke the interpreter
and auto-wrap the class into a self-contained Gerrit
plugin.
Groovy class is compiled into byte-code BUT is
slower than native Java plugin.
17
DOES IT REALLY WORK ?
$ ssh –p 29418 user@localhost hello groovy
Hi from Groovy
No magic … IT IS ALL REAL !
Plugin name (hello) comes from the
script filename.
A new SSH command (groovy) has
been defined in Gerrit associated to
the Groovy script loaded.
18
What about Scala ? Just do it again !
$ cat - > ~/gerrit/plugins/hi-1.0.scala
import com.google.gerrit.sshd._
import com.google.gerrit.extensions.annotations._
@Export("scala")
class ScalaCommand extends SshCommand {
override def run = stdout println "Hi from Scala"
}
^D
Scala is more concise with just 178 chars :
you can take it easy with 2.9 chars/sec to type
it a minute 
https://gist.github.com/lucamilanesio/9687092
19
You have now two plugins loaded
$ ssh –p 29418 user@localhost gerrit plugin ls
Name Version Status File
----------------------------------------------------------------------
hello 1.0 ENABLED hello-1.0.groovy
hi 1.0 ENABLED hi-1.0.scala
There are no differences for Gerrit between
Java, Groovy or Scala plugins: they have the same
dignity and power.
Scala compiler takes longer but byte-code runs at
the same speed as a native Java plugin!
20
Reactions from the Gerrit mailing list …
So i checked it out and tried it, and
what should i say ...
Wow, it rocks! ;-)
Combined with new and shiny
Plugin API the code is really short.
So i started new repo
on gh [1] and created two working
plugins [2], [3], and sure would add
more, so to say,
cookbook-groovy-plugin:
$>ssh gerrit review approve
I59302cbb
$>Approve change: I59302cbb.
What do
YOU think
? 
21
WHAT can scripting plugins do now ?
1. Define new SSH commands
2. Define new REST APIs
3. Listen to Gerrit events
22
HOW can I get Gerrit with scripting plugins ?
Download the Gerrit master with scripting extensions from:
http://ci.gerritforge.com/job/Gerrit-master-scripting/lastSuccessfulBuild/artifact/buck-
out/gen/gerrit-scripting.war
Run Gerrit init and say Y for installing the Scala and Groovy scripting plugins
providers:
*** Plugins
***
Install plugin groovy-provider version v2.9-rc1-325-g96d0d43 [y/N]? Y
Install plugin scala-provider version v2.9-rc1-325-g96d0d43 [y/N]? y
You may want to increase the JVM PermGen on gerrit.config when
loading/unloading scripting plugins
[container]
javaOptions = -XX:MaxPermSize=1024m
23
Scripting plugins
showcase
24
Create a branch through Gerrit API
$ cat - > ~/gerrit/plugins/branch-1.0.groovy
import com.google.gerrit.sshd.*
import com.google.gerrit.extensions.annotations.*
import com.google.gerrit.extensions.api.*
import com.google.gerrit.extensions.api.projects.*
import com.google.inject.*
import org.kohsuke.args4j.*
@Export("create")
@CommandMetaData(name = "create-branch", description = "Create branch")
class CreateBranch extends SshCommand {
@Inject GerritApi api
@Argument(index = 0, usage = "Project name", metaVar = "PROJECT")
String projectName
@Argument(index = 1, usage = "Branch name", metaVar = "BRANCH")
String branchName
@Argument(index = 2, usage = "Branch point sha1", metaVar = "SHA1")
String sha1
void run() {
BranchInput branch = new BranchInput()
branch.revision = sha1
api.projects().name(projectName).branch(branchName).create(branch)
stdout.println("Branch created: " + branchName)
}
}
^D
https://gist.github.com/lucamilanesio/9687118
25
Script classes get
Plugins Guice
Injections
(but cannot add Guice Modules)
26
List projects via REST API
$ cat - > ~/gerrit/plugins/projects-1.0.scala
import com.google.inject._
import com.google.gerrit.extensions.annotations._
import com.google.gerrit.server.project._
import javax.servlet.http._
import scala.collection.JavaConversions._
@Singleton @Export("/")
class Projects @Inject() (val pc: ProjectCache) extends HttpServlet {
override def doGet(req: HttpServletRequest, resp: HttpServletResponse) =
resp.getWriter print
"[" +
(pc.all map ( """ + _.get + """ ) mkString ",") +
"]"
}
^D
https://gist.github.com/lucamilanesio/9687133
27
Scala is
COMPACT + FUNCTIONAL
NOTE: for Guice injection
@Inject() before constructor vals
28
FUNCTIONAL
IS PERFECT
FOR …
29
Scalable in-process hooks
$ cat - > ~/gerrit/plugins/inprochook-1.0.scala
import org.slf4j.LoggerFactory._
import com.google.inject._
import com.google.gerrit.common._
import com.google.gerrit.server.events._
import com.google.gerrit.extensions.registration.DynamicSet
@Singleton
class ListenToChanges extends ChangeListener {
val log = getLogger(classOf[ListenToChanges])
override def onChangeEvent(e: ChangeEvent) = e match {
case comm: CommentAddedEvent =>
log info s"${comm.author.name} said '${comm.comment}' " +
s"on ${comm.change.project}/${comm.change.number}'"
case _ =>
}
}
class HooksModule extends AbstractModule {
override def configure =
DynamicSet bind(binder, classOf[ChangeListener]) to classOf[ListenToChanges]
}
^D
 Less CPU overhead (no exec/fork)  faster and scalable
 Works on behalf of user thread identity
 Access Gerrit injected objects (Cache, ReviewDb, JGit)
https://gist.github.com/lucamilanesio/9687154
30
Commit message validation
$ cat - > ~/gerrit/plugins/commitheadline-1.0.scala
import scala.collection.JavaConversions._
import com.google.inject._
import com.google.gerrit.extensions.annotations._
import com.google.gerrit.server.git.validators._
import com.google.gerrit.server.events._
@Singleton
@Listen
class HasIssueInCommitHeadline extends CommitValidationListener {
override def onCommitReceived(e: CommitReceivedEvent) =
"[A-Z]+-[0-9]+".r findFirstIn e.commit.getShortMessage map {
issue => Seq(new CommitValidationMessage(s"Issue: $issue", false))
} getOrElse {
throw new CommitValidationException("Missing JIRA-ID in commit headline")
}
}
^D
 Native pattern-matching support
 Java regex available out-of-the box
 Support for Optional values (map/getOrElse)
https://gist.github.com/lucamilanesio/9687174
31
And there is more …
audit events
download commands
project and group events
message of the day
Prolog predicates
auth backends
avatars
32
WHEN scripting plugins will be included in Gerrit ?
Hackathon #6
24-26th March we will keep
on hacking on Gerrit
MISSION:
improve, stabilise and
merge scripting plugins into
Gerrit master !
TARGET VERSION:
Gerrit Ver. 2.10 (?)
33
Try it on-line (with scripting): http://gerrithub.io/login
Read the Gerrit book: http://gerrithub.io/book
Keep in touch: http://gitenterprise.me
Learn more about Gerrit with
20% OFF Book discount for
Gerrit User Summit 2014
Book PROMO-CODE: LGCRB20
eBook PROMO-CODE: LGCReB20
1 of 34

Recommended

Gerrit Code Review with GitHub plugin by
Gerrit Code Review with GitHub pluginGerrit Code Review with GitHub plugin
Gerrit Code Review with GitHub pluginLuca Milanesio
15K views31 slides
Introduction to Grafana Loki by
Introduction to Grafana LokiIntroduction to Grafana Loki
Introduction to Grafana LokiJulien Pivotto
190 views11 slides
Designing a complete ci cd pipeline using argo events, workflow and cd products by
Designing a complete ci cd pipeline using argo events, workflow and cd productsDesigning a complete ci cd pipeline using argo events, workflow and cd products
Designing a complete ci cd pipeline using argo events, workflow and cd productsJulian Mazzitelli
11.5K views20 slides
Google Kubernetes Engine (GKE) deep dive by
Google Kubernetes Engine (GKE) deep diveGoogle Kubernetes Engine (GKE) deep dive
Google Kubernetes Engine (GKE) deep diveAkash Agrawal
991 views21 slides
OpenTelemetryを用いたObservability基礎の実装 with AWS Distro for OpenTelemetry(Kuberne... by
OpenTelemetryを用いたObservability基礎の実装 with AWS Distro for OpenTelemetry(Kuberne...OpenTelemetryを用いたObservability基礎の実装 with AWS Distro for OpenTelemetry(Kuberne...
OpenTelemetryを用いたObservability基礎の実装 with AWS Distro for OpenTelemetry(Kuberne...NTT DATA Technology & Innovation
144 views19 slides
(Declarative) Jenkins Pipelines by
(Declarative) Jenkins Pipelines(Declarative) Jenkins Pipelines
(Declarative) Jenkins PipelinesSteffen Gebert
21.7K views52 slides

More Related Content

What's hot

Meetup 23 - 03 - Application Delivery on K8S with GitOps by
Meetup 23 - 03 - Application Delivery on K8S with GitOpsMeetup 23 - 03 - Application Delivery on K8S with GitOps
Meetup 23 - 03 - Application Delivery on K8S with GitOpsVietnam Open Infrastructure User Group
462 views30 slides
Unit 2: Microservices Secrets Management 101 by
Unit 2: Microservices Secrets Management 101Unit 2: Microservices Secrets Management 101
Unit 2: Microservices Secrets Management 101NGINX, Inc.
111 views25 slides
NGINX Ingress Controller for Kubernetes by
NGINX Ingress Controller for KubernetesNGINX Ingress Controller for Kubernetes
NGINX Ingress Controller for KubernetesNGINX, Inc.
2.1K views28 slides
Kubernetes Application Deployment with Helm - A beginner Guide! by
Kubernetes Application Deployment with Helm - A beginner Guide!Kubernetes Application Deployment with Helm - A beginner Guide!
Kubernetes Application Deployment with Helm - A beginner Guide!Krishna-Kumar
2.3K views19 slides
CI with Gitlab & Docker by
CI with Gitlab & DockerCI with Gitlab & Docker
CI with Gitlab & DockerJoerg Henning
3.9K views15 slides
Distributed tracing 101 by
Distributed tracing 101Distributed tracing 101
Distributed tracing 101Itiel Shwartz
1K views25 slides

What's hot(20)

Unit 2: Microservices Secrets Management 101 by NGINX, Inc.
Unit 2: Microservices Secrets Management 101Unit 2: Microservices Secrets Management 101
Unit 2: Microservices Secrets Management 101
NGINX, Inc.111 views
NGINX Ingress Controller for Kubernetes by NGINX, Inc.
NGINX Ingress Controller for KubernetesNGINX Ingress Controller for Kubernetes
NGINX Ingress Controller for Kubernetes
NGINX, Inc.2.1K views
Kubernetes Application Deployment with Helm - A beginner Guide! by Krishna-Kumar
Kubernetes Application Deployment with Helm - A beginner Guide!Kubernetes Application Deployment with Helm - A beginner Guide!
Kubernetes Application Deployment with Helm - A beginner Guide!
Krishna-Kumar 2.3K views
CI with Gitlab & Docker by Joerg Henning
CI with Gitlab & DockerCI with Gitlab & Docker
CI with Gitlab & Docker
Joerg Henning3.9K views
Intro to Github Actions @likecoin by William Chong
Intro to Github Actions @likecoinIntro to Github Actions @likecoin
Intro to Github Actions @likecoin
William Chong202 views
Monitoring Kubernetes with Prometheus (Kubernetes Ireland, 2016) by Brian Brazil
Monitoring Kubernetes with Prometheus (Kubernetes Ireland, 2016)Monitoring Kubernetes with Prometheus (Kubernetes Ireland, 2016)
Monitoring Kubernetes with Prometheus (Kubernetes Ireland, 2016)
Brian Brazil16.5K views
The 12 Factor App by rudiyardley
The 12 Factor AppThe 12 Factor App
The 12 Factor App
rudiyardley6.7K views
Why Users Are Moving on from Docker and Leaving Its Security Risks Behind (Sp... by Amazon Web Services
Why Users Are Moving on from Docker and Leaving Its Security Risks Behind (Sp...Why Users Are Moving on from Docker and Leaving Its Security Risks Behind (Sp...
Why Users Are Moving on from Docker and Leaving Its Security Risks Behind (Sp...
Amazon Web Services1.9K views
OpenTelemetry Introduction by DimitrisFinas1
OpenTelemetry Introduction OpenTelemetry Introduction
OpenTelemetry Introduction
DimitrisFinas1711 views
DCSF19 Dockerfile Best Practices by Docker, Inc.
DCSF19 Dockerfile Best PracticesDCSF19 Dockerfile Best Practices
DCSF19 Dockerfile Best Practices
Docker, Inc.24.9K views
Kubernetes or OpenShift - choosing your container platform for Dev and Ops by Tomasz Cholewa
Kubernetes or OpenShift - choosing your container platform for Dev and OpsKubernetes or OpenShift - choosing your container platform for Dev and Ops
Kubernetes or OpenShift - choosing your container platform for Dev and Ops
Tomasz Cholewa847 views
Observability: Beyond the Three Pillars with Spring by VMware Tanzu
Observability: Beyond the Three Pillars with SpringObservability: Beyond the Three Pillars with Spring
Observability: Beyond the Three Pillars with Spring
VMware Tanzu364 views
Kubernetes by Henry He
KubernetesKubernetes
Kubernetes
Henry He161 views
CI-CD Jenkins, GitHub Actions, Tekton by Araf Karsh Hamid
CI-CD Jenkins, GitHub Actions, Tekton CI-CD Jenkins, GitHub Actions, Tekton
CI-CD Jenkins, GitHub Actions, Tekton
Araf Karsh Hamid1.2K views
OpenShift 4 installation by Robert Bohne
OpenShift 4 installationOpenShift 4 installation
OpenShift 4 installation
Robert Bohne983 views

Viewers also liked

Gerrit Code Review Analytics by
Gerrit Code Review AnalyticsGerrit Code Review Analytics
Gerrit Code Review AnalyticsLuca Milanesio
3.9K views23 slides
Speed up Continuous Delivery with BigData Analytics by
Speed up Continuous Delivery with BigData AnalyticsSpeed up Continuous Delivery with BigData Analytics
Speed up Continuous Delivery with BigData AnalyticsLuca Milanesio
1.1K views27 slides
Mobile Application Lifecycle with Jekins, Trello and CollabNet TeamForge by
Mobile Application Lifecycle with Jekins, Trello and CollabNet TeamForgeMobile Application Lifecycle with Jekins, Trello and CollabNet TeamForge
Mobile Application Lifecycle with Jekins, Trello and CollabNet TeamForgeLuca Milanesio
7.6K views39 slides
Gerrit jenkins-big data-continuous-delivery by
Gerrit jenkins-big data-continuous-deliveryGerrit jenkins-big data-continuous-delivery
Gerrit jenkins-big data-continuous-deliveryLuca Milanesio
1.7K views20 slides
Git workshop 33degree 2011 krakow by
Git workshop 33degree 2011 krakowGit workshop 33degree 2011 krakow
Git workshop 33degree 2011 krakowLuca Milanesio
2.2K views112 slides
Is TDD dead or alive? by
Is TDD dead or alive?Is TDD dead or alive?
Is TDD dead or alive?Luca Milanesio
2.6K views66 slides

Viewers also liked(13)

Gerrit Code Review Analytics by Luca Milanesio
Gerrit Code Review AnalyticsGerrit Code Review Analytics
Gerrit Code Review Analytics
Luca Milanesio3.9K views
Speed up Continuous Delivery with BigData Analytics by Luca Milanesio
Speed up Continuous Delivery with BigData AnalyticsSpeed up Continuous Delivery with BigData Analytics
Speed up Continuous Delivery with BigData Analytics
Luca Milanesio1.1K views
Mobile Application Lifecycle with Jekins, Trello and CollabNet TeamForge by Luca Milanesio
Mobile Application Lifecycle with Jekins, Trello and CollabNet TeamForgeMobile Application Lifecycle with Jekins, Trello and CollabNet TeamForge
Mobile Application Lifecycle with Jekins, Trello and CollabNet TeamForge
Luca Milanesio7.6K views
Gerrit jenkins-big data-continuous-delivery by Luca Milanesio
Gerrit jenkins-big data-continuous-deliveryGerrit jenkins-big data-continuous-delivery
Gerrit jenkins-big data-continuous-delivery
Luca Milanesio1.7K views
Git workshop 33degree 2011 krakow by Luca Milanesio
Git workshop 33degree 2011 krakowGit workshop 33degree 2011 krakow
Git workshop 33degree 2011 krakow
Luca Milanesio2.2K views
Gerrit: how to cook a plugin in only 10 mins by Luca Milanesio
Gerrit: how to cook a plugin in only 10 minsGerrit: how to cook a plugin in only 10 mins
Gerrit: how to cook a plugin in only 10 mins
Luca Milanesio10.9K views
GitBlit plugin for Gerrit Code Review by Luca Milanesio
GitBlit plugin for Gerrit Code ReviewGitBlit plugin for Gerrit Code Review
GitBlit plugin for Gerrit Code Review
Luca Milanesio12.1K views
GerritHub.io - present, past, future by Luca Milanesio
GerritHub.io - present, past, futureGerritHub.io - present, past, future
GerritHub.io - present, past, future
Luca Milanesio8.2K views
Devoxx 2016 Using Jenkins, Gerrit and Spark for Continuous Delivery Analytics by Luca Milanesio
Devoxx 2016 Using Jenkins, Gerrit and Spark for Continuous Delivery AnalyticsDevoxx 2016 Using Jenkins, Gerrit and Spark for Continuous Delivery Analytics
Devoxx 2016 Using Jenkins, Gerrit and Spark for Continuous Delivery Analytics
Luca Milanesio1.3K views
Zero-Downtime Gerrit Code Review Upgrade by Luca Milanesio
Zero-Downtime Gerrit Code Review UpgradeZero-Downtime Gerrit Code Review Upgrade
Zero-Downtime Gerrit Code Review Upgrade
Luca Milanesio3.4K views
Gerrit is Getting Native with RPM, Deb and Docker by Luca Milanesio
Gerrit is Getting Native with RPM, Deb and DockerGerrit is Getting Native with RPM, Deb and Docker
Gerrit is Getting Native with RPM, Deb and Docker
Luca Milanesio3.2K views
Jenkins User Conference - Continuous Delivery on Mobile by Luca Milanesio
Jenkins User Conference - Continuous Delivery on MobileJenkins User Conference - Continuous Delivery on Mobile
Jenkins User Conference - Continuous Delivery on Mobile
Luca Milanesio2.9K views

Similar to Gerrit Code Review: how to script a plugin with Scala and Groovy

GIT By Sivakrishna by
GIT By SivakrishnaGIT By Sivakrishna
GIT By SivakrishnaNyros Technologies
1.1K views28 slides
Github By Nyros Developer by
Github By Nyros DeveloperGithub By Nyros Developer
Github By Nyros DeveloperNyros Technologies
682 views24 slides
Introduction to git and Github by
Introduction to git and GithubIntroduction to git and Github
Introduction to git and GithubWycliff1
125 views22 slides
FOSDEM 2017: GitLab CI by
FOSDEM 2017:  GitLab CIFOSDEM 2017:  GitLab CI
FOSDEM 2017: GitLab CIOlinData
1.1K views19 slides
Matt Gauger - Git & Github web414 December 2010 by
Matt Gauger - Git & Github web414 December 2010Matt Gauger - Git & Github web414 December 2010
Matt Gauger - Git & Github web414 December 2010Matt Gauger
1.7K views132 slides
GDSC GIT AND GITHUB by
GDSC GIT AND GITHUB GDSC GIT AND GITHUB
GDSC GIT AND GITHUB GDSCIIITDHARWAD
51 views13 slides

Similar to Gerrit Code Review: how to script a plugin with Scala and Groovy(20)

Introduction to git and Github by Wycliff1
Introduction to git and GithubIntroduction to git and Github
Introduction to git and Github
Wycliff1125 views
FOSDEM 2017: GitLab CI by OlinData
FOSDEM 2017:  GitLab CIFOSDEM 2017:  GitLab CI
FOSDEM 2017: GitLab CI
OlinData1.1K views
Matt Gauger - Git & Github web414 December 2010 by Matt Gauger
Matt Gauger - Git & Github web414 December 2010Matt Gauger - Git & Github web414 December 2010
Matt Gauger - Git & Github web414 December 2010
Matt Gauger1.7K views
Gitlab Training with GIT and SourceTree by Teerapat Khunpech
Gitlab Training with GIT and SourceTreeGitlab Training with GIT and SourceTree
Gitlab Training with GIT and SourceTree
Teerapat Khunpech34.5K views
Testing of javacript by Lei Kang
Testing of javacriptTesting of javacript
Testing of javacript
Lei Kang3K views
2015-ghci-presentation-git_gerritJenkins_final by Mythri P K
2015-ghci-presentation-git_gerritJenkins_final2015-ghci-presentation-git_gerritJenkins_final
2015-ghci-presentation-git_gerritJenkins_final
Mythri P K345 views
Openstack contribution process by Syed Armani
Openstack contribution processOpenstack contribution process
Openstack contribution process
Syed Armani1.8K views
OpenStack Contribution Process by openstackindia
OpenStack Contribution ProcessOpenStack Contribution Process
OpenStack Contribution Process
openstackindia497 views
introductiontogitandgithub-120702044048-phpapp01.pdf by BruceLee275640
introductiontogitandgithub-120702044048-phpapp01.pdfintroductiontogitandgithub-120702044048-phpapp01.pdf
introductiontogitandgithub-120702044048-phpapp01.pdf
BruceLee2756407 views
Git Distributed Version Control System by Victor Wong
Git   Distributed Version Control SystemGit   Distributed Version Control System
Git Distributed Version Control System
Victor Wong825 views
Introduction to git and github by Aderemi Dadepo
Introduction to git and githubIntroduction to git and github
Introduction to git and github
Aderemi Dadepo4.3K views
Grails beginners workshop by JacobAae
Grails beginners workshopGrails beginners workshop
Grails beginners workshop
JacobAae369 views

More from Luca Milanesio

What's new in Gerrit Code Review v3.1 and beyond by
What's new in Gerrit Code Review v3.1 and beyondWhat's new in Gerrit Code Review v3.1 and beyond
What's new in Gerrit Code Review v3.1 and beyondLuca Milanesio
231 views18 slides
Gerrit Analytics applied to Android source code by
Gerrit Analytics applied to Android source codeGerrit Analytics applied to Android source code
Gerrit Analytics applied to Android source codeLuca Milanesio
241 views26 slides
Cloud-native Gerrit Code Review by
Cloud-native Gerrit Code ReviewCloud-native Gerrit Code Review
Cloud-native Gerrit Code ReviewLuca Milanesio
229 views24 slides
Gerrit Code Review migrations step-by-step by
Gerrit Code Review migrations step-by-stepGerrit Code Review migrations step-by-step
Gerrit Code Review migrations step-by-stepLuca Milanesio
311 views39 slides
Gerrit Code Review v3.2 and v3.3 by
Gerrit Code Review v3.2 and v3.3Gerrit Code Review v3.2 and v3.3
Gerrit Code Review v3.2 and v3.3Luca Milanesio
142 views25 slides
ChronicleMap non-blocking cache for Gerrit v3.3 by
ChronicleMap non-blocking cache for Gerrit v3.3ChronicleMap non-blocking cache for Gerrit v3.3
ChronicleMap non-blocking cache for Gerrit v3.3Luca Milanesio
85 views17 slides

More from Luca Milanesio(18)

What's new in Gerrit Code Review v3.1 and beyond by Luca Milanesio
What's new in Gerrit Code Review v3.1 and beyondWhat's new in Gerrit Code Review v3.1 and beyond
What's new in Gerrit Code Review v3.1 and beyond
Luca Milanesio231 views
Gerrit Analytics applied to Android source code by Luca Milanesio
Gerrit Analytics applied to Android source codeGerrit Analytics applied to Android source code
Gerrit Analytics applied to Android source code
Luca Milanesio241 views
Cloud-native Gerrit Code Review by Luca Milanesio
Cloud-native Gerrit Code ReviewCloud-native Gerrit Code Review
Cloud-native Gerrit Code Review
Luca Milanesio229 views
Gerrit Code Review migrations step-by-step by Luca Milanesio
Gerrit Code Review migrations step-by-stepGerrit Code Review migrations step-by-step
Gerrit Code Review migrations step-by-step
Luca Milanesio311 views
Gerrit Code Review v3.2 and v3.3 by Luca Milanesio
Gerrit Code Review v3.2 and v3.3Gerrit Code Review v3.2 and v3.3
Gerrit Code Review v3.2 and v3.3
Luca Milanesio142 views
ChronicleMap non-blocking cache for Gerrit v3.3 by Luca Milanesio
ChronicleMap non-blocking cache for Gerrit v3.3ChronicleMap non-blocking cache for Gerrit v3.3
ChronicleMap non-blocking cache for Gerrit v3.3
Luca Milanesio85 views
Gerrit Code Review multi-site by Luca Milanesio
Gerrit Code Review multi-siteGerrit Code Review multi-site
Gerrit Code Review multi-site
Luca Milanesio1.1K views
What's new in Gerrit Code Review 3.0 by Luca Milanesio
What's new in Gerrit Code Review 3.0What's new in Gerrit Code Review 3.0
What's new in Gerrit Code Review 3.0
Luca Milanesio1.1K views
Gerrit User Summit 2019 Keynote by Luca Milanesio
Gerrit User Summit 2019 KeynoteGerrit User Summit 2019 Keynote
Gerrit User Summit 2019 Keynote
Luca Milanesio296 views
Gerrit multi-master / multi-site at GerritHub by Luca Milanesio
Gerrit multi-master / multi-site at GerritHubGerrit multi-master / multi-site at GerritHub
Gerrit multi-master / multi-site at GerritHub
Luca Milanesio908 views
GerritHub a true Gerrit migration story to v2.15 by Luca Milanesio
GerritHub a true Gerrit migration story to v2.15GerritHub a true Gerrit migration story to v2.15
GerritHub a true Gerrit migration story to v2.15
Luca Milanesio438 views
Gerrit User Summit 2018 - Keynote by Luca Milanesio
Gerrit User Summit 2018 - Keynote Gerrit User Summit 2018 - Keynote
Gerrit User Summit 2018 - Keynote
Luca Milanesio588 views
Jenkins plugin for Gerrit Code Review pipelines by Luca Milanesio
Jenkins plugin for Gerrit Code Review pipelinesJenkins plugin for Gerrit Code Review pipelines
Jenkins plugin for Gerrit Code Review pipelines
Luca Milanesio1K views
Gerrit User Summit 2017 Keynote by Luca Milanesio
Gerrit User Summit 2017 KeynoteGerrit User Summit 2017 Keynote
Gerrit User Summit 2017 Keynote
Luca Milanesio495 views
How to keep Jenkins logs forever without performance issues by Luca Milanesio
How to keep Jenkins logs forever without performance issuesHow to keep Jenkins logs forever without performance issues
How to keep Jenkins logs forever without performance issues
Luca Milanesio1.1K views
Jenkins Pipeline on your Local Box to Reduce Cycle Time by Luca Milanesio
Jenkins Pipeline on your Local Box to Reduce Cycle TimeJenkins Pipeline on your Local Box to Reduce Cycle Time
Jenkins Pipeline on your Local Box to Reduce Cycle Time
Luca Milanesio498 views
Jenkins world 2017 - Data-Driven CI Pipeline with Gerrit Code Review by Luca Milanesio
Jenkins world 2017 - Data-Driven CI Pipeline with Gerrit Code ReviewJenkins world 2017 - Data-Driven CI Pipeline with Gerrit Code Review
Jenkins world 2017 - Data-Driven CI Pipeline with Gerrit Code Review
Luca Milanesio1.1K views
Stable master workflow with Gerrit Code Review by Luca Milanesio
Stable master workflow with Gerrit Code ReviewStable master workflow with Gerrit Code Review
Stable master workflow with Gerrit Code Review
Luca Milanesio1.9K views

Recently uploaded

Solar System and Galaxies.pptx by
Solar System and Galaxies.pptxSolar System and Galaxies.pptx
Solar System and Galaxies.pptxDrHafizKosar
91 views26 slides
UNIDAD 3 6º C.MEDIO.pptx by
UNIDAD 3 6º C.MEDIO.pptxUNIDAD 3 6º C.MEDIO.pptx
UNIDAD 3 6º C.MEDIO.pptxMarcosRodriguezUcedo
78 views32 slides
Recap of our Class by
Recap of our ClassRecap of our Class
Recap of our ClassCorinne Weisgerber
77 views15 slides
MIXING OF PHARMACEUTICALS.pptx by
MIXING OF PHARMACEUTICALS.pptxMIXING OF PHARMACEUTICALS.pptx
MIXING OF PHARMACEUTICALS.pptxAnupkumar Sharma
77 views35 slides
Computer Introduction-Lecture06 by
Computer Introduction-Lecture06Computer Introduction-Lecture06
Computer Introduction-Lecture06Dr. Mazin Mohamed alkathiri
89 views12 slides
ICS3211_lecture 08_2023.pdf by
ICS3211_lecture 08_2023.pdfICS3211_lecture 08_2023.pdf
ICS3211_lecture 08_2023.pdfVanessa Camilleri
149 views30 slides

Recently uploaded(20)

Solar System and Galaxies.pptx by DrHafizKosar
Solar System and Galaxies.pptxSolar System and Galaxies.pptx
Solar System and Galaxies.pptx
DrHafizKosar91 views
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx by ISSIP
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptxEIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx
ISSIP369 views
Monthly Information Session for MV Asterix (November) by Esquimalt MFRC
Monthly Information Session for MV Asterix (November)Monthly Information Session for MV Asterix (November)
Monthly Information Session for MV Asterix (November)
Esquimalt MFRC55 views
11.28.23 Social Capital and Social Exclusion.pptx by mary850239
11.28.23 Social Capital and Social Exclusion.pptx11.28.23 Social Capital and Social Exclusion.pptx
11.28.23 Social Capital and Social Exclusion.pptx
mary850239298 views
Psychology KS5 by WestHatch
Psychology KS5Psychology KS5
Psychology KS5
WestHatch93 views
How to empty an One2many field in Odoo by Celine George
How to empty an One2many field in OdooHow to empty an One2many field in Odoo
How to empty an One2many field in Odoo
Celine George65 views
Ch. 8 Political Party and Party System.pptx by Rommel Regala
Ch. 8 Political Party and Party System.pptxCh. 8 Political Party and Party System.pptx
Ch. 8 Political Party and Party System.pptx
Rommel Regala49 views
Create a Structure in VBNet.pptx by Breach_P
Create a Structure in VBNet.pptxCreate a Structure in VBNet.pptx
Create a Structure in VBNet.pptx
Breach_P75 views
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB... by Nguyen Thanh Tu Collection
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant... by Ms. Pooja Bhandare
Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant...Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant...
Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant...
Ch. 7 Political Participation and Elections.pptx by Rommel Regala
Ch. 7 Political Participation and Elections.pptxCh. 7 Political Participation and Elections.pptx
Ch. 7 Political Participation and Elections.pptx
Rommel Regala97 views
AI Tools for Business and Startups by Svetlin Nakov
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and Startups
Svetlin Nakov107 views

Gerrit Code Review: how to script a plugin with Scala and Groovy

  • 2. 2 About Luca • Luca Milanesio Co-founder of GerritForge • over 20 years of experience in Agile Development SCM, CI and ALM worldwide • Contributor to Jenkins since 2007 (and previously Hudson) • Git SCM mentor for the Enterprise since 2009 • Contributor to Gerrit Code Review community since 2011
  • 3. 3 About GerritForge Founded in 2009 in London UK Mission: Agile Enterprise Products:
  • 4. 4 Agenda  Where we come from ?  2 years ago: Gerrit plugins  We want more plugins  Create a plugin in 60 seconds  What, how and when are coming?  Plugins showcase
  • 6. 6 Why don’t we introduce plugins ? They have been so successful with Jenkins
  • 7. 7 Gerrit Hackathon 7th of May 2012 The first “helloworld” plugin was born
  • 8. 8 WHO REMEMBERS THIS ? Gerrit Summit 2012 Ver. 2.5.x
  • 10. 10 Can we do more ? Let's ask Jenkins 
  • 13. 13 That would be very useful for Gerrit Administrators to be able to write quick automation scripts for their daily tasks. I would prioritize groovy plugin more for the popularity of groovy scripting. I think adding a "scripting language plugin" to support plugins written in scripting languages is a very good idea Hi all, I was thinking about extending Gerrit capabilities to load plugins / extensions by providing "wrapper" for other JVM languages. So … we asked the Community I would prefer scala, because I've already written my plugins with it. imho the builld process is the main impediment.
  • 15. 15 NO STEPS, NO BUILD … just do it ! $ cat - > ~/gerrit/plugins/hello-1.0.groovy import com.google.gerrit.sshd.* import com.google.gerrit.extensions.annotations.* @Export("groovy") class GroovyCommand extends SshCommand { public void run() { stdout.println "Hi from Groovy" } } ^D This sample has 190 chars: you need to type at least 3.2 chars/sec to complete the sample in only one minute  https://gist.github.com/lucamilanesio/9687053
  • 16. 16 IS THAT TRUE ? Let’s check on Gerrit $ ssh –p 29418 user@localhost gerrit plugin ls Name Version Status File ---------------------------------------------------------------------- hello 1.0 ENABLED hello-1.0.groovy Gerrit auto-detects the new Groovy file under $GERRIT_SITE/plugins, invoke the interpreter and auto-wrap the class into a self-contained Gerrit plugin. Groovy class is compiled into byte-code BUT is slower than native Java plugin.
  • 17. 17 DOES IT REALLY WORK ? $ ssh –p 29418 user@localhost hello groovy Hi from Groovy No magic … IT IS ALL REAL ! Plugin name (hello) comes from the script filename. A new SSH command (groovy) has been defined in Gerrit associated to the Groovy script loaded.
  • 18. 18 What about Scala ? Just do it again ! $ cat - > ~/gerrit/plugins/hi-1.0.scala import com.google.gerrit.sshd._ import com.google.gerrit.extensions.annotations._ @Export("scala") class ScalaCommand extends SshCommand { override def run = stdout println "Hi from Scala" } ^D Scala is more concise with just 178 chars : you can take it easy with 2.9 chars/sec to type it a minute  https://gist.github.com/lucamilanesio/9687092
  • 19. 19 You have now two plugins loaded $ ssh –p 29418 user@localhost gerrit plugin ls Name Version Status File ---------------------------------------------------------------------- hello 1.0 ENABLED hello-1.0.groovy hi 1.0 ENABLED hi-1.0.scala There are no differences for Gerrit between Java, Groovy or Scala plugins: they have the same dignity and power. Scala compiler takes longer but byte-code runs at the same speed as a native Java plugin!
  • 20. 20 Reactions from the Gerrit mailing list … So i checked it out and tried it, and what should i say ... Wow, it rocks! ;-) Combined with new and shiny Plugin API the code is really short. So i started new repo on gh [1] and created two working plugins [2], [3], and sure would add more, so to say, cookbook-groovy-plugin: $>ssh gerrit review approve I59302cbb $>Approve change: I59302cbb. What do YOU think ? 
  • 21. 21 WHAT can scripting plugins do now ? 1. Define new SSH commands 2. Define new REST APIs 3. Listen to Gerrit events
  • 22. 22 HOW can I get Gerrit with scripting plugins ? Download the Gerrit master with scripting extensions from: http://ci.gerritforge.com/job/Gerrit-master-scripting/lastSuccessfulBuild/artifact/buck- out/gen/gerrit-scripting.war Run Gerrit init and say Y for installing the Scala and Groovy scripting plugins providers: *** Plugins *** Install plugin groovy-provider version v2.9-rc1-325-g96d0d43 [y/N]? Y Install plugin scala-provider version v2.9-rc1-325-g96d0d43 [y/N]? y You may want to increase the JVM PermGen on gerrit.config when loading/unloading scripting plugins [container] javaOptions = -XX:MaxPermSize=1024m
  • 24. 24 Create a branch through Gerrit API $ cat - > ~/gerrit/plugins/branch-1.0.groovy import com.google.gerrit.sshd.* import com.google.gerrit.extensions.annotations.* import com.google.gerrit.extensions.api.* import com.google.gerrit.extensions.api.projects.* import com.google.inject.* import org.kohsuke.args4j.* @Export("create") @CommandMetaData(name = "create-branch", description = "Create branch") class CreateBranch extends SshCommand { @Inject GerritApi api @Argument(index = 0, usage = "Project name", metaVar = "PROJECT") String projectName @Argument(index = 1, usage = "Branch name", metaVar = "BRANCH") String branchName @Argument(index = 2, usage = "Branch point sha1", metaVar = "SHA1") String sha1 void run() { BranchInput branch = new BranchInput() branch.revision = sha1 api.projects().name(projectName).branch(branchName).create(branch) stdout.println("Branch created: " + branchName) } } ^D https://gist.github.com/lucamilanesio/9687118
  • 25. 25 Script classes get Plugins Guice Injections (but cannot add Guice Modules)
  • 26. 26 List projects via REST API $ cat - > ~/gerrit/plugins/projects-1.0.scala import com.google.inject._ import com.google.gerrit.extensions.annotations._ import com.google.gerrit.server.project._ import javax.servlet.http._ import scala.collection.JavaConversions._ @Singleton @Export("/") class Projects @Inject() (val pc: ProjectCache) extends HttpServlet { override def doGet(req: HttpServletRequest, resp: HttpServletResponse) = resp.getWriter print "[" + (pc.all map ( """ + _.get + """ ) mkString ",") + "]" } ^D https://gist.github.com/lucamilanesio/9687133
  • 27. 27 Scala is COMPACT + FUNCTIONAL NOTE: for Guice injection @Inject() before constructor vals
  • 29. 29 Scalable in-process hooks $ cat - > ~/gerrit/plugins/inprochook-1.0.scala import org.slf4j.LoggerFactory._ import com.google.inject._ import com.google.gerrit.common._ import com.google.gerrit.server.events._ import com.google.gerrit.extensions.registration.DynamicSet @Singleton class ListenToChanges extends ChangeListener { val log = getLogger(classOf[ListenToChanges]) override def onChangeEvent(e: ChangeEvent) = e match { case comm: CommentAddedEvent => log info s"${comm.author.name} said '${comm.comment}' " + s"on ${comm.change.project}/${comm.change.number}'" case _ => } } class HooksModule extends AbstractModule { override def configure = DynamicSet bind(binder, classOf[ChangeListener]) to classOf[ListenToChanges] } ^D  Less CPU overhead (no exec/fork)  faster and scalable  Works on behalf of user thread identity  Access Gerrit injected objects (Cache, ReviewDb, JGit) https://gist.github.com/lucamilanesio/9687154
  • 30. 30 Commit message validation $ cat - > ~/gerrit/plugins/commitheadline-1.0.scala import scala.collection.JavaConversions._ import com.google.inject._ import com.google.gerrit.extensions.annotations._ import com.google.gerrit.server.git.validators._ import com.google.gerrit.server.events._ @Singleton @Listen class HasIssueInCommitHeadline extends CommitValidationListener { override def onCommitReceived(e: CommitReceivedEvent) = "[A-Z]+-[0-9]+".r findFirstIn e.commit.getShortMessage map { issue => Seq(new CommitValidationMessage(s"Issue: $issue", false)) } getOrElse { throw new CommitValidationException("Missing JIRA-ID in commit headline") } } ^D  Native pattern-matching support  Java regex available out-of-the box  Support for Optional values (map/getOrElse) https://gist.github.com/lucamilanesio/9687174
  • 31. 31 And there is more … audit events download commands project and group events message of the day Prolog predicates auth backends avatars
  • 32. 32 WHEN scripting plugins will be included in Gerrit ? Hackathon #6 24-26th March we will keep on hacking on Gerrit MISSION: improve, stabilise and merge scripting plugins into Gerrit master ! TARGET VERSION: Gerrit Ver. 2.10 (?)
  • 33. 33
  • 34. Try it on-line (with scripting): http://gerrithub.io/login Read the Gerrit book: http://gerrithub.io/book Keep in touch: http://gitenterprise.me Learn more about Gerrit with 20% OFF Book discount for Gerrit User Summit 2014 Book PROMO-CODE: LGCRB20 eBook PROMO-CODE: LGCReB20