Merge pull request #9201 from amolgautam25/issue_5057

Properly set launched_by for jobs launched by scheduled workflows : Issue 5057

SUMMARY

with respect to the issue 5057, there should be a way by which the UI should know , if a job was launched from a workflow , and that workflow was launched from a schedule.

ISSUE TYPE


Feature Pull Request

COMPONENT NAME


API

AWX VERSION

awx: 17.0.1

ADDITIONAL INFORMATION


This PR tries to solve the issue 5057. 
There is an edge case , where the API response is not very clear. When a JT is invoked from a workflow , and that workflow is invoked from a schedule, the API response does not convey how the job was launched. 

So, I have added the schedule id of the schedule that invoked the workflow and which in turn invokes the JT. The new key in API response is 'launched_by', and it has the schedule id. In all  the 5 previous cases mentioned in the initial issue , the 'launched_by' field is blank ( that is empty string ( "" )).

Reviewed-by: Jeff Bradberry <None>
Reviewed-by: Ryan Petrello <None>
Reviewed-by: Elijah DeLee <kdelee@redhat.com>
Reviewed-by: Amol Gautam <amol_gautam25@yahoo.co.in>
Reviewed-by: Nana  <natr@hey.com>
This commit is contained in:
softwarefactory-project-zuul[bot] 2021-03-24 17:48:39 +00:00 committed by GitHub
commit 23d72a9b6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 0 deletions

View File

@ -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:

View File

@ -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