Skip to content

Add worker for sending emails notifications for expiring deploy tokens

What does this MR do and why?

This MR implements a worker which would run everyday at 1:00 am daily, and fetch all the deploy tokens that are nearing their expiry date(ie any token which is being expired in the next 60 days). It then calls the notification service on all those deploy tokens to send notifications.

This is 3/3 MR & a follow up of Adding a mailer function to send e-mail notifications to project owners for expiring deploy tokens

How to Setup & Validate locally

1. Create a User and Project locally or use existing ones

project = Project.find_by_full_path('your-project-path') 
user = User.find_by_username('your-username')
project.add_owner(user) 

1. Enable the Feature Flag

Feature.enable(:project_deploy_token_expiring_notifications)

2. Create Test Deploy Tokens with different expiration periods:

token_7_days = DeployToken.create!(
  name: 'test-token-7days',
  expires_at: 5.days.from_now.iso8601,
  deploy_token_type: :project_type,
  read_repository: true
)

token_30_days = DeployToken.create!(
  name: 'test-token-30days', 
  expires_at: 15.days.from_now.iso8601,
  deploy_token_type: :project_type,
  read_repository: true
)

token_60_days = DeployToken.create!(
  name: 'test-token-60days',
  expires_at: 45.days.from_now.iso8601, 
  deploy_token_type: :project_type,
  read_repository: true
)

project.project_deploy_tokens.create!(deploy_token: token_7_days)
project.project_deploy_tokens.create!(deploy_token: token_30_days)
project.project_deploy_tokens.create!(deploy_token: token_60_days)

3. Execute the worker manually & verify notification timestamps were updated:

DeployTokens::ExpiringWorker.new.perform

# All should return timestamps indicating notifications were sent
token_7_days.reload.seven_days_notification_sent_at
token_30_days.reload.thirty_days_notification_sent_at  
token_60_days.reload.sixty_days_notification_sent_at

4. Access the letter_opener web interface to see all generated emails:

http://localhost:3000/rails/letter_opener

5. Manually generate email to preview content

mail = Notify.deploy_token_about_to_expire_email(user, 'token_7_days', project, days_to_expire: 7)
puts "Email subject: #{mail.subject}"

6. Test Feature Flag Implementation

Feature.disable(:project_deploy_token_expiring_notifications)

# Worker should not process any tokens
DeployTokens::ExpiringWorker.new.perform

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

References

Relates to Send an e-mail notification to project owners for expiring deploy tokens

Edited by Pratibha Gupta

Merge request reports

Loading