Class: Dradis::Plugins::Slack::ActivitySubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/dradis/plugins/slack/activity_subscriber.rb

Class Method Summary collapse

Class Method Details

.fields_for(trackable) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dradis/plugins/slack/activity_subscriber.rb', line 24

def self.fields_for(trackable)
  result = []

  options = ActionMailer::Base.default_url_options
  url_helpers = Rails.application.routes.url_helpers
  item_url = url_for(trackable, options, url_helpers)

  # Project
  if trackable.project
    result << {
      title: 'Project',
      value: "<#{url_helpers.project_url(trackable.project, options)}|#{trackable.project.name}>"
    }
  end

  # Title
  title = if trackable.respond_to?(:title) && trackable.title?
    trackable.title
          elsif trackable.respond_to?(:label) && trackable.label?
            trackable.label
          elsif trackable.class.name == 'Comment'
            trackable.commentable.title
  end

  if title.present?
    result << {
      title: 'Title',
      value: "<#{item_url}|#{title}>"
    }
  end

  # Content (for comments)
  if trackable.class.name == 'Comment'
    result << {
      title: 'Content',
      value: trackable.content
    }
  end

  result
end

.handle(payload) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dradis/plugins/slack/activity_subscriber.rb', line 3

def self.handle(payload)
  action = payload[:action]
  trackable = payload[:trackable]
  user = payload[:user]

  text =
    if trackable
      "[Dradis] #{trackable.class.name} ID=#{trackable.id} #{action.sub(/e?\z/, 'ed')} by #{user}"
    else
      "[Dradis] An item was deleted by #{user}"
    end

  Slack::Notifier.new(Dradis::Plugins::Slack::Engine.settings.webhook).post(
    # icon_emoji: ':robot_face:',
    icon_url: Dradis::Plugins::Slack::Engine.settings.icon,
    text: text,
    fields: trackable ? fields_for(trackable) : []
  )
end

.url_for(trackable, options, helpers) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dradis/plugins/slack/activity_subscriber.rb', line 66

def self.url_for(trackable, options, helpers)
  components = [trackable.project]

  target = trackable.class.name == 'Comment' ? trackable.commentable : trackable

  # Don't need Issue because L74
  case target
  when Card
    components << target.list.board
    components << target.list
  when Evidence, Note
    # FIXME - ISSUE/NOTE INHERITANCE
    unless target.is_a?(Issue)
      components << target.node
    end
  end

  components << target
  helpers.polymorphic_url(components, options)
end