Module: Rails::Approvals

Defined in:
lib/rails/approvals.rb,
lib/rails/approvals/engine.rb,
lib/rails/approvals/railtie.rb,
lib/rails/approvals/version.rb,
app/models/rails/approvals/request.rb,
app/jobs/rails/approvals/application_job.rb,
app/models/rails/approvals/slack_message.rb,
app/models/rails/approvals/slack_response.rb,
app/models/rails/approvals/application_record.rb,
app/helpers/rails/approvals/application_helper.rb,
app/mailers/rails/approvals/application_mailer.rb,
app/controllers/rails/approvals/slack_controller.rb,
app/controllers/rails/approvals/application_controller.rb

Defined Under Namespace

Modules: ApplicationHelper Classes: ApplicationController, ApplicationJob, ApplicationMailer, ApplicationRecord, Engine, Railtie, Request, SlackController, SlackMessage, SlackResponse

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.awaitObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rails/approvals.rb', line 50

def self.await
  request = Rails::Approvals::Request.request!
  poll = request.requested?
  canceled = false

  Signal.trap('SIGINT') do
    # Stop the polling
    poll = false

    # Canceling the request inside of a trap will fail if it results in
    # any log output. Cancel the request outside of the scope of the trap
    # and everything will work as expected.
    canceled = true
  end

  while poll
    if request.expired?
      request.timed_out!
      break
    end

    request.reload
    poll = request.requested?

    sleep 0.1
  end

  if canceled
    request.canceled!
  end

  request
end

.slackObject



84
85
86
# File 'lib/rails/approvals.rb', line 84

def self.slack
  @slack ||= Slack::Web::Client.new(token: Rails.application.config.rails.approvals.slack.token)
end

.start!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails/approvals.rb', line 9

def self.start!
  # We only want to start an approval request if it's enabled.
  return unless ::Rails.application.config.rails.approvals.enabled

  if Rails.application.config.rails.approvals.slack.token.blank?
    msg = <<~MSG
    Please provide your Slack API token either in the `RAILS_APPROVALS_SLACK_TOKEN` environment variable, or configured in your environment file:

    Rails.application.configure do
      config.rails.approvals.enabled = true
      config.rails.approvals.slack.channel = "#rails-approvals"
      config.rails.approvals.slack.token = "your-token-here"
    end
    MSG

    puts msg
    exit 1
  end

  request = Rails::Approvals.await
  case
  when request.approved?
    puts "✅ Request to run #{request.command} approved by #{request.responder}."
  when request.denied?
    puts "🛑 Request to run #{request.command} denied by #{request.responder}."
    exit 1
  when request.timed_out?
    puts "⚠️ Request to run #{request.command} timed out."
    exit 1
  when request.canceled?
    puts "👋 You canceled your approval request to run #{request.command}."
    exit 0
  end
rescue TTY::Reader::InputInterrupt
  exit 0
rescue Slack::Web::Api::Errors::ChannelNotFound
  channel = Rails.application.config.rails.approvals.slack.channel
  puts "Rails::Approvals was configured to send approval requests to #{channel} in your Slack workspace but it either does not exist or the Slack workspace user needs to be invited to the room."
  exit 1
end