示例#1
0
def _handle_assessment_deleted(sender, obj=None, service=None):
    """Handles assessment delete event."""
    del sender, service  # Unused

    if not common_handlers.global_synchronization_enabled():
        return

    issue_obj = all_models.IssuetrackerIssue.get_issue(_ASSESSMENT_MODEL_NAME,
                                                       obj.id)

    if issue_obj:
        if (issue_obj.enabled and issue_obj.issue_id
                and _is_issue_tracker_enabled(audit=obj.audit)):
            issue_params = {
                'status':
                'OBSOLETE',
                'comment':
                ('Assessment has been deleted. Changes to this GGRC '
                 'Assessment will no longer be tracked within this bug.'),
            }
            try:
                issues.Client().update_issue(issue_obj.issue_id, issue_params)
            except integrations_errors.Error as error:
                logger.error(
                    'Unable to update a ticket ID=%s while deleting'
                    ' assessment ID=%d: %s', issue_obj.issue_id, obj.id, error)
        db.session.delete(issue_obj)
示例#2
0
def handle_assessment_create(assessment, src):
    """Handles issue tracker related data."""
    if not common_handlers.global_synchronization_enabled():
        return

    # Get issue tracker data from request.
    info = src.get('issue_tracker') or {}

    if not info:
        # Check assessment template for issue tracker data.
        template = referenced_objects.get(
            src.get('template', {}).get('type'),
            src.get('template', {}).get('id'),
        )
        if template:
            info = template.issue_tracker

    if not info:
        # Check audit for issue tracker data.
        audit = referenced_objects.get(
            src.get('audit', {}).get('type'),
            src.get('audit', {}).get('id'),
        )
        if audit:
            info = audit.issue_tracker

    _create_issuetracker_info(assessment, info)
示例#3
0
def _handle_issuetracker(sender, obj=None, src=None, **kwargs):  # noqa
    """Handles IssueTracker information during assessment update event."""
    del sender  # Unused

    if not common_handlers.global_synchronization_enabled():
        return

    if not _is_issue_tracker_enabled(audit=obj.audit):
        # Skip updating issue and info if feature is disabled on Audit level.
        return

    issue_obj = all_models.IssuetrackerIssue.get_issue(_ASSESSMENT_MODEL_NAME,
                                                       obj.id)

    initial_info = issue_obj.to_dict(
        include_issue=True,
        include_private=True) if issue_obj is not None else {}
    new_info = src.get('issue_tracker') or {}

    issue_tracker_info = dict(initial_info, **new_info)

    initial_issue_id = initial_info.get('issue_id')
    old_ticket_id = int(initial_issue_id) if initial_issue_id else None
    get_ticket_id = issue_tracker_info.get('issue_id', None)
    ticket_id = int(get_ticket_id) if get_ticket_id else None

    needs_creation = (not issue_obj) or (not old_ticket_id) or (not ticket_id)

    if issue_tracker_info.get('enabled'):
        if needs_creation:
            _create_issuetracker_info(obj, issue_tracker_info)
            if not obj.warnings:
                it_issue = all_models.IssuetrackerIssue.get_issue(
                    obj.__class__.__name__, obj.id)
                new_ticket_id = it_issue.issue_id if it_issue else None
                if old_ticket_id and new_ticket_id and old_ticket_id != new_ticket_id:
                    _detach_assessment(new_ticket_id, old_ticket_id)
            return

        _, issue_tracker_info['cc_list'] = _collect_assessment_emails(obj)

    else:
        issue_tracker_info['enabled'] = False

    initial_assessment = kwargs.pop('initial_state', None)

    issue_tracker_info['title'] = obj.title
    if not issue_tracker_info.get('due_date'):
        issue_tracker_info['due_date'] = obj.start_date
    issue_tracker_info['status'] = ASSESSMENT_STATUSES_MAPPING.get(obj.status)

    if ticket_id != old_ticket_id and issue_tracker_info['enabled']:
        _link_assessment(obj, issue_tracker_info)
        if not obj.warnings:
            _detach_assessment(ticket_id, old_ticket_id)
        return

    try:
        _update_issuetracker_issue(obj, issue_tracker_info, initial_assessment,
                                   initial_info, src)
    except integrations_errors.Error as error:
        if error.status == 429:
            logger.error(
                'The request updating ticket ID=%d for assessment ID=%d was '
                'rate limited: %s', ticket_id, obj.id, error)
        else:
            logger.error(
                'Unable to update a ticket ID=%d while updating '
                'assessment ID=%d: %s', ticket_id, obj.id, error)
        obj.add_warning('Unable to update a ticket.')

    _update_issuetracker_info(obj, issue_tracker_info)