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

Use the correct email address when committing via a file service

parent 983bd38f
...@@ -7,8 +7,10 @@ module Files ...@@ -7,8 +7,10 @@ module Files
def initialize(*args) def initialize(*args)
super super
@author_email = params[:author_email] || current_user&.email git_user = Gitlab::Git::User.from_gitlab(current_user) if current_user.present?
@author_name = params[:author_name] || current_user&.name
@author_email = params[:author_email] || git_user&.email
@author_name = params[:author_name] || git_user&.name
@commit_message = params[:commit_message] @commit_message = params[:commit_message]
@last_commit_sha = params[:last_commit_sha] @last_commit_sha = params[:last_commit_sha]
......
...@@ -58,6 +58,14 @@ FactoryBot.define do ...@@ -58,6 +58,14 @@ FactoryBot.define do
project_view :readme project_view :readme
end end
trait :commit_email do
after(:create) do |user, evaluator|
additional = create(:email, :confirmed, user: user, email: "commit-#{user.email}")
user.update!(commit_email: additional.email)
end
end
factory :omniauth_user do factory :omniauth_user do
transient do transient do
extern_uid '123456' extern_uid '123456'
......
...@@ -3,7 +3,7 @@ require "spec_helper" ...@@ -3,7 +3,7 @@ require "spec_helper"
describe Files::CreateService do describe Files::CreateService do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:repository) { project.repository } let(:repository) { project.repository }
let(:user) { create(:user) } let(:user) { create(:user, :commit_email) }
let(:file_content) { 'Test file content' } let(:file_content) { 'Test file content' }
let(:branch_name) { project.default_branch } let(:branch_name) { project.default_branch }
let(:start_branch) { branch_name } let(:start_branch) { branch_name }
...@@ -20,6 +20,8 @@ describe Files::CreateService do ...@@ -20,6 +20,8 @@ describe Files::CreateService do
} }
end end
let(:commit) { repository.head_commit }
subject { described_class.new(project, user, commit_params) } subject { described_class.new(project, user, commit_params) }
before do before do
...@@ -75,4 +77,15 @@ describe Files::CreateService do ...@@ -75,4 +77,15 @@ describe Files::CreateService do
end end
end end
end end
context 'commit attribute' do
let(:file_path) { 'test-commit-attributes.txt' }
it 'uses the commit email' do
subject.execute
expect(commit.author_email).to eq(user.commit_email)
expect(commit.committer_email).to eq(user.commit_email)
end
end
end end
...@@ -8,6 +8,7 @@ describe Files::DeleteService do ...@@ -8,6 +8,7 @@ describe Files::DeleteService do
let(:file_path) { 'files/ruby/popen.rb' } let(:file_path) { 'files/ruby/popen.rb' }
let(:branch_name) { project.default_branch } let(:branch_name) { project.default_branch }
let(:last_commit_sha) { nil } let(:last_commit_sha) { nil }
let(:commit) { project.repository.head_commit }
let(:commit_params) do let(:commit_params) do
{ {
...@@ -34,6 +35,13 @@ describe Files::DeleteService do ...@@ -34,6 +35,13 @@ describe Files::DeleteService do
expect(blob).to be_nil expect(blob).to be_nil
end end
it 'uses the commit email' do
subject.execute
expect(commit.author_email).to eq(user.commit_email)
expect(commit.committer_email).to eq(user.commit_email)
end
end end
before do before do
......
...@@ -9,6 +9,7 @@ describe Files::UpdateService do ...@@ -9,6 +9,7 @@ describe Files::UpdateService do
let(:new_contents) { 'New Content' } let(:new_contents) { 'New Content' }
let(:branch_name) { project.default_branch } let(:branch_name) { project.default_branch }
let(:last_commit_sha) { nil } let(:last_commit_sha) { nil }
let(:commit) { project.repository.commit }
let(:commit_params) do let(:commit_params) do
{ {
...@@ -54,6 +55,13 @@ describe Files::UpdateService do ...@@ -54,6 +55,13 @@ describe Files::UpdateService do
expect(results.data).to eq(new_contents) expect(results.data).to eq(new_contents)
end end
it 'uses the commit email' do
subject.execute
expect(commit.author_email).to eq(user.commit_email)
expect(commit.committer_email).to eq(user.commit_email)
end
end end
context "when the last_commit_sha is not supplied" do context "when the last_commit_sha is not supplied" do
......
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