Commit 33d4e66d authored by Miguel Rincon's avatar Miguel Rincon Committed by Olena Horal-Koretska

Load configured variables when ref changes

This changes loads the variables from the CI file and puts them in
the "Run pipeline" form as default variables.
parent 06873677
<script> <script>
import Vue from 'vue';
import { uniqueId } from 'lodash'; import { uniqueId } from 'lodash';
import { import {
GlAlert, GlAlert,
...@@ -50,6 +51,10 @@ export default { ...@@ -50,6 +51,10 @@ export default {
type: String, type: String,
required: true, required: true,
}, },
configVariablesPath: {
type: String,
required: true,
},
projectId: { projectId: {
type: String, type: String,
required: true, required: true,
...@@ -86,7 +91,7 @@ export default { ...@@ -86,7 +91,7 @@ export default {
return { return {
searchTerm: '', searchTerm: '',
refValue: this.refParam, refValue: this.refParam,
variables: [], form: {},
error: null, error: null,
warnings: [], warnings: [],
totalWarnings: 0, totalWarnings: 0,
...@@ -110,60 +115,122 @@ export default { ...@@ -110,60 +115,122 @@ export default {
shouldShowWarning() { shouldShowWarning() {
return this.warnings.length > 0 && !this.isWarningDismissed; return this.warnings.length > 0 && !this.isWarningDismissed;
}, },
variables() {
return this.form[this.refValue]?.variables ?? [];
},
descriptions() {
return this.form[this.refValue]?.descriptions ?? {};
},
}, },
created() { created() {
this.addEmptyVariable(); this.setRefSelected(this.refValue);
},
methods: {
addEmptyVariable(refValue) {
const { variables } = this.form[refValue];
if (this.variableParams) { const lastVar = variables[variables.length - 1];
this.setVariableParams(VARIABLE_TYPE, this.variableParams); if (lastVar?.key === '' && lastVar?.value === '') {
return;
} }
if (this.fileParams) { variables.push({
this.setVariableParams(FILE_TYPE, this.fileParams); uniqueId: uniqueId(`var-${refValue}`),
} variable_type: VARIABLE_TYPE,
key: '',
value: '',
});
}, },
methods: { setVariable(refValue, type, key, value) {
setVariable(type, key, value) { const { variables } = this.form[refValue];
const variable = this.variables.find(v => v.key === key);
const variable = variables.find(v => v.key === key);
if (variable) { if (variable) {
variable.type = type; variable.type = type;
variable.value = value; variable.value = value;
} else { } else {
// insert before the empty variable variables.push({
this.variables.splice(this.variables.length - 1, 0, { uniqueId: uniqueId(`var-${refValue}`),
uniqueId: uniqueId('var'),
key, key,
value, value,
variable_type: type, variable_type: type,
}); });
} }
}, },
setVariableParams(type, paramsObj) { setVariableParams(refValue, type, paramsObj) {
Object.entries(paramsObj).forEach(([key, value]) => { Object.entries(paramsObj).forEach(([key, value]) => {
this.setVariable(type, key, value); this.setVariable(refValue, type, key, value);
}); });
}, },
setRefSelected(ref) { setRefSelected(refValue) {
this.refValue = ref; this.refValue = refValue;
if (!this.form[refValue]) {
this.fetchConfigVariables(refValue)
.then(({ descriptions, params }) => {
Vue.set(this.form, refValue, {
variables: [],
descriptions,
});
// Add default variables from yml
this.setVariableParams(refValue, VARIABLE_TYPE, params);
})
.catch(() => {
Vue.set(this.form, refValue, {
variables: [],
descriptions: {},
});
})
.finally(() => {
// Add/update variables, e.g. from query string
if (this.variableParams) {
this.setVariableParams(refValue, VARIABLE_TYPE, this.variableParams);
}
if (this.fileParams) {
this.setVariableParams(refValue, FILE_TYPE, this.fileParams);
}
// Adds empty var at the end of the form
this.addEmptyVariable(refValue);
});
}
}, },
isSelected(ref) { isSelected(ref) {
return ref === this.refValue; return ref === this.refValue;
}, },
addEmptyVariable() {
this.variables.push({
uniqueId: uniqueId('var'),
variable_type: VARIABLE_TYPE,
key: '',
value: '',
});
},
removeVariable(index) { removeVariable(index) {
this.variables.splice(index, 1); this.variables.splice(index, 1);
}, },
canRemove(index) { canRemove(index) {
return index < this.variables.length - 1; return index < this.variables.length - 1;
}, },
fetchConfigVariables(refValue) {
if (gon?.features?.newPipelineFormPrefilledVars) {
return axios
.get(this.configVariablesPath, {
params: {
sha: refValue,
},
})
.then(({ data }) => {
const params = {};
const descriptions = {};
Object.entries(data).forEach(([key, { value, description }]) => {
if (description !== null) {
params[key] = value;
descriptions[key] = description;
}
});
return { params, descriptions };
});
}
return Promise.resolve({ params: {}, descriptions: {} });
},
createPipeline() { createPipeline() {
const filteredVariables = this.variables const filteredVariables = this.variables
.filter(({ key, value }) => key !== '' && value !== '') .filter(({ key, value }) => key !== '' && value !== '')
...@@ -261,8 +328,11 @@ export default { ...@@ -261,8 +328,11 @@ export default {
<div <div
v-for="(variable, index) in variables" v-for="(variable, index) in variables"
:key="variable.uniqueId" :key="variable.uniqueId"
class="gl-display-flex gl-align-items-stretch gl-align-items-center gl-mb-4 gl-ml-n3 gl-pb-2 gl-border-b-solid gl-border-gray-200 gl-border-b-1 gl-flex-direction-column gl-md-flex-direction-row" class="gl-mb-3 gl-ml-n3 gl-pb-2"
data-testid="ci-variable-row" data-testid="ci-variable-row"
>
<div
class="gl-display-flex gl-align-items-stretch gl-flex-direction-column gl-md-flex-direction-row"
> >
<gl-form-select <gl-form-select
v-model="variable.variable_type" v-model="variable.variable_type"
...@@ -274,12 +344,13 @@ export default { ...@@ -274,12 +344,13 @@ export default {
:placeholder="s__('CiVariables|Input variable key')" :placeholder="s__('CiVariables|Input variable key')"
:class="$options.formElementClasses" :class="$options.formElementClasses"
data-testid="pipeline-form-ci-variable-key" data-testid="pipeline-form-ci-variable-key"
@change.once="addEmptyVariable()" @change="addEmptyVariable(refValue)"
/> />
<gl-form-input <gl-form-input
v-model="variable.value" v-model="variable.value"
:placeholder="s__('CiVariables|Input variable value')" :placeholder="s__('CiVariables|Input variable value')"
class="gl-mb-3" class="gl-mb-3"
data-testid="pipeline-form-ci-variable-value"
/> />
<template v-if="variables.length > 1"> <template v-if="variables.length > 1">
...@@ -301,6 +372,10 @@ export default { ...@@ -301,6 +372,10 @@ export default {
/> />
</template> </template>
</div> </div>
<div v-if="descriptions[variable.key]" class="gl-text-gray-500 gl-mb-3">
{{ descriptions[variable.key] }}
</div>
</div>
<template #description <template #description
><gl-sprintf :message="$options.variablesDescription"> ><gl-sprintf :message="$options.variablesDescription">
......
...@@ -6,6 +6,7 @@ export default () => { ...@@ -6,6 +6,7 @@ export default () => {
const { const {
projectId, projectId,
pipelinesPath, pipelinesPath,
configVariablesPath,
refParam, refParam,
varParam, varParam,
fileParam, fileParam,
...@@ -25,6 +26,7 @@ export default () => { ...@@ -25,6 +26,7 @@ export default () => {
props: { props: {
projectId, projectId,
pipelinesPath, pipelinesPath,
configVariablesPath,
refParam, refParam,
variableParams, variableParams,
fileParams, fileParams,
......
...@@ -17,6 +17,7 @@ class Projects::PipelinesController < Projects::ApplicationController ...@@ -17,6 +17,7 @@ class Projects::PipelinesController < Projects::ApplicationController
push_frontend_feature_flag(:pipelines_security_report_summary, project) push_frontend_feature_flag(:pipelines_security_report_summary, project)
push_frontend_feature_flag(:new_pipeline_form, project) push_frontend_feature_flag(:new_pipeline_form, project)
push_frontend_feature_flag(:graphql_pipeline_header, project, type: :development, default_enabled: false) push_frontend_feature_flag(:graphql_pipeline_header, project, type: :development, default_enabled: false)
push_frontend_feature_flag(:new_pipeline_form_prefilled_vars, project, type: :development)
end end
before_action :ensure_pipeline, only: [:show] before_action :ensure_pipeline, only: [:show]
......
...@@ -7,7 +7,15 @@ ...@@ -7,7 +7,15 @@
%hr %hr
- if Feature.enabled?(:new_pipeline_form, @project) - if Feature.enabled?(:new_pipeline_form, @project)
#js-new-pipeline{ data: { project_id: @project.id, pipelines_path: project_pipelines_path(@project), ref_param: params[:ref] || @project.default_branch, var_param: params[:var].to_json, file_param: params[:file_var].to_json, ref_names: @project.repository.ref_names.to_json.html_safe, settings_link: project_settings_ci_cd_path(@project), max_warnings: ::Gitlab::Ci::Warnings::MAX_LIMIT } } #js-new-pipeline{ data: { project_id: @project.id,
pipelines_path: project_pipelines_path(@project),
config_variables_path: config_variables_namespace_project_pipelines_path(@project.namespace, @project),
ref_param: params[:ref] || @project.default_branch,
var_param: params[:var].to_json,
file_param: params[:file_var].to_json,
ref_names: @project.repository.ref_names.to_json.html_safe,
settings_link: project_settings_ci_cd_path(@project),
max_warnings: ::Gitlab::Ci::Warnings::MAX_LIMIT } }
- else - else
= form_for @pipeline, as: :pipeline, url: project_pipelines_path(@project), html: { id: "new-pipeline-form", class: "js-new-pipeline-form js-requires-input" } do |f| = form_for @pipeline, as: :pipeline, url: project_pipelines_path(@project), html: { id: "new-pipeline-form", class: "js-new-pipeline-form js-requires-input" } do |f|
......
---
name: new_pipeline_form_prefilled_vars
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/44120
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/263276
type: development
group: group::continuous integration
default_enabled: false
...@@ -2,6 +2,7 @@ import { mount, shallowMount } from '@vue/test-utils'; ...@@ -2,6 +2,7 @@ import { mount, shallowMount } from '@vue/test-utils';
import { GlDropdown, GlDropdownItem, GlForm, GlSprintf } from '@gitlab/ui'; import { GlDropdown, GlDropdownItem, GlForm, GlSprintf } from '@gitlab/ui';
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import waitForPromises from 'helpers/wait_for_promises'; import waitForPromises from 'helpers/wait_for_promises';
import httpStatusCodes from '~/lib/utils/http_status';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import PipelineNewForm from '~/pipeline_new/components/pipeline_new_form.vue'; import PipelineNewForm from '~/pipeline_new/components/pipeline_new_form.vue';
import { mockRefs, mockParams, mockPostParams, mockProjectId, mockError } from '../mock_data'; import { mockRefs, mockParams, mockPostParams, mockProjectId, mockError } from '../mock_data';
...@@ -11,7 +12,8 @@ jest.mock('~/lib/utils/url_utility', () => ({ ...@@ -11,7 +12,8 @@ jest.mock('~/lib/utils/url_utility', () => ({
redirectTo: jest.fn(), redirectTo: jest.fn(),
})); }));
const pipelinesPath = '/root/project/-/pipleines'; const pipelinesPath = '/root/project/-/pipelines';
const configVariablesPath = '/root/project/-/pipelines/config_variables';
const postResponse = { id: 1 }; const postResponse = { id: 1 };
describe('Pipeline New Form', () => { describe('Pipeline New Form', () => {
...@@ -28,6 +30,7 @@ describe('Pipeline New Form', () => { ...@@ -28,6 +30,7 @@ describe('Pipeline New Form', () => {
const findVariableRows = () => wrapper.findAll('[data-testid="ci-variable-row"]'); const findVariableRows = () => wrapper.findAll('[data-testid="ci-variable-row"]');
const findRemoveIcons = () => wrapper.findAll('[data-testid="remove-ci-variable-row"]'); const findRemoveIcons = () => wrapper.findAll('[data-testid="remove-ci-variable-row"]');
const findKeyInputs = () => wrapper.findAll('[data-testid="pipeline-form-ci-variable-key"]'); const findKeyInputs = () => wrapper.findAll('[data-testid="pipeline-form-ci-variable-key"]');
const findValueInputs = () => wrapper.findAll('[data-testid="pipeline-form-ci-variable-value"]');
const findErrorAlert = () => wrapper.find('[data-testid="run-pipeline-error-alert"]'); const findErrorAlert = () => wrapper.find('[data-testid="run-pipeline-error-alert"]');
const findWarningAlert = () => wrapper.find('[data-testid="run-pipeline-warning-alert"]'); const findWarningAlert = () => wrapper.find('[data-testid="run-pipeline-warning-alert"]');
const findWarningAlertSummary = () => findWarningAlert().find(GlSprintf); const findWarningAlertSummary = () => findWarningAlert().find(GlSprintf);
...@@ -39,6 +42,7 @@ describe('Pipeline New Form', () => { ...@@ -39,6 +42,7 @@ describe('Pipeline New Form', () => {
propsData: { propsData: {
projectId: mockProjectId, projectId: mockProjectId,
pipelinesPath, pipelinesPath,
configVariablesPath,
refs: mockRefs, refs: mockRefs,
defaultBranch: 'master', defaultBranch: 'master',
settingsLink: '', settingsLink: '',
...@@ -55,6 +59,7 @@ describe('Pipeline New Form', () => { ...@@ -55,6 +59,7 @@ describe('Pipeline New Form', () => {
beforeEach(() => { beforeEach(() => {
mock = new MockAdapter(axios); mock = new MockAdapter(axios);
mock.onGet(configVariablesPath).reply(httpStatusCodes.OK, {});
}); });
afterEach(() => { afterEach(() => {
...@@ -66,7 +71,7 @@ describe('Pipeline New Form', () => { ...@@ -66,7 +71,7 @@ describe('Pipeline New Form', () => {
describe('Dropdown with branches and tags', () => { describe('Dropdown with branches and tags', () => {
beforeEach(() => { beforeEach(() => {
mock.onPost(pipelinesPath).reply(200, postResponse); mock.onPost(pipelinesPath).reply(httpStatusCodes.OK, postResponse);
}); });
it('displays dropdown with all branches and tags', () => { it('displays dropdown with all branches and tags', () => {
...@@ -87,17 +92,27 @@ describe('Pipeline New Form', () => { ...@@ -87,17 +92,27 @@ describe('Pipeline New Form', () => {
}); });
describe('Form', () => { describe('Form', () => {
beforeEach(() => { beforeEach(async () => {
createComponent('', mockParams, mount); createComponent('', mockParams, mount);
mock.onPost(pipelinesPath).reply(200, postResponse); mock.onPost(pipelinesPath).reply(httpStatusCodes.OK, postResponse);
await waitForPromises();
}); });
it('displays the correct values for the provided query params', async () => { it('displays the correct values for the provided query params', async () => {
expect(findDropdown().props('text')).toBe('tag-1'); expect(findDropdown().props('text')).toBe('tag-1');
expect(findVariableRows()).toHaveLength(3);
});
await wrapper.vm.$nextTick(); it('displays a variable from provided query params', () => {
expect(findKeyInputs().at(0).element.value).toBe('test_var');
expect(findValueInputs().at(0).element.value).toBe('test_var_val');
});
expect(findVariableRows()).toHaveLength(3); it('displays an empty variable for the user to fill out', async () => {
expect(findKeyInputs().at(2).element.value).toBe('');
expect(findValueInputs().at(2).element.value).toBe('');
}); });
it('does not display remove icon for last row', () => { it('does not display remove icon for last row', () => {
...@@ -124,13 +139,143 @@ describe('Pipeline New Form', () => { ...@@ -124,13 +139,143 @@ describe('Pipeline New Form', () => {
}); });
it('creates blank variable on input change event', async () => { it('creates blank variable on input change event', async () => {
const input = findKeyInputs().at(2);
input.element.value = 'test_var_2';
input.trigger('change');
await wrapper.vm.$nextTick();
expect(findVariableRows()).toHaveLength(4);
expect(findKeyInputs().at(3).element.value).toBe('');
expect(findValueInputs().at(3).element.value).toBe('');
});
describe('when the form has been modified', () => {
const selectRef = i =>
findDropdownItems()
.at(i)
.vm.$emit('click');
beforeEach(async () => {
const input = findKeyInputs().at(0);
input.element.value = 'test_var_2';
input.trigger('change');
findRemoveIcons()
.at(1)
.trigger('click');
await wrapper.vm.$nextTick();
});
it('form values are restored when the ref changes', async () => {
expect(findVariableRows()).toHaveLength(2);
selectRef(1);
await waitForPromises();
expect(findVariableRows()).toHaveLength(3);
expect(findKeyInputs().at(0).element.value).toBe('test_var');
});
it('form values are restored again when the ref is reverted', async () => {
selectRef(1);
await waitForPromises();
selectRef(2);
await waitForPromises();
expect(findVariableRows()).toHaveLength(2);
expect(findKeyInputs().at(0).element.value).toBe('test_var_2');
});
});
});
describe('when feature flag new_pipeline_form_prefilled_vars is enabled', () => {
let origGon;
const mockYmlKey = 'yml_var';
const mockYmlValue = 'yml_var_val';
const mockYmlDesc = 'A var from yml.';
beforeAll(() => {
origGon = window.gon;
window.gon = { features: { newPipelineFormPrefilledVars: true } };
});
afterAll(() => {
window.gon = origGon;
});
describe('when yml defines a variable with description', () => {
beforeEach(async () => {
createComponent('', mockParams, mount);
mock.onGet(configVariablesPath).reply(httpStatusCodes.OK, {
[mockYmlKey]: {
value: mockYmlValue,
description: mockYmlDesc,
},
});
await waitForPromises();
});
it('displays all the variables', async () => {
expect(findVariableRows()).toHaveLength(4);
});
it('displays a variable from yml', () => {
expect(findKeyInputs().at(0).element.value).toBe(mockYmlKey);
expect(findValueInputs().at(0).element.value).toBe(mockYmlValue);
});
it('displays a variable from provided query params', () => {
expect(findKeyInputs().at(1).element.value).toBe('test_var');
expect(findValueInputs().at(1).element.value).toBe('test_var_val');
});
it('adds a description to the first variable from yml', () => {
expect(
findVariableRows()
.at(0)
.text(),
).toContain(mockYmlDesc);
});
it('removes the description when a variable key changes', async () => {
findKeyInputs().at(0).element.value = 'yml_var_modified';
findKeyInputs() findKeyInputs()
.at(2) .at(0)
.trigger('change'); .trigger('change');
await wrapper.vm.$nextTick(); await wrapper.vm.$nextTick();
expect(findVariableRows()).toHaveLength(4); expect(
findVariableRows()
.at(0)
.text(),
).not.toContain(mockYmlDesc);
});
});
describe('when yml defines a variable without description', () => {
beforeEach(async () => {
createComponent('', mockParams, mount);
mock.onGet(configVariablesPath).reply(httpStatusCodes.OK, {
[mockYmlKey]: {
value: mockYmlValue,
description: null,
},
});
await waitForPromises();
});
it('displays all the variables', async () => {
expect(findVariableRows()).toHaveLength(3);
});
}); });
}); });
...@@ -138,7 +283,7 @@ describe('Pipeline New Form', () => { ...@@ -138,7 +283,7 @@ describe('Pipeline New Form', () => {
beforeEach(() => { beforeEach(() => {
createComponent(); createComponent();
mock.onPost(pipelinesPath).reply(400, mockError); mock.onPost(pipelinesPath).reply(httpStatusCodes.BAD_REQUEST, mockError);
findForm().vm.$emit('submit', dummySubmitEvent); findForm().vm.$emit('submit', dummySubmitEvent);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment