Skip to content

Commit

Permalink
fix webhook in workflow job templates
Browse files Browse the repository at this point in the history
Issue: AAP-23770
  • Loading branch information
jerabekjiri committed May 9, 2024
1 parent f58f385 commit aa279f0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion frontend/awx/resources/templates/JobTemplateInputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export function JobTemplateInputs(props: { jobtemplate?: JobTemplateForm }) {
/>
</FormSection>
) : null}
{isWebhookEnabled ? <WebhookSubForm /> : null}
{isWebhookEnabled ? <WebhookSubForm templateType="job_templates" /> : null}
</>
);
}
39 changes: 30 additions & 9 deletions frontend/awx/resources/templates/WorkflowJobTemplateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,18 @@ export function EditWorkflowJobTemplate() {
const id = Number(params.id);
const {
data: workflowJobTemplate,
error,
refresh,
isLoading,
error: wfjtError,
refresh: wfjtRefresh,
isLoading: isWfjtLoading,
} = useGet<WorkflowJobTemplate>(awxAPI`/workflow_job_templates/${id.toString()}/`);
const {
data: whkData,
isLoading: isWhkDataLoading,
error: whkDataError,
refresh: whkDataRefresh,
} = useGet<{ webhook_key: string }>(
awxAPI`/workflow_job_templates/${id.toString()}/webhook_key/`
);

const onSubmit: PageFormSubmitHandler<WorkflowJobTemplateForm> = async (
values: WorkflowJobTemplateForm
Expand Down Expand Up @@ -86,17 +94,28 @@ export function EditWorkflowJobTemplate() {
scm_branch: workflowJobTemplate.scm_branch || '',
skip_tags: parseStringToTagArray(workflowJobTemplate.job_tags || ''),
webhook_credential: workflowJobTemplate.summary_fields.webhook_credential || null,
webhook_key: workflowJobTemplate.related.webhook_key || '',
webhook_key:
whkData?.webhook_key || t('a new webhook key will be generated on save.').toUpperCase(),
webhook_url: workflowJobTemplate.related?.webhook_receiver
? `${document.location.origin}${workflowJobTemplate.related.webhook_receiver}`
: t('a new webhook url will be generated on save.').toUpperCase(),
webhook_receiver: workflowJobTemplate.related.webhook_receiver,
webhook_service: workflowJobTemplate.webhook_service || '',
};
}, [workflowJobTemplate]);
}, [t, workflowJobTemplate, whkData]);
const { cache } = useSWRConfig();

if (error instanceof Error) {
return <AwxError error={error} handleRefresh={refresh} />;
const wfjtFormError = wfjtError || whkDataError;

if (wfjtFormError instanceof Error) {
return (
<AwxError
error={wfjtFormError}
handleRefresh={wfjtFormError ? wfjtRefresh : whkDataRefresh}
/>
);
}
if (isLoading) return <LoadingPage />;
if (isWfjtLoading || isWhkDataLoading) return <LoadingPage />;
return (
<PageLayout>
<PageHeader
Expand Down Expand Up @@ -147,8 +166,10 @@ export function CreateWorkflowJobTemplate() {
job_tags: parseStringToTagArray('') || [],
skip_tags: parseStringToTagArray('') || [],
extra_vars: '---\n',
webhook_key: t('a new webhook key will be generated on save.').toUpperCase(),
webhook_url: t('a new webhook url will be generated on save.').toUpperCase(),
}),
[]
[t]
);

const getPageUrl = useGetPageUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export function WorkflowJobTemplateInputs(props: {
/>
</PageFormSection>

{isWebhookEnabled ? <WebhookSubForm /> : null}
{isWebhookEnabled ? <WebhookSubForm templateType="workflow_job_templates" /> : null}
</>
);
}
13 changes: 8 additions & 5 deletions frontend/awx/resources/templates/components/WebhookSubForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ import { CredentialType } from '../../../interfaces/CredentialType';
import { JobTemplateForm } from '../../../interfaces/JobTemplateForm';
import { WorkflowJobTemplateForm } from '../../../interfaces/WorkflowJobTemplate';

export function WebhookSubForm() {
export function WebhookSubForm(props: {
templateType: 'job_templates' | 'workflow_job_templates';
}) {
const { t } = useTranslation();
const params = useParams<{ id?: string }>();
const { setValue } = useFormContext<JobTemplateForm | WorkflowJobTemplateForm>();
const webhookKey = useWatch({ name: 'webhook_key' }) as string;
const webhookService = useWatch({ name: 'webhook_service' }) as string;
const isWebhookEnabled = useWatch({ name: 'isWebhookEnabled' }) as boolean;
const { templateType } = props;

const { pathname } = useLocation();

Expand All @@ -35,13 +38,13 @@ export function WebhookSubForm() {
const handleGenerateWebhookKey = useCallback(async () => {
if (isWebhookEnabled && params.id) {
const { webhook_key: webhookKey } = await postRequest<{ webhook_key: string }>(
awxAPI`/job_templates/${params.id}/webhook_key/`,
awxAPI`/${templateType}/${params.id}/webhook_key/`,
{}
);
setValue('webhook_key', webhookKey);
return;
}
}, [isWebhookEnabled, setValue, params]);
}, [isWebhookEnabled, setValue, params, templateType]);

useGet<AwxItemsResponse<CredentialType>>(
awxAPI`/credential_types/?namespace=${webhookService}_token`
Expand All @@ -53,7 +56,7 @@ export function WebhookSubForm() {
'related.webhook_receiver',
pathname.endsWith('/create')
? t`a new webhook url will be generated on save.`.toUpperCase()
: `${document.location.origin}${awxAPI`/job_template/`}${
: `${document.location.origin}${awxAPI`/${templateType}/`}${
params.id as string
}/${webhookService}/`
);
Expand All @@ -63,7 +66,7 @@ export function WebhookSubForm() {
'related.webhook_key',
t`a new webhook key will be generated on save.`.toUpperCase()
);
}, [webhookService, setValue, pathname, params.id, t]);
}, [webhookService, setValue, pathname, params.id, t, templateType]);

const isUpdateKeyDisabled =
pathname.endsWith('/create') || webhookKey === 'A NEW WEBHOOK KEY WILL BE GENERATED ON SAVE.';
Expand Down

0 comments on commit aa279f0

Please sign in to comment.