SlideShare a Scribd company logo
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

More Related Content

What's hot

What's hot (20)

CICD Pipeline Using Github Actions
CICD Pipeline Using Github ActionsCICD Pipeline Using Github Actions
CICD Pipeline Using Github Actions
 
Gitlab ci-cd
Gitlab ci-cdGitlab ci-cd
Gitlab ci-cd
 
Github in Action
Github in ActionGithub in Action
Github in Action
 
Git101
Git101Git101
Git101
 
Github basics
Github basicsGithub basics
Github basics
 
Introduction to Tekton
Introduction to TektonIntroduction to Tekton
Introduction to Tekton
 
GitLab.pptx
GitLab.pptxGitLab.pptx
GitLab.pptx
 
CI / CD ( 지속적인 통합 / 지속적인 전달 ) 발표 자료 다운로드
CI / CD ( 지속적인 통합 / 지속적인 전달 ) 발표 자료 다운로드CI / CD ( 지속적인 통합 / 지속적인 전달 ) 발표 자료 다운로드
CI / CD ( 지속적인 통합 / 지속적인 전달 ) 발표 자료 다운로드
 
GCP Best Practices for SRE Team
GCP Best Practices for SRE TeamGCP Best Practices for SRE Team
GCP Best Practices for SRE Team
 
Devops Porto - CI/CD at Gitlab
Devops Porto - CI/CD at GitlabDevops Porto - CI/CD at Gitlab
Devops Porto - CI/CD at Gitlab
 
Double your Ass Jeff – Häh? - WSJF Praxis, Erweiterungen und Grenzen
Double your Ass Jeff – Häh? - WSJF Praxis, Erweiterungen und GrenzenDouble your Ass Jeff – Häh? - WSJF Praxis, Erweiterungen und Grenzen
Double your Ass Jeff – Häh? - WSJF Praxis, Erweiterungen und Grenzen
 
Gerrit: how to cook a plugin in only 10 mins
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
 
Introducing GitLab (June 2018)
Introducing GitLab (June 2018)Introducing GitLab (June 2018)
Introducing GitLab (June 2018)
 
Domain Driven Design
Domain Driven Design Domain Driven Design
Domain Driven Design
 
Gitlab Training with GIT and SourceTree
Gitlab Training with GIT and SourceTreeGitlab Training with GIT and SourceTree
Gitlab Training with GIT and SourceTree
 
Jenkins vs GitLab CI
Jenkins vs GitLab CIJenkins vs GitLab CI
Jenkins vs GitLab CI
 
Building MAUI UI in C#.pptx
Building MAUI UI in C#.pptxBuilding MAUI UI in C#.pptx
Building MAUI UI in C#.pptx
 
Jenkins Overview
Jenkins OverviewJenkins Overview
Jenkins Overview
 
Git flow
Git flowGit flow
Git flow
 
Intro to Git and GitHub
Intro to Git and GitHubIntro to Git and GitHub
Intro to Git and GitHub
 

Viewers also liked

Viewers also liked (13)

Gerrit Code Review Analytics
Gerrit Code Review AnalyticsGerrit Code Review Analytics
Gerrit Code Review Analytics
 
Speed up Continuous Delivery with BigData Analytics
Speed up Continuous Delivery with BigData AnalyticsSpeed up Continuous Delivery with BigData Analytics
Speed up Continuous Delivery with BigData Analytics
 
Mobile Application Lifecycle with Jekins, Trello and CollabNet TeamForge
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
 
Gerrit jenkins-big data-continuous-delivery
Gerrit jenkins-big data-continuous-deliveryGerrit jenkins-big data-continuous-delivery
Gerrit jenkins-big data-continuous-delivery
 
Git workshop 33degree 2011 krakow
Git workshop 33degree 2011 krakowGit workshop 33degree 2011 krakow
Git workshop 33degree 2011 krakow
 
Is TDD dead or alive?
Is TDD dead or alive?Is TDD dead or alive?
Is TDD dead or alive?
 
GitBlit plugin for Gerrit Code Review
GitBlit plugin for Gerrit Code ReviewGitBlit plugin for Gerrit Code Review
GitBlit plugin for Gerrit Code Review
 
GerritHub.io - present, past, future
GerritHub.io - present, past, futureGerritHub.io - present, past, future
GerritHub.io - present, past, future
 
Devoxx 2016 Using Jenkins, Gerrit and Spark for Continuous Delivery Analytics
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
 
Zero-Downtime Gerrit Code Review Upgrade
Zero-Downtime Gerrit Code Review UpgradeZero-Downtime Gerrit Code Review Upgrade
Zero-Downtime Gerrit Code Review Upgrade
 
Gerrit is Getting Native with RPM, Deb and Docker
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
 
Jenkins User Conference - Continuous Delivery on Mobile
Jenkins User Conference - Continuous Delivery on MobileJenkins User Conference - Continuous Delivery on Mobile
Jenkins User Conference - Continuous Delivery on Mobile
 
Gerrit Code Review with GitHub plugin
Gerrit Code Review with GitHub pluginGerrit Code Review with GitHub plugin
Gerrit Code Review with GitHub plugin
 

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

Testing of javacript
Testing of javacriptTesting of javacript
Testing of javacript
Lei Kang
 
2015-ghci-presentation-git_gerritJenkins_final
2015-ghci-presentation-git_gerritJenkins_final2015-ghci-presentation-git_gerritJenkins_final
2015-ghci-presentation-git_gerritJenkins_final
Mythri P K
 
Openstack contribution process
Openstack contribution processOpenstack contribution process
Openstack contribution process
Syed Armani
 
OpenStack Contribution Process
OpenStack Contribution ProcessOpenStack Contribution Process
OpenStack Contribution Process
openstackindia
 
introductiontogitandgithub-120702044048-phpapp01.pdf
introductiontogitandgithub-120702044048-phpapp01.pdfintroductiontogitandgithub-120702044048-phpapp01.pdf
introductiontogitandgithub-120702044048-phpapp01.pdf
BruceLee275640
 
Git Distributed Version Control System
Git   Distributed Version Control SystemGit   Distributed Version Control System
Git Distributed Version Control System
Victor Wong
 

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

GIT By Sivakrishna
GIT By SivakrishnaGIT By Sivakrishna
GIT By Sivakrishna
 
Github By Nyros Developer
Github By Nyros DeveloperGithub By Nyros Developer
Github By Nyros Developer
 
Introduction to git and Github
Introduction to git and GithubIntroduction to git and Github
Introduction to git and Github
 
FOSDEM 2017: GitLab CI
FOSDEM 2017:  GitLab CIFOSDEM 2017:  GitLab CI
FOSDEM 2017: GitLab CI
 
Matt Gauger - Git & Github web414 December 2010
Matt Gauger - Git & Github web414 December 2010Matt Gauger - Git & Github web414 December 2010
Matt Gauger - Git & Github web414 December 2010
 
GDSC GIT AND GITHUB
GDSC GIT AND GITHUB GDSC GIT AND GITHUB
GDSC GIT AND GITHUB
 
Introduction to Git and Github
Introduction to Git and GithubIntroduction to Git and Github
Introduction to Git and Github
 
Testing of javacript
Testing of javacriptTesting of javacript
Testing of javacript
 
Managing Github via Terrafom.pdf
Managing Github via Terrafom.pdfManaging Github via Terrafom.pdf
Managing Github via Terrafom.pdf
 
git github PPT_GDSCIIITK.pptx
git github PPT_GDSCIIITK.pptxgit github PPT_GDSCIIITK.pptx
git github PPT_GDSCIIITK.pptx
 
GIT training - advanced for software projects
GIT training - advanced for software projectsGIT training - advanced for software projects
GIT training - advanced for software projects
 
2015-ghci-presentation-git_gerritJenkins_final
2015-ghci-presentation-git_gerritJenkins_final2015-ghci-presentation-git_gerritJenkins_final
2015-ghci-presentation-git_gerritJenkins_final
 
Openstack contribution process
Openstack contribution processOpenstack contribution process
Openstack contribution process
 
OpenStack Contribution Process
OpenStack Contribution ProcessOpenStack Contribution Process
OpenStack Contribution Process
 
GWT Contributor Workshop
GWT Contributor WorkshopGWT Contributor Workshop
GWT Contributor Workshop
 
introductiontogitandgithub-120702044048-phpapp01.pdf
introductiontogitandgithub-120702044048-phpapp01.pdfintroductiontogitandgithub-120702044048-phpapp01.pdf
introductiontogitandgithub-120702044048-phpapp01.pdf
 
Git Distributed Version Control System
Git   Distributed Version Control SystemGit   Distributed Version Control System
Git Distributed Version Control System
 
Introduction to git and github
Introduction to git and githubIntroduction to git and github
Introduction to git and github
 
Grails beginners workshop
Grails beginners workshopGrails beginners workshop
Grails beginners workshop
 
git-commands-cheat-sheet-infopediya-com.pdf
git-commands-cheat-sheet-infopediya-com.pdfgit-commands-cheat-sheet-infopediya-com.pdf
git-commands-cheat-sheet-infopediya-com.pdf
 

More from Luca Milanesio

More from Luca Milanesio (18)

What's new in Gerrit Code Review v3.1 and beyond
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
 
Gerrit Analytics applied to Android source code
Gerrit Analytics applied to Android source codeGerrit Analytics applied to Android source code
Gerrit Analytics applied to Android source code
 
Cloud-native Gerrit Code Review
Cloud-native Gerrit Code ReviewCloud-native Gerrit Code Review
Cloud-native Gerrit Code Review
 
Gerrit Code Review migrations step-by-step
Gerrit Code Review migrations step-by-stepGerrit Code Review migrations step-by-step
Gerrit Code Review migrations step-by-step
 
Gerrit Code Review v3.2 and v3.3
Gerrit Code Review v3.2 and v3.3Gerrit Code Review v3.2 and v3.3
Gerrit Code Review v3.2 and v3.3
 
ChronicleMap non-blocking cache for Gerrit v3.3
ChronicleMap non-blocking cache for Gerrit v3.3ChronicleMap non-blocking cache for Gerrit v3.3
ChronicleMap non-blocking cache for Gerrit v3.3
 
Gerrit Code Review multi-site
Gerrit Code Review multi-siteGerrit Code Review multi-site
Gerrit Code Review multi-site
 
What's new in Gerrit Code Review 3.0
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
 
Gerrit User Summit 2019 Keynote
Gerrit User Summit 2019 KeynoteGerrit User Summit 2019 Keynote
Gerrit User Summit 2019 Keynote
 
Gerrit multi-master / multi-site at GerritHub
Gerrit multi-master / multi-site at GerritHubGerrit multi-master / multi-site at GerritHub
Gerrit multi-master / multi-site at GerritHub
 
GerritHub a true Gerrit migration story to v2.15
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
 
Gerrit User Summit 2018 - Keynote
Gerrit User Summit 2018 - Keynote Gerrit User Summit 2018 - Keynote
Gerrit User Summit 2018 - Keynote
 
Jenkins plugin for Gerrit Code Review pipelines
Jenkins plugin for Gerrit Code Review pipelinesJenkins plugin for Gerrit Code Review pipelines
Jenkins plugin for Gerrit Code Review pipelines
 
Gerrit User Summit 2017 Keynote
Gerrit User Summit 2017 KeynoteGerrit User Summit 2017 Keynote
Gerrit User Summit 2017 Keynote
 
How to keep Jenkins logs forever without performance issues
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
 
Jenkins Pipeline on your Local Box to Reduce Cycle Time
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
 
Jenkins world 2017 - Data-Driven CI Pipeline with Gerrit Code Review
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
 
Stable master workflow with Gerrit Code Review
Stable master workflow with Gerrit Code ReviewStable master workflow with Gerrit Code Review
Stable master workflow with Gerrit Code Review
 

Recently uploaded

Recently uploaded (20)

B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 

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