Skip to content

Commit

Permalink
[api] add a return-to header to the event mails
Browse files Browse the repository at this point in the history
  • Loading branch information
coolo committed Dec 9, 2013
1 parent 2f4d44f commit fc4ee9f
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 13 deletions.
8 changes: 7 additions & 1 deletion src/api/app/mailers/event_mailer.rb
Expand Up @@ -17,8 +17,14 @@ def event(user, e)
headers(e.custom_headers)

template_name = e.template_name
mail(to: user.email,
orig = e.originator
to = user.email
# no need to tell user about this own actions
# TODO: make configurable?
return if orig == to
mail(to: to,
subject: e.subject,
reply_to: orig,
from: e.mail_sender,
template_name: template_name)
end
Expand Down
12 changes: 12 additions & 0 deletions src/api/app/models/event/base.rb
Expand Up @@ -163,6 +163,18 @@ def mail_sender
'obs-email@opensuse.org'
end

def payload_address(field)
if payload[field]
u = User.find_by_login(payload[field])
return u.email if u
end
nil
end

def originator
payload_address('sender') || mail_sender
end

def template_name
self.class.name.gsub('Event::', '').underscore
end
Expand Down
7 changes: 6 additions & 1 deletion src/api/app/models/event/comment.rb
Expand Up @@ -7,10 +7,15 @@ def self.included(base)
end

def expanded_payload
p = payload
p = payload.dup
p['commenter'] = User.find(p['commenter'])
p
end

def originator
User.find(payload['commenter']).email
end

end

class Event::CommentForProject < ::Event::Project
Expand Down
4 changes: 4 additions & 0 deletions src/api/app/models/event/request.rb
Expand Up @@ -12,6 +12,10 @@ def my_message_id
Event::Request.message_id(payload['id'])
end

def originator
payload_address('who') || mail_sender
end

def custom_headers
mid = my_message_id
h = super
Expand Down
@@ -1,9 +1,10 @@
From: obs-email@opensuse.org
Reply-To: tschmidt@example.com
To: Iggy@pop.org
message-id: test@localhost
In-Reply-To: obs-request-REQUESTID@localhost
References: obs-request-REQUESTID@localhost
Subject: Request state of REQUESTID (set_bugowner home:tom) changed to revoked
Subject: Request state of REQUESTID (set_bugowner home:tom) changed to declined
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Expand All @@ -15,15 +16,15 @@ Auto-Submitted: auto-generated
X-OBS-event-type: request_statechange
X-OBS-Request-Creator: Iggy
X-OBS-Request-Id: REQUESTID
X-OBS-Request-State: revoked
X-OBS-Request-State: declined
X-OBS-Request-Action-type: set_bugowner
X-OBS-Request-Action-target: home:tom

Visit http://localhost/request/show/REQUESTID

State of request REQUESTID was changed by Iggy:
State of request REQUESTID was changed by tom:

new -> revoked
new -> declined

Actions:
- set the bugowner of project home:tom to Iggy
Expand Down
5 changes: 3 additions & 2 deletions src/api/test/fixtures/event_mailer/comment_event
@@ -1,7 +1,8 @@
From: obs-email@opensuse.org
Reply-To: tschmidt@example.com
To: Iggy@pop.org
Message-ID: <test@localhost>
Subject: New comment in request 4 by Iggy
Subject: New comment in request 4 by tom
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Expand All @@ -14,7 +15,7 @@ X-OBS-event-type: comment_for_request

Visit http://localhost/request/show/4

Iggy Pop wrote in request 4:
Thor wrote in request 4:
Comment Body

--
Expand Down
1 change: 1 addition & 0 deletions src/api/test/fixtures/event_mailer/request_event
@@ -1,4 +1,5 @@
From: obs-email@opensuse.org
Reply-To: Iggy@pop.org
To: user1@example.com
Message-ID: obs-request-REQUESTID@localhost
Subject: Iggy created request REQUESTID (add_role home:tom)
Expand Down
1 change: 1 addition & 0 deletions src/api/test/fixtures/event_mailer/review_wanted
@@ -1,4 +1,5 @@
From: obs-email@opensuse.org
Reply-To: Iggy@pop.org
To: tschmidt@example.com
Message-ID: <test@localhost>
In-Reply-To: obs-request-4@localhost
Expand Down
1 change: 1 addition & 0 deletions src/api/test/fixtures/event_mailer/set_bugowner_event
@@ -1,4 +1,5 @@
From: obs-email@opensuse.org
Reply-To: Iggy@pop.org
To: user1@example.com
Message-ID: obs-request-REQUESTID@localhost
Subject: Iggy created request REQUESTID (set_bugowner home:tom)
Expand Down
8 changes: 5 additions & 3 deletions src/api/test/functional/request_events_test.rb
Expand Up @@ -55,10 +55,12 @@ def verify_email(fixture_name, myid, email)

ActionMailer::Base.deliveries.clear

login_tom

# now check if we get an email about revokes
# user1 should get one (as always) and Iggy, the creator
assert_difference 'ActionMailer::Base.deliveries.size', +2 do
raw_post "/request/#{myid}?cmd=changestate&newstate=revoked", ''
raw_post "/request/#{myid}?cmd=changestate&newstate=declined", ''
assert_response :success
end
email = nil
Expand All @@ -67,8 +69,8 @@ def verify_email(fixture_name, myid, email)
end

email.message_id = 'test@localhost' # easier to compare :)
assert_equal "Request state of #{myid} (set_bugowner home:tom) changed to revoked", email.subject
verify_email('iggy_revoked', myid, email)
assert_equal "Request state of #{myid} (set_bugowner home:tom) changed to declined", email.subject
verify_email('adrian_declined', myid, email)
end

test 'group emails' do
Expand Down
4 changes: 2 additions & 2 deletions src/api/test/functional/webui/request_controller_test.rb
Expand Up @@ -260,7 +260,7 @@ def verify_email(fixture_name, myid, email)
end

test 'comment event' do
login_Iggy to: request_show_path(4)
login_tom to: request_show_path(4)

Timecop.travel(2013, 8, 20, 12, 0, 0)

Expand All @@ -273,7 +273,7 @@ def verify_email(fixture_name, myid, email)

email = ActionMailer::Base.deliveries.last

assert_equal "New comment in request 4 by Iggy", email.subject
assert_equal "New comment in request 4 by tom", email.subject
assert_equal %w(Iggy@pop.org), email.to
verify_email('comment_event', "4", email)
end
Expand Down

0 comments on commit fc4ee9f

Please sign in to comment.