def watch_pr(): state_key = 'github_pr.last_updated' date_format = '%Y-%m-%dT%H:%M:%SZ' st = State() last_updated = st.get(state_key) st.set(state_key, datetime.utcnow().strftime(date_format)) if last_updated is None: # first run return else: last_updated = datetime.strptime(last_updated, date_format) watched = defaultdict(list) for chat_id in get_module_chat_ids(module_name): wl = get_chat_conf(chat_id, module_name, default_options)['watch_list'] for repo in wl: watched[tuple(repo)].append(chat_id) if not watched: return pool = Pool(10) res = pool.imap_unordered(partial(get_new_pr, last_updated), watched) for prs in res: for pr in prs: repo = get_repo(pr['base']['repo']['full_name']) pr['chat_ids'] = watched[repo] yield pr
def list_handler(state, value): conf = get_chat_conf(value.chat_id, module_name, default_options) watch_list = conf['watch_list'] url = 'https://%s/' % conf['host'] if watch_list: msg = 'Watched:\n%s' % '\n'.join(url + '/'.join(r) for r in watch_list) return Response(text=msg) else: return Response(text="Your watchlist is empty")
def remove_handler(state, value): repo = get_repo(value.text) if repo: conf = get_chat_conf(value.chat_id, module_name, default_options) watch_list = set(conf['watch_list']) if repo in watch_list: watch_list.remove(repo) set_chat_conf(value.chat_id, module_name, watch_list=list(watch_list)) return Response(text='Deleted.') else: return Response(text='Repository not in watchlist.') else: return Response(text=error_msg)
def add_handler(state, value): repo = get_repo(value.text) if repo: conf = get_chat_conf(value.chat_id, module_name, default_options) watch_list = list(conf['watch_list']) watch_list.append(repo) set_chat_conf(value.chat_id, module_name, watch_list=watch_list) msg = ('I will notify about all pull requests in %s repository' % '/'.join(repo)) return Response(text=msg) else: return Response(text=error_msg, next=add_handler)