Commit 1d2ef4c6 authored by Douwe Maan's avatar Douwe Maan

Merge branch '3845-committers-cannot-approve-mr-ce' into 'master'

Add committers and authors methods on MergeRequest

See merge request gitlab-org/gitlab-ce!24204
parents 9c8c9ab1 a2842241
...@@ -19,6 +19,12 @@ class CommitCollection ...@@ -19,6 +19,12 @@ class CommitCollection
commits.each(&block) commits.each(&block)
end end
def committers
emails = commits.reject(&:merge_commit?).map(&:committer_email).uniq
User.by_any_email(emails)
end
# Sets the pipeline status for every commit. # Sets the pipeline status for every commit.
# #
# Setting this status ahead of time removes the need for running a query for # Setting this status ahead of time removes the need for running a query for
......
...@@ -284,6 +284,14 @@ class MergeRequest < ActiveRecord::Base ...@@ -284,6 +284,14 @@ class MergeRequest < ActiveRecord::Base
work_in_progress?(title) ? title : "WIP: #{title}" work_in_progress?(title) ? title : "WIP: #{title}"
end end
def committers
@committers ||= commits.committers
end
def authors
User.from_union([committers, User.where(id: self.author_id)])
end
# Verifies if title has changed not taking into account WIP prefix # Verifies if title has changed not taking into account WIP prefix
# for merge requests. # for merge requests.
def wipless_title_changed(old_title) def wipless_title_changed(old_title)
...@@ -327,13 +335,15 @@ class MergeRequest < ActiveRecord::Base ...@@ -327,13 +335,15 @@ class MergeRequest < ActiveRecord::Base
end end
def commits def commits
if persisted? return merge_request_diff.commits if persisted?
merge_request_diff.commits
elsif compare_commits commits_arr = if compare_commits
compare_commits.reverse compare_commits.reverse
else else
[] []
end end
CommitCollection.new(source_project, commits_arr, source_branch)
end end
def commits_count def commits_count
......
...@@ -71,7 +71,7 @@ describe Projects::MergeRequests::CreationsController do ...@@ -71,7 +71,7 @@ describe Projects::MergeRequests::CreationsController do
expect(response).to be_success expect(response).to be_success
total = assigns(:total_commit_count) total = assigns(:total_commit_count)
expect(assigns(:commits)).to be_an Array expect(assigns(:commits)).to be_an CommitCollection
expect(total).to be > 0 expect(total).to be > 0
expect(assigns(:hidden_commit_count)).to eq(0) expect(assigns(:hidden_commit_count)).to eq(0)
expect(response).to have_gitlab_http_status(200) expect(response).to have_gitlab_http_status(200)
......
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
describe CommitCollection do describe CommitCollection do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:commit) { project.commit } let(:commit) { project.commit("c1c67abbaf91f624347bb3ae96eabe3a1b742478") }
describe '#each' do describe '#each' do
it 'yields every commit' do it 'yields every commit' do
...@@ -12,6 +12,29 @@ describe CommitCollection do ...@@ -12,6 +12,29 @@ describe CommitCollection do
end end
end end
describe '.committers' do
it 'returns a relation of users when users are found' do
user = create(:user, email: commit.committer_email.upcase)
collection = described_class.new(project, [commit])
expect(collection.committers).to contain_exactly(user)
end
it 'returns empty array when committers cannot be found' do
collection = described_class.new(project, [commit])
expect(collection.committers).to be_empty
end
it 'excludes authors of merge commits' do
commit = project.commit("60ecb67744cb56576c30214ff52294f8ce2def98")
create(:user, email: commit.committer_email.upcase)
collection = described_class.new(project, [commit])
expect(collection.committers).to be_empty
end
end
describe '#with_pipeline_status' do describe '#with_pipeline_status' do
it 'sets the pipeline status for every commit so no additional queries are necessary' do it 'sets the pipeline status for every commit so no additional queries are necessary' do
create( create(
......
...@@ -991,6 +991,34 @@ describe MergeRequest do ...@@ -991,6 +991,34 @@ describe MergeRequest do
end end
end end
describe '#committers' do
it 'returns all the committers of every commit in the merge request' do
users = subject.commits.map(&:committer_email).uniq.map do |email|
create(:user, email: email)
end
expect(subject.committers).to match_array(users)
end
it 'returns an empty array if no committer is associated with a user' do
expect(subject.committers).to be_empty
end
end
describe '#authors' do
it 'returns a list with all the committers in the merge request and author' do
users = subject.commits.map(&:committer_email).uniq.map do |email|
create(:user, email: email)
end
expect(subject.authors).to match_array([subject.author, *users])
end
it 'returns only the author if no committer is associated with a user' do
expect(subject.authors).to contain_exactly(subject.author)
end
end
describe '#hook_attrs' do describe '#hook_attrs' do
it 'delegates to Gitlab::HookData::MergeRequestBuilder#build' do it 'delegates to Gitlab::HookData::MergeRequestBuilder#build' do
builder = double builder = double
......
...@@ -509,7 +509,7 @@ describe MergeRequests::RefreshService do ...@@ -509,7 +509,7 @@ describe MergeRequests::RefreshService do
committed_date: Time.now committed_date: Time.now
) )
allow_any_instance_of(MergeRequest).to receive(:commits).and_return([commit]) allow_any_instance_of(MergeRequest).to receive(:commits).and_return(CommitCollection.new(@project, [commit], 'feature'))
end end
context 'when the merge request is sourced from the same project' do context 'when the merge request is sourced from the same project' 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