diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-06-20 14:10:13 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-06-20 14:10:13 +0300 |
commit | 0ea3fcec397b69815975647f5e2aa5fe944a8486 (patch) | |
tree | 7979381b89d26011bcf9bdc989a40fcc2f1ed4ff /lib/gitlab/mailgun | |
parent | 72123183a20411a36d607d70b12d57c484394c8e (diff) |
Add latest changes from gitlab-org/gitlab@15-1-stable-eev15.1.0-rc42
Diffstat (limited to 'lib/gitlab/mailgun')
-rw-r--r-- | lib/gitlab/mailgun/webhook_processors/base.rb | 20 | ||||
-rw-r--r-- | lib/gitlab/mailgun/webhook_processors/failure_logger.rb | 36 | ||||
-rw-r--r-- | lib/gitlab/mailgun/webhook_processors/member_invites.rb | 48 |
3 files changed, 104 insertions, 0 deletions
diff --git a/lib/gitlab/mailgun/webhook_processors/base.rb b/lib/gitlab/mailgun/webhook_processors/base.rb new file mode 100644 index 00000000000..9402637a51d --- /dev/null +++ b/lib/gitlab/mailgun/webhook_processors/base.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module Gitlab + module Mailgun + module WebhookProcessors + class Base + def initialize(payload) + @payload = payload || {} + end + + def execute + end + + private + + attr_reader :payload + end + end + end +end diff --git a/lib/gitlab/mailgun/webhook_processors/failure_logger.rb b/lib/gitlab/mailgun/webhook_processors/failure_logger.rb new file mode 100644 index 00000000000..a7a85bd1672 --- /dev/null +++ b/lib/gitlab/mailgun/webhook_processors/failure_logger.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module Gitlab + module Mailgun + module WebhookProcessors + class FailureLogger < Base + def execute + log_failure if permanent_failure? || temporary_failure_over_threshold? + end + + def permanent_failure? + payload['event'] == 'failed' && payload['severity'] == 'permanent' + end + + def temporary_failure_over_threshold? + payload['event'] == 'failed' && payload['severity'] == 'temporary' && + Gitlab::ApplicationRateLimiter.throttled?(:temporary_email_failure, scope: payload['recipient']) + end + + private + + def log_failure + Gitlab::ErrorTracking::Logger.error( + event: 'email_delivery_failure', + mailgun_event_id: payload['id'], + recipient: payload['recipient'], + failure_type: payload['severity'], + failure_reason: payload['reason'], + failure_code: payload.dig('delivery-status', 'code'), + failure_message: payload.dig('delivery-status', 'message') + ) + end + end + end + end +end diff --git a/lib/gitlab/mailgun/webhook_processors/member_invites.rb b/lib/gitlab/mailgun/webhook_processors/member_invites.rb new file mode 100644 index 00000000000..f54c44381f0 --- /dev/null +++ b/lib/gitlab/mailgun/webhook_processors/member_invites.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +module Gitlab + module Mailgun + module WebhookProcessors + class MemberInvites < Base + ProcessWebhookServiceError = Class.new(StandardError) + + def execute + return unless should_process? + + @member = Member.find_by_invite_token(invite_token) + update_member_and_log if member + rescue ProcessWebhookServiceError => e + Gitlab::ErrorTracking.track_exception(e) + end + + private + + attr_reader :member + + def should_process? + payload['event'] == 'failed' && payload['severity'] == 'permanent' && + payload['tags']&.include?(::Members::Mailgun::INVITE_EMAIL_TAG) + end + + def update_member_and_log + log_update_event if member.update(invite_email_success: false) + end + + def log_update_event + Gitlab::AppLogger.info( + message: "UPDATED MEMBER INVITE_EMAIL_SUCCESS: member_id: #{member.id}", + event: 'updated_member_invite_email_success' + ) + end + + def invite_token + # may want to validate schema in some way using ::JSONSchemer.schema(SCHEMA_PATH).valid?(message) if this + # gets more complex + payload.dig('user-variables', ::Members::Mailgun::INVITE_EMAIL_TOKEN_KEY) || + raise(ProcessWebhookServiceError, "Expected to receive #{::Members::Mailgun::INVITE_EMAIL_TOKEN_KEY} " \ + "in user-variables: #{payload}") + end + end + end + end +end |