This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from drnic/item_listener
Adding ItemListener wrapper
- Loading branch information
Showing
6 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
module Jenkins::Listeners | ||
require 'jenkins/listeners/item_listener' | ||
require 'jenkins/listeners/item_listener_proxy' | ||
require 'jenkins/listeners/run_listener' | ||
require 'jenkins/listeners/run_listener_proxy' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
module Jenkins::Listeners | ||
# Receives notifications about CRUD operations of Item. | ||
# | ||
# Include this module in your class in order to receive callbacks | ||
# when items are created, copied, updated, deleted, etc... | ||
# | ||
# To receive a callback, override the method with the same name as | ||
# the event. E.g. | ||
# | ||
# class MyRunListener | ||
# include Jenkins::Listeners::ItemListener | ||
# | ||
# def updated(item) | ||
# puts "#{item.native.inspect} updated!" | ||
# end | ||
# end | ||
# | ||
# Note: currently the +item+ objects being passed in are OpaqueJavaObject proxies | ||
# around Java objects. You must invoke `.native` on them to access the underlying | ||
# Java object that you want to interact with. | ||
module ItemListener | ||
extend Jenkins::Plugin::Behavior | ||
include Jenkins::Extension | ||
|
||
implemented do |cls| | ||
Jenkins.plugin.register_extension ItemListenerProxy.new(Jenkins.plugin, cls.new) | ||
end | ||
|
||
# Called after a new job is created and added to jenkins.model.Jenkins, | ||
# before the initial configuration page is provided. | ||
# | ||
# This is useful for changing the default initial configuration of newly created jobs. | ||
# For example, you can enable/add builders, etc. | ||
def created(item) | ||
end | ||
|
||
# Called after a new job is created by copying from an existing job. | ||
# | ||
# For backward compatibility, the default implementation of this method calls onCreated. | ||
# If you choose to handle this method, think about whether you want to call super.onCopied or not. | ||
# | ||
# @param src_item | ||
# The source item that the new one was copied from. Never null. | ||
# @param item | ||
# The newly created item. Never null. | ||
def copied(src_item, item) | ||
end | ||
|
||
# Called after all the jobs are loaded from disk into jenkins.model.Jenkins | ||
# object. | ||
def loaded() | ||
end | ||
|
||
# Called right before a job is going to be deleted. | ||
# | ||
# At this point the data files of the job is already gone. | ||
def deleted(item) | ||
end | ||
|
||
# Called after a job is renamed. | ||
# | ||
# @param item | ||
# The job being renamed. | ||
# @param oldName | ||
# The old name of the job. | ||
# @param newName | ||
# The new name of the job. Same as Item#getName(). | ||
def renamed(item, oldName, newName) | ||
end | ||
|
||
# Called after a job has its configuration updated. | ||
# | ||
# @since 1.460 | ||
def updated(item) | ||
end | ||
|
||
# Called at the begenning of the orderly shutdown sequence to | ||
# allow plugins to clean up stuff | ||
def before_shutdown() | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module Jenkins::Listeners | ||
class ItemListenerProxy < Java.hudson.model.listeners.ItemListener | ||
include Jenkins::Plugin::Proxy | ||
|
||
def initialize(plugin, object) | ||
super(plugin, object) | ||
end | ||
|
||
# Called after a new job is created and added to jenkins.model.Jenkins, | ||
# before the initial configuration page is provided. | ||
# | ||
# This is useful for changing the default initial configuration of newly created jobs. | ||
# For example, you can enable/add builders, etc. | ||
def onCreated(item) | ||
@object.created(import(item)) | ||
end | ||
|
||
# Called after a new job is created by copying from an existing job. | ||
# | ||
# For backward compatibility, the default implementation of this method calls onCreated. | ||
# If you choose to handle this method, think about whether you want to call super.onCopied or not. | ||
# | ||
# @param src_item | ||
# The source item that the new one was copied from. Never null. | ||
# @param item | ||
# The newly created item. Never null. | ||
def onCopied(src_item, item) | ||
@object.copied(import(src_item), import(item)); | ||
end | ||
|
||
# Called after all the jobs are loaded from disk into jenkins.model.Jenkins | ||
# object. | ||
def onLoaded() | ||
@object.loaded | ||
end | ||
|
||
# Called right before a job is going to be deleted. | ||
# | ||
# At this point the data files of the job is already gone. | ||
def onDeleted(item) | ||
@object.deleted(import(item)) | ||
end | ||
|
||
# Called after a job is renamed. | ||
# | ||
# @param item | ||
# The job being renamed. | ||
# @param oldName | ||
# The old name of the job. | ||
# @param newName | ||
# The new name of the job. Same as Item#getName(). | ||
def onRenamed(item, oldName, newName) | ||
@object.renamed(import(item), import(oldName), import(newName)) | ||
end | ||
|
||
# Called after a job has its configuration updated. | ||
# | ||
# @since 1.460 | ||
def onUpdated(item) | ||
@object.updated(import(item)) | ||
end | ||
|
||
# Called at the begenning of the orderly shutdown sequence to | ||
# allow plugins to clean up stuff | ||
def onBeforeShutdown() | ||
@object.before_shutdown | ||
end | ||
|
||
end | ||
end |
82 changes: 82 additions & 0 deletions
82
ruby-runtime/spec/jenkins/listeners/item_listener_proxy_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require 'spec_helper' | ||
require 'rspec-spies' | ||
|
||
describe Jenkins::Listeners::ItemListenerProxy do | ||
include ProxyHelper | ||
before do | ||
@proxy = Jenkins::Listeners::ItemListenerProxy.new(@plugin, @listener) | ||
@item = mock | ||
@src_item = mock | ||
end | ||
|
||
describe "when created" do | ||
before do | ||
@listener.stub(:created) | ||
@proxy.onCreated(@item) | ||
end | ||
it 'invokes the created callback' do | ||
@listener.should have_received(:created).with(@item) | ||
end | ||
end | ||
|
||
|
||
describe "when copied" do | ||
before do | ||
@listener.stub(:copied) | ||
@proxy.onCopied(@src_item, @item) | ||
end | ||
it 'invokes the copied callback' do | ||
@listener.should have_received(:copied).with(@src_item, @item) | ||
end | ||
end | ||
|
||
describe "when loaded" do | ||
before do | ||
@listener.stub(:loaded) | ||
@proxy.onLoaded() | ||
end | ||
it 'invokes the loaded callback' do | ||
@listener.should have_received(:loaded) | ||
end | ||
end | ||
|
||
describe "when deleted" do | ||
before do | ||
@listener.stub(:deleted) | ||
@proxy.onDeleted(@item) | ||
end | ||
it 'invokes the deleted callback' do | ||
@listener.should have_received(:deleted).with(@item) | ||
end | ||
end | ||
|
||
describe "when renamed" do | ||
before do | ||
@listener.stub(:renamed) | ||
@proxy.onRenamed(@item, "oldname", "newname") | ||
end | ||
it 'invokes the renamed callback' do | ||
@listener.should have_received(:renamed).with(@item, "oldname", "newname") | ||
end | ||
end | ||
|
||
describe "when updated" do | ||
before do | ||
@listener.stub(:updated) | ||
@proxy.onUpdated(@item) | ||
end | ||
it 'invokes the updated callback' do | ||
@listener.should have_received(:updated).with(@item) | ||
end | ||
end | ||
|
||
describe "when before_shutdown" do | ||
before do | ||
@listener.stub(:before_shutdown) | ||
@proxy.onBeforeShutdown() | ||
end | ||
it 'invokes the before_shutdown callback' do | ||
@listener.should have_received(:before_shutdown) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
ruby-tools/jpi/lib/jenkins/plugin/cli/templates/item_listener.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Receives notifications about CRUD operations of Items. | ||
# | ||
# This class will receive callbacks | ||
# when items are created, copied, updated, deleted, etc... | ||
# | ||
# To receive a callback, override the method with the same name as | ||
# the event. E.g. | ||
# | ||
# class MyRunListener | ||
# include Jenkins::Listeners::ItemListener | ||
# | ||
# def updated(item) | ||
# puts "#{item.native.inspect} updated!" | ||
# end | ||
# end | ||
# | ||
# Note: currently the +item+ objects being passed in are OpaqueJavaObject proxies | ||
# around Java objects. You must invoke `.native` on them to access the underlying | ||
# Java object that you want to interact with. | ||
class <%= name.capitalize %>Listener | ||
include Jenkins::Listeners::ItemListener | ||
|
||
# Called after a new job is created and added to jenkins.model.Jenkins, | ||
# before the initial configuration page is provided. | ||
# | ||
# This is useful for changing the default initial configuration of newly created jobs. | ||
# For example, you can enable/add builders, etc. | ||
def created(item) | ||
end | ||
|
||
# Called after a new job is created by copying from an existing job. | ||
# | ||
# For backward compatibility, the default implementation of this method calls onCreated. | ||
# If you choose to handle this method, think about whether you want to call super.onCopied or not. | ||
# | ||
# @param [Jenkins::Model::Item] The source item that the new one was copied from. Never null. | ||
# @param [Jenkins::Model::Item] The newly created item. Never null. | ||
def copied(src_item, item) | ||
end | ||
|
||
# Called after all the jobs are loaded from disk into jenkins.model.Jenkins | ||
# object. | ||
def loaded() | ||
end | ||
|
||
# Called right before a job is going to be deleted. | ||
# | ||
# At this point the data files of the job is already gone. | ||
# @param [Jenkins::Model::Item] The item being deleted. | ||
def deleted(item) | ||
end | ||
|
||
# Called after a job is renamed. | ||
# | ||
# @param [Jenkins::Model::Item] The item being renamed | ||
# @param The old name of the job. | ||
# @param The new name of the job. Same as Item#getName(). | ||
def renamed(item, oldName, newName) | ||
end | ||
|
||
# Called after a job has its configuration updated. | ||
# | ||
# @param [Jenkins::Model::Item] The item being updated | ||
def updated(item) | ||
end | ||
|
||
# Called at the begenning of the orderly shutdown sequence to | ||
# allow plugins to clean up stuff | ||
def before_shutdown() | ||
end | ||
end |