コード例 #1
0
def new_jira(request):
    if request.method == 'POST':
        jform = JIRAForm(request.POST, instance=JIRA_Instance())
        if jform.is_valid():
            jira_server = jform.cleaned_data.get('url').rstrip('/')
            jira_username = jform.cleaned_data.get('username')
            jira_password = jform.cleaned_data.get('password')

            logger.debug('calling get_jira_connection_raw')
            jira = jira_helper.get_jira_connection_raw(jira_server,
                                                       jira_username,
                                                       jira_password)

            new_j = jform.save(commit=False)
            new_j.url = jira_server
            new_j.save()
            messages.add_message(request,
                                 messages.SUCCESS,
                                 'JIRA Configuration Successfully Created.',
                                 extra_tags='alert-success')
            create_notification(
                event='other',
                title='New addition of JIRA: %s' %
                jform.cleaned_data.get('configuration_name'),
                description='JIRA "%s" was added by %s' %
                (jform.cleaned_data.get('configuration_name'), request.user),
                url=request.build_absolute_uri(reverse('jira')),
            )
            return HttpResponseRedirect(reverse('jira', ))
    else:
        jform = JIRAForm()
        add_breadcrumb(title="New Jira Configuration",
                       top_level=False,
                       request=request)
    return render(request, 'dojo/new_jira.html', {'jform': jform})
コード例 #2
0
def edit_jira(request, jid):
    jira = JIRA_Instance.objects.get(pk=jid)
    jira_password_from_db = jira.password
    if request.method == 'POST':
        jform = JIRAForm(request.POST, instance=jira)
        if jform.is_valid():
            jira_server = jform.cleaned_data.get('url').rstrip('/')
            jira_username = jform.cleaned_data.get('username')

            if jform.cleaned_data.get('password'):
                jira_password = jform.cleaned_data.get('password')
            else:
                # on edit the password is optional
                jira_password = jira_password_from_db

            jira = jira_helper.get_jira_connection_raw(jira_server,
                                                       jira_username,
                                                       jira_password,
                                                       validate=True)

            new_j = jform.save(commit=False)
            new_j.url = jira_server
            # on edit the password is optional
            new_j.password = jira_password
            new_j.save()
            messages.add_message(request,
                                 messages.SUCCESS,
                                 'JIRA Configuration Successfully Saved.',
                                 extra_tags='alert-success')
            create_notification(
                event='other',
                title='Edit of JIRA: %s' %
                jform.cleaned_data.get('configuration_name'),
                description='JIRA "%s" was edited by %s' %
                (jform.cleaned_data.get('configuration_name'), request.user),
                url=request.build_absolute_uri(reverse('jira')),
            )
            return HttpResponseRedirect(reverse('jira', ))

    else:
        jform = JIRAForm(instance=jira)
        add_breadcrumb(title="Edit JIRA Configuration",
                       top_level=False,
                       request=request)

    return render(request, 'dojo/edit_jira.html', {
        'jform': jform,
    })
コード例 #3
0
    def test_add_jira_instance_unknown_host(self):
        data = self.data_jira_instance
        data['url'] = 'https://jira.hj23412341hj234123421341234ljl.nl'

        # test UI validation error

        # self.client.force_login('admin', backend='django.contrib.auth.backends.ModelBackend')
        # Client.raise_request_exception = False  # needs Django 3.0
        # can't use helper method which has patched connection raw method
        response = self.client.post(
            reverse('add_jira'),
            urlencode(data),
            content_type='application/x-www-form-urlencoded')

        self.assertEqual(200, response.status_code)
        content = response.content.decode('utf-8')
        self.assertTrue('Name or service not known' in content)

        # test raw connection error
        with self.assertRaises(requests.exceptions.RequestException):
            jira = jira_helper.get_jira_connection_raw(data['url'],
                                                       data['username'],
                                                       data['password'])
コード例 #4
0
def express_new_jira(request):
    if request.method == 'POST':
        jform = ExpressJIRAForm(request.POST, instance=JIRA_Instance())
        if jform.is_valid():
            jira_server = jform.cleaned_data.get('url').rstrip('/')
            jira_username = jform.cleaned_data.get('username')
            jira_password = jform.cleaned_data.get('password')

            try:
                jira = jira_helper.get_jira_connection_raw(
                    jira_server, jira_username, jira_password)
            except Exception as e:
                logger.exception(e)  # already logged in jira_helper
                messages.add_message(
                    request,
                    messages.ERROR,
                    'Unable to authenticate. Please check credentials.',
                    extra_tags='alert-danger')
                return render(request, 'dojo/express_new_jira.html',
                              {'jform': jform})
            # authentication successful
            # Get the open and close keys
            try:
                issue_id = jform.cleaned_data.get('issue_key')
                key_url = jira_server.strip(
                    '/'
                ) + '/rest/api/latest/issue/' + issue_id + '/transitions?expand=transitions.fields'
                response = jira._session.get(key_url).json()
                open_key = close_key = None
                for node in response['transitions']:
                    if node['to']['statusCategory']['name'] == 'To Do':
                        open_key = int(
                            node['id']) if not open_key else open_key
                    if node['to']['statusCategory']['name'] == 'Done':
                        close_key = int(
                            node['id']) if not close_key else close_key
            except Exception as e:
                logger.exception(e)  # already logged in jira_helper
                messages.add_message(
                    request,
                    messages.ERROR,
                    'Unable to find Open/Close ID\'s. They will need to be found manually',
                    extra_tags='alert-danger')
                return render(request, 'dojo/new_jira.html', {'jform': jform})
            # Get the epic id name
            try:
                epic_name = get_custom_field(jira, 'Epic Name')
            except Exception as e:
                logger.exception(e)  # already logged in jira_helper
                messages.add_message(
                    request,
                    messages.ERROR,
                    'Unable to find Epic Name. It will need to be found manually',
                    extra_tags='alert-danger')
                return render(request, 'dojo/new_jira.html', {'jform': jform})

            jira_instance = JIRA_Instance(
                username=jira_username,
                password=jira_password,
                url=jira_server,
                configuration_name=jform.cleaned_data.get(
                    'configuration_name'),
                info_mapping_severity='Lowest',
                low_mapping_severity='Low',
                medium_mapping_severity='Medium',
                high_mapping_severity='High',
                critical_mapping_severity='Highest',
                epic_name_id=epic_name,
                open_status_key=open_key,
                close_status_key=close_key,
                finding_text='',
                default_issue_type=jform.cleaned_data.get(
                    'default_issue_type'))
            jira_instance.save()
            messages.add_message(request,
                                 messages.SUCCESS,
                                 'JIRA Configuration Successfully Created.',
                                 extra_tags='alert-success')
            create_notification(
                event='other',
                title='New addition of JIRA: %s' %
                jform.cleaned_data.get('configuration_name'),
                description='JIRA "%s" was added by %s' %
                (jform.cleaned_data.get('configuration_name'), request.user),
                url=request.build_absolute_uri(reverse('jira')),
            )
            return HttpResponseRedirect(reverse('jira', ))
    else:
        jform = ExpressJIRAForm()
        add_breadcrumb(title="New Jira Configuration (Express)",
                       top_level=False,
                       request=request)
    return render(request, 'dojo/express_new_jira.html', {'jform': jform})
コード例 #5
0
def express_new_jira(request):
    if request.method == 'POST':
        jform = ExpressJIRAForm(request.POST, instance=JIRA_Instance())
        if jform.is_valid():
            try:
                jira_server = jform.cleaned_data.get('url').rstrip('/')
                jira_username = jform.cleaned_data.get('username')
                jira_password = jform.cleaned_data.get('password')

                try:
                    jira = jira_helper.get_jira_connection_raw(
                        jira_server, jira_username, jira_password)
                except Exception as e:
                    logger.exception(e)  # already logged in jira_helper
                    return render(request, 'dojo/express_new_jira.html',
                                  {'jform': jform})
                # authentication successful
                # Get the open and close keys
                issue_id = jform.cleaned_data.get('issue_key')
                key_url = jira_server + '/rest/api/latest/issue/' + issue_id + '/transitions?expand=transitions.fields'
                data = json.loads(
                    requests.get(key_url,
                                 auth=(jira_username, jira_password)).text)
                for node in data['transitions']:
                    if node['to']['name'] == 'To Do':
                        open_key = int(node['to']['id'])
                    if node['to']['name'] == 'Done':
                        close_key = int(node['to']['id'])
                # Get the epic id name
                key_url = jira_server + '/rest/api/2/field'
                data = json.loads(
                    requests.get(key_url,
                                 auth=(jira_username, jira_password)).text)
                for node in data:
                    if 'Epic Name' in node['clauseNames']:
                        epic_name = int(node['clauseNames'][0][3:-1])
                        break

                jira_instance = JIRA_Instance(
                    username=jira_username,
                    password=jira_password,
                    url=jira_server,
                    configuration_name=jform.cleaned_data.get(
                        'configuration_name'),
                    info_mapping_severity='Lowest',
                    low_mapping_severity='Low',
                    medium_mapping_severity='Medium',
                    high_mapping_severity='High',
                    critical_mapping_severity='Highest',
                    epic_name_id=epic_name,
                    open_status_key=open_key,
                    close_status_key=close_key,
                    finding_text='',
                    default_issue_type=jform.cleaned_data.get(
                        'default_issue_type'))
                jira_instance.save()
                messages.add_message(
                    request,
                    messages.SUCCESS,
                    'JIRA Configuration Successfully Created.',
                    extra_tags='alert-success')
                create_notification(
                    event='other',
                    title='New addition of JIRA: %s' %
                    jform.cleaned_data.get('configuration_name'),
                    description='JIRA "%s" was added by %s' %
                    (jform.cleaned_data.get('configuration_name'),
                     request.user),
                    url=request.build_absolute_uri(reverse('jira')),
                )
                return HttpResponseRedirect(reverse('jira', ))
            except:
                messages.add_message(
                    request,
                    messages.ERROR,
                    'Unable to query other required fields. They must be entered manually.',
                    extra_tags='alert-danger')
                return HttpResponseRedirect(reverse('add_jira', ))
            return render(request, 'dojo/express_new_jira.html',
                          {'jform': jform})
    else:
        jform = ExpressJIRAForm()
        add_breadcrumb(title="New Jira Configuration (Express)",
                       top_level=False,
                       request=request)
    return render(request, 'dojo/express_new_jira.html', {'jform': jform})