Example #1
0
def process_jira_project_form(request,
                              instance=None,
                              product=None,
                              engagement=None):
    if not get_system_setting('enable_jira'):
        return True, None

    error = False
    jira_project = None
    # supply empty instance to form so it has default values needed to make has_changed() work
    # jform = JIRAProjectForm(request.POST, instance=instance if instance else JIRA_Project(), product=product)
    jform = JIRAProjectForm(request.POST,
                            instance=instance,
                            product=product,
                            engagement=engagement)
    # logging has_changed because it sometimes doesn't do what we expect
    logger.debug('jform has changed: %s', str(jform.has_changed()))

    if jform.has_changed():  # if no data was changed, no need to do anything!
        logger.debug('jform changed_data: %s', jform.changed_data)
        logger.debug('jform: %s', vars(jform))
        if jform.is_valid():
            try:
                jira_project = jform.save(commit=False)
                # could be a new jira_project, so set product_id
                if engagement:
                    jira_project.engagement_id = engagement.id
                    obj = engagement
                elif product:
                    jira_project.product_id = product.id
                    obj = product

                if not jira_project.product_id and not jira_project.engagement_id:
                    raise ValueError(
                        'encountered JIRA_Project without product_id and without engagement_id'
                    )

                # only check jira project if form is sufficiently populated
                if jira_project.jira_instance and jira_project.project_key:
                    # is_jira_project_valid already adds messages if not a valid jira project
                    if not is_jira_project_valid(jira_project):
                        logger.debug(
                            'unable to retrieve jira project from jira instance, invalid?!'
                        )
                        error = True
                    else:
                        logger.debug(vars(jira_project))
                        jira_project.save()
                        # update the in memory instance to make jira_project attribute work and it can be retrieved when pushing
                        # an epic in the next step

                        obj.jira_project = jira_project

                        messages.add_message(
                            request,
                            messages.SUCCESS,
                            'JIRA Project config stored successfully.',
                            extra_tags='alert-success')
                        error = False
                        logger.debug('stored JIRA_Project succesfully')
            except Exception as e:
                error = True
                logger.exception(e)
                pass
        else:
            logger.debug(jform.errors)
            error = True

        if error:
            messages.add_message(
                request,
                messages.ERROR,
                'JIRA Project config not stored due to errors.',
                extra_tags='alert-danger')
    return not error, jform
Example #2
0
def edit_engagement(request, eid):
    engagement = Engagement.objects.get(pk=eid)
    is_ci_cd = engagement.engagement_type == "CI/CD"
    jira_epic_form = None
    jira_project = jira_helper.get_jira_project(engagement,
                                                use_inheritance=False)
    jira_error = False

    if request.method == 'POST':
        form = EngForm(request.POST,
                       instance=engagement,
                       cicd=is_ci_cd,
                       product=engagement.product.id,
                       user=request.user)

        jira_project_form = JIRAProjectForm(request.POST,
                                            prefix='jira-project-form',
                                            instance=jira_project,
                                            target='engagement')
        jira_epic_form = JIRAEngagementForm(request.POST,
                                            prefix='jira-epic-form',
                                            instance=engagement)

        if (form.is_valid()
                and (jira_project_form is None or jira_project_form.is_valid())
                and (jira_epic_form is None or jira_epic_form.is_valid())):

            # first save engagement details
            new_status = form.cleaned_data.get('status')
            engagement = form.save(commit=False)
            if (new_status == "Cancelled" or new_status == "Completed"):
                engagement.active = False
            else:
                engagement.active = True
            engagement.save()

            tags = request.POST.getlist('tags')
            t = ", ".join('"{0}"'.format(w) for w in tags)
            engagement.tags = t

            # save jira project config
            jira_project = jira_project_form.save(commit=False)
            jira_project.engagement = engagement
            # only check jira project if form is sufficiently populated
            if jira_project.jira_instance and jira_project.project_key:
                jira_error = not jira_helper.is_jira_project_valid(
                    jira_project)

                if not jira_error:
                    jira_project.save()

                    messages.add_message(
                        request,
                        messages.SUCCESS,
                        'JIRA Project config added successfully.',
                        extra_tags='alert-success')

            # push epic
            if jira_epic_form.cleaned_data.get('push_to_jira'):
                if jira_helper.push_to_jira(engagement):
                    messages.add_message(
                        request,
                        messages.SUCCESS,
                        'Push to JIRA for Epic queued succesfully, check alerts on the top right for errors',
                        extra_tags='alert-success')
                else:
                    jira_error = True

                    messages.add_message(
                        request,
                        messages.SUCCESS,
                        'Push to JIRA for Epic failed, check alerts on the top right for errors',
                        extra_tags='alert-danger')

            messages.add_message(request,
                                 messages.SUCCESS,
                                 'Engagement updated successfully.',
                                 extra_tags='alert-success')

            if not jira_error:
                if '_Add Tests' in request.POST:
                    return HttpResponseRedirect(
                        reverse('add_tests', args=(engagement.id, )))
                else:
                    return HttpResponseRedirect(
                        reverse('view_engagement', args=(engagement.id, )))

        else:
            # if forms invalid, page will just reload and show errors
            if jira_project_form.errors or jira_epic_form.errors:
                messages.add_message(request,
                                     messages.ERROR,
                                     'Errors in JIRA forms, see below',
                                     extra_tags='alert-danger')

    else:
        form = EngForm(initial={'product': engagement.product},
                       instance=engagement,
                       cicd=is_ci_cd,
                       product=engagement.product,
                       user=request.user)

        jira_project_form = None
        jira_epic_form = None
        if get_system_setting('enable_jira'):
            jira_project_form = JIRAProjectForm(prefix='jira-project-form',
                                                instance=jira_project,
                                                target='engagement',
                                                product=engagement.product)
            if jira_project:
                logger.debug('showing jira-epic-form')
                jira_epic_form = JIRAEngagementForm(prefix='jira-epic-form',
                                                    instance=engagement)

    form.initial['tags'] = [tag.name for tag in engagement.tags]

    title = ' CI/CD' if is_ci_cd else ''
    product_tab = Product_Tab(engagement.product.id,
                              title="Edit" + title + " Engagement",
                              tab="engagements")
    product_tab.setEngagement(engagement)
    return render(
        request, 'dojo/new_eng.html', {
            'product_tab': product_tab,
            'form': form,
            'edit': True,
            'jira_epic_form': jira_epic_form,
            'jira_project_form': jira_project_form,
        })