2013-06-21 18:52:39 +08:00
import json
2013-08-01 10:39:15 +12:00
import urllib2
2013-06-21 18:52:39 +08:00
2014-02-14 16:36:57 +13:00
from util import hook , http
2013-06-21 18:52:39 +08:00
shortcuts = { " cloudbot " : " ClouDev/CloudBot " }
2013-09-04 18:30:04 +08:00
2013-06-21 18:52:39 +08:00
def truncate ( msg ) :
2013-11-26 16:32:54 -06:00
nmsg = msg . split ( )
2013-06-21 18:52:39 +08:00
out = None
x = 0
for i in nmsg :
2013-09-04 18:30:04 +08:00
if x < = 7 :
if out :
out = out + " " + nmsg [ x ]
else :
out = nmsg [ x ]
x + = 1
2013-06-21 18:52:39 +08:00
if x < = 7 :
2013-09-04 18:30:04 +08:00
return out
2013-06-21 18:52:39 +08:00
else :
2013-09-04 18:30:04 +08:00
return out + " ... "
2013-06-21 18:52:39 +08:00
@hook.command
def ghissues ( inp ) :
""" ghissues username/repo [number] - Get specified issue summary, or open issue count """
args = inp . split ( " " )
try :
2013-09-04 18:30:04 +08:00
if args [ 0 ] in shortcuts :
repo = shortcuts [ args [ 0 ] ]
else :
repo = args [ 0 ]
2013-09-05 09:46:49 +08:00
url = " https://api.github.com/repos/ {} /issues " . format ( repo )
2013-06-21 18:52:39 +08:00
except IndexError :
2013-09-04 18:30:04 +08:00
return " Invalid syntax. .github issues username/repo [number] "
2013-06-21 18:52:39 +08:00
try :
2013-09-04 18:30:04 +08:00
url + = " / %s " % args [ 1 ]
number = True
2013-06-21 18:52:39 +08:00
except IndexError :
2013-09-04 18:30:04 +08:00
number = False
2013-06-21 18:52:39 +08:00
try :
2013-09-04 18:30:04 +08:00
data = json . loads ( http . open ( url ) . read ( ) )
print url
if not number :
try :
data = data [ 0 ]
except IndexError :
print data
return " Repo has no open issues "
2013-06-21 18:52:39 +08:00
except ValueError :
2013-09-04 18:30:04 +08:00
return " Invalid data returned. Check arguments (.github issues username/repo [number] "
2013-11-26 16:23:59 -06:00
fmt = " Issue: # %s ( %s ) by %s : %s | %s %s " # (number, state, user.login, title, truncate(body), gitio.gitio(data.url))
fmt1 = " Issue: # %s ( %s ) by %s : %s %s " # (number, state, user.login, title, gitio.gitio(data.url))
2013-06-21 18:52:39 +08:00
number = data [ " number " ]
if data [ " state " ] == " open " :
2013-09-04 18:30:04 +08:00
state = u " \x03 3 \x02 OPEN \x02 \x0f "
2013-06-21 18:52:39 +08:00
else :
2013-09-05 09:46:49 +08:00
state = u " \x03 4 \x02 CLOSED \x02 \x0f by {} " . format ( data [ " closed_by " ] [ " login " ] )
2013-06-21 18:52:39 +08:00
user = data [ " user " ] [ " login " ]
title = data [ " title " ]
summary = truncate ( data [ " body " ] )
2013-08-19 18:21:46 +08:00
gitiourl = gitio ( data [ " html_url " ] )
2013-08-29 20:48:22 +08:00
if " Failed to get URL " in gitiourl :
gitiourl = gitio ( data [ " html_url " ] + " " + repo . split ( " / " ) [ 1 ] + number )
2013-06-26 00:21:28 +08:00
if summary == " " :
2013-09-04 18:30:04 +08:00
return fmt1 % ( number , state , user , title , gitiourl )
2013-06-26 00:21:28 +08:00
else :
2013-09-04 18:30:04 +08:00
return fmt % ( number , state , user , title , summary , gitiourl )
2013-08-01 10:39:15 +12:00
@hook.command
def gitio ( inp ) :
2013-09-04 18:30:04 +08:00
""" gitio <url> [code] -- Shorten Github URLs with git.io. [code] is
a optional custom short code . """
2013-08-01 10:39:15 +12:00
split = inp . split ( " " )
url = split [ 0 ]
try :
code = split [ 1 ]
except :
code = None
# if the first 8 chars of "url" are not "https://" then append
# "https://" to the url, also convert "http://" to "https://"
if url [ : 8 ] != " https:// " :
if url [ : 7 ] != " http:// " :
url = " https:// " + url
else :
url = " https:// " + url [ 7 : ]
url = ' url= ' + str ( url )
if code :
url = url + ' &code= ' + str ( code )
req = urllib2 . Request ( url = ' http://git.io ' , data = url )
# try getting url, catch http error
try :
f = urllib2 . urlopen ( req )
except urllib2 . HTTPError :
return " Failed to get URL! "
urlinfo = str ( f . info ( ) )
# loop over the rows in urlinfo and pick out location and
# status (this is pretty odd code, but urllib2.Request is weird)
for row in urlinfo . split ( " \n " ) :
if row . find ( " Status " ) != - 1 :
status = row
if row . find ( " Location " ) != - 1 :
location = row
print status
if not " 201 " in status :
return " Failed to get URL! "
# this wont work for some reason, so lets ignore it ^
# return location, minus the first 10 chars
return location [ 10 : ]