async def list_queue(conn: aioredis.Redis, args: dict, config: Config) -> web.Response: '''/qaueue list: lists the items in the pipeline and their current status''' if args.get('help', False): body = slack.message_body(args, **slack.help_func(list_queue)) return json_resp(body) body = slack.message_body( args, **(await slack.list_items(await db.QAueueQueue.items(), config))) return json_resp(body)
async def get_item_status(conn: aioredis.Redis, args: dict, config: Config) -> web.Response: item_id = args.get('<status_item_id>') if item_id is None: return await usage_help(conn, args, config) item_id_type, get_item_kwargs = get_item_id_type(item_id) if get_item_kwargs is None: attachment = slack.attachment({ 'fallback': f'Item not found: {item_id}', 'color': colors.RED, 'text': f'Item not found. Unrecognized item ID type: {item_id}', 'fields': [ slack.attachment_field(item_id_type, item_id), ], }) return json_resp( slack.message_body( args, **{ 'text': 'Item Status', 'attachments': [attachment], })) item: db.Item = await db.Item.get(**get_item_kwargs) if item is None: attachment = slack.attachment({ 'fallback': f'Item not found: {item_id}', 'color': colors.RED, 'text': 'Item not found', 'fields': [ slack.attachment_field(item_id_type, item_id), ] }) return json_resp( slack.message_body( args, **{ 'text': 'Item Status', 'attachments': [attachment], })) return json_resp( slack.message_body( args, **{ 'text': 'Item Status', 'attachments': [await slack.render_item_as_attachment(item)], }))
async def usage_help(conn: aioredis.Redis, args: dict, config: Config) -> web.Response: '''/qaueue [-v] help: displays this help message''' body = slack.message_body(args, **{ 'text': f'```{USAGE}```', }) return json_resp(body)
async def add_item(conn: aioredis.Redis, args: dict, config: Config) -> web.Response: '''/qaueue add <item>: adds the item to the pipeline''' item_url = args.get('<item>') if item_url is None: return await usage_help(conn, args, config) if await db.Item.exists(item_url=item_url): item = await db.Item.get(item_url=item_url) return json_resp( slack.message_body( args, **{ 'text': 'Add Item', 'attachments': [item_error('Item already exists', item)], })) item_name = None if github.is_pull_request_url(item_url): g = github.new_client(config.GITHUB_ACCESS_TOKEN) pr = await github.get_pull_request(g, item_url) item_name = pr.title if pivotal.is_pivotal_story_url(item_url): if pivotal.is_full_story_url(item_url): project_id, story_id = pivotal.get_project_story_ids_from_full_url( item_url) story = await pivotal.get_story(story_id, [project_id]) else: story_id = pivotal.get_story_id_from_url(item_url) story = await pivotal.get_story(story_id, config.PIVOTAL_PROJECT_IDS) item_name = story.get('name') if item_name is None: class UnableToGetItemNameError(Exception): pass raise UnableToGetItemNameError(item_url) item = await db.Item.create(url=item_url, name=item_name) return json_resp( slack.message_body( args, **{ 'text': 'Add Item', 'attachments': [ await slack.render_item_as_attachment(item, color=colors.GREEN) ], }))
def channel_command_not_enabled( args: dict, channel: str, command: str, enabled_channels: typing.List[str] = None) -> web.Response: err_msg = f'Command is not enabled in #{channel}' if len(enabled_channels) > 0: err_msg += '. Use one of the following channels: ' + ', '.join( [f'#{c}' for c in enabled_channels]) return json_resp( slack.message_body( args, **{ 'attachments': [ slack.attachment({ 'color': colors.RED, 'text': err_msg, }), ], }))
async def prioritize_item(conn: aioredis.Redis, args: dict, config: Config) -> web.Response: '''/qaueue prioritize <prioritize_item_id> <priority_index>: reorders an item in the pipeline (1 indexed)''' item_id = args.get('<prioritize_item_id>') new_pindex = int(args.get('<priority_index>')) item_id_type, get_item_kwargs = get_item_id_type(item_id) if get_item_kwargs is None: attachment = slack.attachment({ 'fallback': f'Item not found: {item_id}', 'color': colors.RED, 'text': f'Item not found. Unrecognized item ID type: {item_id}', }) return json_resp( slack.message_body( args, **{ 'text': 'Prioritize Item', 'attachments': [ attachment, ], })) item: db.Item = await db.Item.get(**get_item_kwargs) if new_pindex < 0: return json_resp( slack.message_body( args, **{ 'text': 'Prioritize Item', 'attachments': [ slack.attachment({ 'text': ((f'Invalid priority index \'{new_pindex}\': ' f'must be greater than or equal to 0')), }), ], })) if item is None: return json_resp( slack.message_body( args, **{ 'text': 'Prioritize Item', 'attachments': [ slack.attachment({ 'fallback': f'Item not found: {item_id}', 'color': colors.RED, 'text': 'Item not found', 'fields': [ slack.attachment_field(item_id_type, item_id), ], }), ], })) if item.status != statuses.INITIAL: return json_resp( slack.message_body( args, **{ 'text': 'Prioritize Item', 'attachments': [ slack.attachment({ 'fallback': f'Item is not queued: {item.item_id}', 'color': colors.RED, 'text': 'Item is not queued', 'title': item.url, 'title_link': item.url, 'fields': [ slack.attachment_field('Status', item.status), ] }), ], })) await item.set_priority(new_pindex) item = await db.Item.get(item_id=item.item_id) return json_resp( slack.message_body( args, **{ 'text': 'Prioritize Item', 'attachments': [ await slack.render_item_as_attachment(item, color=colors.GREEN) ], }))
async def set_item_status(conn: aioredis.Redis, args: dict, config: Config) -> web.Response: '''/qaueue update <update_item_id> <status>: sets the items current status''' item_id = args.get('<update_item_id>') new_status = args.get('<status>') item_id_type, get_item_kwargs = get_item_id_type(item_id) if get_item_kwargs is None: attachment = slack.attachment({ 'fallback': f'Item not found: {item_id}', 'color': colors.RED, 'text': f'Item not found. Unrecognized item ID type: {item_id}', 'fields': [ slack.attachment_field(item_id_type, item_id), ], }) return json_resp( slack.message_body( args, **{ 'text': 'Set Item Status', 'attachments': [ attachment, ], })) item: db.Item = await db.Item.get(**get_item_kwargs) if item is None: return json_resp( slack.message_body( args, **{ 'text': 'Set Item Status', 'attachments': [ slack.attachment({ 'fallback': f'Item not found: {item_id}', 'color': colors.RED, 'text': 'Item not found', 'fields': [ slack.attachment_field(item_id_type, item_id), ], }), ], })) item.status = new_status error = None if new_status == statuses.COMPLETED: error = await _complete_item(item) await item.update() item = await db.Item.get(item_id=item.item_id) if error is not None: return json_resp( slack.message_body( args, **{ 'text': 'Set Item Status', 'attachments': [ slack.attachment({ 'color': colors.RED, 'text': 'Unable to tag Pivotal story', 'fields': [ slack.attachment_field('Error', str(error), short=False), ], }), await slack.render_item_as_attachment(item, color=colors.GREEN), ], })) return json_resp( slack.message_body( args, **{ 'text': 'Set Item Status', 'attachments': [ await slack.render_item_as_attachment(item, color=colors.GREEN), ], }))