예제 #1
0
    def get(self, request: Request, project: Project) -> Response:
        """
        Retrieve List of CODEOWNERS configurations for a project
        ````````````````````````````````````````````

        Return a list of a project's CODEOWNERS configuration.

        :auth: required
        """

        if not self.has_feature(request, project):
            raise PermissionDenied

        expand = request.GET.getlist("expand", [])
        expand.append("errors")

        codeowners = list(ProjectCodeOwners.objects.filter(project=project).order_by("-date_added"))

        return Response(
            serialize(
                codeowners,
                request.user,
                serializer=projectcodeowners_serializers.ProjectCodeOwnersSerializer(expand=expand),
            ),
            status.HTTP_200_OK,
        )
    def put(self, request, project, codeowners):
        """
        Update a CodeOwners
        `````````````

        :pparam string organization_slug: the slug of the organization.
        :pparam string project_slug: the slug of the project to get.
        :pparam string codeowners_id: id of codeowners object
        :param string raw: the raw CODEOWNERS text
        :param string codeMappingId: id of the RepositoryProjectPathConfig object
        :auth: required
        """
        if not self.has_feature(request, project):
            self.track_response_code("update", PermissionDenied.status_code)
            raise PermissionDenied

        serializer = ProjectCodeOwnerSerializer(
            instance=codeowners,
            context={"ownership": self.get_ownership(project), "project": project},
            partial=True,
            data={**request.data},
        )
        if serializer.is_valid():
            updated_codeowners = serializer.save()

            analytics.record(
                "codeowners.updated",
                user_id=request.user.id if request.user and request.user.id else None,
                organization_id=project.organization_id,
                project_id=project.id,
                codeowners_id=updated_codeowners.id,
            )
            self.track_response_code("update", status.HTTP_200_OK)
            return Response(
                serialize(
                    updated_codeowners,
                    request.user,
                    serializer=projectcodeowners_serializers.ProjectCodeOwnersSerializer(
                        expand=["ownershipSyntax", "errors"]
                    ),
                ),
                status=status.HTTP_200_OK,
            )

        self.track_response_code("update", status.HTTP_400_BAD_REQUEST)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)