X-Git-Url: http://andersk.mit.edu/gitweb/jira-zephyrbot.git/blobdiff_plain/8b8cecc4126ff4b0e25b0a96155028352993b8af..f719e4334981f765a44a1ed0f5c7c5ab0312c24d:/jirabot.py?ds=sidebyside diff --git a/jirabot.py b/jirabot.py index 9d36e91..b9b5be4 100755 --- a/jirabot.py +++ b/jirabot.py @@ -26,7 +26,12 @@ def jira_init(): def jira_login(b): b.open("https://jira.mit.edu/jira/secure/Dashboard.jspa") - b.follow_link(text="MIT Touchstone") + try: + b.follow_link(text="MIT Touchstone") + except mechanize.LinkNotFoundError: + return + if (urlparse.urlparse(b.geturl())[1] == "jira.mit.edu"): + return b.select_form("wayfForm1") b.submit() b.select_form(predicate=lambda f: any(c.name == 'login_certificate' @@ -35,6 +40,26 @@ def jira_login(b): b.select_form(nr=0) b.submit() +def feed_to_zephyrs(thing, rss, parse): + zephyrs = [] + try: + feed = feedparser.parse(rss) + for e in feed.entries: + global old_time, new_time + t = int(calendar.timegm(e.date_parsed)) + if t <= old_time: + continue + if t > new_time: + new_time = t + try: + z = parse(e) + except: + z = zerror("Error parsing " + thing + ":\n" + e.id + "\n" + traceback.format_exc()) + zephyrs.append((t, z)) + except: + zephyrs.append((0, zerror("Error parsing " + thing + "s feed:\n" + traceback.format_exc()))) + return zephyrs + def parse_issue(e): issue = urlparse.urlparse(e.id)[2].rsplit('/', 1)[1] url = e.id @@ -43,6 +68,7 @@ def parse_issue(e): return zephyr.ZNotice( sender=zephyr_sender, auth=False, + opcode='auto', cls=zephyr_class, instance=issue, fields=[e.title, msg], @@ -64,6 +90,7 @@ def parse_comment(e): return zephyr.ZNotice( sender=zephyr_sender, auth=False, + opcode='auto', cls=zephyr_class, instance=issue, fields=[e.title, msg], @@ -73,54 +100,43 @@ def zerror(msg): return zephyr.ZNotice( sender=zephyr_sender, auth=False, + opcode='auto', cls=zephyr_class, instance='jira-error', fields=['Jira bot error', msg] ) b = jira_init() +zephyr.init() -jira_login(b) -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") -issues_rss = b.response().read() -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") -comments_rss = b.response().read() +while True: + time_file_new = time_file + '.' + ''.join(random.sample(string.letters, 8)) -time_file_new = time_file + '.' + ''.join(random.sample(string.letters, 8)) + try: + os.rename(time_file, time_file_new) + except OSError: + print "warning: could not acquire timestamp lock" + time.sleep(17) + continue -try: - os.rename(time_file, time_file_new) -except OSError: - exit() + jira_login(b) + 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") + issues_rss = b.response().read() + 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") + comments_rss = b.response().read() -old_time = int(open(time_file_new).read()) -new_time = old_time + old_time = int(open(time_file_new).read()) + new_time = old_time -zephyr.init() -zephyrs = [] + zephyrs = (feed_to_zephyrs('issue', issues_rss, parse_issue) + + feed_to_zephyrs('comment', comments_rss, parse_comment)) -for (thing, rss, parse) in [('issue', issues_rss, parse_issue), - ('comment', comments_rss, parse_comment)]: - try: - feed = feedparser.parse(rss) - for e in feed.entries: - t = int(calendar.timegm(e.date_parsed)) - if t <= old_time: - continue - if t > new_time: - new_time = t - try: - z = parse(e) - except: - z = zerror("Error parsing " + thing + ":\n" + e.id + "\n" + traceback.format_exc()) - zephyrs.append((t, z)) - except: - zephyrs.append((0, zerror("Error parsing " + thing + "s feed:\n" + traceback.format_exc()))) + open(time_file_new, 'w').write(str(new_time)) -open(time_file_new, 'w').write(str(new_time)) + zephyrs.sort(key=lambda tz: tz[0]) + for (t, z) in zephyrs: + z.send() -os.rename(time_file_new, time_file) + os.rename(time_file_new, time_file) -zephyrs.sort(key=lambda tz: tz[0]) -for (t, z) in zephyrs: - z.send() + time.sleep(60)