2016-10-07 09:20:57 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe ProcessCommitWorker do
|
2018-07-10 12:11:31 -04:00
|
|
|
include ProjectForksHelper
|
|
|
|
|
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
|
|
|
|
|
|
|
|
it 'processes the commit message' do
|
|
|
|
expect(worker).to receive(:process_commit_message).and_call_original
|
|
|
|
|
2016-11-24 09:07:44 -05:00
|
|
|
worker.perform(project.id, user.id, commit.to_hash)
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the issue metrics' do
|
|
|
|
expect(worker).to receive(:update_issue_metrics).and_call_original
|
|
|
|
|
2016-11-24 09:07:44 -05:00
|
|
|
worker.perform(project.id, user.id, commit.to_hash)
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|
2017-05-18 16:17:58 -04:00
|
|
|
|
2018-07-10 12:11:31 -04:00
|
|
|
context 'when the project is forked' do
|
|
|
|
context 'when commit already exists in the upstream project' do
|
|
|
|
it 'does not process the commit message' do
|
|
|
|
forked = fork_project(project, user, repository: true)
|
|
|
|
|
|
|
|
expect(worker).not_to receive(:process_commit_message)
|
|
|
|
|
|
|
|
worker.perform(forked.id, user.id, forked.commit.to_hash)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the commit does not exist in the upstream project' do
|
|
|
|
it 'processes the commit message' do
|
|
|
|
empty_project = create(:project, :public)
|
|
|
|
forked = fork_project(empty_project, user, repository: true)
|
|
|
|
|
|
|
|
TestEnv.copy_repo(forked,
|
|
|
|
bare_repo: TestEnv.factory_repo_path_bare,
|
|
|
|
refs: TestEnv::BRANCH_SHA)
|
|
|
|
|
|
|
|
expect(worker).to receive(:process_commit_message)
|
|
|
|
|
|
|
|
worker.perform(forked.id, user.id, forked.commit.to_hash)
|
|
|
|
end
|
|
|
|
end
|
2017-05-18 16:17:58 -04:00
|
|
|
|
2018-07-10 12:11:31 -04:00
|
|
|
context 'when the upstream project no longer exists' do
|
|
|
|
it 'processes the commit message' do
|
|
|
|
forked = fork_project(project, user, repository: true)
|
|
|
|
project.destroy!
|
2017-05-18 16:17:58 -04:00
|
|
|
|
2018-07-10 12:11:31 -04:00
|
|
|
expect(worker).to receive(:process_commit_message)
|
2017-05-18 16:17:58 -04:00
|
|
|
|
2018-07-10 12:11:31 -04:00
|
|
|
worker.perform(forked.id, user.id, forked.commit.to_hash)
|
|
|
|
end
|
2017-05-18 16:17:58 -04:00
|
|
|
end
|
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
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')
|
|
|
|
|
|
|
|
MergeRequests::MergeService
|
|
|
|
.new(project, merge_request.author)
|
|
|
|
.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
|
|
|
|
context 'when the user can update the issues' do
|
|
|
|
it 'closes the issues' do
|
|
|
|
worker.close_issues(project, user, user, commit, [issue])
|
|
|
|
|
|
|
|
issue.reload
|
|
|
|
|
|
|
|
expect(issue.closed?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user can not update the issues' do
|
|
|
|
it 'does not close the issues' do
|
|
|
|
other_user = create(:user)
|
|
|
|
|
|
|
|
worker.close_issues(project, other_user, other_user, commit, [issue])
|
|
|
|
|
|
|
|
issue.reload
|
|
|
|
|
|
|
|
expect(issue.closed?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#update_issue_metrics' do
|
|
|
|
it 'updates any existing issue metrics' 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
|
|
|
|
|
|
|
worker.update_issue_metrics(commit, user)
|
|
|
|
|
|
|
|
metric = Issue::Metrics.first
|
|
|
|
|
|
|
|
expect(metric.first_mentioned_in_commit_at).to eq(commit.committed_date)
|
|
|
|
end
|
2017-04-04 06:54:58 -04:00
|
|
|
|
|
|
|
it "doesn't execute any queries with false conditions" do
|
2018-02-12 09:02:15 -05:00
|
|
|
allow(commit).to receive(:safe_message).and_return("Lorem Ipsum")
|
2017-04-04 06:54:58 -04:00
|
|
|
|
2018-02-12 09:02:15 -05:00
|
|
|
expect { worker.update_issue_metrics(commit, user) }
|
|
|
|
.not_to make_queries_matching(/WHERE (?:1=0|0=1)/)
|
2017-04-04 06:54:58 -04:00
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|
2016-11-24 09:07:44 -05:00
|
|
|
|
|
|
|
describe '#build_commit' do
|
|
|
|
it 'returns a Commit' do
|
|
|
|
commit = worker.build_commit(project, id: '123')
|
|
|
|
|
|
|
|
expect(commit).to be_an_instance_of(Commit)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses date strings into Time instances' do
|
2018-02-12 09:02:15 -05:00
|
|
|
commit = worker.build_commit(project,
|
|
|
|
id: '123',
|
|
|
|
authored_date: Time.now.to_s)
|
2016-11-24 09:07:44 -05:00
|
|
|
|
|
|
|
expect(commit.authored_date).to be_an_instance_of(Time)
|
|
|
|
end
|
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|