Skip to content

Commit eb44e06

Browse files
coheigeaMaffooch
authored andcommitted
Add all existing finding notes to the JIRA when created (DefectDojo#6449)
* Add all existing finding notes to the JIRA when created * Add unittests for pushing existing notes to jira Co-authored-by: Cody Maffucci <46459665+Maffooch@users.noreply.github.com>
1 parent 2928c02 commit eb44e06

5 files changed

+2886
-1108
lines changed

dojo/jira_link/helper.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,19 @@ def get_jira_status(finding):
441441
return issue.fields.status
442442

443443

444+
# Used for unit testing so geting all the connections is manadatory
445+
def get_jira_comments(finding):
446+
if finding.has_jira_issue:
447+
j_issue = finding.jira_issue.jira_id
448+
elif finding.finding_group and finding.finding_group.has_jira_issue:
449+
j_issue = finding.finding_group.jira_issue.jira_id
450+
451+
if j_issue:
452+
project = get_jira_project(finding)
453+
issue = jira_get_issue(project, j_issue)
454+
return issue.fields.comment.comments
455+
456+
444457
# Logs the error to the alerts table, which appears in the notification toolbar
445458
def log_jira_generic_alert(title, description):
446459
create_notification(
@@ -731,6 +744,13 @@ def add_jira_issue(obj, *args, **kwargs):
731744
issue = jira.issue(new_issue.id)
732745

733746
logger.info('Created the following jira issue for %d:%s', obj.id, to_str_typed(obj))
747+
748+
# Add any notes that already exist in the finding to the JIRA
749+
for find in findings:
750+
if find.notes.all():
751+
for note in find.notes.all().reverse():
752+
add_comment(obj, note)
753+
734754
return True
735755
except TemplateDoesNotExist as e:
736756
logger.exception(e)

unittests/dojo_test_case.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ def get_jira_issue_updated(self, finding_id):
327327
updated = jira_helper.get_jira_updated(finding)
328328
return updated
329329

330+
def get_jira_comments(self, finding_id):
331+
finding = Finding.objects.get(id=finding_id)
332+
comments = jira_helper.get_jira_comments(finding)
333+
return comments
334+
330335
def get_jira_issue_updated_map(self, test_id):
331336
findings = Test.objects.get(id=test_id).finding_set.all()
332337
updated_map = {}

unittests/test_jira_import_and_pushing_api.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,28 @@ def test_import_with_push_to_jira_add_comment(self):
488488

489489
response = self.post_finding_notes_api(finding_id, 'testing note. creating it and pushing it to JIRA')
490490
self.patch_finding_api(finding_id, {"push_to_jira": True})
491+
# Make sure the number of comments match
492+
self.assertEqual(len(self.get_jira_comments(finding_id)), 1)
493+
# by asserting full cassette is played we know all calls to JIRA have been made as expected
494+
self.assert_cassette_played()
495+
return test_id
496+
497+
def test_import_add_comments_then_push_to_jira(self):
498+
import0 = self.import_scan_with_params(self.zap_sample5_filename, push_to_jira=False)
499+
test_id = import0['test']
500+
501+
findings = self.get_test_findings_api(test_id)
491502

503+
finding_id = findings['results'][0]['id']
504+
505+
response = self.post_finding_notes_api(finding_id, 'testing note. creating it and pushing it to JIRA')
506+
response = self.post_finding_notes_api(finding_id, 'testing second note. creating it and pushing it to JIRA')
507+
self.patch_finding_api(finding_id, {"push_to_jira": True})
508+
509+
self.assert_jira_issue_count_in_test(test_id, 1)
510+
self.assert_jira_group_issue_count_in_test(test_id, 0)
511+
# Make sure the number of comments match
512+
self.assertEqual(len(self.get_jira_comments(finding_id)), 2)
492513
# by asserting full cassette is played we know all calls to JIRA have been made as expected
493514
self.assert_cassette_played()
494515
return test_id

0 commit comments

Comments
 (0)