Commit 57f4b6cb authored by ng's avatar ng

make sure we fetch from correctly AND a user can also be member of the group

parent 6c22418f
Pipeline #10634 passed with stage
in 4 minutes and 35 seconds
......@@ -35,7 +35,7 @@ module SchleuderGitlabTicketing
labels = Set.new(ticket.labels)
desc << "\n\nSubject: #{title}"
comment_ticket(ticket_id,desc)
assignee_id = mail.from.addresses.collect{|addr| team_member_id(addr) }.compact.first
assignee_id = team_member_id(mail.from.first)
if assignee_id && ((as = ticket.assignee).nil? || as.id != assignee_id)
updates[:assignee_id] = assignee_id
end
......@@ -81,7 +81,7 @@ module SchleuderGitlabTicketing
private
def desc(mail)
res = []
res << "From: #{mail.from.addresses.join(',')}"
res << "From: #{mail.from.first}"
res << "Message-Id: #{mail.message_id}"
res.join("\n\n")
end
......@@ -136,7 +136,7 @@ module SchleuderGitlabTicketing
def team_member_id(email)
if user_id = find_user_id(email)
user_id if gitlab.team_member(project.id, user_id)
user_id if project_member?(project, user_id)
else
nil
end
......@@ -144,6 +144,16 @@ module SchleuderGitlabTicketing
nil
end
def project_member?(project, user_id)
begin
gitlab.team_member(project.id, user_id)
rescue Gitlab::Error::NotFound
gitlab.group_member(project.namespace.id, user_id)
end
rescue Gitlab::Error::NotFound => e
nil
end
def find_user_id(username_or_email)
users = gitlab.user_search(username_or_email)
# did we look for an email => exact match
......@@ -162,7 +172,7 @@ module SchleuderGitlabTicketing
def ignore_mail?(mail)
return true if mail.respond_to?(:[]) && mail['X-Spam-Flag'].to_s.downcase == 'yes'
return true if sender_filters.any?{|s| mail.from.addresses.any?{|a| a =~ /#{s}/ } }
return true if sender_filters.any?{|s| mail.from.first =~ /#{s}/ }
return true if subject_filters.any?{|s| mail.subject =~ /#{s}/ }
false
end
......
......@@ -73,6 +73,7 @@ describe SchleuderGitlabTicketing::List do
allow(list1_project).to receive(:name).and_return('tickets')
allow(list1_project).to receive(:id).and_return(123)
allow(list1_project).to receive_message_chain(:namespace,:name).and_return('support')
allow(list1_project).to receive_message_chain(:namespace,:id).and_return(91)
allow(gitlab).to receive(:search_projects).with('tickets').and_return([list1_project])
end
context 'ignores spam' do
......@@ -178,6 +179,27 @@ describe SchleuderGitlabTicketing::List do
expect(m.subject).to eql('question [gl/tickets#42]')
end
it 'updates an existing ticket and assignes it to the member if not member of project but group and puts it into inprocess' do
m = MailStub.new('member@example.com','question [gl/tickets#42]')
user = double
expect(user).to receive(:id).and_return(99)
expect(gitlab).to receive(:user_search).with('member@example.com').and_return([user])
allow_any_instance_of(Gitlab::Error::NotFound).to receive(:build_error_message).and_return('bla')
expect(gitlab).to receive(:team_member).with(123,99).and_raise(Gitlab::Error::NotFound,'404 Not found')
expect(gitlab).to receive(:group_member).with(91,99).and_return([user])
ticket = double
expect(ticket).not_to receive(:iid)
expect(ticket).to receive(:state).and_return('open')
expect(ticket).to receive(:labels).twice.and_return([])
expect(ticket).to receive(:assignee).and_return(nil)
expect(gitlab).to receive(:issue).with(123,42).and_return(ticket)
expect(gitlab).to receive(:create_issue_note).with(123,42, "From: member@example.com\n\nMessage-Id: 1-abc-3\n\nSubject: question")
expect(gitlab).to receive(:edit_issue).with(123,42,{ labels: 'inprocess', assignee_id: 99 })
expect(list1.process(m)).to be(true)
expect(m.subject).to eql('question [gl/tickets#42]')
end
it 'updates an existing ticket and won\'t assign it if user is not team_member' do
m = MailStub.new('member3@example.com','question [gl/tickets#42]')
user = double
......@@ -185,6 +207,7 @@ describe SchleuderGitlabTicketing::List do
expect(gitlab).to receive(:user_search).with('member3@example.com').and_return([user])
allow_any_instance_of(Gitlab::Error::NotFound).to receive(:build_error_message).and_return('bla')
expect(gitlab).to receive(:team_member).with(123,95).and_raise(Gitlab::Error::NotFound,'404 Not found')
expect(gitlab).to receive(:group_member).with(91,95).and_raise(Gitlab::Error::NotFound,'404 Not found')
ticket = double
expect(ticket).not_to receive(:iid)
expect(ticket).to receive(:state).and_return('open')
......@@ -197,6 +220,7 @@ describe SchleuderGitlabTicketing::List do
expect(list1.process(m)).to be(true)
expect(m.subject).to eql('question [gl/tickets#42]')
end
it 'updates an existing ticket and reassing it if already assigned' do
m = MailStub.new('member2@example.com','question [gl/tickets#42]')
user = double
......
......@@ -17,19 +17,9 @@ class MailStub
end
def from
@from_stub ||= FromStub.new(@from)
@from_arr ||= Array(@from)
end
def original_message
end
end
class FromStub
def initialize(from)
@addr = from
end
def addresses
Array(@addr)
end
end
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