Subversion/Hooks: Unterschied zwischen den Versionen

Aus schokokeks.org Wiki
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 46: Zeile 46:


def SendJabberNotification( address, message )
def SendJabberNotification( address, message )
myJID = JID::new('nu2mbot@mabber.com/CaptainHook')
myJID = JID::new('bot@strojny.net/CaptainHook')
myPassword = 'erisdiscordia2342abc'
myPassword = 'foobarfoobar'
JID::new(address)
JID::new(address)
cl = Client::new(myJID, false)
cl = Client::new(myJID, false)

Version vom 15. Februar 2006, 12:24 Uhr

#! /usr/bin/ruby

$:.unshift '../../../../../lib'

require 'optparse'
require 'xmpp4r'
require 'inifile'
require 'optparse'
require 'net/smtp'
include Jabber


revision = nil
repository = String.new

OptionParser::new do |opts|
	opts.banner = "notification.rb: Sends notifcation about svn commits via mail and jabber"
	opts.banner << "\n\n"
	opts.banner << 'Usage: notification.rb --revision \'revision\' --repository \'repository\''
	opts.separator ''
	opts.on( '-r', '--revision REVISION',     'revision of the project' ) { |r| revision   = r }
	opts.on( '-d', '--repository REPOSITORY', 'path to the repository'  ) { |d| repository = d }
	opts.on_tail('-h', '--help', 'Show this message') do
		puts opts
 		exit 2
  end
  opts.parse!(ARGV)
end


if revision.nil? or repository == "" 
	exit 1
end

class SVNLook
	def initialize( repository, revision)
		@repository = repository
		@revision = revision
	end
	
	def GetAttrib( function )
		return `/usr/bin/svnlook #{function} #{@repository} -r #{@revision}`
	end
end


def SendJabberNotification( address, message )
	myJID = JID::new('bot@strojny.net/CaptainHook')
	myPassword = 'foobarfoobar'
	JID::new(address)
	cl = Client::new(myJID, false)
	cl.connect
	cl.auth(myPassword)
	m = Message::new(address, message).set_type(:chat).set_id('1')
	cl.send(m)
	cl.close
end


def GetAddress( project, type, short = false)
	ini = IniFile::new( )
	ini.load( "/svn/usermap.ini" )
	ret = Array.new
	ini.sections do |username|
		if ini[username]['project'].split(",").find {|p| project.strip == p.strip }
			if type == "jabber" and  ini[username]['notification'].split(",").find {|v| v.strip == "jabber"}
				ret.push( ini[username]["jabber"] )
			elsif type == "mail" and ini[username]['notification'].split(",").find {|v| v.strip == "mail"}
				if short == true
					ret.push( ini[username]["mail"] )
				else
					ret.push( ini[username]["name"].to_s << " <" << ini[username]["mail"].to_s << ">")
				end
			end
		end
	end
	return ret
end

def SendMailNotification( receiver, subject, message )
	Net::SMTP.start('localhost', 25) do |smtp|
		smtp.open_message_stream( "no-reply@dev.nu2m.de", receiver ) do |stream|
			stream.puts "Subject: #{subject}"
			stream.puts
			stream.puts "#{message}"
		end
	end
end

svnlook = SVNLook::new( repository, revision )
diff = svnlook.GetAttrib( "diff" )
log = svnlook.GetAttrib( "log" )
author = svnlook.GetAttrib( "author" )
changed = svnlook.GetAttrib( "changed" )
project = repository.split("/").last
address_jabber = GetAddress( project, "jabber" )

message =  "-------------------------------------------------------------------------\n"
message << "Project: #{project} | New Revision #{revision} | Author:  #{author}"
message << "-------------------------------------------------------------------------\n"
message << "Log: #{log}"
message << "-------------------------------------------------------------------------\n"
message << "Files:\n"
message << "#{changed}"
message << "-------------------------------------------------------------------------\n"

subject = "[Nu2M-commits] " << project << " "
mail_message = message + diff
jabber_message = "\n" + message

SendMailNotification( GetAddress( project, "mail", true ), subject, mail_message)
GetAddress( project, "jabber" ).each { |address| SendJabberNotification( address, jabber_message ) }