17 zephyr_sender
= 'jira'
18 zephyr_class
= 'andersk-test'
19 time_file
= 'jirabot.time'
21 class UnicodeHTMLParser(htmllib
.HTMLParser
):
22 entitydefs
= dict((k
, unichr(v
)) for (k
, v
) in htmlentitydefs
.name2codepoint
.items())
24 def convert_charref(self
, name
):
29 return self
.convert_codepoint(n
)
31 def convert_codepoint(self
, codepoint
):
32 return unichr(codepoint
)
35 b
= mechanize
.Browser()
36 b
.set_handle_robots(False)
37 b
.add_client_certificate("https://idp.mit.edu:9443", "cert.pem", "cert.pem")
38 b
.addheaders
= [("Accept-Language", "en-us,en;q=0.5"),]
42 b
.open("https://jira.mit.edu/jira/secure/Dashboard.jspa")
44 b
.follow_link(text
="MIT Touchstone")
45 except mechanize
.LinkNotFoundError
:
47 if (urlparse
.urlparse(b
.geturl())[1] == "jira.mit.edu"):
49 b
.select_form("wayfForm1")
51 b
.select_form(predicate
=lambda f
: any(c
.name
== 'login_certificate'
57 def feed_to_zephyrs(thing
, rss
, parse
):
60 feed
= feedparser
.parse(rss
)
61 for e
in feed
.entries
:
62 global old_time
, new_time
63 t
= int(calendar
.timegm(e
.date_parsed
))
71 z
= zerror("Error parsing " + thing
+ ":\n" + e
.id + "\n" + traceback
.format_exc())
72 zephyrs
.append((t
, z
))
74 zephyrs
.append((0, zerror("Error parsing " + thing
+ "s feed:\n" + traceback
.format_exc())))
78 issue
= urlparse
.urlparse(e
.id)[2].rsplit('/', 1)[1]
80 msg
= e
.id + "\nThis issue was updated."
82 return zephyr
.ZNotice(
88 fields
=[e
.title
, msg
],
92 url
= urlparse
.urlunsplit(urlparse
.urlparse(e
.id)[0:3] + (None,None))
93 issue
= url
.rsplit('/', 1)[1]
95 s
= StringIO
.StringIO()
96 parser
= UnicodeHTMLParser(formatter
.AbstractFormatter(formatter
.DumbWriter(s
)))
97 parser
.feed(e
.summary
.rsplit('<table>', 1)[0])
99 comment
= s
.getvalue()
101 msg
= e
.author
+ " added a comment:\n" + comment
.rstrip()
103 return zephyr
.ZNotice(
104 sender
=zephyr_sender
,
109 fields
=[e
.title
, msg
],
113 return zephyr
.ZNotice(
114 sender
=zephyr_sender
,
118 instance
='jira-error',
119 fields
=['Jira bot error', msg
]
126 time_file_new
= time_file
+ '.' + ''.join(random
.sample(string
.letters
, 8))
129 os
.rename(time_file
, time_file_new
)
131 print "warning: could not acquire timestamp lock"
136 b
.open("https://jira.mit.edu/jira/sr/jira.issueviews:searchrequest-rss/temp/SearchRequest.xml?&pid=10185&updated%3Aprevious=-1w&sorter/field=updated&sorter/order=DESC&tempMax=1000")
137 issues_rss
= b
.response().read()
138 b
.open("https://jira.mit.edu/jira/sr/jira.issueviews:searchrequest-comments-rss/temp/SearchRequest.xml?&pid=10185&updated%3Aprevious=-1w&sorter/field=updated&sorter/order=DESC&tempMax=1000")
139 comments_rss
= b
.response().read()
141 old_time
= int(open(time_file_new
).read())
144 zephyrs
= (feed_to_zephyrs('issue', issues_rss
, parse_issue
) +
145 feed_to_zephyrs('comment', comments_rss
, parse_comment
))
146 zephyrs
.sort(key
=lambda tz
: tz
[0])
147 for (t
, z
) in zephyrs
:
150 open(time_file_new
, 'w').write(str(new_time
))
151 os
.rename(time_file_new
, time_file
)