Skip to content

Commit 191c237

Browse files
committed
Refactor GitHub integration error handling for improved clarity and robustness
1 parent 88bed96 commit 191c237

1 file changed

Lines changed: 41 additions & 34 deletions

File tree

dojo/github.py

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,29 @@
1515

1616

1717
def reopen_external_issue_github(find, note, prod, eng):
18-
18+
# Ensure the system setting for GitHub integration is enabled
1919
from dojo.utils import get_system_setting # noqa: PLC0415 circular import
2020
if not get_system_setting("enable_github"):
2121
return
22-
2322
# Check if we have github info related to the product
24-
if GITHUB_PKey.objects.filter(product=prod).count() == 0:
23+
if not GITHUB_PKey.objects.filter(product=prod).exists():
2524
return
26-
25+
# Get the GitHub product configuration
2726
github_product = GITHUB_PKey.objects.get(product=prod)
2827
if github_product is None:
2928
logger.error("Unable to get project key")
3029
return
31-
32-
github_conf = github_product.git_conf
30+
# Check if we have github info related to the finding
31+
if not GITHUB_PKey.objects.filter(finding=find).exists():
32+
return
33+
# Get the GitHub issue related to the finding
3334
g_issue = GITHUB_Issue.objects.get(finding=find)
35+
if not g_issue:
36+
logger.error("Unable to get github issue")
37+
return
3438

3539
try:
36-
g_ctx = Github(auth=Auth.Token(github_conf.api_key))
40+
g_ctx = Github(auth=Auth.Token(github_product.git_conf.api_key))
3741
repo = g_ctx.get_repo(github_product.git_project)
3842
issue = repo.get_issue(int(g_issue.issue_id))
3943
except:
@@ -46,25 +50,29 @@ def reopen_external_issue_github(find, note, prod, eng):
4650

4751

4852
def close_external_issue_github(find, note, prod, eng):
49-
53+
# Ensure the system setting for GitHub integration is enabled
5054
from dojo.utils import get_system_setting # noqa: PLC0415 circular import
5155
if not get_system_setting("enable_github"):
5256
return
53-
5457
# Check if we have github info related to the product
55-
if GITHUB_PKey.objects.filter(product=prod).count() == 0:
58+
if not GITHUB_PKey.objects.filter(product=prod).exists():
5659
return
57-
60+
# Get the GitHub product configuration
5861
github_product = GITHUB_PKey.objects.get(product=prod)
5962
if github_product is None:
6063
logger.error("Unable to get project key")
6164
return
62-
63-
github_conf = github_product.git_conf
65+
# Check if we have github info related to the finding
66+
if not GITHUB_PKey.objects.filter(finding=find).exists():
67+
return
68+
# Get the GitHub issue related to the finding
6469
g_issue = GITHUB_Issue.objects.get(finding=find)
70+
if not g_issue:
71+
logger.error("Unable to get github issue")
72+
return
6573

6674
try:
67-
g_ctx = Github(auth=Auth.Token(github_conf.api_key))
75+
g_ctx = Github(auth=Auth.Token(github_product.git_conf.api_key))
6876
repo = g_ctx.get_repo(github_product.git_project)
6977
issue = repo.get_issue(int(g_issue.issue_id))
7078
except:
@@ -77,25 +85,29 @@ def close_external_issue_github(find, note, prod, eng):
7785

7886

7987
def update_external_issue_github(find, prod, eng):
80-
88+
# Ensure the system setting for GitHub integration is enabled
8189
from dojo.utils import get_system_setting # noqa: PLC0415 circular import
8290
if not get_system_setting("enable_github"):
8391
return
84-
8592
# Check if we have github info related to the product
86-
if GITHUB_PKey.objects.filter(product=prod).count() == 0:
93+
if not GITHUB_PKey.objects.filter(product=prod).exists():
8794
return
88-
95+
# Get the GitHub product configuration
8996
github_product = GITHUB_PKey.objects.get(product=prod)
9097
if github_product is None:
9198
logger.error("Unable to get project key")
9299
return
93-
94-
github_conf = github_product.git_conf
100+
# Check if we have github info related to the finding
101+
if not GITHUB_PKey.objects.filter(finding=find).exists():
102+
return
103+
# Get the GitHub issue related to the finding
95104
g_issue = GITHUB_Issue.objects.get(finding=find)
105+
if not g_issue:
106+
logger.error("Unable to get github issue")
107+
return
96108

97109
try:
98-
g_ctx = Github(auth=Auth.Token(github_conf.api_key))
110+
g_ctx = Github(auth=Auth.Token(github_product.git_conf.api_key))
99111
repo = g_ctx.get_repo(github_product.git_project)
100112
issue = repo.get_issue(int(g_issue.issue_id))
101113
issue.edit(title=find.title, body=github_body(find), labels=["defectdojo", "security / " + find.severity])
@@ -105,32 +117,27 @@ def update_external_issue_github(find, prod, eng):
105117

106118

107119
def add_external_issue_github(find, prod, eng):
108-
120+
# Ensure the system setting for GitHub integration is enabled
109121
from dojo.utils import get_system_setting # noqa: PLC0415 circular import
110122
if not get_system_setting("enable_github"):
111123
return
112-
113124
# Check if we have github info related to the product
114-
if GITHUB_PKey.objects.filter(product=prod).count() == 0:
115-
logger.debug("cannot find github conf for this product")
125+
if not GITHUB_PKey.objects.filter(product=prod).exists():
116126
return
117-
118-
github_pkey = GITHUB_PKey.objects.get(product=prod)
119-
if github_pkey is None:
120-
logger.error("Unable to get product conf")
127+
# Get the GitHub product configuration
128+
github_product = GITHUB_PKey.objects.get(product=prod)
129+
if github_product is None:
130+
logger.error("Unable to get project key")
121131
return
122-
123-
github_conf = github_pkey.git_conf
124-
125132
# We push only active and verified issues
126133
if "Active" in find.status() and ("Verified" in find.status() and get_system_setting("enforce_verified_status", True)):
127134
eng = Engagement.objects.get(test=find.test)
128135
prod = Product.objects.get(engagement=eng)
129136
github_product_key = GITHUB_PKey.objects.get(product=prod)
130-
logger.info("Create issue with github profile: " + str(github_conf) + " on product: " + str(github_product_key))
137+
logger.info("Create issue with github profile: " + str(github_product_key.git_conf) + " on product: " + str(github_product_key))
131138

132139
try:
133-
g = Github(auth=Auth.Token(github_conf.api_key))
140+
g = Github(auth=Auth.Token(github_product_key.git_conf.api_key))
134141
user = g.get_user()
135142
logger.debug("logged in with github user: " + user.login)
136143
logger.debug("Look for project: " + github_product_key.git_project)

0 commit comments

Comments
 (0)