示例#1
0
def create_ticket_for_new_issue(obj, issue_tracker_info):
    """Create new IssueTracker ticket for issue"""
    builder = issue_tracker_params_builder.IssueParamsBuilder()
    issue_tracker_params = builder.build_create_issue_tracker_params(
        obj, issue_tracker_info)

    if issue_tracker_params.is_empty():
        return

    # Query to IssueTracker.
    issue_tracker_query = issue_tracker_params.get_issue_tracker_params()

    # Parameters for creation IssuetrackerIssue object in GGRC.
    issuetracker_issue_params = issue_tracker_params.get_params_for_ggrc_object(
    )

    try:
        res = issues.Client().create_issue(issue_tracker_query)

        issue_url = integration_utils.build_issue_tracker_url(res["issueId"])
        issuetracker_issue_params["issue_url"] = issue_url
        issuetracker_issue_params["issue_id"] = res["issueId"]
    except integrations_errors.Error as error:
        logger.error(
            "Unable to create a ticket while creating object ID=%d: %s",
            obj.id, error)
        obj.add_warning("Unable to create a ticket in issue tracker.")
        issuetracker_issue_params["enabled"] = False

    # Create object in GGRC with info about issue tracker integration.
    all_models.IssuetrackerIssue.create_or_update_from_dict(
        obj, issuetracker_issue_params)
示例#2
0
def prepare_issue_update_json(issue, issue_tracker_info=None):
    """Prepare issuetracker issue json for Issue object update."""
    if not issue_tracker_info:
        issue_tracker_info = issue.issue_tracker

    builder = issue_tracker_params_builder.IssueParamsBuilder()
    builder.handle_issue_tracker_info(issue, issue_tracker_info)
    issue_tracker_params = builder.params
    params = issue_tracker_params.get_issue_tracker_params()
    return params
示例#3
0
def detach_issue(new_ticket_id, old_ticket_id):
    """Send to old IssueTracker ticket detachment comment."""
    builder = issue_tracker_params_builder.IssueParamsBuilder()
    params = builder.build_detach_comment(new_ticket_id)
    query = params.get_issue_tracker_params()

    try:
        issues.Client().update_issue(old_ticket_id, query)
    except integrations_errors.Error as error:
        logger.error("Unable to add detach comment to ticket issue ID=%d: %s",
                     old_ticket_id, error)
示例#4
0
def link_issue(obj, ticket_id, issue_tracker_info):
  """Link issue to existing IssueTracker ticket"""

  if _is_already_linked(ticket_id):
    logger.error(
        "Unable to link a ticket while creating object ID=%d: %s ticket ID is "
        "already linked to another GGRC object",
        obj.id,
        ticket_id,
    )
    obj.add_warning(
        "This ticket was already linked to another GGRC issue, assessment "
        "or review object. Linking the same ticket to multiple objects is not "
        "allowed due to potential for conflicting updates."
    )
    return

  builder = issue_tracker_params_builder.IssueParamsBuilder()
  issue_tracker_container = builder.build_params_for_issue_link(
      obj,
      ticket_id,
      issue_tracker_info,
  )

  if issue_tracker_container.is_empty():
    return

  # Query to IssueTracker.
  issue_tracker_query = issue_tracker_container.get_issue_tracker_params()

  # Parameters for creation IssuetrackerIssue object in GGRC.
  issuetracker_issue_params = \
      issue_tracker_container.get_params_for_ggrc_object()

  try:
    issues.Client().update_issue(ticket_id, issue_tracker_query)

    ticket_url = integration_utils.build_issue_tracker_url(ticket_id)
    issuetracker_issue_params["issue_url"] = ticket_url
    issuetracker_issue_params["issue_id"] = ticket_id
    update_initial_issue(obj, issue_tracker_container)
  except integrations_errors.Error as error:
    logger.error("Unable to update a ticket ID=%s while deleting"
                 " issue ID=%d: %s",
                 ticket_id, obj.id, error)
    obj.add_warning("Unable to update a ticket in issue tracker.")
    issuetracker_issue_params["enabled"] = False
    return

  if issuetracker_issue_params:
    all_models.IssuetrackerIssue.create_or_update_from_dict(
        obj, issuetracker_issue_params
    )
示例#5
0
def update_issue_handler(obj, initial_state, new_issue_tracker_info=None):
    """Event handler for issue object renewal."""
    issue_tracker_object = all_models.IssuetrackerIssue.get_issue(
        "Issue", obj.id)

    if not issue_tracker_object:
        if new_issue_tracker_info and new_issue_tracker_info["enabled"]:
            create_issue_handler(obj, new_issue_tracker_info)
        return

    # Try to create ticket in Issue tracker if previous try failed.
    if new_issue_tracker_info and new_issue_tracker_info["enabled"] \
       and not issue_tracker_object.issue_id:
        create_issue_handler(obj, new_issue_tracker_info)
        return

    current_issue_tracker_info = issue_tracker_object.to_dict(
        include_issue=True, include_private=True)

    if not new_issue_tracker_info:
        # Use existing issue tracker info if object is updating via import
        new_issue_tracker_info = current_issue_tracker_info

    # Build query
    builder = issue_tracker_params_builder.IssueParamsBuilder()
    issue_tracker_params = builder.build_update_issue_tracker_params(
        obj, initial_state, new_issue_tracker_info, current_issue_tracker_info)

    # Query to IssueTracker.
    issue_tracker_query = issue_tracker_params.get_issue_tracker_params()

    # Parameters for creation IssuetrackerIssue object in GGRC.
    issuetracker_issue_params = issue_tracker_params.get_params_for_ggrc_object(
    )

    if not issue_tracker_params.is_empty():
        try:
            issue_id = issue_tracker_object.issue_id
            issues.Client().update_issue(issue_id, issue_tracker_query)
        except integrations_errors.Error as error:
            logger.error(
                "Unable to update a ticket ID=%s while deleting"
                " issue ID=%d: %s", issue_tracker_object.issue_id, obj.id,
                error)
            obj.add_warning("Unable to update a ticket in issue tracker.")

    if issuetracker_issue_params:
        all_models.IssuetrackerIssue.create_or_update_from_dict(
            obj, issuetracker_issue_params)
示例#6
0
def prepare_issue_json(issue, issue_tracker_info=None):
    """Prepare issuetracker issue json for Issue object."""
    if not issue_tracker_info:
        issue_tracker_info = issue.issuetracker_issue.to_dict()

    builder = issue_tracker_params_builder.IssueParamsBuilder()
    issue_tracker_params = builder.build_create_issue_tracker_params(
        issue, issue_tracker_info)

    if issue_tracker_params.is_empty():
        return {}

    params = issue_tracker_params.get_issue_tracker_params()
    if "type" not in params:
        params["type"] = issue_tracker_info.get("issue_type")
    return params
示例#7
0
def create_comment_handler(sync_object, comment, author):
    """Event handler for adding comment to Issue object."""
    issue_tracker_object = all_models.IssuetrackerIssue.get_issue(
        "Issue", sync_object.id)

    if not issue_tracker_object or not issue_tracker_object.enabled:
        return

    builder = issue_tracker_params_builder.IssueParamsBuilder()
    params = builder.build_params_for_comment(sync_object, comment.description,
                                              author)
    query = params.get_issue_tracker_params()

    try:
        issues.Client().update_issue(issue_tracker_object.issue_id, query)
    except integrations_errors.Error as error:
        logger.error("Unable to add comment to ticket issue ID=%d: %s",
                     issue_tracker_object.issue_id, error)
        sync_object.add_warning("Unable to update a ticket in issue tracker.")
示例#8
0
def delete_issue_handler(obj, **kwargs):
  """Event handler for issue object deletion."""
  issue_tracker_object = all_models.IssuetrackerIssue.get_issue("Issue",
                                                                obj.id)

  if issue_tracker_object:
    if issue_tracker_object.enabled and issue_tracker_object.issue_id:
      builder = issue_tracker_params_builder.IssueParamsBuilder()
      issue_tracker_params = builder.build_delete_issue_tracker_params()
      issue_tracker_query = issue_tracker_params.get_issue_tracker_params()
      try:
        issues.Client().update_issue(issue_tracker_object.issue_id,
                                     issue_tracker_query)
      except integrations_errors.Error as error:
        logger.error("Unable to update a ticket ID=%s while deleting"
                     " issue ID=%d: %s",
                     issue_tracker_object.issue_id, obj.id, error)
        obj.add_warning("Unable to update a ticket in issue tracker.")
    db.session.delete(issue_tracker_object)
示例#9
0
def create_issue_handler(obj, issue_tracker_info):
    """Event handler for issue object creation."""
    if not issue_tracker_info or not issue_tracker_info.get("enabled"):
        return

    # We need in flush here because we need object id for URL generation.
    db.session.flush()

    builder = issue_tracker_params_builder.IssueParamsBuilder()
    issue_tracker_params = builder.build_create_issue_tracker_params(
        obj, issue_tracker_info)

    if issue_tracker_params.is_empty():
        return

    # Query to IssueTracker.
    issue_tracker_query = issue_tracker_params.get_issue_tracker_params()

    # Parameters for creation IssuetrackerIssue object in GGRC.
    issuetracker_issue_params = issue_tracker_params.get_params_for_ggrc_object(
    )

    try:
        res = issues.Client().create_issue(issue_tracker_query)

        issue_url = integration_utils.build_issue_tracker_url(res["issueId"])
        issuetracker_issue_params["issue_url"] = issue_url
        issuetracker_issue_params["issue_id"] = res["issueId"]
    except integrations_errors.Error as error:
        logger.error(
            "Unable to create a ticket while creating object ID=%d: %s",
            obj.id, error)
        obj.add_warning("Unable to create a ticket in issue tracker.")
        issuetracker_issue_params["enabled"] = False

    # Create object in GGRC with info about issue tracker integration.
    all_models.IssuetrackerIssue.create_or_update_from_dict(
        obj, issuetracker_issue_params)
示例#10
0
 def setUp(self):
     """Perform initialisation for each test cases."""
     self.builder = params_builder.IssueParamsBuilder()
示例#11
0
def prepare_comment_update_json(object_, comment, author):
    """Prepare json for adding comment to IssueTracker issue"""
    builder = issue_tracker_params_builder.IssueParamsBuilder()
    params = builder.build_params_for_comment(object_, comment, author)
    return params.get_issue_tracker_params()
示例#12
0
def update_issue_handler(obj,
                         initial_state,
                         new_issuetracker_info=None):  # noqa
    """Event handler for issue object renewal."""
    # TODO: refactor this handler to be not so complex and more generic
    if not new_issuetracker_info:
        return

    it_object = all_models.IssuetrackerIssue.get_issue("Issue", obj.id)
    old_ticket_id = None
    if it_object:
        old_ticket_id = int(it_object.issue_id) if it_object.issue_id else None

    get_id = new_issuetracker_info.get("issue_id") if new_issuetracker_info \
        else None

    new_ticket_id = int(get_id) if get_id else None

    # We should create new ticket if new ticket_id is empty, we don't store
    # IssueTrackerIssue object or it contains empty ticket_id
    needs_creation = (not it_object) or \
                     (not old_ticket_id) or (not new_ticket_id)

    if needs_creation:
        create_issue_handler(obj, new_issuetracker_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_issue(new_ticket_id, old_ticket_id)
        return

    if not it_object:
        return

    if (new_ticket_id != old_ticket_id) and new_issuetracker_info["enabled"]:
        link_issue(obj, new_ticket_id, new_issuetracker_info)
        if not obj.warnings:
            detach_issue(new_ticket_id, old_ticket_id)
        return

    current_issue_tracker_info = it_object.to_dict(include_issue=True,
                                                   include_private=True)

    # Build query
    builder = issue_tracker_params_builder.IssueParamsBuilder()
    issue_tracker_params = builder.build_update_issue_tracker_params(
        obj, initial_state, new_issuetracker_info, current_issue_tracker_info)

    # Query to IssueTracker.
    issue_tracker_query = issue_tracker_params.get_issue_tracker_params()

    # Parameters for creation IssuetrackerIssue object in GGRC.
    issuetracker_issue_params = issue_tracker_params.get_params_for_ggrc_object(
    )

    if not issue_tracker_params.is_empty():
        try:
            issue_id = it_object.issue_id
            issues.Client().update_issue(issue_id, issue_tracker_query)
        except integrations_errors.Error as error:
            logger.error(
                "Unable to update a ticket ID=%s while deleting"
                " issue ID=%d: %s", it_object.issue_id, obj.id, error)
            obj.add_warning("Unable to update a ticket in issue tracker.")

    if issuetracker_issue_params:
        all_models.IssuetrackerIssue.create_or_update_from_dict(
            obj, issuetracker_issue_params)