2019-03-30 03:15:48 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-07 09:20:57 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe ProcessCommitWorker do
|
2016-10-07 09:20:57 -04:00
|
|
|
let(:worker) { described_class.new }
|
|
|
|
let(:user) { create(:user) }
|
2017-03-27 16:42:08 -04:00
|
|
|
let(:project) { create(:project, :public, :repository) }
|
2016-10-07 09:20:57 -04:00
|
|
|
let(:issue) { create(:issue, project: project, author: user) }
|
|
|
|
let(:commit) { project.commit }
|
|
|
|
|
|
|
|
describe '#perform' do
|
|
|
|
it 'does not process the commit when the project does not exist' do
|
|
|
|
expect(worker).not_to receive(:close_issues)
|
|
|
|
|
2016-11-24 09:07:44 -05:00
|
|
|
worker.perform(-1, user.id, commit.to_hash)
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not process the commit when the user does not exist' do
|
|
|
|
expect(worker).not_to receive(:close_issues)
|
|
|
|
|
2016-11-24 09:07:44 -05:00
|
|
|
worker.perform(project.id, -1, commit.to_hash)
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|
|
|
|
|
2020-04-27 17:10:10 -04:00
|
|
|
include_examples 'an idempotent worker' do
|
|
|
|
subject do
|
|
|
|
perform_multiple([project.id, user.id, commit.to_hash], worker: worker)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'processes the commit message' do
|
|
|
|
expect(worker).to receive(:process_commit_message)
|
|
|
|
.exactly(IdempotentWorkerHelper::WORKER_EXEC_TIMES)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the issue metrics' do
|
|
|
|
expect(worker).to receive(:update_issue_metrics)
|
|
|
|
.exactly(IdempotentWorkerHelper::WORKER_EXEC_TIMES)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#process_commit_message' do
|
|
|
|
context 'when pushing to the default branch' do
|
2018-03-05 09:15:58 -05:00
|
|
|
before do
|
2018-02-12 09:02:15 -05:00
|
|
|
allow(commit).to receive(:safe_message).and_return("Closes #{issue.to_reference}")
|
2018-03-05 09:15:58 -05:00
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
|
2018-03-05 09:15:58 -05:00
|
|
|
it 'closes issues that should be closed per the commit message' do
|
2018-02-12 09:02:15 -05:00
|
|
|
expect(worker).to receive(:close_issues).with(project, user, user, commit, [issue])
|
2016-10-07 09:20:57 -04:00
|
|
|
|
|
|
|
worker.process_commit_message(project, commit, user, user, true)
|
|
|
|
end
|
2018-03-05 09:15:58 -05:00
|
|
|
|
|
|
|
it 'creates cross references' do
|
|
|
|
expect(commit).to receive(:create_cross_references!).with(user, [issue])
|
|
|
|
|
|
|
|
worker.process_commit_message(project, commit, user, user, true)
|
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pushing to a non-default branch' do
|
|
|
|
it 'does not close any issues' do
|
2018-02-12 09:02:15 -05:00
|
|
|
allow(commit).to receive(:safe_message).and_return("Closes #{issue.to_reference}")
|
2016-10-07 09:20:57 -04:00
|
|
|
|
|
|
|
expect(worker).not_to receive(:close_issues)
|
|
|
|
|
|
|
|
worker.process_commit_message(project, commit, user, user, false)
|
|
|
|
end
|
2018-03-05 09:15:58 -05:00
|
|
|
|
|
|
|
it 'does not create cross references' do
|
|
|
|
expect(commit).to receive(:create_cross_references!).with(user, [])
|
|
|
|
|
|
|
|
worker.process_commit_message(project, commit, user, user, false)
|
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|
|
|
|
|
2018-03-05 09:15:58 -05:00
|
|
|
context 'when commit is a merge request merge commit to the default branch' do
|
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request,
|
|
|
|
description: "Closes #{issue.to_reference}",
|
|
|
|
source_branch: 'feature-merged',
|
|
|
|
target_branch: 'master',
|
|
|
|
source_project: project)
|
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
|
2018-03-05 09:15:58 -05:00
|
|
|
let(:commit) do
|
|
|
|
project.repository.create_branch('feature-merged', 'feature')
|
2019-11-15 10:06:12 -05:00
|
|
|
project.repository.after_create_branch
|
2018-03-05 09:15:58 -05:00
|
|
|
|
|
|
|
MergeRequests::MergeService
|
2021-05-11 23:10:21 -04:00
|
|
|
.new(project: project, current_user: merge_request.author, params: { sha: merge_request.diff_head_sha })
|
2018-03-05 09:15:58 -05:00
|
|
|
.execute(merge_request)
|
|
|
|
|
|
|
|
merge_request.reload.merge_commit
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not close any issues from the commit message' do
|
|
|
|
expect(worker).not_to receive(:close_issues)
|
|
|
|
|
|
|
|
worker.process_commit_message(project, commit, user, user, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'still creates cross references' do
|
|
|
|
expect(commit).to receive(:create_cross_references!).with(user, [])
|
|
|
|
|
|
|
|
worker.process_commit_message(project, commit, user, user, true)
|
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#close_issues' do
|
2022-09-14 08:12:34 -04:00
|
|
|
it 'creates Issue::CloseWorker jobs' do
|
|
|
|
expect do
|
2016-10-07 09:20:57 -04:00
|
|
|
worker.close_issues(project, user, user, commit, [issue])
|
2022-09-14 08:12:34 -04:00
|
|
|
end.to change(Issues::CloseWorker.jobs, :size).by(1)
|
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|
|
|
|
|
2021-06-10 14:10:05 -04:00
|
|
|
describe '#update_issue_metrics', :clean_gitlab_redis_cache do
|
2019-12-04 13:08:46 -05:00
|
|
|
context 'when commit has issue reference' do
|
|
|
|
subject(:update_metrics_and_reload) do
|
|
|
|
-> {
|
|
|
|
worker.update_issue_metrics(commit, user)
|
|
|
|
issue.metrics.reload
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(commit).to receive(:safe_message).and_return("Closes #{issue.to_reference}")
|
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
|
2019-12-04 13:08:46 -05:00
|
|
|
context 'when issue has no first_mentioned_in_commit_at set' do
|
|
|
|
it 'updates issue metrics' do
|
|
|
|
expect(update_metrics_and_reload)
|
|
|
|
.to change { issue.metrics.first_mentioned_in_commit_at }.to(commit.committed_date)
|
|
|
|
end
|
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
|
2019-12-04 13:08:46 -05:00
|
|
|
context 'when issue has first_mentioned_in_commit_at earlier than given committed_date' do
|
|
|
|
before do
|
2020-08-03 14:10:05 -04:00
|
|
|
issue.metrics.update!(first_mentioned_in_commit_at: commit.committed_date - 1.day)
|
2019-12-04 13:08:46 -05:00
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
|
2019-12-04 13:08:46 -05:00
|
|
|
it "doesn't update issue metrics" do
|
|
|
|
expect(update_metrics_and_reload).not_to change { issue.metrics.first_mentioned_in_commit_at }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when issue has first_mentioned_in_commit_at later than given committed_date' do
|
|
|
|
before do
|
2020-08-03 14:10:05 -04:00
|
|
|
issue.metrics.update!(first_mentioned_in_commit_at: commit.committed_date + 1.day)
|
2019-12-04 13:08:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't update issue metrics" do
|
|
|
|
expect(update_metrics_and_reload)
|
|
|
|
.to change { issue.metrics.first_mentioned_in_commit_at }.to(commit.committed_date)
|
|
|
|
end
|
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|
2017-04-04 06:54:58 -04:00
|
|
|
|
2019-12-04 13:08:46 -05:00
|
|
|
context 'when commit has no issue references' do
|
|
|
|
it "doesn't execute any queries with false conditions" do
|
|
|
|
allow(commit).to receive(:safe_message).and_return("Lorem Ipsum")
|
2017-04-04 06:54:58 -04:00
|
|
|
|
2019-12-04 13:08:46 -05:00
|
|
|
expect { worker.update_issue_metrics(commit, user) }
|
|
|
|
.not_to make_queries_matching(/WHERE (?:1=0|0=1)/)
|
|
|
|
end
|
2017-04-04 06:54:58 -04:00
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|
|
|
|
end
|