def test_get_slack_token_no_token(): """Test get_slack_token() when ENV variable is not set.""" exception = None try: rss2slack.get_slack_token() except ValueError as value_error: exception = value_error assert isinstance(exception, ValueError) is True assert exception.args[0] == 'SLACK_TOKEN must be set.'
def main(): """Fetch issues/PRs from GitHub and post them to Slack.""" logging.basicConfig(stream=sys.stdout, level=logging.ERROR) logger = logging.getLogger('gh2slack') args = parse_args() if args.verbosity: logger.setLevel(logging.DEBUG) try: slack_token = rss2slack.get_slack_token() url = get_gh_api_url(args.gh_owner, args.gh_repo, args.gh_section) pages = gh_request(logger, url) logger.debug('Got %i pages from GH.', len(pages)) if not pages: logger.info('No %s for %s/%s.', args.gh_section, args.gh_owner, args.gh_repo) sys.exit(0) cache = rss2irc.read_cache(logger, args.cache) scrub_cache(logger, cache) # Note: I have failed to find web link to repo in GH response. # Therefore, let's create one. repository_url = get_gh_repository_url(args.gh_owner, args.gh_repo) item_expiration = int(time.time()) + args.cache_expiration to_publish = process_page_items(logger, cache, pages, item_expiration, repository_url) if not args.cache_init and to_publish: slack_client = rss2slack.get_slack_web_client( slack_token, args.slack_base_url, args.slack_timeout) for html_url in to_publish: cache_item = cache.items[html_url] try: msg_blocks = [ format_message(logger, args.gh_owner, args.gh_repo, ALIASES[args.gh_section], html_url, cache_item) ] rss2slack.post_to_slack( logger, msg_blocks, slack_client, args.slack_channel, ) except Exception: logger.error(traceback.format_exc()) cache.items.pop(html_url) finally: time.sleep(args.sleep) rss2irc.write_cache(cache, args.cache) except Exception: logger.debug(traceback.format_exc()) # TODO(zstyblik): # 1. touch error file # 2. send error message to the channel finally: sys.exit(0)
def main(): """Post new commits in given repository to Slack.""" logging.basicConfig(stream=sys.stdout, level=logging.ERROR) logger = logging.getLogger('git-commits2slack') args = parse_args() if args.verbosity: logger.setLevel(logging.DEBUG) try: slack_token = rss2slack.get_slack_token() if not os.path.isdir(args.git_clone_dir): git_clone(args.git_clone_dir, args.git_repo) os.chdir(args.git_clone_dir) commit_ref = git_pull(args.git_clone_dir) if not commit_ref: logger.info('No new commits.') sys.exit(0) commits = git_show(args.git_clone_dir, commit_ref) if not commits: # FIXME(zstyblik): error? send message to Slack? logger.warning('There should be new commits, but we have none.') sys.exit(0) repo_name = os.path.basename(args.git_clone_dir) branch_name = git_branch(args.git_clone_dir) commit_count = len(commits) msg_blocks = [ format_commit_message(args.git_web, commit[0], commit[1]) for commit in commits ] heading = format_heading(args.git_web, branch_name, repo_name, commit_count) msg_blocks.insert(0, heading) slack_client = rss2slack.get_slack_web_client(slack_token, args.slack_base_url, args.slack_timeout) rss2slack.post_to_slack( logger, msg_blocks, slack_client, args.slack_channel, ) except Exception: logger.debug(traceback.format_exc()) # TODO(zstyblik): # 1. touch error file # 2. send error message to the channel finally: sys.exit(0)
def main(): """Main.""" logging.basicConfig(stream=sys.stdout, level=logging.ERROR) logger = logging.getLogger('phpbb2slack') args = parse_args() if args.verbosity: logger.setLevel(logging.DEBUG) if args.cache_expiration < 0: logger.error("Cache expiration can't be less than 0.") sys.exit(1) slack_token = rss2slack.get_slack_token() authors = get_authors_from_file(logger, args.authors_file) news = {} for rss_url in args.rss_urls: data = rss2irc.get_rss(logger, rss_url, args.rss_http_timeout) if not data: logger.error('Failed to get RSS from %s', rss_url) sys.exit(1) parse_news(data, news, authors) if not news: logger.info('No news?') sys.exit(0) cache = rss2irc.read_cache(logger, args.cache) scrub_cache(logger, cache) for key in news.keys(): if key not in cache: continue logger.debug('Key %s found in cache', key) if int(cache[key]['comments_cnt']) == int(news[key]['comments_cnt']): cache[key]['expiration'] = int(time.time()) + args.cache_expiration news.pop(key) slack_client = SlackClient(slack_token) if not args.cache_init: for url in news.keys(): message = format_message(url, news[url], args.handle) try: rss2slack.post_to_slack(logger, message, slack_client, args.slack_channel, args.slack_timeout) except ValueError: news.pop(url) finally: time.sleep(args.sleep) expiration = int(time.time()) + args.cache_expiration update_cache(cache, news, expiration) rss2irc.write_cache(cache, args.cache)
def main(): """Main.""" logging.basicConfig(stream=sys.stdout, level=logging.ERROR) logger = logging.getLogger('gh2slack') args = parse_args() if args.verbosity: logger.setLevel(logging.DEBUG) slack_token = rss2slack.get_slack_token() url = get_gh_api_url(args.gh_owner, args.gh_repo, args.gh_section) pages = gh_request(logger, url) logger.debug('Got %i pages from GH.', len(pages)) if not pages: logger.info('No %s for %s/%s.', args.gh_section, args.gh_owner, args.gh_repo) sys.exit(0) cache = rss2irc.read_cache(logger, args.cache) scrub_cache(logger, cache) # Note: I have failed to find web link to repo in GH response. Therefore, # let's create one. repository_url = get_gh_repository_url(args.gh_owner, args.gh_repo) item_expiration = int(time.time()) + args.cache_expiration to_publish = process_page_items( logger, cache, pages, item_expiration, repository_url ) if not args.cache_init and to_publish: slack_client = SlackClient(slack_token) for html_url in to_publish: cache_item = cache[html_url] try: message = assembly_slack_message( logger, args.gh_owner, args.gh_repo, ALIASES[args.gh_section], html_url, cache_item ) rss2slack.post_to_slack( logger, message, slack_client, args.slack_channel, args.slack_timeout ) except Exception: logger.error(traceback.format_exc()) cache.pop(html_url) finally: time.sleep(args.sleep) rss2irc.write_cache(cache, args.cache)
def main(): """Main.""" logging.basicConfig(stream=sys.stdout, level=logging.ERROR) logger = logging.getLogger('git-commits2slack') args = parse_args() if args.verbosity: logger.setLevel(logging.DEBUG) slack_token = rss2slack.get_slack_token() if not os.path.isdir(args.git_clone_dir): git_clone(args.git_clone_dir, args.git_repo) os.chdir(args.git_clone_dir) out = git_pull(args.git_clone_dir) if out.startswith('Already up-to-date.'): logger.info('No new commits.') sys.exit(0) commits = git_show(args.git_clone_dir) if not commits: logger.warning('There should be new commits, but we have none.') sys.exit(0) repo_name = os.path.basename(args.git_clone_dir) branch_name = git_branch(args.git_clone_dir) commit_count = len(commits) if commit_count > 1: suffix = 's' else: suffix = '' messages = [ '<{}/commit/{}|{}> {}'.format(args.git_web, commit[0], commit[0], commit[1]) for commit in commits ] heading = '<{}/tree/{}|[{}:{}]> {:d} commit{}'.format( args.git_web, branch_name, repo_name, branch_name, commit_count, suffix) messages.insert(0, heading) slack_client = SlackClient(slack_token) rss2slack.post_to_slack(logger, '\n'.join(messages), slack_client, args.slack_channel, args.slack_timeout)
def main(): """Fetch phpBB RSS feed and post RSS news to Slack.""" logging.basicConfig(stream=sys.stdout, level=logging.ERROR) logger = logging.getLogger('phpbb2slack') args = parse_args() if args.verbosity: logger.setLevel(logging.DEBUG) if args.cache_expiration < 0: logger.error("Cache expiration can't be less than 0.") sys.exit(1) try: slack_token = rss2slack.get_slack_token() authors = get_authors_from_file(logger, args.authors_file) data = rss2irc.get_rss(logger, args.rss_url, args.rss_http_timeout) if not data: logger.error('Failed to get RSS from %s', args.rss_url) sys.exit(1) news = parse_news(data, authors) if not news: logger.info('No news?') sys.exit(0) cache = rss2irc.read_cache(logger, args.cache) scrub_cache(logger, cache) for key in list(news.keys()): if key not in cache.items: continue logger.debug('Key %s found in cache', key) comments_cached = int(cache.items[key]['comments_cnt']) comments_actual = int(news[key]['comments_cnt']) if comments_cached == comments_actual: cache.items[key]['expiration'] = (int(time.time()) + args.cache_expiration) news.pop(key) slack_client = rss2slack.get_slack_web_client(slack_token, args.slack_base_url, args.slack_timeout) if not args.cache_init: for url in list(news.keys()): msg_blocks = [format_message(url, news[url], args.handle)] try: rss2slack.post_to_slack( logger, msg_blocks, slack_client, args.slack_channel, ) except ValueError: news.pop(url) finally: time.sleep(args.sleep) expiration = int(time.time()) + args.cache_expiration update_cache(cache, news, expiration) rss2irc.write_cache(cache, args.cache) except Exception: logger.debug(traceback.format_exc()) # TODO(zstyblik): # 1. touch error file # 2. send error message to the channel finally: sys.exit(0)
def test_get_slack_token(monkeypatch): """Test get_slack_token().""" monkeypatch.setenv('SLACK_TOKEN', 'test') token = rss2slack.get_slack_token() assert token == 'test'