Skip to content

Commit 3187525

Browse files
Refactor link creation in jobUtils (#2619)
1 parent 79e214d commit 3187525

File tree

8 files changed

+202
-115
lines changed

8 files changed

+202
-115
lines changed

frontend/awx/common/JobColumns.tsx

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { useOptions } from '../../common/crud/useOptions';
1818
import { ActionsResponse, OptionsResponse } from '../interfaces/OptionsResponse';
1919
import { UnifiedJob } from '../interfaces/UnifiedJob';
2020
import { AwxRoute } from '../main/AwxRoutes';
21-
import { getLaunchedByDetails, getScheduleUrl, isJobRunning } from '../views/jobs/jobUtils';
21+
import { useGetLaunchedByDetails, useGetScheduleUrl, isJobRunning } from '../views/jobs/jobUtils';
2222
import { awxAPI } from './api/awx-utils';
2323

2424
export function useJobIdColumn<T extends UnifiedJob>() {
@@ -208,15 +208,18 @@ export function useJobScheduleColumn<T extends UnifiedJob>(
208208
defaultSort?: 'asc' | 'desc'
209209
) {
210210
const { t } = useTranslation();
211-
211+
const getScheduleUrl = useGetScheduleUrl();
212212
const column = useMemo<ITableColumn<T>>(
213213
() => ({
214214
header: t('Schedule'),
215-
cell: (job: UnifiedJob) => (
216-
<Link to={job.summary_fields?.schedule ? getScheduleUrl(job) ?? '' : ''}>
217-
{job.summary_fields?.schedule?.name}
218-
</Link>
219-
),
215+
cell: (job: UnifiedJob) => {
216+
const scheduleUrl = getScheduleUrl(job);
217+
return (
218+
<Link to={job.summary_fields?.schedule && scheduleUrl ? scheduleUrl : ''}>
219+
{job.summary_fields?.schedule?.name}
220+
</Link>
221+
);
222+
},
220223
value: (job: UnifiedJob) => job.summary_fields?.schedule?.name,
221224
table: tableOption ?? ColumnTableOption.expanded,
222225
card: cardOption ?? ColumnCardOption.hidden,
@@ -225,7 +228,16 @@ export function useJobScheduleColumn<T extends UnifiedJob>(
225228
modal: modalOption ?? ColumnModalOption.hidden,
226229
dashboard: dashboardOption ?? ColumnDashboardOption.hidden,
227230
}),
228-
[cardOption, dashboardOption, defaultSort, listOption, modalOption, t, tableOption]
231+
[
232+
cardOption,
233+
dashboardOption,
234+
defaultSort,
235+
listOption,
236+
modalOption,
237+
t,
238+
tableOption,
239+
getScheduleUrl,
240+
]
229241
);
230242

231243
return column;
@@ -333,6 +345,7 @@ export function useJobLaunchedByColumn<T extends UnifiedJob>(
333345
defaultSort?: 'asc' | 'desc'
334346
) {
335347
const { t } = useTranslation();
348+
const getLaunchedByDetails = useGetLaunchedByDetails();
336349

337350
const column = useMemo<ITableColumn<T>>(
338351
() => ({
@@ -352,7 +365,16 @@ export function useJobLaunchedByColumn<T extends UnifiedJob>(
352365
modal: modalOption ?? ColumnModalOption.hidden,
353366
dashboard: dashboardOption ?? ColumnDashboardOption.hidden,
354367
}),
355-
[cardOption, dashboardOption, defaultSort, listOption, modalOption, t, tableOption]
368+
[
369+
cardOption,
370+
dashboardOption,
371+
defaultSort,
372+
listOption,
373+
modalOption,
374+
t,
375+
tableOption,
376+
getLaunchedByDetails,
377+
]
356378
);
357379

358380
return column;
@@ -367,6 +389,7 @@ export function useJobScehduleColumn<T extends UnifiedJob>(
367389
defaultSort?: 'asc' | 'desc'
368390
) {
369391
const { t } = useTranslation();
392+
const getScheduleUrl = useGetScheduleUrl();
370393

371394
const column = useMemo<ITableColumn<T>>(
372395
() => ({
@@ -389,7 +412,16 @@ export function useJobScehduleColumn<T extends UnifiedJob>(
389412
modal: modalOption ?? ColumnModalOption.hidden,
390413
dashboard: dashboardOption ?? ColumnDashboardOption.hidden,
391414
}),
392-
[cardOption, dashboardOption, defaultSort, listOption, modalOption, t, tableOption]
415+
[
416+
cardOption,
417+
dashboardOption,
418+
defaultSort,
419+
listOption,
420+
modalOption,
421+
t,
422+
tableOption,
423+
getScheduleUrl,
424+
]
393425
);
394426

395427
return column;

frontend/awx/interfaces/UnifiedJob.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ export interface UnifiedJob
9393
schedule?: string;
9494
system_job_template?: string;
9595
};
96+
launch_type?:
97+
| 'manual'
98+
| 'relaunch'
99+
| 'callback'
100+
| 'scheduled'
101+
| 'dependency'
102+
| 'workflow'
103+
| 'webhook'
104+
| 'sync'
105+
| 'scm'
106+
| undefined;
96107
summary_fields: JobSummaryFields;
97108
launched_by: {
98109
id: number;

frontend/awx/views/jobs/JobDetails.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { AwxRoute } from '../../main/AwxRoutes';
1616
import { LastModifiedPageDetail } from '../../../common/LastModifiedPageDetail';
1717
import { Job } from '../../interfaces/Job';
1818
import { useVerbosityString } from '../../common/useVerbosityString';
19+
import { UnifiedJob } from '../../interfaces/UnifiedJob';
1920

2021
export function JobDetails() {
2122
const { t } = useTranslation();
@@ -29,7 +30,7 @@ export function JobDetails() {
2930

3031
return (
3132
<PageDetails>
32-
<PageDetailsFromColumns columns={columns} item={job} />
33+
<PageDetailsFromColumns columns={columns} item={job as UnifiedJob} />
3334
<PageDetail isEmpty={!job.playbook} label={t('Playbook')}>
3435
{job.playbook}
3536
</PageDetail>

frontend/awx/views/jobs/JobExpanded.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import { awxAPI } from '../../common/api/awx-utils';
1010
import { ActionsResponse, OptionsResponse } from '../../interfaces/OptionsResponse';
1111
import { UnifiedJob } from '../../interfaces/UnifiedJob';
1212
import { AwxRoute } from '../../main/AwxRoutes';
13-
import { getLaunchedByDetails, getScheduleUrl, isJobRunning } from './jobUtils';
13+
import { useGetLaunchedByDetails, useGetScheduleUrl, isJobRunning } from './jobUtils';
1414

1515
export function JobExpanded(job: UnifiedJob) {
16-
const { value: launchedByValue, link: launchedByLink } = useMemo(
17-
() => getLaunchedByDetails(job) ?? {},
18-
[job]
19-
);
16+
const getLaunchedByDetails = useGetLaunchedByDetails();
17+
const { value: launchedByValue, link: launchedByLink } = useMemo(() => {
18+
const launchedByDetails = getLaunchedByDetails(job);
19+
return launchedByDetails ? launchedByDetails : {};
20+
}, [job, getLaunchedByDetails]);
2021
const { t } = useTranslation();
2122
const getPageUrl = useGetPageUrl();
2223
const { data } = useOptions<OptionsResponse<ActionsResponse>>(awxAPI`/inventory_sources/`);
@@ -31,10 +32,11 @@ export function JobExpanded(job: UnifiedJob) {
3132
: [],
3233
[data]
3334
);
34-
const scheduleUrl = useMemo(
35-
() => (job.summary_fields?.schedule ? getScheduleUrl(job) ?? '' : ''),
36-
[job]
37-
);
35+
const getScheduleUrl = useGetScheduleUrl();
36+
const scheduleUrl = useMemo(() => {
37+
const scheduleUrl = getScheduleUrl(job);
38+
return job.summary_fields?.schedule ? (scheduleUrl ? scheduleUrl : '') : '';
39+
}, [job, getScheduleUrl]);
3840
const inventoryUrlPaths: { [key: string]: string } = {
3941
'': 'inventory',
4042
smart: 'smart_inventory',

frontend/awx/views/jobs/hooks/useJobsColumns.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ import { awxAPI } from '../../../common/api/awx-utils';
1818
import { ActionsResponse, OptionsResponse } from '../../../interfaces/OptionsResponse';
1919
import { UnifiedJob } from '../../../interfaces/UnifiedJob';
2020
import { AwxRoute } from '../../../main/AwxRoutes';
21-
import { getLaunchedByDetails, getScheduleUrl } from '../jobUtils';
21+
import { useGetLaunchedByDetails, useGetScheduleUrl } from '../jobUtils';
2222
import { useGetJobOutputUrl } from '../useGetJobOutputUrl';
2323

2424
export function useJobsColumns(options?: { disableSort?: boolean; disableLinks?: boolean }) {
2525
const { t } = useTranslation();
2626
const getPageUrl = useGetPageUrl();
27+
const getScheduleUrl = useGetScheduleUrl();
2728

2829
const { data } = useOptions<OptionsResponse<ActionsResponse>>(awxAPI`/inventory_sources/`);
2930
const inventorySourceChoices = useMemo(
@@ -39,6 +40,7 @@ export function useJobsColumns(options?: { disableSort?: boolean; disableLinks?:
3940
);
4041

4142
const getJobOutputUrl = useGetJobOutputUrl();
43+
const getLaunchedByDetails = useGetLaunchedByDetails();
4244

4345
const tableColumns = useMemo<ITableColumn<UnifiedJob>[]>(
4446
() => [
@@ -147,11 +149,13 @@ export function useJobsColumns(options?: { disableSort?: boolean; disableLinks?:
147149
},
148150
{
149151
header: t('Schedule'),
150-
cell: (job: UnifiedJob) => (
151-
<Link to={job.summary_fields?.schedule ? getScheduleUrl(job) ?? '' : ''}>
152-
{job.summary_fields?.schedule?.name}
153-
</Link>
154-
),
152+
cell: (job: UnifiedJob) => {
153+
return (
154+
<Link to={job.summary_fields?.schedule ? getScheduleUrl(job) ?? '' : ''}>
155+
{job.summary_fields?.schedule?.name}
156+
</Link>
157+
);
158+
},
155159
value: (job: UnifiedJob) => job.summary_fields?.schedule?.name,
156160
table: ColumnTableOption.expanded,
157161
card: 'hidden',
@@ -380,7 +384,15 @@ export function useJobsColumns(options?: { disableSort?: boolean; disableLinks?:
380384
dashboard: 'hidden',
381385
},
382386
],
383-
[getJobOutputUrl, getPageUrl, inventorySourceChoices, options?.disableLinks, t]
387+
[
388+
getJobOutputUrl,
389+
getPageUrl,
390+
inventorySourceChoices,
391+
options?.disableLinks,
392+
t,
393+
getLaunchedByDetails,
394+
getScheduleUrl,
395+
]
384396
);
385397
return tableColumns;
386398
}

frontend/awx/views/jobs/hooks/useRelaunchJob.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
WorkflowJobRelaunch,
1313
} from '../../../interfaces/RelaunchConfiguration';
1414
import { UnifiedJob } from '../../../interfaces/UnifiedJob';
15-
import { getRelaunchEndpoint } from '../jobUtils';
15+
import { relaunchEndpoint } from '../jobUtils';
1616
import { useGetJobOutputUrl } from '../useGetJobOutputUrl';
1717
import { AwxRoute } from '../../../main/AwxRoutes';
1818

@@ -25,33 +25,33 @@ export function useRelaunchJob(jobRelaunchParams?: JobRelaunch) {
2525
const pageNavigate = usePageNavigate();
2626

2727
return async (job: UnifiedJob) => {
28-
const relaunchEndpoint = getRelaunchEndpoint(job);
29-
3028
if (!relaunchEndpoint) {
3129
return Promise.reject(new Error('Unable to retrieve launch configuration'));
3230
}
3331
try {
3432
let relaunchConfig;
3533
switch (job.type) {
3634
case 'ad_hoc_command': {
37-
relaunchConfig =
38-
await requestGet<AwxItemsResponse<AdHocCommandRelaunch>>(relaunchEndpoint);
35+
relaunchConfig = await requestGet<AwxItemsResponse<AdHocCommandRelaunch>>(
36+
relaunchEndpoint(job)
37+
);
3938
break;
4039
}
4140
case 'workflow_job': {
42-
relaunchConfig =
43-
await requestGet<AwxItemsResponse<WorkflowJobRelaunch>>(relaunchEndpoint);
41+
relaunchConfig = await requestGet<AwxItemsResponse<WorkflowJobRelaunch>>(
42+
relaunchEndpoint(job)
43+
);
4444
break;
4545
}
4646
case 'job': {
47-
relaunchConfig = await requestGet<JobRelaunch>(relaunchEndpoint);
47+
relaunchConfig = await requestGet<JobRelaunch>(relaunchEndpoint(job));
4848
break;
4949
}
5050
case 'inventory_update':
51-
relaunchConfig = await requestGet<InventorySourceUpdate>(relaunchEndpoint);
51+
relaunchConfig = await requestGet<InventorySourceUpdate>(relaunchEndpoint(job));
5252
break;
5353
case 'project_update':
54-
relaunchConfig = await requestGet<ProjectUpdateView>(relaunchEndpoint);
54+
relaunchConfig = await requestGet<ProjectUpdateView>(relaunchEndpoint(job));
5555
break;
5656
}
5757

@@ -69,24 +69,24 @@ export function useRelaunchJob(jobRelaunchParams?: JobRelaunch) {
6969
let relaunchJob;
7070
switch (job.type) {
7171
case 'ad_hoc_command': {
72-
relaunchJob = await postRequest(relaunchEndpoint, {} as AdHocCommandRelaunch);
72+
relaunchJob = await postRequest(relaunchEndpoint(job), {} as AdHocCommandRelaunch);
7373
break;
7474
}
7575
case 'workflow_job': {
76-
relaunchJob = await postRequest(relaunchEndpoint, {} as WorkflowJobRelaunch);
76+
relaunchJob = await postRequest(relaunchEndpoint(job), {} as WorkflowJobRelaunch);
7777
break;
7878
}
7979
case 'job': {
80-
relaunchJob = await postRequest(relaunchEndpoint, {
80+
relaunchJob = await postRequest(relaunchEndpoint(job), {
8181
...jobRelaunchParams,
8282
} as JobRelaunch);
8383
break;
8484
}
8585
case 'inventory_update':
86-
relaunchJob = await postRequest(relaunchEndpoint, {} as InventorySourceUpdate);
86+
relaunchJob = await postRequest(relaunchEndpoint(job), {} as InventorySourceUpdate);
8787
break;
8888
case 'project_update':
89-
relaunchJob = await postRequest(relaunchEndpoint, {} as ProjectUpdateView);
89+
relaunchJob = await postRequest(relaunchEndpoint(job), {} as ProjectUpdateView);
9090
break;
9191
}
9292
navigate(getJobOutputUrl(relaunchJob as UnifiedJob));

frontend/awx/views/jobs/jobUtils.cy.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AwxItemsResponse } from '../../common/AwxItemsResponse';
22
import { UnifiedJob } from '../../interfaces/UnifiedJob';
3-
import { getJobsAPIUrl, getRelaunchEndpoint } from './jobUtils';
3+
import { getJobsAPIUrl, relaunchEndpoint } from './jobUtils';
44

55
describe('jobUtils', () => {
66
it('Returns correct endpoint based on job type', () => {
@@ -10,7 +10,7 @@ describe('jobUtils', () => {
1010

1111
it('Returns correct relaunch endpoint based on job type', () => {
1212
cy.fixture('jobs.json').then((response: AwxItemsResponse<UnifiedJob>) => {
13-
const endpoint = getRelaunchEndpoint(response.results[0]);
13+
const endpoint = relaunchEndpoint(response.results[0]);
1414
expect(endpoint).to.equal('/api/v2/workflow_jobs/491/relaunch/');
1515
});
1616
});

0 commit comments

Comments
 (0)