From e7e18b854f64ba83b9f68fd1a47731d45cf7fa8c Mon Sep 17 00:00:00 2001 From: fedora Date: Mon, 1 Feb 2021 11:43:50 -0500 Subject: [PATCH] =?UTF-8?q?Added=20=E2=80=98launched=5Fby=E2=80=99=20prope?= =?UTF-8?q?rty=20and=20=E2=80=98ancestor=5Fjob=E2=80=99=20property=20in=20?= =?UTF-8?q?UnifiedJob=20class=20'launched=5Fby=E2=80=99=20property=20retur?= =?UTF-8?q?ns=20summary=20=20{=20id,type,name,url=20}=20of=20object=20that?= =?UTF-8?q?=20launched=20the=20current=20UnifiedJob=20'ancestor=5Fjob?= =?UTF-8?q?=E2=80=99=20property=20returns=20summary=20{=20id,type,name,url?= =?UTF-8?q?=20}=20of=20the=20first=20workflow=20in=20case=20the=20current?= =?UTF-8?q?=20UnifiedJob=20was=20started=20by=20a=20workflow=20or=20a=20wo?= =?UTF-8?q?rkflow=20chain=20Added=20=E2=80=98launched=5Fby=E2=80=99=20fiel?= =?UTF-8?q?d=20and=20=E2=80=98get=5Flaunched=5Fby=E2=80=99=20function=20in?= =?UTF-8?q?=20=E2=80=98UnifiedJobSerializer=E2=80=99=20,=20to=20expose=20t?= =?UTF-8?q?he=20=E2=80=98launched=5Fby=E2=80=99=20field=20in=20GET=20?= =?UTF-8?q?=E2=80=98api/v2/unified=5Fjob/id=E2=80=99=20response=20Added=20?= =?UTF-8?q?=E2=80=98ancestor=5Fjob=E2=80=99=20field=20in=20the=20summary?= =?UTF-8?q?=20field=20of=20UnifiedJob=20in=20the=20GET=20=E2=80=98api/v2/u?= =?UTF-8?q?nified=5Fjob/id=E2=80=99=20response?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- awx/api/serializers.py | 17 +++++++++++++++++ awx/main/models/unified_jobs.py | 26 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 9a6f0a086b..93793a90f2 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -734,6 +734,7 @@ class UnifiedJobSerializer(BaseSerializer): class Meta: model = UnifiedJob + fields = ( '*', 'unified_job_template', @@ -753,7 +754,9 @@ class UnifiedJobSerializer(BaseSerializer): 'controller_node', 'result_traceback', 'event_processing_finished', + 'launched_by', ) + extra_kwargs = { 'unified_job_template': {'source': 'unified_job_template_id', 'label': 'unified job template'}, 'job_env': {'read_only': True, 'label': 'job_env'}, @@ -799,6 +802,16 @@ class UnifiedJobSerializer(BaseSerializer): if val is not None: summary_fields['source_workflow_job'][field] = val + if self.is_detail_view: + ancestor = obj.ancestor_job + if ancestor != obj: + summary_fields['ancestor_job'] = { + 'id': ancestor.id, + 'name': ancestor.name, + 'type': get_type_for_model(ancestor), + 'url': ancestor.get_absolute_url(), + } + return summary_fields def get_sub_serializer(self, obj): @@ -843,6 +856,10 @@ class UnifiedJobSerializer(BaseSerializer): ret['job_explanation'] = _(obj.job_explanation) return ret + def get_launched_by(self, obj): + if obj is not None: + return obj.launched_by + class UnifiedJobListSerializer(UnifiedJobSerializer): class Meta: diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index 176896f19b..76a2895bf8 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -1483,3 +1483,29 @@ class UnifiedJob( else: msg = f"{self._meta.model_name}-{self.id} {state.replace('_', ' ')}" logger_job_lifecycle.debug(msg, extra=extra) + + @property + def launched_by(self): + ancestor_job = self.ancestor_job + + if ancestor_job.launch_type == "dependency": + return {'id': None, 'name': 'Generated by AWX', 'type': 'Dependency', 'url': None} + + attr = { + "manual": "created_by", + "relaunch": "created_by", + "scheduled": "schedule", + "workflow": "workflow", + "webhook": "job_template", + "sync": "project", + "scm": "inventory", + } + + obj = getattr(ancestor_job, attr.get(ancestor_job.launch_type, ''), None) + if obj is not None: + return {'id': obj.id, 'name': getattr(obj, 'name', None) or obj.username, 'type': get_type_for_model(obj), 'url': obj.get_absolute_url()} + return {} + + @property + def ancestor_job(self): + return self.get_workflow_job().ancestor_job if self.spawned_by_workflow else self