Commit 961bfe82 authored by Nicolas Dular's avatar Nicolas Dular

Select CI template based on URL param

This allows to select a pre-defined CI template provided by the URL
param `template` with the name of the template.

e.g. `-/new/master?file_name=.gitlab-ci.yml&template=Android`
parent 8474a0ba
...@@ -19,6 +19,17 @@ export default class FileTemplateSelector { ...@@ -19,6 +19,17 @@ export default class FileTemplateSelector {
this.$dropdownToggleText = this.$wrapper.find('.dropdown-toggle-text'); this.$dropdownToggleText = this.$wrapper.find('.dropdown-toggle-text');
this.initDropdown(); this.initDropdown();
this.selectInitialTemplate();
}
selectInitialTemplate() {
const template = this.$dropdown.data('selected');
if (!template) {
return;
}
this.mediator.selectTemplateFile(this, template);
} }
show() { show() {
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
.metrics-dashboard-selector.js-metrics-dashboard-selector-wrap.js-template-selector-wrap.hidden .metrics-dashboard-selector.js-metrics-dashboard-selector-wrap.js-template-selector-wrap.hidden
= dropdown_tag(_("Apply a template"), options: { toggle_class: 'js-metrics-dashboard-selector qa-metrics-dashboard-dropdown', dropdown_class: 'dropdown-menu-selectable', filter: true, placeholder: "Filter", data: { data: metrics_dashboard_ymls(@project) } } ) = dropdown_tag(_("Apply a template"), options: { toggle_class: 'js-metrics-dashboard-selector qa-metrics-dashboard-dropdown', dropdown_class: 'dropdown-menu-selectable', filter: true, placeholder: "Filter", data: { data: metrics_dashboard_ymls(@project) } } )
#gitlab-ci-yml-selector.gitlab-ci-yml-selector.js-gitlab-ci-yml-selector-wrap.js-template-selector-wrap.hidden #gitlab-ci-yml-selector.gitlab-ci-yml-selector.js-gitlab-ci-yml-selector-wrap.js-template-selector-wrap.hidden
= dropdown_tag(_("Apply a template"), options: { toggle_class: 'js-gitlab-ci-yml-selector qa-gitlab-ci-yml-dropdown', dropdown_class: 'dropdown-menu-selectable', filter: true, placeholder: "Filter", data: { data: gitlab_ci_ymls(@project) } } ) = dropdown_tag(_("Apply a template"), options: { toggle_class: 'js-gitlab-ci-yml-selector qa-gitlab-ci-yml-dropdown', dropdown_class: 'dropdown-menu-selectable', filter: true, placeholder: "Filter", data: { data: gitlab_ci_ymls(@project), selected: params[:template] } } )
- if experiment_enabled?(:ci_syntax_templates_b, subject: current_user) && @project.namespace.recent? - if experiment_enabled?(:ci_syntax_templates_b, subject: current_user) && @project.namespace.recent?
.gitlab-ci-syntax-yml-selector.js-gitlab-ci-syntax-yml-selector-wrap.js-template-selector-wrap.hidden .gitlab-ci-syntax-yml-selector.js-gitlab-ci-syntax-yml-selector-wrap.js-template-selector-wrap.hidden
= dropdown_tag(_("Learn CI/CD syntax"), options: { toggle_class: 'js-gitlab-ci-syntax-yml-selector qa-gitlab-ci-syntax-yml-dropdown', dropdown_class: 'dropdown-menu-selectable', filter: true, placeholder: "Filter", data: { data: gitlab_ci_syntax_ymls(@project) } } ) = dropdown_tag(_("Learn CI/CD syntax"), options: { toggle_class: 'js-gitlab-ci-syntax-yml-selector qa-gitlab-ci-syntax-yml-dropdown', dropdown_class: 'dropdown-menu-selectable', filter: true, placeholder: "Filter", data: { data: gitlab_ci_syntax_ymls(@project) } } )
......
---
title: Allow selecting a CI template by providing the template name as a URL param gitlab_ci_yml
merge_request: 56861
author:
type: added
...@@ -5,10 +5,14 @@ require 'spec_helper' ...@@ -5,10 +5,14 @@ require 'spec_helper'
RSpec.describe 'Projects > Files > User wants to add a .gitlab-ci.yml file', :js do RSpec.describe 'Projects > Files > User wants to add a .gitlab-ci.yml file', :js do
include Spec::Support::Helpers::Features::EditorLiteSpecHelpers include Spec::Support::Helpers::Features::EditorLiteSpecHelpers
let(:params) { {} }
let(:filename) { '.gitlab-ci.yml' }
let_it_be(:project) { create(:project, :repository) }
before do before do
project = create(:project, :repository)
sign_in project.owner sign_in project.owner
visit project_new_blob_path(project, 'master', file_name: '.gitlab-ci.yml') visit project_new_blob_path(project, 'master', file_name: filename, **params)
end end
it 'user can pick a template from the dropdown' do it 'user can pick a template from the dropdown' do
...@@ -29,4 +33,38 @@ RSpec.describe 'Projects > Files > User wants to add a .gitlab-ci.yml file', :js ...@@ -29,4 +33,38 @@ RSpec.describe 'Projects > Files > User wants to add a .gitlab-ci.yml file', :js
expect(editor_get_value).to have_content('This file is a template, and might need editing before it works on your project') expect(editor_get_value).to have_content('This file is a template, and might need editing before it works on your project')
expect(editor_get_value).to have_content('jekyll build -d test') expect(editor_get_value).to have_content('jekyll build -d test')
end end
context 'when template param is provided' do
let(:params) { { template: 'Jekyll' } }
it 'uses the given template' do
wait_for_requests
expect(page).to have_css('.gitlab-ci-yml-selector .dropdown-toggle-text', text: 'Apply a template')
expect(editor_get_value).to have_content('This file is a template, and might need editing before it works on your project')
expect(editor_get_value).to have_content('jekyll build -d test')
end
end
context 'when provided template param is not a valid template name' do
let(:params) { { template: 'non-existing-template' } }
it 'leaves the editor empty' do
wait_for_requests
expect(page).to have_css('.gitlab-ci-yml-selector .dropdown-toggle-text', text: 'Apply a template')
expect(editor_get_value).to have_content('')
end
end
context 'when template is not available for the given file' do
let(:filename) { 'Dockerfile' }
let(:params) { { template: 'Jekyll' } }
it 'leaves the editor empty' do
wait_for_requests
expect(editor_get_value).to have_content('')
end
end
end end
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