Merge pull request #10044 from AlexSCorey/9977-ScheduleSurvey

Properly adds survey default values on Schedules

SUMMARY
This addresses #9977. This also fixes a bug where the extra_data was not rendering properly in schedule details
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME

UI

Reviewed-by: Kersom <None>
Reviewed-by: Tiago Góes <tiago.goes2009@gmail.com>
This commit is contained in:
softwarefactory-project-zuul[bot] 2021-05-04 19:43:18 +00:00 committed by GitHub
commit d7742d7340
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 196 additions and 4 deletions

View File

@ -61,6 +61,15 @@ function ScheduleAdd({
);
let extraVars;
const surveyValues = getSurveyValues(values);
if (
!Object.values(surveyValues).length &&
surveyConfiguration?.spec?.length
) {
surveyConfiguration.spec.forEach(q => {
surveyValues[q.variable] = q.default;
});
}
const initialExtraVars =
launchConfiguration?.ask_variables_on_launch &&
(values.extra_vars || '---');
@ -86,7 +95,9 @@ function ScheduleAdd({
if (requestData.extra_data) {
requestData.extra_data.days = values.daysToKeep;
} else {
requestData.extra_data = JSON.stringify({ days: values.daysToKeep });
requestData.extra_data = JSON.stringify({
days: values.daysToKeep,
});
}
}

View File

@ -403,4 +403,68 @@ describe('<ScheduleAdd />', () => {
expect(SchedulesAPI.associateCredential).toBeCalledWith(3, 10);
expect(SchedulesAPI.associateCredential).toBeCalledWith(3, 20);
});
test('should submit survey with default values properly, without opening prompt wizard', async () => {
let scheduleSurveyWrapper;
await act(async () => {
scheduleSurveyWrapper = mountWithContexts(
<ScheduleAdd
apiModel={JobTemplatesAPI}
resource={{
id: 700,
type: 'job_template',
inventory: 2,
summary_fields: { credentials: [] },
}}
launchConfig={launchConfig}
surveyConfig={{
spec: [
{
question_name: 'text',
question_description: '',
required: true,
type: 'text',
variable: 'text',
min: 0,
max: 1024,
default: 'text variable',
choices: '',
new_question: true,
},
{
question_name: 'mc',
question_description: '',
required: true,
type: 'multiplechoice',
variable: 'mc',
min: 0,
max: 1024,
default: 'first',
choices: 'first\nsecond',
new_question: true,
},
],
}}
/>
);
});
await act(async () => {
scheduleSurveyWrapper.find('Formik').invoke('onSubmit')({
description: 'test description',
end: 'never',
frequency: 'none',
interval: 1,
name: 'Run once schedule',
startDateTime: '2020-03-25T10:00:00',
timezone: 'America/New_York',
});
});
expect(JobTemplatesAPI.createSchedule).toHaveBeenCalledWith(700, {
description: 'test description',
name: 'Run once schedule',
extra_data: { mc: 'first', text: 'text variable' },
rrule:
'DTSTART;TZID=America/New_York:20200325T100000 RRULE:INTERVAL=1;COUNT=1;FREQ=MINUTELY',
});
});
});

View File

@ -26,7 +26,7 @@ import DeleteButton from '../../DeleteButton';
import ErrorDetail from '../../ErrorDetail';
import ChipGroup from '../../ChipGroup';
import { VariablesDetail } from '../../CodeEditor';
import { parseVariableField } from '../../../util/yaml';
import { parseVariableField, jsonToYaml } from '../../../util/yaml';
const PromptDivider = styled(Divider)`
margin-top: var(--pf-global--spacer--lg);
@ -366,7 +366,7 @@ function ScheduleDetail({ hasDaysToKeepField, schedule, surveyConfig }) {
)}
{showVariablesDetail && (
<VariablesDetail
value={extra_data}
value={jsonToYaml(JSON.stringify(extra_data))}
rows={4}
label={t`Variables`}
/>

View File

@ -61,6 +61,16 @@ function ScheduleEdit({
let extraVars;
const surveyValues = getSurveyValues(values);
if (
!Object.values(surveyValues).length &&
surveyConfiguration?.spec?.length
) {
surveyConfiguration.spec.forEach(q => {
surveyValues[q.variable] = q.default;
});
}
const initialExtraVars =
launchConfiguration?.ask_variables_on_launch &&
(values.extra_vars || '---');
@ -91,7 +101,9 @@ function ScheduleEdit({
if (Object.keys(values).includes('daysToKeep')) {
if (!requestData.extra_data) {
requestData.extra_data = JSON.stringify({ days: values.daysToKeep });
requestData.extra_data = JSON.stringify({
days: values.daysToKeep,
});
} else {
requestData.extra_data.days = values.daysToKeep;
}

View File

@ -633,4 +633,109 @@ describe('<ScheduleEdit />', () => {
'DTSTART;TZID=America/New_York:20200402T144500 RRULE:INTERVAL=1;COUNT=1;FREQ=MINUTELY',
});
});
test('should submit survey with default values properly, without opening prompt wizard', async () => {
let scheduleSurveyWrapper;
await act(async () => {
scheduleSurveyWrapper = mountWithContexts(
<ScheduleEdit
schedule={mockSchedule}
resource={{
id: 700,
type: 'job_template',
iventory: 1,
summary_fields: {
credentials: [
{ name: 'job template credential', id: 75, kind: 'ssh' },
],
},
}}
resourceDefaultCredentials={[]}
launchConfig={{
can_start_without_user_input: false,
passwords_needed_to_start: [],
ask_scm_branch_on_launch: false,
ask_variables_on_launch: false,
ask_tags_on_launch: false,
ask_diff_mode_on_launch: false,
ask_skip_tags_on_launch: false,
ask_job_type_on_launch: false,
ask_limit_on_launch: false,
ask_verbosity_on_launch: false,
ask_inventory_on_launch: true,
ask_credential_on_launch: true,
survey_enabled: true,
variables_needed_to_start: [],
credential_needed_to_start: true,
inventory_needed_to_start: true,
job_template_data: {
name: 'Demo Job Template',
id: 7,
description: '',
},
defaults: {
extra_vars: '---',
diff_mode: false,
limit: '',
job_tags: '',
skip_tags: '',
job_type: 'run',
verbosity: 0,
inventory: {
name: null,
id: null,
},
scm_branch: '',
credentials: [],
},
}}
surveyConfig={{
spec: [
{
question_name: 'text',
question_description: '',
required: true,
type: 'text',
variable: 'text',
min: 0,
max: 1024,
default: 'text variable',
choices: '',
new_question: true,
},
{
question_name: 'mc',
question_description: '',
required: true,
type: 'multiplechoice',
variable: 'mc',
min: 0,
max: 1024,
default: 'first',
choices: 'first\nsecond',
new_question: true,
},
],
}}
/>
);
});
await act(async () => {
scheduleSurveyWrapper.find('Formik').invoke('onSubmit')({
description: 'test description',
end: 'never',
frequency: 'none',
interval: 1,
name: 'Run once schedule',
startDateTime: '2020-03-25T10:00:00',
timezone: 'America/New_York',
});
});
expect(SchedulesAPI.update).toHaveBeenCalledWith(27, {
description: 'test description',
name: 'Run once schedule',
extra_data: { mc: 'first', text: 'text variable' },
rrule:
'DTSTART;TZID=America/New_York:20200325T100000 RRULE:INTERVAL=1;COUNT=1;FREQ=MINUTELY',
});
});
});