Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions dojo/api_v2/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Standard library imports
import logging

from django.http import HttpResponse, Http404
from django.shortcuts import get_object_or_404
from django.utils import timezone
Expand Down Expand Up @@ -28,6 +31,7 @@
from dojo.api_v2 import serializers, permissions
from django.db.models import Count, Q

logger = logging.getLogger(__name__)

class EndPointViewSet(mixins.ListModelMixin,
mixins.RetrieveModelMixin,
Expand Down Expand Up @@ -275,23 +279,33 @@ def tags(self, request, pk=None):
def notes(self, request, pk=None):
finding = get_object_or_404(Finding.objects, id=pk)
if request.method == 'POST':
note_type = None
jira_id = -1
new_note = serializers.AddNewNoteOptionSerializer(data=request.data)
if new_note.is_valid():
entry = new_note.validated_data['entry']
private = new_note.validated_data['private']
note_type = new_note.validated_data['note_type']
try:
entry = new_note.validated_data['entry']
private = new_note.validated_data['private']
if 'note_type' in request.data:
note_type = request.data['note_type']
if 'jira_id' in request.data:
jira_id = request.data['jira_id']
except Exception as e:
logger.error(e)
return Response(new_note.errors,
status=status.HTTP_400_BAD_REQUEST)
else:
return Response(new_note.errors,
status=status.HTTP_400_BAD_REQUEST)

author = request.user
note = Notes(entry=entry, author=author, private=private, note_type=note_type)
note = Notes(entry=entry, author=author, private=private, note_type=note_type, jira_id=jira_id)
note.save()
finding.notes.add(note)

serialized_note = serializers.NoteSerializer({
"author": author, "entry": entry,
"private": private
"private": private, "jira_id": note.jira_id
})
result = serializers.FindingToNotesSerializer({
"finding_id": finding, "notes": [serialized_note.data]
Expand Down
18 changes: 18 additions & 0 deletions dojo/db_migrations/0051_notes_jira_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.15 on 2020-08-31 20:36

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dojo', '0050_deduplication_on_engagement'),
]

operations = [
migrations.AddField(
model_name='notes',
name='jira_id',
field=models.IntegerField(blank=True, null=True),
),
]
42 changes: 24 additions & 18 deletions dojo/jira_link/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,30 @@ def webhook(request):
eng.save()
"""
if parsed.get('webhookEvent') == 'comment_created':
comment_text = parsed['comment']['body']
commentor = parsed['comment']['updateAuthor']['displayName']
jid = parsed['comment']['self'].split('/')[7]
jissue = JIRA_Issue.objects.get(jira_id=jid)
jira = JIRA_Conf.objects.values_list('username', flat=True)
for jira_userid in jira:
if jira_userid.lower() in commentor.lower():
return HttpResponse('')
break
finding = jissue.finding
new_note = Notes()
new_note.entry = '(%s): %s' % (commentor, comment_text)
new_note.author, created = User.objects.get_or_create(username='JIRA')
new_note.save()
finding.notes.add(new_note)
finding.jira_change = timezone.now()
finding.save()
create_notification(event='other', title='JIRA Update - %s' % (jissue.finding), url=reverse("view_finding", args=(jissue.id,)), icon='check')
# Only create a note if it doesn't exist locally (to avoid loops).
jira_note_creation_date = datetime.strptime(str(parsed['comment']['created']), "%Y-%m-%dT%H:%M:%S.%f%z")
try:
note = Notes.objects.get(jira_id=parsed['comment']['id'])
logger.info('Note already created from DefectDojo, skipping creation')
except Notes.DoesNotExist:
logger.info('Note created from Jira, creating it in DefectDojo')
comment_text = parsed['comment']['body']
commentor = parsed['comment']['updateAuthor']['displayName']
jid = parsed['comment']['self'].split('/')[7]
jissue = JIRA_Issue.objects.get(jira_id=jid)
finding = jissue.finding
new_note = Notes()
new_note.entry = '(%s): %s' % (commentor, comment_text)
new_note.author, created = User.objects.get_or_create(username='JIRA')
new_note.date = jira_note_creation_date
new_note.save()
finding.notes.add(new_note)
finding.jira_change = timezone.now()
finding.save()
create_notification(event='other', title='JIRA Update - %s' % (jissue.finding), url=reverse("view_finding", args=(jissue.id,)), icon='check')
except Exception as e:
print("catch all")

@madchap madchap Sep 8, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using logger would be better than print. Or remove.

logger.error(str(e))

if parsed.get('webhookEvent') not in ['comment_created', 'jira:issue_updated']:
logger.info('Unrecognized JIRA webhook event received: {}'.format(parsed.get('webhookEvent')))
Expand Down
1 change: 1 addition & 0 deletions dojo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,7 @@ class Notes(models.Model):
entry = models.TextField()
date = models.DateTimeField(null=False, editable=False,
default=get_current_datetime)
jira_id = models.IntegerField(null=True, blank=True)
author = models.ForeignKey(User, related_name='editor_notes_set', editable=False, on_delete=models.CASCADE)
private = models.BooleanField(default=False)
edited = models.BooleanField(default=False)
Expand Down
4 changes: 3 additions & 1 deletion dojo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1705,9 +1705,11 @@ def add_comment(find, note, force_push=False):
server=jira_conf.url,
basic_auth=(jira_conf.username, jira_conf.password))
j_issue = JIRA_Issue.objects.get(finding=find)
jira.add_comment(
jira_comment = jira.add_comment(
j_issue.jira_id,
'(%s): %s' % (note.author.get_full_name(), note.entry))
note.jira_id = int(jira_comment.id)
note.save()
except Exception as e:
log_jira_generic_alert('Jira Add Comment Error', str(e))
pass
Expand Down