Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kentaro committed Jun 1, 2012
0 parents commit 7f89278
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pkg/
work/
jar/
Gemfile.lock
1 change: 1 addition & 0 deletions .rbenv_version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jruby-1.6.7
13 changes: 13 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
source :rubygems

gem "jenkins-plugin-runtime", "~> 0.2.0"
gem 'columnize'

group :development do
gem 'jpi', '~> 0.3.5'
gem 'ruby-debug'
end

group :test do
gem 'rspec'
end
11 changes: 11 additions & 0 deletions ikachan.pluginspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Jenkins::Plugin::Specification.new do |plugin|
plugin.name = "ikachan"
plugin.display_name = "Ikachan Plugin"
plugin.version = '0.0.1'
plugin.description = 'Publisher for an IRC bot named Ikachan.'
plugin.url = 'https://wiki.jenkins-ci.org/display/JENKINS/Ikachan+Plugin'
plugin.developed_by "kentaro", "Kentaro Kuribayashi <kentarok@gmail.com>"
plugin.uses_repository :github => "kentaro/jenkins-ikachan-plugin"
plugin.depends_on 'ruby-runtime', '0.10'
plugin.depends_on 'git', '1.1.11'
end
6 changes: 6 additions & 0 deletions lib/ikachan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'java'
import 'hudson.model.Result'

require 'ikachan/client'
require 'ikachan/message'
require 'ikachan/message_factory'
28 changes: 28 additions & 0 deletions lib/ikachan/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'uri'
require 'net/http'

module Ikachan
class Client
attr_reader :base_url, :listener

def initialize(base_url, listener)
@base_url = base_url.match(/^https?:\/\/[^\/]+\//) ? base_url : "http:\/\/#{base_url}\/"
@listener = listener
end

def join(channel)
dispatch :join, 'channel' => channel
end

def notice(channel, message)
dispatch :notice, 'channel' => channel, 'message' => message
end

private

def dispatch(type, params = {})
uri = URI.parse "#{base_url}#{type.to_s}"
Net::HTTP.post_form uri, params
end
end
end
88 changes: 88 additions & 0 deletions lib/ikachan/message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
module Ikachan
module Message
attr_reader :build
attr_reader :color
attr_reader :notify
attr_reader :status

def message
msg = "#{build.full_display_name} - #{status} after #{build.duration_string}"
instance = Java::jenkins::model::Jenkins.instance
if instance && instance.root_url
msg << " (<a href=\"#{instance.root_url.chomp('/')}/#{build.url}\">Open</a>)"
end
msg
end
end

class SuccessMessage
include Message

def initialize(build)
@build = build
@status = 'Success'
@color = 'green'
@notify = false
end
end

class UnstableMessage
include Message

def initialize(build)
@build = build
@status = 'Unstable'
@color = 'yellow'
@notify = true
end
end

class FailureMessage
include Message

def initialize(build)
@build = build
@status = 'FAILURE'
@color = 'red'
@notify = true
end

def message
msg = super
items = build.change_set.items
unless items.empty?
msg << ': changed by '
msg << items.map { |i|
if i.respond_to? :author_name # GitChangeSet
"@\"#{i.author_name}\""
else
"@#{i.author.full_name}"
end
}.uniq.join(' ')
end
msg
end
end

class NotBuiltMessage
include Message

def initialize(build)
@build = build
@status = 'Not Built'
@color = 'yellow'
@notify = true
end
end

class AbortedMessage
include Message

def initialize(build)
@build = build
@status = 'ABORTED'
@color = 'yellow'
@notify = false
end
end
end
22 changes: 22 additions & 0 deletions lib/ikachan/message_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Ikachan
class MessageFactory
def self.create(build)
case build.result
when Result::SUCCESS
SuccessMessage.new(build)

when Result::UNSTABLE
UnstableMessage.new(build)

when Result::FAILURE
FailureMessage.new(build)

when Result::NOT_BUILT
NotBuiltMessage.new(build)

when Result::ABORTED
AbortedMessage.new(build)
end
end
end
end
22 changes: 22 additions & 0 deletions models/ikachan_publisher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'ikachan'

class IkachanPublisher < Jenkins::Tasks::Publisher

display_name "Ikachan publisher"
attr_reader :base_url, :channel, :ikachan

def initialize(attrs = {})
@base_url = attrs['base_url']
@channel = attrs['channel']
end

def prebuild(build, listener)
@ikachan = Ikachan::Client.new @base_url, listener
@ikachan.join channel
end

def perform(build, launcher, listener)
message = Ikachan::MessageFactory.create(build.native).message
ikachan.notice channel, message
end
end
6 changes: 6 additions & 0 deletions spec/lib/ikachan_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'spec_helper'

describe Ikachan do
let(:ikachan) { Ikachan.new 'http://example.com/' }
pending 'how to do it'
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'ikachan'
17 changes: 17 additions & 0 deletions views/ikachan_publisher/config.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%
f = taglib("/lib/form")
f.entry(
:title => 'Base URL',
:field => 'base_url',
:description => "Ikachan's Base URL (ex. http://ikachan.example.com/)"
) do
f.textbox
end
f.entry(
:title => 'Channel',
:field => 'channel',
:description => "A channel to be published."
) do
f.textbox
end
%>

0 comments on commit 7f89278

Please sign in to comment.