diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2016-05-19 01:19:25 +0300 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2016-05-19 01:25:45 +0300 |
commit | c337e748d385fea5c768a8e7de55975dca7fa484 (patch) | |
tree | f9646fbb68614b633964bc43d725296b61047fec /lib/gitlab/email/handler | |
parent | 3f4a6412dcc35c182d993cd1350459e8a4a1b8d1 (diff) |
so we use separate classes to handle different tasks
Diffstat (limited to 'lib/gitlab/email/handler')
-rw-r--r-- | lib/gitlab/email/handler/create_issue.rb | 62 | ||||
-rw-r--r-- | lib/gitlab/email/handler/create_note.rb | 55 |
2 files changed, 117 insertions, 0 deletions
diff --git a/lib/gitlab/email/handler/create_issue.rb b/lib/gitlab/email/handler/create_issue.rb new file mode 100644 index 00000000000..24f8f59900d --- /dev/null +++ b/lib/gitlab/email/handler/create_issue.rb @@ -0,0 +1,62 @@ + +require 'gitlab/email/handler' + +module Gitlab + module Email + class Handler + class CreateIssue < Handler + def can_handle? + !!project + end + + def execute + # Must be private project without access + raise ProjectNotFound unless author.can?(:read_project, project) + + validate_permission!(:create_issue) + validate_authentication_token! + + verify_record( + create_issue, + InvalidIssueError, + "The issue could not be created for the following reasons:" + ) + end + + def author + @author ||= mail.from.find do |email| + user = User.find_by_any_email(email) + break user if user + end + end + + def project + @project ||= Project.find_with_namespace(project_namespace) + end + + private + def authentication_token + mail_key[/[^\+]+$/] + end + + def project_namespace + mail_key[/^[^\+]+/] + end + + def create_issue + Issues::CreateService.new( + project, + author, + title: mail.subject, + description: message + ).execute + end + + def validate_authentication_token! + raise UserNotAuthorizedError unless author.authentication_token == + authentication_token + end + end + end + end +end diff --git a/lib/gitlab/email/handler/create_note.rb b/lib/gitlab/email/handler/create_note.rb new file mode 100644 index 00000000000..32deb5a311e --- /dev/null +++ b/lib/gitlab/email/handler/create_note.rb @@ -0,0 +1,55 @@ + +require 'gitlab/email/handler' + +module Gitlab + module Email + class Handler + class CreateNote < Handler + def can_handle? + !!sent_notification + end + + def execute + raise SentNotificationNotFoundError unless sent_notification + raise AutoGeneratedEmailError if mail.header.to_s =~ /auto-(generated|replied)/ + + validate_permission!(:create_note) + + raise NoteableNotFoundError unless sent_notification.noteable + raise EmptyEmailError if message.blank? + + verify_record( + create_note, + InvalidNoteError, + "The comment could not be created for the following reasons:" + ) + end + + def author + sent_notification.recipient + end + + def project + sent_notification.project + end + + def sent_notification + @sent_notification ||= SentNotification.for(mail_key) + end + + private + def create_note + Notes::CreateService.new( + project, + author, + note: message, + noteable_type: sent_notification.noteable_type, + noteable_id: sent_notification.noteable_id, + commit_id: sent_notification.commit_id, + line_code: sent_notification.line_code + ).execute + end + end + end + end +end |