/
Art86
/
DefectDojo-DevSecOps
Обзор
Документация
Войти
/
Art86
/
DefectDojo-DevSecOps
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
dojo/github.py
159 строк
6 KB
Cody Maffucci
Refactor GitHub integration error handling (#13913)
17 дек 2025, 00:01
17 дек 2025, 00:01
7958337
Код
Авторство
О чём код?
# python import logging import sys from django.template.loader import render_to_string # External libs from github import Auth, Github # Dojo related imports from dojo.models import Engagement, GITHUB_Issue, GITHUB_PKey, Product # Create global logger = logging.getLogger(__name__) def reopen_external_issue_github(find, note, prod, eng): # Ensure the system setting for GitHub integration is enabled from dojo.utils import get_system_setting # noqa: PLC0415 circular import if not get_system_setting("enable_github"): return # Check if we have github info related to the product if not GITHUB_PKey.objects.filter(product=prod).exists(): return # Get the GitHub product configuration github_product = GITHUB_PKey.objects.get(product=prod) if github_product is None: logger.error("Unable to get project key") return # Check if we have github info related to the finding if not GITHUB_Issue.objects.filter(finding=find).exists(): return # Get the GitHub issue related to the finding g_issue = GITHUB_Issue.objects.get(finding=find) if not g_issue: logger.error("Unable to get github issue") return try: g_ctx = Github(auth=Auth.Token(github_product.git_conf.api_key)) repo = g_ctx.get_repo(github_product.git_project) issue = repo.get_issue(int(g_issue.issue_id)) except: e = sys.exc_info()[0] logger.error("cannot update finding in github: " + e) logger.info("Will close github issue " + g_issue.issue_id) issue.edit(state="open") issue.create_comment(note) def close_external_issue_github(find, note, prod, eng): # Ensure the system setting for GitHub integration is enabled from dojo.utils import get_system_setting # noqa: PLC0415 circular import if not get_system_setting("enable_github"): return # Check if we have github info related to the product if not GITHUB_PKey.objects.filter(product=prod).exists(): return # Get the GitHub product configuration github_product = GITHUB_PKey.objects.get(product=prod) if github_product is None: logger.error("Unable to get project key") return # Check if we have github info related to the finding if not GITHUB_Issue.objects.filter(finding=find).exists(): return # Get the GitHub issue related to the finding g_issue = GITHUB_Issue.objects.get(finding=find) if not g_issue: logger.error("Unable to get github issue") return try: g_ctx = Github(auth=Auth.Token(github_product.git_conf.api_key)) repo = g_ctx.get_repo(github_product.git_project) issue = repo.get_issue(int(g_issue.issue_id)) except: e = sys.exc_info()[0] logger.error("cannot update finding in github: " + e) logger.info("Will close github issue " + g_issue.issue_id) issue.edit(state="closed") issue.create_comment(note) def update_external_issue_github(find, prod, eng): # Ensure the system setting for GitHub integration is enabled from dojo.utils import get_system_setting # noqa: PLC0415 circular import if not get_system_setting("enable_github"): return # Check if we have github info related to the product if not GITHUB_PKey.objects.filter(product=prod).exists(): return # Get the GitHub product configuration github_product = GITHUB_PKey.objects.get(product=prod) if github_product is None: logger.error("Unable to get project key") return # Check if we have github info related to the finding if not GITHUB_Issue.objects.filter(finding=find).exists(): return # Get the GitHub issue related to the finding g_issue = GITHUB_Issue.objects.get(finding=find) if not g_issue: logger.error("Unable to get github issue") return try: g_ctx = Github(auth=Auth.Token(github_product.git_conf.api_key)) repo = g_ctx.get_repo(github_product.git_project) issue = repo.get_issue(int(g_issue.issue_id)) issue.edit(title=find.title, body=github_body(find), labels=["defectdojo", "security / " + find.severity]) except: e = sys.exc_info()[0] logger.error("cannot update finding in github: " + e) def add_external_issue_github(find, prod, eng): # Ensure the system setting for GitHub integration is enabled from dojo.utils import get_system_setting # noqa: PLC0415 circular import if not get_system_setting("enable_github"): return # Check if we have github info related to the product if not GITHUB_PKey.objects.filter(product=prod).exists(): return # Get the GitHub product configuration github_product = GITHUB_PKey.objects.get(product=prod) if github_product is None: logger.error("Unable to get project key") return # We push only active and verified issues if "Active" in find.status() and ("Verified" in find.status() and get_system_setting("enforce_verified_status", True)): eng = Engagement.objects.get(test=find.test) prod = Product.objects.get(engagement=eng) github_product_key = GITHUB_PKey.objects.get(product=prod) logger.info("Create issue with github profile: " + str(github_product_key.git_conf) + " on product: " + str(github_product_key)) try: g = Github(auth=Auth.Token(github_product_key.git_conf.api_key)) user = g.get_user() logger.debug("logged in with github user: " + user.login) logger.debug("Look for project: " + github_product_key.git_project) repo = g.get_repo(github_product_key.git_project) logger.debug("Found repo: " + str(repo.url)) issue = repo.create_issue(title=find.title, body=github_body(find), labels=["defectdojo", "security / " + find.severity]) logger.debug("created issue: " + str(issue.html_url)) g_issue = GITHUB_Issue(issue_id=issue.number, issue_url=issue.html_url, finding=find) g_issue.save() except: e = sys.exc_info()[0] logger.error("cannot create finding in github: " + e) def github_body(find): template = "issue-trackers/jira_full/jira-description.tpl" kwargs = {} kwargs["finding"] = find return render_to_string(template, kwargs)