diff --git a/app/controllers/projects/settings/repository_controller.rb b/app/controllers/projects/settings/repository_controller.rb index e3bd89def9982d13e6e93ddb5038aa5c7fafb77a..0bf9b05b3b584adebe7e2eaa79b9d5908ec518bb 100644 --- a/app/controllers/projects/settings/repository_controller.rb +++ b/app/controllers/projects/settings/repository_controller.rb @@ -116,6 +116,7 @@ def project_params def project_params_attributes [ + :issue_branch_template, :default_branch, :autoclose_referenced_issues ] diff --git a/app/models/issue.rb b/app/models/issue.rb index 61fc0264360468ec294fdcbd26ef3c8649d28d31..df28c48c9397a9552210d1a548cd34ce6bff27d7 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -39,6 +39,7 @@ class Issue < ApplicationRecord DueNextMonthAndPreviousTwoWeeks = DueDateStruct.new('Due Next Month And Previous Two Weeks', 'next_month_and_previous_two_weeks').freeze SORTING_PREFERENCE_FIELD = :issues_sort + MAX_BRANCH_TEMPLATE = 255 # Types of issues that should be displayed on issue lists across the app # for example, project issues list, group issues list, and issues dashboard. @@ -394,10 +395,21 @@ def self.column_order_id_asc ) end - def self.to_branch_name(*args) - branch_name = args.map(&:to_s).each_with_index.map do |arg, i| - arg.parameterize(preserve_case: i == 0).presence - end.compact.join('-') + def self.to_branch_name(id, title, project: nil) + params = { + 'id' => id.to_s.parameterize(preserve_case: true), + 'title' => title.to_s.parameterize + } + template = project&.issue_branch_template + + branch_name = + if template.present? + Gitlab::StringPlaceholderReplacer.replace_string_placeholders(template, /(#{params.keys.join('|')})/) do |arg| + params[arg] + end + else + params.values.select(&:present?).join('-') + end if branch_name.length > 100 truncated_string = branch_name[0, 100] @@ -475,7 +487,7 @@ def to_branch_name if self.confidential? "#{iid}-confidential-issue" else - self.class.to_branch_name(iid, title) + self.class.to_branch_name(iid, title, project: project) end end diff --git a/app/models/project.rb b/app/models/project.rb index db30a74112d24b5816135ceba7130190ad5dcb64..8ecfdeb98f57e282566d5b0728caf896371405b9 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -491,6 +491,7 @@ def self.integration_association_name(name) to: :project_setting delegate :merge_commit_template, :merge_commit_template=, to: :project_setting, allow_nil: true delegate :squash_commit_template, :squash_commit_template=, to: :project_setting, allow_nil: true + delegate :issue_branch_template, :issue_branch_template=, to: :project_setting, allow_nil: true delegate :log_jira_dvcs_integration_usage, :jira_dvcs_server_last_sync_at, :jira_dvcs_cloud_last_sync_at, to: :feature_usage diff --git a/app/models/project_setting.rb b/app/models/project_setting.rb index 6d40544fad4ee95d9bb3e196212c23bc83107a81..4c3be49fddd5685ef6284b53cf52a85dc5909790 100644 --- a/app/models/project_setting.rb +++ b/app/models/project_setting.rb @@ -20,6 +20,7 @@ class ProjectSetting < ApplicationRecord validates :merge_commit_template, length: { maximum: Project::MAX_COMMIT_TEMPLATE_LENGTH } validates :squash_commit_template, length: { maximum: Project::MAX_COMMIT_TEMPLATE_LENGTH } + validates :issue_branch_template, length: { maximum: Issue::MAX_BRANCH_TEMPLATE } validates :target_platforms, inclusion: { in: ALLOWED_TARGET_PLATFORMS } validates :suggested_reviewers_enabled, inclusion: { in: [true, false] } diff --git a/app/views/projects/branch_defaults/_branch_names_fields.html.haml b/app/views/projects/branch_defaults/_branch_names_fields.html.haml new file mode 100644 index 0000000000000000000000000000000000000000..65f975fbd9e3d8fc36275028b4f10326d39b3c75 --- /dev/null +++ b/app/views/projects/branch_defaults/_branch_names_fields.html.haml @@ -0,0 +1,14 @@ +- if @project.project_feature.issues_access_level > 0 + %fieldset#branch-names-settings + .form-group + = f.label :issue_branch_template, _('Branch name template'), class: 'label-bold' + %p= s_('ProjectSettings|Branches created from issues follow this pattern.') + + .form-group + .gl-mb-2 + = f.text_field :issue_branch_template, class: 'form-control gl-mb-2', placeholder: "%{id}-%{title}" + %p.form-text.text-muted + = s_('ProjectSettings|Leave empty to use default template.') + = sprintf(s_('ProjectSettings|Maximum %{maxLength} characters.'), { maxLength: Issue::MAX_BRANCH_TEMPLATE }) + - branch_name_help_link = help_page_path('user/project/repository/web_editor.md', anchor: 'create-a-new-branch-from-an-issue') + = link_to _('What variables can I use?'), branch_name_help_link, target: "_blank" diff --git a/app/views/projects/branch_defaults/_show.html.haml b/app/views/projects/branch_defaults/_show.html.haml index 6790a658dceae2b118b252cb50e11d0411589ff8..4ecbc3b7fc871ca089b824946f2fe93288a2d5ca 100644 --- a/app/views/projects/branch_defaults/_show.html.haml +++ b/app/views/projects/branch_defaults/_show.html.haml @@ -13,4 +13,5 @@ = gitlab_ui_form_for @project, url: url, method: :put, html: { multipart: true, class: "issue-settings-form js-issue-settings-form" }, authenticity_token: true do |f| %input{ name: 'update_section', type: 'hidden', value: 'js-issue-settings' } = render 'projects/branch_defaults/default_branch_fields', f: f + = render 'projects/branch_defaults/branch_names_fields', f: f = f.submit _('Save changes'), pajamas_button: true, data: { qa_selector: 'save_changes_button' } diff --git a/db/migrate/20220721065723_add_issue_branch_template_to_project_settings.rb b/db/migrate/20220721065723_add_issue_branch_template_to_project_settings.rb new file mode 100644 index 0000000000000000000000000000000000000000..d65bd2c21e712cd623b4a5aa2e7e16695aa2495c --- /dev/null +++ b/db/migrate/20220721065723_add_issue_branch_template_to_project_settings.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class AddIssueBranchTemplateToProjectSettings < Gitlab::Database::Migration[2.0] + disable_ddl_transaction! + + def up + with_lock_retries do + add_column :project_settings, :issue_branch_template, :text, if_not_exists: true + end + + add_text_limit :project_settings, :issue_branch_template, 255 + end + + def down + remove_column :project_settings, :issue_branch_template, if_exists: true + end +end diff --git a/db/schema_migrations/20220721065723 b/db/schema_migrations/20220721065723 new file mode 100644 index 0000000000000000000000000000000000000000..78b1fedaff1cd08f3045f0b3d8ff13030670e3a4 --- /dev/null +++ b/db/schema_migrations/20220721065723 @@ -0,0 +1 @@ +5e3fbb2c033f8512e5fd14b8ce8c6088866c596a2b769e115dcc1feb9ce9d041 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index 7f9d177a3eaf55f43e5e4ce71e304fc2836c71bc..8f8809afeac3bae767628d85f958c1cf777c9ad2 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -20172,12 +20172,14 @@ CREATE TABLE project_settings ( target_platforms character varying[] DEFAULT '{}'::character varying[] NOT NULL, enforce_auth_checks_on_uploads boolean DEFAULT true NOT NULL, selective_code_owner_removals boolean DEFAULT false NOT NULL, + issue_branch_template text, show_diff_preview_in_email boolean DEFAULT true NOT NULL, jitsu_key text, suggested_reviewers_enabled boolean DEFAULT false NOT NULL, only_allow_merge_if_all_status_checks_passed boolean DEFAULT false NOT NULL, CONSTRAINT check_2981f15877 CHECK ((char_length(jitsu_key) <= 100)), CONSTRAINT check_3a03e7557a CHECK ((char_length(previous_default_branch) <= 4096)), + CONSTRAINT check_3ca5cbffe6 CHECK ((char_length(issue_branch_template) <= 255)), CONSTRAINT check_b09644994b CHECK ((char_length(squash_commit_template) <= 500)), CONSTRAINT check_bde223416c CHECK ((show_default_award_emojis IS NOT NULL)), CONSTRAINT check_eaf7cfb6a7 CHECK ((char_length(merge_commit_template) <= 500)) diff --git a/lib/api/entities/project.rb b/lib/api/entities/project.rb index f158695f605c3bb6b8e724731652d7034ac5eddf..9b0a472400decfbd01d74e34c81c953dcfa890a7 100644 --- a/lib/api/entities/project.rb +++ b/lib/api/entities/project.rb @@ -133,6 +133,7 @@ class Project < BasicProjectDetails expose :suggestion_commit_message expose :merge_commit_template expose :squash_commit_template + expose :issue_branch_template expose :statistics, using: 'API::Entities::ProjectStatistics', if: -> (project, options) { options[:statistics] && Ability.allowed?(options[:current_user], :read_statistics, project) } diff --git a/lib/api/helpers/projects_helpers.rb b/lib/api/helpers/projects_helpers.rb index 07b19742c2e51098995f30a545f9e4f33c625d69..c95bf0f0c218d4e4625f55a54973588baedc563e 100644 --- a/lib/api/helpers/projects_helpers.rb +++ b/lib/api/helpers/projects_helpers.rb @@ -65,6 +65,7 @@ module ProjectsHelpers optional :suggestion_commit_message, type: String, desc: 'The commit message used to apply merge request suggestions' optional :merge_commit_template, type: String, desc: 'Template used to create merge commit message' optional :squash_commit_template, type: String, desc: 'Template used to create squash commit message' + optional :issue_branch_template, type: String, desc: 'Template used to create a branch from an issue' optional :initialize_with_readme, type: Boolean, desc: "Initialize a project with a README.md" optional :ci_default_git_depth, type: Integer, desc: 'Default number of revisions for shallow cloning' optional :auto_devops_enabled, type: Boolean, desc: 'Flag indication if Auto DevOps is enabled' @@ -174,6 +175,7 @@ def self.update_params_at_least_one_of :suggestion_commit_message, :merge_commit_template, :squash_commit_template, + :issue_branch_template, :repository_storage, :packages_enabled, :service_desk_enabled, diff --git a/locale/gitlab.pot b/locale/gitlab.pot index a353ed4e5447b1f0d7938b5ad1fceec82388b610..8170c2f9f1a03b0a87417e11b59e5feccb6d91ed 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -6831,6 +6831,9 @@ msgstr "" msgid "Branch name" msgstr "" +msgid "Branch name template" +msgstr "" + msgid "Branch not loaded - %{branchId}" msgstr "" @@ -31799,6 +31802,9 @@ msgstr "" msgid "ProjectSettings|Badges" msgstr "" +msgid "ProjectSettings|Branches created from issues follow this pattern." +msgstr "" + msgid "ProjectSettings|Build, test, and deploy your changes." msgstr "" @@ -45616,6 +45622,9 @@ msgstr "" msgid "What templates can I create?" msgstr "" +msgid "What variables can I use?" +msgstr "" + msgid "What will you use this group for?" msgstr "" diff --git a/spec/controllers/projects/settings/repository_controller_spec.rb b/spec/controllers/projects/settings/repository_controller_spec.rb index 31fefee3daaa3783a815f79973e8f84d6b40ab8f..6e04e3991ab4a5943611819b00d06762ff1e07e3 100644 --- a/spec/controllers/projects/settings/repository_controller_spec.rb +++ b/spec/controllers/projects/settings/repository_controller_spec.rb @@ -168,5 +168,37 @@ end end end + + context 'when updating branch names template from issues' do + let(:branch_name_template) { 'feat/GL-%{id}-%{title}' } + + let(:request_params) { base_params.merge({ project: project_params_attributes }) } + + subject { put :update, params: request_params } + + context('with a good request') do + let(:project_params_attributes) { { issue_branch_template: branch_name_template } } + + it "updates issue_branch_template and redirect to project_settings_repository_path" do + subject + + expect(response).to redirect_to project_settings_repository_path(project) + expect(controller).to set_flash[:notice].to("Project settings were successfully updated.") + expect(project.reload.issue_branch_template).to eq(branch_name_template) + end + end + + context('with a bad input') do + let(:project_params_attributes) { { issue_branch_template: 'a' * 260 } } + + it "updates issue_branch_template and redirect to project_settings_repository_path" do + subject + + expect(response).to redirect_to project_settings_repository_path(project) + expect(controller).to set_flash[:alert].to("Project setting issue branch template is too long (maximum is 255 characters)") + expect(project.reload.issue_branch_template).to eq(nil) + end + end + end end end diff --git a/spec/features/projects/settings/branch_names_settings_spec.rb b/spec/features/projects/settings/branch_names_settings_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..fdd883bc2b6fb65693f0315b2d8f89d2528056b8 --- /dev/null +++ b/spec/features/projects/settings/branch_names_settings_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'Project settings > repositories > Branch names', :js do + let_it_be(:project) { create(:project, :public) } + let(:user) { create(:user) } + + before do + project.add_maintainer(user) + + sign_in(user) + end + + context 'when Issues are initially disabled' do + let(:project_feature) { project.project_feature } + + before do + project_feature.update!(issues_access_level: ProjectFeature::DISABLED) + visit project_settings_repository_path(project) + end + + it 'do not render the Branch names settings' do + expect(page).not_to have_content('Branch name template') + end + end + + context 'when Issues are initially enabled' do + before do + visit project_settings_repository_path(project) + end + + it 'shows the Branch names settings' do + expect(page).to have_content('Branch name template') + + value = "feature-%{id}" + + within('section#branch-defaults-settings') do + fill_in 'project[issue_branch_template]', with: value + + click_on('Save changes') + end + + expect(project.reload.issue_branch_template).to eq(value) + expect(page).to have_content('Branch name template') + end + end +end diff --git a/spec/lib/gitlab/import_export/safe_model_attributes.yml b/spec/lib/gitlab/import_export/safe_model_attributes.yml index 64fed73303f9df5be2da379b2734921f3d244294..75d980cd5f47342fee8e3f3918e159759d325f00 100644 --- a/spec/lib/gitlab/import_export/safe_model_attributes.yml +++ b/spec/lib/gitlab/import_export/safe_model_attributes.yml @@ -568,6 +568,7 @@ Project: - suggestion_commit_message - merge_commit_template - squash_commit_template +- issue_branch_template Author: - name ProjectFeature: diff --git a/spec/models/issue_spec.rb b/spec/models/issue_spec.rb index f8c731b0d1090e3a7ac867da97848a090a1c48ff..9dfe89b881454324e6cd769a26d0a2e816eb712d 100644 --- a/spec/models/issue_spec.rb +++ b/spec/models/issue_spec.rb @@ -864,7 +864,7 @@ describe '.to_branch_name' do it 'parameterizes arguments and joins with dashes' do - expect(described_class.to_branch_name(123, 'foo bar', '!@#$%', 'f!o@o#b$a%r^')).to eq('123-foo-bar-f-o-o-b-a-r') + expect(described_class.to_branch_name(123, 'foo bar!@#$%f!o@o#b$a%r^')).to eq('123-foo-bar-f-o-o-b-a-r') end it 'preserves the case in the first argument' do @@ -872,7 +872,7 @@ end it 'truncates branch name to at most 100 characters' do - expect(described_class.to_branch_name('a' * 101)).to eq('a' * 100) + expect(described_class.to_branch_name('a' * 101, 'a')).to eq('a' * 100) end it 'truncates dangling parts of the branch name' do @@ -884,6 +884,13 @@ # 100 characters would've got us "999-lorem...lacus-custom-fri". expect(branch_name).to eq('999-lorem-ipsum-dolor-sit-amet-consectetur-adipiscing-elit-mauris-sit-amet-ipsum-id-lacus-custom') end + + it 'takes issue branch template into account' do + project = create(:project) + project.project_setting.update!(issue_branch_template: 'feature-%{id}-%{title}') + + expect(described_class.to_branch_name(123, 'issue title', project: project)).to eq('feature-123-issue-title') + end end describe '#to_branch_name' do diff --git a/spec/models/project_setting_spec.rb b/spec/models/project_setting_spec.rb index eb0292c70058f1e24f6a1d8f0292e19b53e79f62..b33cf0219d3145085fecee8b93488d3fd5a4b729 100644 --- a/spec/models/project_setting_spec.rb +++ b/spec/models/project_setting_spec.rb @@ -20,6 +20,7 @@ describe 'validations' do it { is_expected.not_to allow_value(nil).for(:target_platforms) } it { is_expected.to allow_value([]).for(:target_platforms) } + it { is_expected.to validate_length_of(:issue_branch_template).is_at_most(255) } it { is_expected.not_to allow_value(nil).for(:suggested_reviewers_enabled) } it { is_expected.to allow_value(true).for(:suggested_reviewers_enabled) }