Skip to content

Instantly share code, notes, and snippets.

@dzwarg
Created October 20, 2011 19:42
Show Gist options
  • Save dzwarg/1302111 to your computer and use it in GitHub Desktop.
Save dzwarg/1302111 to your computer and use it in GitHub Desktop.
DistrictBuilder IRC git info bot
global repo
set repo "/projects/PublicMapping/DistrictBuilder"
global ghurl
set ghurl "https://github.com/PublicMapping/DistrictBuilder/"
bind msg -|- ".help" helphandle
proc helphandle {nick host handle txt} {
puthelp "PRIVMSG $nick :I recongize the following commands:"
puthelp "PRIVMSG $nick : .rXYZ : Get the author, time, log message, and URL of commit XYZ."
puthelp "PRIVMSG $nick : .#XYZ : Get the title and URL to issue XYZ in GitHub."
puthelp "PRIVMSG $nick : wiki:XYZ : Get the full GitHub wiki link to page XYZ."
puthelp "PRIVMSG $nick : src:X/Y/Z : Get the full GitHub source blob link to sourcefile X/Y/Z."
puthelp "PRIVMSG $nick : .git-fetch : Only for channel ops, this refreshes the bot's version of the repository."
return 0
}
# This command updates the server's github repo (set above) to the most recent version
# of the repository.
bind pubm o|- "% .git-fetch" fetchhandle
proc fetchhandle {nick host handle channel txt} {
global repo
cd $repo
set status 0
if {[catch {exec git fetch -q} results options]} {
set details [dict get $options -errorcode]
if {[lindex $details 0] eq "CHILDSTATUS"} {
set status [lindex $details 2]
}
}
if {$status eq 0} {
puthelp "PRIVMSG $channel :Ok, up-to-date."
} else {
puthelp "PRIVMSG $channel :Sorry $nick, git-fetch didn't work out."
}
return 0
}
# This command matches rXXXX to find a matching git commit hash and
# report details about it.
bind pubm -|- "% *.r*" revhandle
proc revhandle {nick host handle channel txt} {
# now regex check $txt
if { [regexp {.*r([\d\w]{4,}).*} $txt whole rev] } {
global repo
global ghurl
cd $repo
set status 1
if {[catch {exec git log --exit-code -n 1 --oneline $rev} results options]} {
set details [dict get $options -errorcode]
if {[lindex $details 0] eq "CHILDSTATUS"} {
set status [lindex $details 2]
}
}
if {$status eq 1} {
set fmt "r%h: %an, %ai"
set authtime [exec git log $rev -n 1 --pretty=$fmt]
set fmt "r%h: %s"
set msg [exec git log $rev -n 1 --pretty=$fmt]
puthelp "PRIVMSG $channel :$authtime: $msg"
set url $ghurl
append url "commit/"
append url $rev
puthelp "PRIVMSG $channel :$url"
} else {
puthelp "PRIVMSG $channel :Sorry $nick, that's not unique, or not recognized as a commit."
}
}
return 0
}
# This command matches #XXXX to get a URL for a ticket, and the title of the ticket
bind pubm -|- "% *.#*" tixhandle
proc tixhandle {nick host handle channel txt} {
# now regex check $txt
if { [regexp {.*#(\d+).*} $txt whole tix] } {
global ghurl
set url $ghurl
append url "issues/"
append url $tix
if {[catch {exec wget -O /tmp/issue.bot -q $url} results options]} {
puthelp "PRIVMSG $channel :Sorry, $nick, that's not a real issues on GitHub."
file delete /tmp/issue.bot
return 0
}
set fl [open /tmp/issue.bot]
set tname ""
while {[gets $fl line] >= 0} {
if {[regexp {.*<title>(.*)( - Issues - .*)</title>.*} $line whole title]} {
set tname $title
break
}
}
close $fl
file delete /tmp/issue.bot
puthelp "PRIVMSG $channel :$tname"
puthelp "PRIVMSG $channel :$url"
}
return 0
}
# This command matches wiki:XXXXX to a wiki URL in github.
bind pubm -|- "% *wiki:*" wikihandle
proc wikihandle {nick host handle channel txt} {
# now regex check $txt
regexp {.*wiki:([[:alpha:]\/\-_]+).*} $txt whole wiki
if { $wiki ne "" } {
global ghurl
set url $ghurl
append url "wiki/"
append url [string trim $wiki "/"]
if {$wiki eq "Home"} {
# Home page is special (will always exist), and it matches the
# page-not-found title, so short circuit here.
puthelp "PRIVMSG $channel :$url"
return 0
}
exec wget -O /tmp/wiki.bot -q $url
set fl [open /tmp/wiki.bot]
set tname ""
while {[gets $fl line] >= 0} {
if {[regexp {.*<title>(.*)( - GitHub)</title>.*} $line whole title extra]} {
set tname $title
break
}
}
close $fl
file delete /tmp/wiki.bot
putlog "title is now '$tname'"
if {$tname eq "DistrictBuilder Wiki"} {
puthelp "PRIVMSG $channel :Sorry, $nick, that wiki page doesn't exist."
} else {
puthelp "PRIVMSG $channel :$url"
}
}
return 0
}
# This command matches source:XXXX to a source blob URL in github
bind pubm -|- "% *src:*" srchandle
proc srchandle {nick host handle channel txt} {
# now regex check $txt
if { [regexp {.*src:([[:alpha:]\/\.\-_]+).*} $txt whole src] } {
global ghurl
set url $ghurl
append url "blob/master/"
append url [string trim $src "/"]
if {[catch {exec wget -O /tmp/src.bot -q $url} results options]} {
puthelp "PRIVMSG $channel :Sorry, $nick, that doesn't look like a source file."
file delete /tmp/src.bot
return 0
}
file delete /tmp/src.bot
puthelp "PRIVMSG $channel :$url"
}
return 0
}
putlog "Loaded git module."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment