Commit 67ea6389 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'issue_19262' into 'master'

Prevent commits from upstream repositories to be re-processed by forks

Closes #19262

See merge request !11511
parents 3ba9702d 00549635
...@@ -17,6 +17,7 @@ class ProcessCommitWorker ...@@ -17,6 +17,7 @@ class ProcessCommitWorker
project = Project.find_by(id: project_id) project = Project.find_by(id: project_id)
return unless project return unless project
return if commit_exists_in_upstream?(project, commit_hash)
user = User.find_by(id: user_id) user = User.find_by(id: user_id)
...@@ -24,8 +25,6 @@ class ProcessCommitWorker ...@@ -24,8 +25,6 @@ class ProcessCommitWorker
commit = build_commit(project, commit_hash) commit = build_commit(project, commit_hash)
return unless commit.matches_cross_reference_regex?
author = commit.author || user author = commit.author || user
process_commit_message(project, commit, user, author, default) process_commit_message(project, commit, user, author, default)
...@@ -76,4 +75,16 @@ class ProcessCommitWorker ...@@ -76,4 +75,16 @@ class ProcessCommitWorker
Commit.from_hash(hash, project) Commit.from_hash(hash, project)
end end
private
# Avoid reprocessing commits that already exist in the upstream
# when project is forked. This will also prevent duplicated system notes.
def commit_exists_in_upstream?(project, commit_hash)
return false unless project.forked?
upstream_project = project.forked_from_project
commit_id = commit_hash.with_indifferent_access[:id]
upstream_project.commit(commit_id).present?
end
end end
---
title: Prevent commits from upstream repositories to be re-processed by forks
merge_request:
author:
...@@ -20,14 +20,6 @@ describe ProcessCommitWorker do ...@@ -20,14 +20,6 @@ describe ProcessCommitWorker do
worker.perform(project.id, -1, commit.to_hash) worker.perform(project.id, -1, commit.to_hash)
end end
it 'does not process the commit when no issues are referenced' do
allow(worker).to receive(:build_commit).and_return(double(matches_cross_reference_regex?: false))
expect(worker).not_to receive(:process_commit_message)
worker.perform(project.id, user.id, commit.to_hash)
end
it 'processes the commit message' do it 'processes the commit message' do
expect(worker).to receive(:process_commit_message).and_call_original expect(worker).to receive(:process_commit_message).and_call_original
...@@ -39,6 +31,18 @@ describe ProcessCommitWorker do ...@@ -39,6 +31,18 @@ describe ProcessCommitWorker do
worker.perform(project.id, user.id, commit.to_hash) worker.perform(project.id, user.id, commit.to_hash)
end end
context 'when commit already exists in upstream project' do
let(:forked) { create(:project, :public) }
it 'does not process commit message' do
create(:forked_project_link, forked_to_project: forked, forked_from_project: project)
expect(worker).not_to receive(:process_commit_message)
worker.perform(forked.id, user.id, forked.commit.to_hash)
end
end
end end
describe '#process_commit_message' do describe '#process_commit_message' 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