Class: Lolcommits::Plugin::Slack

Inherits:
Base
  • Object
show all
Defined in:
lib/lolcommits/plugin/slack.rb

Constant Summary collapse

ENDPOINT_URL =

Slack API File upload endpoint

'https://slack.com/api/files.upload'.freeze
RETRY_COUNT =

Number of times to retry if RestClient.post fails

2

Instance Method Summary collapse

Instance Method Details

#configure_options!Hash

Prompts the user to configure integration with Slack

Prompts user for a Slack ‘access_token` and a comma seperated list of valid Slack channel IDs.

Returns:

  • (Hash)

    a hash of configured plugin options



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/lolcommits/plugin/slack.rb', line 64

def configure_options!
  options = super

  if options[:enabled]
    print "open the url below and issue a token for your user:\n"
    print "https://api.slack.com/custom-integrations/legacy-tokens\n"
    print "enter the generated token below, then press enter: (e.g. xxxx-xxxxxxxxx-xxxx) \n"
    code = parse_user_input(gets.strip)

    print "enter a comma-seperated list of channel ids to post lolcommits in, then press enter: (e.g. c1234567890,c1234567890)\n"
    print "note: you must use channel ids (not channel names). grab them from here; https://api.slack.com/methods/channels.list/test\n"
    channels = parse_user_input(gets.strip)

    options.merge!(
      access_token: code,
      channels: channels
    )
  end

  options
end

#run_capture_readyObject

Capture ready hook, runs after lolcommits captures a snapshot.

Uses ‘RestClient` to post the lolcommit to (one or more) Slack channels. Posting will be retried (`RETRY_COUNT`) times if any error occurs.

The post contains the git commit message, repo name and the SHA is used for the filename. The response from the POST request is sent to the debug log.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lolcommits/plugin/slack.rb', line 26

def run_capture_ready
  retries = RETRY_COUNT
  begin
    print "Posting to Slack ... "
    response = RestClient.post(
      ENDPOINT_URL,
      file: File.new(runner.lolcommit_path),
      token: configuration[:access_token],
      filetype: 'jpg',
      filename: runner.sha,
      title: runner.message + "[#{runner.vcs_info.repo}]",
      channels: configuration[:channels]
    )

    debug response
    print "done!\n"
  rescue => e
    retries -= 1
    print "failed! #{e.message}"
    if retries > 0
      print " - retrying ...\n"
      retry
    else
      print " - giving up ...\n"
      puts 'Try running config again:'
      puts "\tlolcommits --config -p slack"
    end
  end
end