Skip to content
9 changes: 9 additions & 0 deletions dojo/api_v2/risk_acceptance/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import serializers
from dojo.api_v2.serializers import NoteSerializer
from dojo.models import Risk_Acceptance

class RiskAcceptanceToNotesSerializer(serializers.Serializer):
risk_acceptance = serializers.PrimaryKeyRelatedField(
queryset=Risk_Acceptance.objects.all(), many=False, allow_null=True,
)
notes = NoteSerializer(many=True)
53 changes: 51 additions & 2 deletions dojo/api_v2/risk_acceptance/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
get_user_with_permission_key)
from dojo.risk_acceptance.notification import Notification as NotificationRiskAcceptance
from dojo.risk_acceptance.serializers import RiskAcceptanceEmailSerializer
from dojo.api_v2.risk_acceptance.serializers import RiskAcceptanceToNotesSerializer
from dojo.api_v2.views import PrefetchDojoModelViewSet
from dojo.models import Risk_Acceptance
from dojo.models import Risk_Acceptance, Notes
from dojo.risk_acceptance.queries import get_authorized_risk_acceptances
from dojo.risk_acceptance.helper import remove_finding_from_risk_acceptance
from dojo.filters import ApiRiskAcceptanceFilter
Expand Down Expand Up @@ -168,4 +169,52 @@ def accept_bulk(self, request, pk=None):
product=product.name,
product_type=product_type.name
)
raise ApiError.internal_server_error(detail=str(e))
raise ApiError.internal_server_error(detail=str(e))

@extend_schema(
methods=["POST"],
request=serializers.AddNewNoteOptionSerializer,
responses={status.HTTP_201_CREATED: serializers.NoteSerializer},
)
@action(detail=True, methods=["get", "post"])
def notes(self, request, pk=None):
risk_acceptance = self.get_object()
if request.method == "POST":
new_note = serializers.AddNewNoteOptionSerializer(
data=request.data,
)
if new_note.is_valid():
entry = new_note.validated_data["entry"]
private = new_note.validated_data.get("private", False)
note_type = new_note.validated_data.get("note_type", None)
else:
return Response(
new_note.errors, status=status.HTTP_400_BAD_REQUEST,
)

notes = risk_acceptance.notes.filter(note_type=note_type).first()
if notes and note_type and note_type.is_single:
return Response("Only one instance of this note_type allowed on an engagement.", status=status.HTTP_400_BAD_REQUEST)

author = request.user
notes = Notes(
entry=entry,
author=author,
private=private,
note_type=note_type,
)
notes.save()
risk_acceptance.notes.add(notes)

serialized_note = serializers.NoteSerializer(
{"author": author, "entry": entry, "private": private},
)
return Response(
serialized_note.data, status=status.HTTP_201_CREATED,
)
notes = risk_acceptance.notes.all()

serialized_notes = RiskAcceptanceToNotesSerializer(
{"risk_acceptance": risk_acceptance, "notes": notes},
)
return Response(serialized_notes.data, status=status.HTTP_200_OK)
2 changes: 0 additions & 2 deletions dojo/api_v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3549,8 +3549,6 @@ class TransferFindingToNotesSerializer(serializers.Serializer):
)
notes = NoteSerializer(many=True)



class TransferFindingFindingsUpdateSerializer(serializers.Serializer):
engagement_id = serializers.IntegerField(required=False)
findings = serializers.DictField(child=TransferFindingFindingsDetailSerializer())
Expand Down
278 changes: 277 additions & 1 deletion dojo/azure_devops_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@

import logging
from datetime import date, datetime
from dojo.utils import get_product, extract_field_from_text_regex
from typing import Optional

from urllib.parse import urlparse
from dojo.celery import app
from azure.devops.connection import Connection
from azure.devops.v7_1.work.models import TeamContext
from azure.devops.v7_0.work_item_tracking.models import JsonPatchOperation, Wiql
from django.conf import settings
from msrest.authentication import BasicAuthentication

from dojo.models import GeneralSettings, Dojo_User, Finding

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -123,3 +129,273 @@ def _get_team_iterations(self) -> list:
logger.debug(f"Found {len(iterations)} iterations in Azure DevOps")
return iterations

class AzureDevOpsWorkItemHelper:

@property
def _organization_url(self):
organization_url = GeneralSettings.get_value(
"AZURE_DEVOPS_ORGANIZATION_URL",
"https://test.azure.com/GrupoTest",
)
return organization_url

@property
def _project(self):
project = GeneralSettings.get_value(
"AZURE_DEVOPS_PROJECT",
"project name",
)
return project

@property
def _team_name(self):
team_name = GeneralSettings.get_value(
"AZURE_DEVOPS_TEAM_NAME",
"team name",
)
return team_name

@property
def _impact_team(self):
impact_team = GeneralSettings.get_value(
"AZURE_DEVOPS_IMPACT_TEAM",
"impact team name",
)
return impact_team

@property
def _work_item_type(self):
work_item_type = GeneralSettings.get_value(
"AZURE_DEVOPS_WORK_ITEM_TYPE",
"item type",
)
return work_item_type


def _get_connection(self):
credentials = BasicAuthentication("", settings.AZURE_DEVOPS_TOKEN)
return Connection(base_url=self._organization_url.rstrip("/"), creds=credentials)

def _get_profile_connection(self):
parsed = urlparse(self._organization_url.rstrip("/"))
org = parsed.path.strip("/") if parsed.netloc.endswith("dev.azure.com") else parsed.netloc
if parsed.netloc.endswith("dev.azure.com"):
profile_base = f"https://vssps.dev.azure.com/{org}/"
else:
profile_base = self._organization_url.rstrip("/")
return Connection(
base_url=profile_base,
creds=BasicAuthentication("", settings.AZURE_DEVOPS_TOKEN),
)

def _get_allowed_values_for_field(wit_client, field_name, project, work_item_type):
field_name = (field_name or "").strip()
if not field_name:
return []

try:
fields = wit_client.get_work_item_type_fields(project=project, type=work_item_type)
except Exception:
logger.warning("Unable to fetch field definitions for %s/%s", project, work_item_type)
return []

for field in fields or []:
reference_name = getattr(field, "reference_name", None) or getattr(field, "referenceName", None)
friendly_name = getattr(field, "name", None)
if field_name in {reference_name, friendly_name} or (
field_name == "Custom.EquipoImpactado"
and (friendly_name or "").lower() == "equipo impactado"
):
allowed_values = getattr(field, "allowed_values", None) or []
if not allowed_values:
return []
values = []
for item in allowed_values:
if isinstance(item, str):
values.append(item)
elif isinstance(item, dict):
value = item.get("value") or item.get("name") or item.get("id")
if value is not None:
values.append(str(value))
else:
value = str(item)
if value:
values.append(value)
return values
return []

def _get_description_DOR(self, user: Dojo_User, finding: int, description: str):
"""Get the description for Definition of Ready (DOR)."""
findings = Finding.objects.filter(id__in=finding)
user_name = f"{user.username} <{user.email}>"
product = get_product(findings[0])
description ="Findings: " + ",\n".join([str(f.id) for f in findings])
return (
f"- User requesting the advice: {user_name} \n" \
f"- EVC: {product.prod_type.name} \n" \
f"- CELULA: {extract_field_from_text_regex(product.description, 'AREA RESPONSABLE TI')} \n" \
f"Problem description: {description}"
)


def get_or_create_work_item(
self,
title,
description,
user,
evidence_and_image,
finding,

):
target_type = self._work_item_type
evidence_and_image = evidence_and_image if evidence_and_image else "Not file input"

connection = self._get_connection()
wit_client = connection.clients.get_work_item_tracking_client()
work_client = connection.clients.get_work_client()

try:
description = self._get_description_DOR(user, finding, description)
profile_connection = self._get_profile_connection()
profile_client = profile_connection.clients.get_profile_client()
me = profile_client.get_profile("me")
team_context = TeamContext(project=self._project, team=self._team_name)
team_field_values = work_client.get_team_field_values(team_context)
area_path = getattr(team_field_values, "default_value", None)

if not area_path:
logger.warning("No default area path found for team: %s", self._team_name)
return None
iterations = work_client.get_team_iterations(
team_context=team_context,
timeframe="current",
)
if not iterations:
logger.warning("No current iteration found")
return None

current_iteration = iterations[0]
iteration_path = current_iteration.path
logger.info("Current iteration: %s", iteration_path)

wiql_query = (
"SELECT [System.Id] "
"FROM WorkItems "
"WHERE [System.TeamProject] = @project "
f"AND [System.Title] = '{title.replace("'", "''")}' "
"AND [System.IterationPath] = @CurrentIteration"
)
query_result = wit_client.query_by_wiql(
wiql=Wiql(query=wiql_query),
team_context=team_context,
)

if query_result.work_items:
work_item_id = query_result.work_items[0].id
existing_wi = wit_client.get_work_item(id=work_item_id, project=self._project)

if existing_wi.fields.get("System.State") == "Closed":
logger.info("Reactivating existing work item: %s", work_item_id)
patch_doc = [
JsonPatchOperation(
op="replace",
path="/fields/System.State",
value="Active",
)
]
wit_client.update_work_item(
document=patch_doc,
id=work_item_id,
project=self._project,
)
else:
logger.info("Using existing active work item: %s", work_item_id)
return str(work_item_id)

logger.info("Creating new %s for support case: %s", target_type, title)
patch_doc = [
JsonPatchOperation(
op="add",
path="/fields/System.Title",
value=title,
),
JsonPatchOperation(
op="add",
path="/fields/System.Description",
value=description,
),
JsonPatchOperation(
op="add",
path="/fields/System.AreaPath",
value=area_path,
),
JsonPatchOperation(
op="add",
path="/fields/Custom.EquipoImpactado",
value=self._impact_team,
),
JsonPatchOperation(
op="add",
path="/fields/Custom.Analistquereporta",
value="Asesoría técnica en seguridad",
),
JsonPatchOperation(
op="add",
path="/fields/Custom.e13a15e9-0c55-4fb1-b218-f76f047d4e11",
value=evidence_and_image,
),
JsonPatchOperation(
op="add",
path="/fields/System.CreatedBy",
value=f"{user.username} + <{user.email}>",
),
JsonPatchOperation(
op="add",
path="/fields/System.IterationPath",
value=iteration_path,
),
]
new_wi = wit_client.create_work_item(
document=patch_doc,
project=self._project,
type=target_type,
)
logger.info("Created new %s work item: %s", target_type, new_wi.id)
return str(new_wi.id)

except Exception as exc:
logger.error("Error getting or creating Azure DevOps support case %s", exc)
return None


@app.task
def create_azure_work_item_task(
title,
description,
user,
evidence_and_image,
finding_ids
):
try:
user_obj = Dojo_User.objects.get(username=user)
if not finding_ids:
raise ValueError("No finding IDs provided")

work_item_id = AzureDevOpsWorkItemHelper().get_or_create_work_item(
title,
description,
user=user_obj,
evidence_and_image=evidence_and_image,
finding=finding_ids,
)
if finding_ids and work_item_id:
findings = Finding.objects.filter(id__in=finding_ids)
for finding in findings:
finding.add_note(
note_text=f"A work item was automatically created :{work_item_id}",
author=user_obj
)
except Exception as exc:
import traceback
print(traceback.format_exc())
logger.error("Error creating Azure DevOps work item: %s", exc)
Loading
Loading