Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshmi2506 committed Jan 24, 2024
1 parent 0728e2b commit b0faedc
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 9 deletions.
47 changes: 47 additions & 0 deletions docs/api/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,32 @@ paths:
schema:
$ref: '#/components/schemas/ScratchOrg'
description: ''

/api/scratch-orgs/{id}/listmetadata/:
post:
operationId: scratch_orgs_listmetadata
description: Queue a job that retrieves the components for a given non source trackable metadata type.
parameters:
- in: path
name: id
schema:
type: string
format: HashID
description: A unique integer value identifying this scratch org.
required: true
tags:
- scratch-orgs
security:
- tokenAuth: []
- cookieAuth: []
responses:
'202':
content:
application/json:
schema:
$ref: '#/components/schemas/ListMetadataRequest'
description: ''

/api/tasks/:
get:
operationId: tasks_list
Expand Down Expand Up @@ -1671,6 +1697,13 @@ components:
required:
- commit_message
- yaml_path
ListMetadataRequest:
type: object
properties:
desiredType: string
minLength: 1
required:
- desiredType
CommitRequest:
type: object
properties:
Expand Down Expand Up @@ -2510,6 +2543,11 @@ components:
type: string
format: HashID
readOnly: true
metadatatype_changes:
type: object
additionalProperties: {}
readOnly: true
nullable: true
description:
type: string
description_rendered:
Expand Down Expand Up @@ -2553,9 +2591,18 @@ components:
total_unsaved_changes:
type: integer
readOnly: true
total_metadatatype_changes:
type: integer
readOnly: true
has_unsaved_changes:
type: boolean
readOnly: true
has_metadatatype_changes:
type: boolean
readOnly: true
currently_retrieving_nonsource:
type: boolean
readOnly: true
ignored_changes:
type: object
additionalProperties: {}
Expand Down
35 changes: 32 additions & 3 deletions metecho/api/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
ExtractRulesFile,
flatten_declarations,
)
from collections import defaultdict
from cumulusci.tasks.salesforce.nonsourcetracking import ListComponents, ListNonSourceTrackable
from cumulusci.core.config import TaskConfig
from cumulusci.core.runtime import BaseCumulusCI
from cumulusci.salesforce_api.org_schema import Field, Filters, Schema, get_org_schema
from cumulusci.salesforce_api.utils import get_simple_salesforce_connection
Expand Down Expand Up @@ -273,7 +276,7 @@ def create_repository(

else:
repo = org.create_repository(
project.repo_name, description=project.description, private=False
project.repo_name, description=project.description, private= True
)
team.add_repository(repo.full_name, permission="push")
project.repo_id = repo.id
Expand Down Expand Up @@ -602,7 +605,6 @@ def refresh_scratch_org(scratch_org, *, originating_user_id):

refresh_scratch_org_job = job(refresh_scratch_org)


def get_unsaved_changes(scratch_org, *, originating_user_id):
try:
scratch_org.refresh_from_db()
Expand All @@ -621,6 +623,15 @@ def get_unsaved_changes(scratch_org, *, originating_user_id):
repo_root,
)
scratch_org.unsaved_changes = unsaved_changes
with dataset_env(scratch_org) as (project_config, org_config, sf, schema, repo):
components=ListNonSourceTrackable(
org_config=org_config,
project_config=project_config,
task_config=TaskConfig({"options":{}}),
)()
scratch_org.metadatatype_changes= {}
for types in components:
scratch_org.metadatatype_changes[types]=[types+"alpha"]
except Exception as e:
scratch_org.refresh_from_db()
scratch_org.finalize_get_unsaved_changes(
Expand All @@ -633,10 +644,28 @@ def get_unsaved_changes(scratch_org, *, originating_user_id):
scratch_org.finalize_get_unsaved_changes(
originating_user_id=originating_user_id
)
get_unsaved_changes_job = job(get_unsaved_changes)

def get_nonsource_components(scratch_org,*,desired_type,originating_user_id):
try:
scratch_org.refresh_from_db()
with dataset_env(scratch_org) as (project_config, org_config, sf, schema, repo):
components=ListComponents(
org_config=org_config,
project_config=project_config,
task_config=TaskConfig({"options":{"metadata_type":desired_type}}),
)()

get_unsaved_changes_job = job(get_unsaved_changes)
scratch_org.metadatatype_changes[desired_type]=[cmp["MemberName"] for cmp in components]
except Exception as e:
scratch_org.refresh_from_db()

else:
scratch_org.finalize_get_unsaved_changes(
originating_user_id=originating_user_id
)

get_nonsource_components_job = job(get_nonsource_components)

def commit_changes_from_org(
*,
Expand Down
24 changes: 24 additions & 0 deletions metecho/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,12 +1238,16 @@ class ScratchOrg(
unsaved_changes = models.JSONField(
default=dict, encoder=DjangoJSONEncoder, blank=True
)
metadatatype_changes = models.JSONField(
default=dict, encoder=DjangoJSONEncoder, blank=True
)
ignored_changes = models.JSONField(
default=dict, encoder=DjangoJSONEncoder, blank=True
)
latest_revision_numbers = models.JSONField(
default=dict, encoder=DjangoJSONEncoder, blank=True
)
currently_retrieving_nonsource= models.BooleanField(default=False)
currently_refreshing_changes = models.BooleanField(default=False)
currently_retrieving_metadata = models.BooleanField(default=False)
currently_parsing_datasets = models.BooleanField(default=False)
Expand Down Expand Up @@ -1472,6 +1476,13 @@ def queue_get_unsaved_changes(self, *, force_get=False, originating_user_id):

get_unsaved_changes_job.delay(self, originating_user_id=originating_user_id)

def queue_get_nonsource_components(self,*,desired_type,originating_user_id):
from .jobs import get_nonsource_components_job
self.currently_refreshing_changes = True
self.save()
self.notify_changed(originating_user_id=originating_user_id)
get_nonsource_components_job.delay(self,desired_type,originating_user_id=originating_user_id)

def finalize_get_unsaved_changes(self, *, error=None, originating_user_id):
self.currently_refreshing_changes = False
if error is None:
Expand All @@ -1487,6 +1498,19 @@ def finalize_get_unsaved_changes(self, *, error=None, originating_user_id):
originating_user_id=originating_user_id,
)

def finalize_get_nonsource_components(self,*,error=None,originating_user_id):
self.currently_refreshing_changes = False
if error is None:
self.save()
self.notify_changed(originating_user_id=originating_user_id)
else:
self.save()
self.notify_scratch_org_error(
error=error,
type_="SCRATCH_ORG_FETCH_CHANGES_FAILED",
originating_user_id=originating_user_id,
)

def queue_commit_changes(
self,
*,
Expand Down
19 changes: 19 additions & 0 deletions metecho/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,9 @@ class ScratchOrgSerializer(HashIdModelSerializer):
description_rendered = MarkdownField(source="description", read_only=True)
unsaved_changes = serializers.SerializerMethodField()
has_unsaved_changes = serializers.SerializerMethodField()
metadatatype_changes = serializers.SerializerMethodField()
has_metadatatype_changes = serializers.SerializerMethodField()
total_metadatatype_changes = serializers.SerializerMethodField()
total_unsaved_changes = serializers.SerializerMethodField()
ignored_changes = serializers.SerializerMethodField()
has_ignored_changes = serializers.SerializerMethodField()
Expand Down Expand Up @@ -1049,6 +1052,10 @@ class Meta:
"currently_retrieving_omnistudio",
"installed_packages",
"is_omnistudio_installed",
"metadatatype_changes",
"has_metadatatype_changes",
"total_metadatatype_changes",
"currently_retrieving_nonsource"
)
extra_kwargs = {
"last_modified_at": {"read_only": True},
Expand All @@ -1059,6 +1066,7 @@ class Meta:
"last_checked_unsaved_changes_at": {"read_only": True},
"url": {"read_only": True},
"currently_refreshing_changes": {"read_only": True},
"currently_retrieving_nonsource": {"read_only": True},
"currently_retrieving_metadata": {"read_only": True},
"currently_refreshing_org": {"read_only": True},
"currently_reassigning_user": {"read_only": True},
Expand Down Expand Up @@ -1089,9 +1097,18 @@ def _total_X_changes(self, obj, kind) -> int:
def get_unsaved_changes(self, obj) -> dict:
return self._X_changes(obj, "unsaved")

def get_metadatatype_changes(self,obj) -> dict:
return self._X_changes(obj, "metadatatype")

def get_total_metadatatype_changes(self,obj) ->int:
return self._total_X_changes(obj, "metadatatype")

def get_has_unsaved_changes(self, obj) -> bool:
return self._has_X_changes(obj, "unsaved")

def get_has_metadatatype_changes(self,obj) ->bool:
return self._has_X_changes(obj,"metadatatype")

def get_total_unsaved_changes(self, obj) -> int:
return self._total_X_changes(obj, "unsaved")

Expand Down Expand Up @@ -1142,6 +1159,8 @@ class CommitSerializer(serializers.Serializer):
changes = serializers.DictField(child=StringListField())
target_directory = serializers.CharField()

class ListMetadataSerializer(serializers.Serializer):
desiredType = serializers.CharField()

class CommitDatasetSerializer(serializers.Serializer):
commit_message = serializers.CharField()
Expand Down
17 changes: 17 additions & 0 deletions metecho/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
CanReassignSerializer,
CheckRepoNameSerializer,
CommitDatasetSerializer,
ListMetadataSerializer,
CommitOmniStudioSerializer,
CommitSerializer,
CreatePrSerializer,
Expand Down Expand Up @@ -704,6 +705,22 @@ def commit_dataset(self, request, pk=None):
self.get_serializer(scratch_org).data, status=status.HTTP_202_ACCEPTED
)

@extend_schema(request= ListMetadataSerializer, responses={202: ScratchOrgSerializer})
@action(detail=True, methods=["POST"])
def list_metadata(self,request,pk=None):
serializer = ListMetadataSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
scratch_org= self.get_object()
if not request.user == scratch_org.owner:
return Response(
{"error": _("Requesting user did not create Org.")},
status=status.HTTP_403_FORBIDDEN,
)
scratch_org.queue_get_nonsource_components(**serializer.validated_data,originating_user_id=str(request.user.id))
return Response(
self.get_serializer(scratch_org).data, status=status.HTTP_202_ACCEPTED
)

@extend_schema(
request=CommitOmniStudioSerializer, responses={202: ScratchOrgSerializer}
)
Expand Down
Loading

0 comments on commit b0faedc

Please sign in to comment.