Commit 94a788f6 authored by Zeger-Jan van de Weg's avatar Zeger-Jan van de Weg

Only accept open issues and merge requests

parent 9f9f0c35
...@@ -116,7 +116,7 @@ class Projects::IssuesController < Projects::ApplicationController ...@@ -116,7 +116,7 @@ class Projects::IssuesController < Projects::ApplicationController
end end
def closed_by_merge_requests def closed_by_merge_requests
@closed_by_mr = @issue.closed_by_merge_requests(current_user) @closed_by_merge_requests ||= @issue.closed_by_merge_requests(current_user)
end end
protected protected
......
...@@ -84,7 +84,7 @@ module IssuesHelper ...@@ -84,7 +84,7 @@ module IssuesHelper
end end
def merge_requests_sentence(merge_requests) def merge_requests_sentence(merge_requests)
merge_requests.map(&:to_reference).to_sentence merge_requests.map(&:to_reference).to_sentence(last_word_connector: ', or ')
end end
# Required for Gitlab::Markdown::IssueReferenceFilter # Required for Gitlab::Markdown::IssueReferenceFilter
......
...@@ -86,6 +86,10 @@ module Issuable ...@@ -86,6 +86,10 @@ module Issuable
assignee_id_changed? assignee_id_changed?
end end
def open?
opened? || reopened?
end
# #
# Votes # Votes
# #
......
...@@ -98,11 +98,11 @@ class Issue < ActiveRecord::Base ...@@ -98,11 +98,11 @@ class Issue < ActiveRecord::Base
# From all notes on this issue, we'll select the system notes about linked # From all notes on this issue, we'll select the system notes about linked
# merge requests. Of those, the MRs closing `self` are returned. # merge requests. Of those, the MRs closing `self` are returned.
def closed_by_merge_requests(current_user) def closed_by_merge_requests(current_user = nil)
return [] unless open?
notes.system.flat_map do |note| notes.system.flat_map do |note|
ext = Gitlab::ReferenceExtractor.new(self.project, current_user) note.all_references(current_user).merge_requests
ext.analyze(note.note) end.uniq.select { |mr| mr.open? && mr.closes_issue?(self) }
ext.merge_requests
end.uniq.select { |mr| mr.closes_issue?(self) }
end end
end end
...@@ -222,10 +222,6 @@ class MergeRequest < ActiveRecord::Base ...@@ -222,10 +222,6 @@ class MergeRequest < ActiveRecord::Base
self.target_project.events.where(target_id: self.id, target_type: "MergeRequest", action: Event::CLOSED).last self.target_project.events.where(target_id: self.id, target_type: "MergeRequest", action: Event::CLOSED).last
end end
def open?
opened? || reopened?
end
def work_in_progress? def work_in_progress?
!!(title =~ /\A\[?WIP\]?:? /i) !!(title =~ /\A\[?WIP\]?:? /i)
end end
......
.issue-closed-by-widget .issue-closed-by-widget
%i.fa.fa-check = icon('check')
- if @closed_by_mr.count == 1 This issue will be closed automatically when merge request #{gfm(merge_requests_sentence(@closed_by_merge_requests.sort))} is accepted.
This issue will be closed when #{gfm(@closed_by_mr.first.to_reference)} is accepted.
-else
This issue will be closed when any of merge requests #{gfm(merge_requests_sentence(@closed_by_mr.sort))} is accepted.
...@@ -46,7 +46,7 @@ ...@@ -46,7 +46,7 @@
= markdown(@issue.description) = markdown(@issue.description)
%textarea.hidden.js-task-list-field %textarea.hidden.js-task-list-field
= @issue.description = @issue.description
- if @closed_by_mr.present? - if @closed_by_merge_requests.present?
= render 'projects/issues/closed_by_box' = render 'projects/issues/closed_by_box'
.issue-discussion .issue-discussion
= render 'projects/issues/discussion' = render 'projects/issues/discussion'
...@@ -117,4 +117,14 @@ describe IssuesHelper do ...@@ -117,4 +117,14 @@ describe IssuesHelper do
end end
end end
describe "#merge_requests_sentence" do
subject { merge_requests_sentence(merge_requests)}
let(:merge_requests) do
[ build(:merge_request, iid: 1), build(:merge_request, iid: 2),
build(:merge_request, iid: 3)]
end
it { is_expected.to eq("!1, !2, or !3") }
end
end end
...@@ -68,7 +68,6 @@ describe Issue, "Issuable" do ...@@ -68,7 +68,6 @@ describe Issue, "Issuable" do
end end
end end
describe "#to_hook_data" do describe "#to_hook_data" do
let(:hook_data) { issue.to_hook_data(user) } let(:hook_data) { issue.to_hook_data(user) }
......
...@@ -68,6 +68,43 @@ describe Issue do ...@@ -68,6 +68,43 @@ describe Issue do
end end
end end
describe '#closed_by_merge_requests' do
let(:project) { create(:project) }
let(:issue) { create(:issue, project: project, state: "opened")}
let(:closed_issue) { build(:issue, project: project, state: "closed")}
let(:mr) do
opts = {
title: 'Awesome merge_request',
description: "Fixes #{issue.to_reference}",
source_branch: 'feature',
target_branch: 'master'
}
MergeRequests::CreateService.new(project, project.owner, opts).execute
end
let(:closed_mr) do
opts = {
title: 'Awesome merge_request 2',
description: "Fixes #{issue.to_reference}",
source_branch: 'feature',
target_branch: 'master',
state: 'closed'
}
MergeRequests::CreateService.new(project, project.owner, opts).execute
end
it 'returns the merge request to close this issue' do
allow(mr).to receive(:closes_issue?).with(issue).and_return(true)
expect(issue.closed_by_merge_requests).to eq([mr])
end
it "returns an empty array when the current issue is closed already" do
expect(closed_issue.closed_by_merge_requests).to eq([])
end
end
it_behaves_like 'an editable mentionable' do it_behaves_like 'an editable mentionable' do
subject { create(:issue) } subject { create(:issue) }
......
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