Commit 3ff3b414 authored by Nick Thomas's avatar Nick Thomas

Track the previous default branch for a project when it changes

parent 189c2075
...@@ -416,6 +416,7 @@ class Project < ApplicationRecord ...@@ -416,6 +416,7 @@ class Project < ApplicationRecord
prefix: :import, to: :import_state, allow_nil: true prefix: :import, to: :import_state, allow_nil: true
delegate :squash_always?, :squash_never?, :squash_enabled_by_default?, :squash_readonly?, to: :project_setting delegate :squash_always?, :squash_never?, :squash_enabled_by_default?, :squash_readonly?, to: :project_setting
delegate :squash_option, to: :project_setting delegate :squash_option, to: :project_setting
delegate :previous_default_branch, :previous_default_branch=, to: :project_setting
delegate :no_import?, to: :import_state, allow_nil: true delegate :no_import?, to: :import_state, allow_nil: true
delegate :name, to: :owner, allow_nil: true, prefix: true delegate :name, to: :owner, allow_nil: true, prefix: true
delegate :members, to: :team, prefix: true delegate :members, to: :team, prefix: true
......
...@@ -66,6 +66,8 @@ module Projects ...@@ -66,6 +66,8 @@ module Projects
previous_default_branch = project.default_branch previous_default_branch = project.default_branch
if project.change_head(params[:default_branch]) if project.change_head(params[:default_branch])
params[:previous_default_branch] = previous_default_branch
after_default_branch_change(previous_default_branch) after_default_branch_change(previous_default_branch)
else else
raise ValidationError, s_("UpdateProject|Could not set the default branch") raise ValidationError, s_("UpdateProject|Could not set the default branch")
......
...@@ -200,17 +200,32 @@ RSpec.describe Projects::UpdateService do ...@@ -200,17 +200,32 @@ RSpec.describe Projects::UpdateService do
context 'when updating a default branch' do context 'when updating a default branch' do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
it 'changes a default branch' do it 'changes default branch, tracking the previous branch' do
previous_default_branch = project.default_branch
update_project(project, admin, default_branch: 'feature') update_project(project, admin, default_branch: 'feature')
expect(Project.find(project.id).default_branch).to eq 'feature' project.reload
expect(project.default_branch).to eq('feature')
expect(project.previous_default_branch).to eq(previous_default_branch)
update_project(project, admin, default_branch: previous_default_branch)
project.reload
expect(project.default_branch).to eq(previous_default_branch)
expect(project.previous_default_branch).to eq('feature')
end end
it 'does not change a default branch' do it 'does not change a default branch' do
# The branch 'unexisted-branch' does not exist. # The branch 'unexisted-branch' does not exist.
update_project(project, admin, default_branch: 'unexisted-branch') update_project(project, admin, default_branch: 'unexisted-branch')
expect(Project.find(project.id).default_branch).to eq 'master' project.reload
expect(project.default_branch).to eq 'master'
expect(project.previous_default_branch).to be_nil
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