def show(jira, args): values = ['projects', 'issues', 'open', 'done', 'fires', 'issue', 'users', 'statuses'] m = re.match(r'({})*(?: (.*))?'.format('|'.join([v for v in values])), args, re.IGNORECASE) if not m: return utils.not_valid_args(args) type = m.group(1) args = m.group(2) or '' if not type: return utils.not_valid_args(args, message='valid commands are: {}'.format(', '.join(values))) if type == 'projects': return projects(jira, args) elif type == 'issues': return issues(jira, args) elif type == 'open': return open_issues(jira, args) elif type == 'done': return done_issues(jira, args) elif type == 'fires': return fires(jira, args) elif type == 'issue': return show_issue(jira, args) elif type == 'users': return users(jira, args) elif type == 'statuses': return statuses(jira, args)
def fires(jira, args): m = re.match(r'(\w+)?', args) if not m: return utils.not_valid_args(args) project_key = m.group(1) or config.get('jira_default_project') if not project_key: return utils.error('Project name is required') if not utils.check_project(jira, project_key): return utils.error('Project {} does not exist'.format(project_key)) try: query = 'project={0} and labels in (fire)'.format(project_key) issues = jira.search_issues(query) if not issues: return 'No issues found' return '\n'.join([utils.issue_info(issue) for issue in issues]) except JIRAError as e: response = utils.error('{} {}'.format(str(e.status_code), str(e.text))) return response
def status(jira, args): m = re.match(r'(\w+-\d+) (.*)', args) if not m: return utils.not_valid_args(args) issue_key = m.group(1) issue_status = m.group(2) try: issue = jira.issue(issue_key) statuses = jira.statuses() if issue_status not in [s.name for s in statuses]: return utils.error('Status {} does not exist'.format(issue_status)) if issue_status == issue.fields.status.name: return utils.error('Status {} already set'.format(issue_status)) transitions = jira.transitions(issue) transition_id = utils.get_transition(transitions, issue_status) if not transition_id: return utils.error('Operation not permitted') jira.transition_issue(issue, transition_id) issue = jira.issue(issue_key) return utils.issue_info(issue) except JIRAError as e: response = utils.error('{} {}'.format(str(e.status_code), str(e.text))) return response
def close(jira, args): m = re.match(r'(\w+-\d+) ?(.*)', args) if not m: return utils.not_valid_args(args) issue_key = m.group(1) issue_status = 'Closed' comment = m.group(2) try: issue = jira.issue(issue_key) transitions = jira.transitions(issue) transition_id = utils.get_transition(transitions, issue_status) if issue_status == issue.fields.status.name: return utils.error('Issue already closed') if not transition_id: return utils.error('Operation not permitted') jira.transition_issue(issue, transition_id, comment=comment) issue = jira.issue(issue_key) return utils.issue_info(issue) except JIRAError as e: if e.status_code == 500: return utils.error('operation not permitted') response = utils.error('{} {}'.format(str(e.status_code), str(e.text))) return response
def show_issue(jira, args): m = re.match(r'(\w+-\d+)', args) if not m: return utils.not_valid_args(args, message='') issue_key = m.group(1) try: issue = jira.issue(issue_key) return utils.issue_info(issue) except JIRAError as e: response = utils.error('{} {}'.format(str(e.status_code), str(e.text))) return response
def comment(jira, args): m = re.match(r"(\w+-\d+) (.*)", args) if not m: return utils.not_valid_args(args) issue_id = m.group(1) comment = m.group(2) if not comment: return utils.error('Leave a comment') try: jira.add_comment(issue_id, comment) except JIRAError as e: response = utils.error('{} {}'.format(str(e.status_code), str(e.text))) return response
def description(jira, args): m = re.match(r"(\w+-\d+) (.*)", args) if not m: return utils.not_valid_args(args) issue_id = m.group(1) description = m.group(2) or '' try: issue = jira.issue(issue_id) issue.update(description=description) return utils.issue_info(issue) except JIRAError as e: response = utils.error('{} {}'.format(str(e.status_code), str(e.text))) return response
def assign(jira, args): m = re.match(r"(?:@(\w+)) (\w+-\d+)", args) if not m: return utils.not_valid_args(args) user = m.group(1) issue_id = m.group(2) try: jira.assign_issue(issue_id, user) issue = jira.issue(issue_id) return utils.issue_info(issue) except JIRAError as e: response = utils.error('{} {}'.format(str(e.status_code), str(e.text))) return response
def users(jira, args): m = re.match(r'(\w+)?', args) if not m: return utils.not_valid_args(args) project_key = m.group(1) or config.get('jira_default_project') if not project_key: return utils.error('Project name is required') if not utils.check_project(jira, project_key): return utils.error('Project {} does not exist'.format(project_key)) try: users = jira.search_assignable_users_for_projects('', project_key) return '\n'.join([utils.user_info(user) for user in users]) except JIRAError as e: response = utils.error('{} {}'.format(str(e.status_code), str(e.text))) return response
def done_issues(jira, args): # todo m = re.match(r'(\w+)?', args) if not m: return utils.not_valid_args(args) project_key = m.group(1) or config.get('jira_default_project') if not project_key: return utils.error('Project name is required') if not utils.check_project(jira, project_key): return utils.error('Project {} does not exist'.format(project_key)) query = 'project={} and status in (\'Done\', \'Closed\', \'Resolved\')'.format(project_key) issues = jira.search_issues(query) if not issues: return 'No issues found' return '\n\n'.join([utils.issue_info(issue) for issue in issues])
def create(jira, args): m = re.match(r'(\w+)? ?(?:@(\w+))? (.*)', args) if not m: return utils.not_valid_args(args) project_key = m.group(1) or config.get('jira_default_project') if not project_key: return utils.error('Project name is required') assignee = m.group(2) summary = m.group(3) fields = { 'project': {'key': project_key}, 'summary': summary, 'issuetype': {'name': config.get('jira_default_issue_type')}, } try: if assignee: jira.user(assignee) issue = jira.create_issue(fields=fields) issue.fields.labels = config.get('jira_default_labels') issue.update(fields={"labels": issue.fields.labels}) # weird, but user cannot be assigned during creation if assignee: issue.update(assignee={'name': assignee}) return utils.issue_info(issue) except JIRAError as e: response = utils.error('{} {}'.format(str(e.status_code), str(e.text))) return response