def report_email(type, namespace=None, data=None):
    """
    Helper function to alert admins in case of failure.

    :param String type: Type to be used
    :param String namespace: Namespace being used
    :param String data: Data being used
    """
    if SEND_EMAILS == '0':
        log.info(
            f"SEND_EMAILS set to 0 not sending email. Type: {type}. Namespace: {namespace}, Data: {data}"
        )
        return
    # Load in the Sync2Jira config
    config = load_config()

    # Email our admins with the traceback
    templateLoader = jinja2.FileSystemLoader(
        searchpath='usr/local/src/sync2jira/continuous-deployment')
    templateEnv = jinja2.Environment(loader=templateLoader)

    # Load in the type of template
    if type is 'failure':
        template = templateEnv.get_template('failure_template.jinja')
        html_text = template.render(namespace=namespace, response=data)
    elif type is 'success':
        template = templateEnv.get_template('success_template.jinja')
        html_text = template.render(namespace=namespace)

    # Send mail
    send_mail(recipients=[config['sync2jira']['mailing-list']],
              cc=None,
              subject=f"Sync2Jira Build Image Update Status: {type}!",
              text=html_text)
示例#2
0
def report_failure(config):
    """
    Helper function to alert admins in case of failure.


    :param Dict config: Config dict for JIRA
    """
    # Email our admins with the traceback
    templateLoader = jinja2.FileSystemLoader(
        searchpath='usr/local/src/sync2jira/sync2jira/')
    templateEnv = jinja2.Environment(loader=templateLoader)
    template = templateEnv.get_template('failure_template.jinja')
    html_text = template.render(traceback=traceback.format_exc())

    # Send mail
    send_mail(recipients=[config['sync2jira']['mailing-list']],
              cc=None,
              subject=failure_email_subject,
              text=html_text)
示例#3
0
def alert_user_of_duplicate_issues(issue, final_result, results_of_query,
                                   config, client):
    """
    Alerts owner of duplicate downstream issues.

    :param sync2jira.intermediate.Issue issue: Upstream Issue object
    :param List final_result: Issue selected by matching algorithm
    :param List results_of_query: Result of JQL query
    :param Dict config: Config dict
    :param jira.client.JIRA client: JIRA client
    :returns: Nothing
    """
    # First remove final_result from results_of_query
    results_of_query.remove(final_result[0])

    # Check that all duplicate issues are closed
    updated_results = []
    for result in results_of_query:
        if result.fields.status.name != 'Closed':
            updated_results.append(result)
    if not updated_results:
        # Nothing to alert the owner of
        return

    # Get base URL
    jira_instance = issue.downstream.get('jira_instance', False)
    if not jira_instance:
        jira_instance = config['sync2jira'].get('default_jira_instance', False)
    if not jira_instance:
        log.error("No jira_instance for issue and there is no default in the config")
        raise Exception
    base_url = config['sync2jira']['jira'][jira_instance]['options']['server'] + '/browse/'

    # Format the updated results
    template_ready = []
    for update in updated_results:
        url = base_url + update.key
        new_entry = {'url': url, 'title': update.key}
        template_ready.append(new_entry)

    # Get owner name and email from Jira
    ret = client.search_users(issue.downstream.get('owner'))
    if len(ret) > 1:
        log.warning('Found multiple users for username %s' % issue.downstream.get('owner'))
        found = False
        for person in ret:
            if person.key == issue.downstream.get('owner'):
                ret = [person]
                found = True
                break
        if not found:
            log.warning('Could not find JIRA user for username %s' % issue.downstream.get('owner'))
    if not ret:
        message = 'No owner could be found for username %s' % issue.downstream.get('owner')
        log.warning(message.strip())
        return

    user = {'name': ret[0].displayName, 'email': ret[0].emailAddress}

    # Format selected issue
    selected_issue = {'url': base_url + final_result[0].key,
                      'title': final_result[0].key}

    # Get admin information
    admins = []
    admin_template = []
    for admin in config['sync2jira']['admins']:
        admin_username = [name for name in admin][0]
        ret = client.search_users(admin_username)
        if len(ret) > 1:
            log.warning('Found multiple users for admin %s' % list(admin.keys())[0])
            found = False
            for person in ret:
                if person.key == issue.downstream.get('owner'):
                    ret = [person]
                    found = True
                    break
            if not found:
                log.warning('Could not find JIRA user for admin %s' % list(admin.keys())[0])
        if not ret:
            message = 'No admin could be found for username %s' % list(admin.keys())[0]
            log.warning(message.strip())
            raise ValueError(message)
        admins.append(ret[0].emailAddress)
        admin_template.append({'name': ret[0].displayName, 'email': ret[0].emailAddress})

    # Create and send email
    templateLoader = jinja2.FileSystemLoader(
        searchpath='usr/local/src/sync2jira/sync2jira/')
    templateEnv = jinja2.Environment(loader=templateLoader)
    template = templateEnv.get_template('email_template.jinja')
    html_text = template.render(user=user,
                                admins=admin_template,
                                issue=issue,
                                selected_issue=selected_issue,
                                duplicate_issues=template_ready)

    # Send mail
    send_mail(recipients=[user['email']],
              cc=admins,
              subject=duplicate_issues_subject,
              text=html_text)
    log.info('Alerted %s about %s duplicate issue(s)' %
             (user['email'], len(template_ready)))