qiita_team_services 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +36 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +27 -0
  5. data/.travis.yml +11 -0
  6. data/CHANGELOG.md +7 -0
  7. data/Gemfile +3 -0
  8. data/LICENSE +22 -0
  9. data/README.md +5 -0
  10. data/Rakefile +26 -0
  11. data/config/locales/en.yml +31 -0
  12. data/config/locales/ja.yml +32 -0
  13. data/lib/qiita/team/services/engine.rb +6 -0
  14. data/lib/qiita/team/services/errors.rb +7 -0
  15. data/lib/qiita/team/services/events/base.rb +47 -0
  16. data/lib/qiita/team/services/events/item_became_coediting.rb +10 -0
  17. data/lib/qiita/team/services/events/item_comment_created.rb +17 -0
  18. data/lib/qiita/team/services/events/item_comment_destroyed.rb +17 -0
  19. data/lib/qiita/team/services/events/item_comment_updated.rb +17 -0
  20. data/lib/qiita/team/services/events/item_created.rb +10 -0
  21. data/lib/qiita/team/services/events/item_destroyed.rb +10 -0
  22. data/lib/qiita/team/services/events/item_updated.rb +10 -0
  23. data/lib/qiita/team/services/events/project_activated.rb +10 -0
  24. data/lib/qiita/team/services/events/project_archived.rb +10 -0
  25. data/lib/qiita/team/services/events/project_comment_created.rb +19 -0
  26. data/lib/qiita/team/services/events/project_comment_destroyed.rb +19 -0
  27. data/lib/qiita/team/services/events/project_comment_updated.rb +19 -0
  28. data/lib/qiita/team/services/events/project_created.rb +10 -0
  29. data/lib/qiita/team/services/events/project_destroyed.rb +10 -0
  30. data/lib/qiita/team/services/events/project_updated.rb +10 -0
  31. data/lib/qiita/team/services/events/team_member_added.rb +12 -0
  32. data/lib/qiita/team/services/events/team_member_removed.rb +12 -0
  33. data/lib/qiita/team/services/events.rb +28 -0
  34. data/lib/qiita/team/services/hooks/base.rb +45 -0
  35. data/lib/qiita/team/services/hooks/chatwork_v1.rb +149 -0
  36. data/lib/qiita/team/services/hooks/concerns/event_handlable.rb +49 -0
  37. data/lib/qiita/team/services/hooks/concerns/http_client.rb +74 -0
  38. data/lib/qiita/team/services/hooks/concerns/persistable.rb +64 -0
  39. data/lib/qiita/team/services/hooks/concerns/service.rb +31 -0
  40. data/lib/qiita/team/services/hooks/concerns/slack.rb +338 -0
  41. data/lib/qiita/team/services/hooks/hipchat_v1.rb +143 -0
  42. data/lib/qiita/team/services/hooks/slack_v1.rb +25 -0
  43. data/lib/qiita/team/services/hooks/slack_v2.rb +19 -0
  44. data/lib/qiita/team/services/hooks/webhook.rb +193 -0
  45. data/lib/qiita/team/services/hooks.rb +25 -0
  46. data/lib/qiita/team/services/properties/base.rb +20 -0
  47. data/lib/qiita/team/services/properties/boolean_property.rb +12 -0
  48. data/lib/qiita/team/services/properties/string_property.rb +12 -0
  49. data/lib/qiita/team/services/properties.rb +21 -0
  50. data/lib/qiita/team/services/resources/README.md +177 -0
  51. data/lib/qiita/team/services/templates/chatwork_v1.html.erb +25 -0
  52. data/lib/qiita/team/services/templates/hipchat_v1.html.erb +58 -0
  53. data/lib/qiita/team/services/templates/slack_v1.html.erb +54 -0
  54. data/lib/qiita/team/services/templates/slack_v2.html.erb +44 -0
  55. data/lib/qiita/team/services/templates/webhook.html.erb +25 -0
  56. data/lib/qiita/team/services/version.rb +3 -0
  57. data/lib/qiita_team_services.rb +34 -0
  58. data/qiita_team_services.gemspec +42 -0
  59. data/spec/hooks/chatwork_v1_spec.rb +118 -0
  60. data/spec/hooks/hipchat_v1_spec.rb +139 -0
  61. data/spec/hooks/slack_v1_spec.rb +55 -0
  62. data/spec/hooks/slack_v2_spec.rb +50 -0
  63. data/spec/hooks/webhook_spec.rb +465 -0
  64. data/spec/spec_helper.rb +17 -0
  65. data/spec/support/factories/comments.rb +21 -0
  66. data/spec/support/factories/items.rb +26 -0
  67. data/spec/support/factories/projects.rb +17 -0
  68. data/spec/support/factories/taggings.rb +9 -0
  69. data/spec/support/factories/teams.rb +9 -0
  70. data/spec/support/factories/users.rb +11 -0
  71. data/spec/support/factory_girl.rb +13 -0
  72. data/spec/support/helpers/event_helper.rb +49 -0
  73. data/spec/support/helpers/hook_helper.rb +33 -0
  74. data/spec/support/helpers/http_client_stub_helper.rb +19 -0
  75. data/spec/support/helpers/slack_hook_helper.rb +104 -0
  76. data/spec/support/matchers/match_slack_attachments_request.rb +7 -0
  77. data/spec/support/resources/base.rb +23 -0
  78. data/spec/support/resources/comment.rb +29 -0
  79. data/spec/support/resources/item.rb +53 -0
  80. data/spec/support/resources/project.rb +45 -0
  81. data/spec/support/resources/tagging.rb +15 -0
  82. data/spec/support/resources/team.rb +11 -0
  83. data/spec/support/resources/user.rb +17 -0
  84. metadata +323 -0
@@ -0,0 +1,149 @@
1
+ require "active_support/core_ext/string/strip"
2
+
3
+ require "qiita/team/services/hooks/base"
4
+ require "qiita/team/services/hooks/concerns/http_client"
5
+
6
+ module Qiita::Team::Services
7
+ module Hooks
8
+ class ChatworkV1 < Base
9
+ include Concerns::HttpClient
10
+
11
+ define_property :token
12
+ define_property :room_id
13
+
14
+ validates :token, presence: true
15
+ validates :room_id, presence: true
16
+
17
+ # @note Override {Services::Base.service_name}.
18
+ def self.service_name
19
+ "ChatWork"
20
+ end
21
+
22
+ # @return [void]
23
+ def ping
24
+ send_message "Test message sent from Qiita:Team"
25
+ rescue DeliveryError
26
+ nil
27
+ end
28
+
29
+ # @param event [Qiita::Team::Services::Events::ItemCreated]
30
+ # @return [void]
31
+ # @raise [Qiita::Team::Services::DeliveryError]
32
+ def item_created(event)
33
+ send_message <<-EOM.strip_heredoc
34
+ #{event.user.name} created #{event.item.title}.
35
+ #{event.item.url}
36
+ EOM
37
+ end
38
+
39
+ # @param event [Qiita::Team::Services::Events::ItemUpdated]
40
+ # @return [void]
41
+ # @raise [Qiita::Team::Services::DeliveryError]
42
+ def item_updated(event)
43
+ send_message <<-EOM.strip_heredoc
44
+ #{event.user.name} updated #{event.item.title}.
45
+ #{event.item.url}
46
+ EOM
47
+ end
48
+
49
+ # @param event [Qiita::Team::Services::Events::ItemBecameCoediting]
50
+ # @return [void]
51
+ # @raise [Qiita::Team::Services::DeliveryError]
52
+ def item_became_coediting(event)
53
+ send_message <<-EOM.strip_heredoc
54
+ #{event.user.name} changed #{event.item.title} to coedit mode.
55
+ #{event.item.url}
56
+ EOM
57
+ end
58
+
59
+ # @param event [Qiita::Team::Services::Events::ItemCommentCreated]
60
+ # @return [void]
61
+ # @raise [Qiita::Team::Services::DeliveryError]
62
+ def item_comment_created(event)
63
+ send_message <<-EOM.strip_heredoc
64
+ #{event.user.name} commented on #{event.item.title}.
65
+ #{event.item.url}[info]#{event.comment.body.truncate(100)}[/info]
66
+ EOM
67
+ end
68
+
69
+ # @param event [Qiita::Team::Services::Events::ProjectCommentCreated]
70
+ # @return [void]
71
+ # @raise [Qiita::Team::Services::DeliveryError]
72
+ def project_comment_created(event)
73
+ send_message <<-EOM.strip_heredoc
74
+ #{event.user.name} commented on #{event.project.name} project.
75
+ #{event.project.url}[info]#{event.comment.body.truncate(100)}[/info]
76
+ EOM
77
+ end
78
+
79
+ # @param event [Qiita::Team::Services::Events::MemberAdded]
80
+ # @return [void]
81
+ # @raise [Qiita::Team::Services::DeliveryError]
82
+ def team_member_added(event)
83
+ send_message("#{event.member.name} is added to #{event.team.name} team.")
84
+ end
85
+
86
+ # @param event [Qiita::Team::Services::Events::ProjectCreated]
87
+ # @return [void]
88
+ # @raise [Qiita::Team::Services::DeliveryError]
89
+ def project_created(event)
90
+ send_message <<-EOM.strip_heredoc
91
+ #{event.user.name} created #{event.project.name} project.
92
+ #{event.project.url}
93
+ EOM
94
+ end
95
+
96
+ # @param event [Qiita::Team::Services::Events::ProjectUpdated]
97
+ # @return [void]
98
+ # @raise [Qiita::Team::Services::DeliveryError]
99
+ def project_updated(event)
100
+ send_message <<-EOM.strip_heredoc
101
+ #{event.user.name} updated #{event.project.name} project.
102
+ #{event.project.url}
103
+ EOM
104
+ end
105
+
106
+ # @param event [Qiita::Team::Services::Events::ProjectArchived]
107
+ # @return [void]
108
+ # @raise [Qiita::Team::Services::DeliveryError]
109
+ def project_archived(event)
110
+ send_message <<-EOM.strip_heredoc
111
+ #{event.user.name} archived #{event.project.name} project.
112
+ #{event.project.url}
113
+ EOM
114
+ end
115
+
116
+ # @param event [Qiita::Team::Services::Events::ProjectActivated]
117
+ # @return [void]
118
+ # @raise [Qiita::Team::Services::DeliveryError]
119
+ def project_activated(event)
120
+ send_message <<-EOM.strip_heredoc
121
+ #{event.user.name} activated #{event.project.name} project.
122
+ #{event.project.url}
123
+ EOM
124
+ end
125
+
126
+ private
127
+
128
+ # @param message [String]
129
+ def send_message(message)
130
+ http_post({ body: message }.to_query)
131
+ end
132
+
133
+ # @note Override Concerns::HttpClient#url.
134
+ def url
135
+ "https://api.chatwork.com/v1/rooms/#{room_id}/messages"
136
+ end
137
+
138
+ # @note Override Concerns::HttpClient#request_format.
139
+ def request_format
140
+ :url_encoded
141
+ end
142
+
143
+ # @note Override Concerns::HttpClient#request_headers.
144
+ def request_headers
145
+ { "X-ChatWorkToken" => token }
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,49 @@
1
+ require "active_support/concern"
2
+ require "active_support/core_ext/module/delegation"
3
+
4
+ module Qiita::Team::Services
5
+ module Hooks
6
+ module Concerns
7
+ module EventHandlable
8
+ extend ActiveSupport::Concern
9
+
10
+ class_methods do
11
+ # @return [true, false]
12
+ def deprecated?
13
+ @deprecated == true
14
+ end
15
+
16
+ # @return [true, false]
17
+ def pingable?
18
+ public_instance_methods.include?(:ping)
19
+ end
20
+
21
+ # List of implemented event names.
22
+ #
23
+ # @return [Array<Symbol>]
24
+ def available_event_names
25
+ public_instance_methods & Events.event_names
26
+ end
27
+
28
+ private
29
+
30
+ # Mark the service as deprecated.
31
+ #
32
+ # @return [void]
33
+ def deprecated
34
+ @deprecated = true
35
+ end
36
+ end
37
+
38
+ delegate :deprecated?, :pingable?, to: :class
39
+
40
+ # @param event [Qiita::Team::Services::Events::Base]
41
+ # @return [void]
42
+ def handle(event)
43
+ return unless respond_to?(event.class.event_name)
44
+ public_send(event.class.event_name, event)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,74 @@
1
+ require "faraday"
2
+ require "faraday_middleware"
3
+
4
+ module Qiita::Team::Services
5
+ module Hooks
6
+ module Concerns
7
+ module HttpClient
8
+ DEFAULT_ADAPTER = :net_http
9
+ DEFAULT_HEADERS = {
10
+ "Content-Type" => "application/json",
11
+ "User-Agent" => "Qiita:Team",
12
+ }
13
+ TIMEOUT = 5
14
+
15
+ private
16
+
17
+ # @return [Faraday::Connection]
18
+ def http_client
19
+ @http_client ||= Faraday.new(faraday_parameters) do |faraday|
20
+ faraday.request request_format
21
+ faraday.adapter adapter
22
+ end
23
+ end
24
+
25
+ def faraday_parameters
26
+ {
27
+ headers: DEFAULT_HEADERS,
28
+ request: {
29
+ open_timeout: TIMEOUT,
30
+ timeout: TIMEOUT,
31
+ },
32
+ }
33
+ end
34
+
35
+ # @return [String]
36
+ def url
37
+ fail NotImplementedError
38
+ end
39
+
40
+ # @param request_body [Hash, Array] request payload.
41
+ # @return [Faraday::Response]
42
+ # @raise [Qiita::Team::Services::DeliveryError]
43
+ def http_post(request_body, headers = {})
44
+ resp = http_client.post do |req|
45
+ req.url url
46
+ req.body = request_body
47
+ request_headers.merge(headers).each_pair do |key, value|
48
+ req.headers[key] = value
49
+ end
50
+ end
51
+ if resp.success?
52
+ resp
53
+ else
54
+ fail DeliveryError
55
+ end
56
+ end
57
+
58
+ # @return [Symbol] the request format type, `:json` or `:url_encoded`
59
+ def request_format
60
+ :json
61
+ end
62
+
63
+ # @return [Hash{String => String}]
64
+ def request_headers
65
+ {}
66
+ end
67
+
68
+ def adapter
69
+ DEFAULT_ADAPTER
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,64 @@
1
+ require "active_support/concern"
2
+
3
+ require "qiita/team/services/properties"
4
+
5
+ module Qiita::Team::Services
6
+ module Hooks
7
+ module Concerns
8
+ module Persistable
9
+ extend ActiveSupport::Concern
10
+
11
+ class_methods do
12
+ # @return [Array<Qiita::Team::Services::Properties::Base>]
13
+ def service_properties
14
+ @service_properties ||= []
15
+ end
16
+
17
+ # @return [Array<String>]
18
+ def property_names
19
+ service_properties.map(&:name)
20
+ end
21
+
22
+ private
23
+
24
+ # @param name [Symbol]
25
+ # @param type [Symbol] :string or :boolean.
26
+ # @param default [String, true, false]
27
+ # @return [void]
28
+ def define_property(name, type: :string, default: nil)
29
+ service_properties << Properties.create(name, type, default)
30
+ attr_accessor name
31
+ end
32
+ end
33
+
34
+ # @param hash [Hash{String => Object}] deserialized properties hash.
35
+ def initialize(hash)
36
+ self.class.service_properties.each do |property|
37
+ if hash.key?(property.name)
38
+ public_send("#{property.name}=", hash[property.name])
39
+ else
40
+ public_send("#{property.name}=", property.default)
41
+ end
42
+ end
43
+ end
44
+
45
+ # Serialize the service object.
46
+ #
47
+ # @return [Hash{String => Object}] serialized properties.
48
+ def to_hash
49
+ self.class.service_properties.map(&:name).each_with_object({}) do |name, hash|
50
+ hash[name] = public_send(name)
51
+ end
52
+ end
53
+
54
+ # Returns whether or not this record will be destroyed as part of
55
+ # the parents save transaction.
56
+ #
57
+ # @note Override ActiveRecord::AutosaveAssociation#marked_for_destruction?.
58
+ def marked_for_destruction?
59
+ false
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,31 @@
1
+ require "active_support/concern"
2
+ require "active_support/inflector"
3
+
4
+ module Qiita::Team::Services
5
+ module Hooks
6
+ module Concerns
7
+ module Service
8
+ extend ActiveSupport::Concern
9
+
10
+ class_methods do
11
+ # @return [String]
12
+ def hook_type
13
+ name.demodulize.underscore
14
+ end
15
+
16
+ # @return [String]
17
+ def service_name
18
+ fail NotImplementedError
19
+ end
20
+ end
21
+
22
+ # The service name.
23
+ #
24
+ # @return [String]
25
+ def name
26
+ self.class.service_name
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,338 @@
1
+ require "active_support/concern"
2
+ require "slacken"
3
+
4
+ require "qiita/team/services/hooks/concerns/http_client"
5
+
6
+ module Qiita::Team::Services
7
+ module Hooks
8
+ module Concerns
9
+ # Send richly-formatted messages to Slack.
10
+ # Override {HttpClient#url} to implement HttpClient.
11
+ #
12
+ # @see https://api.slack.com/docs/attachments
13
+ module Slack
14
+ extend ActiveSupport::Concern
15
+
16
+ DEFAULT_ICON_URL = "https://cdn.qiita.com/media/qiita-team-slack-icon.png".freeze
17
+ ICON_EMOJI_FORMAT = /\A:[^:]+:\z/
18
+
19
+ included do
20
+ include HttpClient
21
+
22
+ define_property :username, default: "Qiita:Team"
23
+ define_property :icon_emoji
24
+
25
+ validates :username, presence: true
26
+ validates :icon_emoji, format: { with: ICON_EMOJI_FORMAT }, allow_blank: true
27
+ end
28
+
29
+ class_methods do
30
+ # @note Override {Services::Base.service_name}.
31
+ def service_name
32
+ "Slack"
33
+ end
34
+ end
35
+
36
+ # @return [void]
37
+ def ping
38
+ fallback = "Test message sent from Qiita:Team"
39
+ send_message(attachments: [fallback: fallback, pretext: fallback])
40
+ rescue DeliveryError
41
+ nil
42
+ end
43
+
44
+ # @param event [Qiita::Team::Services::Events::ItemCreated]
45
+ # @return [void]
46
+ # @raise [Qiita::Team::Services::DeliveryError]
47
+ def item_created(event)
48
+ fallback = "#{user_link(event.user)} created a new post"
49
+ send_message(
50
+ attachments: [
51
+ fallback: fallback,
52
+ pretext: fallback,
53
+ author_name: "@#{event.user.url_name}",
54
+ author_link: event.user.url,
55
+ author_icon: event.user.profile_image_url,
56
+ title: event.item.title,
57
+ title_link: event.item.url,
58
+ text: Slacken.translate(event.item.rendered_body),
59
+ mrkdwn_in: ["text"],
60
+ ],
61
+ )
62
+ end
63
+
64
+ # @param event [Qiita::Team::Services::Events::ItemUpdated]
65
+ # @return [void]
66
+ # @raise [Qiita::Team::Services::DeliveryError]
67
+ def item_updated(event)
68
+ fallback = "#{user_link(event.user)} updated #{item_link(event.item)}"
69
+ send_message(
70
+ attachments: [
71
+ fallback: fallback,
72
+ pretext: fallback,
73
+ ],
74
+ )
75
+ end
76
+
77
+ # @param event [Qiita::Team::Services::Events::ItemBecameCoediting]
78
+ # @return [void]
79
+ # @raise [Qiita::Team::Services::DeliveryError]
80
+ def item_became_coediting(event)
81
+ fallback = "#{user_link(event.user)} changed #{item_link(event.item)} to coedit mode"
82
+ send_message(
83
+ attachments: [
84
+ fallback: fallback,
85
+ pretext: fallback,
86
+ ],
87
+ )
88
+ end
89
+
90
+ # @param event [Qiita::Team::Services::Events::ItemDestroyed]
91
+ # @return [void]
92
+ # @raise [Qiita::Team::Services::DeliveryError]
93
+ def item_destroyed(event)
94
+ fallback = "#{user_link(event.user)} deleted #{event.item.title}"
95
+ send_message(
96
+ attachments: [
97
+ fallback: fallback,
98
+ pretext: fallback,
99
+ ],
100
+ )
101
+ end
102
+
103
+ # @param event [Qiita::Team::Services::Events::ItemCommentCreated]
104
+ # @return [void]
105
+ # @raise [Qiita::Team::Services::DeliveryError]
106
+ def item_comment_created(event)
107
+ fallback =
108
+ if event.item.coediting?
109
+ "New comment on #{item_link(event.item)}"
110
+ else
111
+ "New comment on #{user_link(event.item.user)}'s #{item_link(event.item)}"
112
+ end
113
+ send_message(
114
+ attachments: [
115
+ fallback: fallback,
116
+ pretext: fallback,
117
+ author_name: "@#{event.user.url_name}",
118
+ author_link: event.user.url,
119
+ author_icon: event.user.profile_image_url,
120
+ text: Slacken.translate(event.comment.rendered_body),
121
+ mrkdwn_in: ["text"],
122
+ ],
123
+ )
124
+ end
125
+
126
+ # @param event [Qiita::Team::Services::Events::ItemCommentUpdated]
127
+ # @return [void]
128
+ # @raise [Qiita::Team::Services::DeliveryError]
129
+ def item_comment_updated(event)
130
+ fallback = "#{user_link(event.user)} updated a #{comment_link(event.comment)}"
131
+ fallback << " on #{item_link(event.item)}"
132
+ send_message(
133
+ attachments: [
134
+ fallback: fallback,
135
+ pretext: fallback,
136
+ ],
137
+ )
138
+ end
139
+
140
+ # @param event [Qiita::Team::Services::Events::ItemCommentDestroyed]
141
+ # @return [void]
142
+ # @raise [Qiita::Team::Services::DeliveryError]
143
+ def item_comment_destroyed(event)
144
+ fallback = "#{user_link(event.user)} deleted a comemnt on #{item_link(event.item)}"
145
+ send_message(
146
+ attachments: [
147
+ fallback: fallback,
148
+ pretext: fallback,
149
+ ],
150
+ )
151
+ end
152
+
153
+ # @param event [Qiita::Team::Services::Events::ProjectCommentCreated]
154
+ # @return [void]
155
+ # @raise [Qiita::Team::Services::DeliveryError]
156
+ def project_comment_created(event)
157
+ fallback = "New comment on #{project_link(event.project)}"
158
+ send_message(
159
+ attachments: [
160
+ fallback: fallback,
161
+ pretext: fallback,
162
+ author_name: "@#{event.user.url_name}",
163
+ author_link: event.user.url,
164
+ author_icon: event.user.profile_image_url,
165
+ text: Slacken.translate(event.comment.rendered_body),
166
+ mrkdwn_in: ["text"],
167
+ ],
168
+ )
169
+ end
170
+
171
+ # @param event [Qiita::Team::Services::Events::ProjectCommentUpdated]
172
+ # @return [void]
173
+ # @raise [Qiita::Team::Services::DeliveryError]
174
+ def project_comment_updated(event)
175
+ fallback = "#{user_link(event.user)} updated a #{comment_link(event.comment)}"
176
+ fallback << " on #{project_link(event.project)}"
177
+ send_message(
178
+ attachments: [
179
+ fallback: fallback,
180
+ pretext: fallback,
181
+ ],
182
+ )
183
+ end
184
+
185
+ # @param event [Qiita::Team::Services::Events::ProjectCommentDestroyed]
186
+ # @return [void]
187
+ # @raise [Qiita::Team::Services::DeliveryError]
188
+ def project_comment_destroyed(event)
189
+ fallback = "#{user_link(event.user)} deleted a comemnt on #{project_link(event.project)}"
190
+ send_message(
191
+ attachments: [
192
+ fallback: fallback,
193
+ pretext: fallback,
194
+ ],
195
+ )
196
+ end
197
+
198
+ # @param event [Qiita::Team::Services::Events::MemberAdded]
199
+ # @return [void]
200
+ # @raise [Qiita::Team::Services::DeliveryError]
201
+ def team_member_added(event)
202
+ fallback = "#{user_link(event.member)} was added to the #{team_link(event.team)} team"
203
+ send_message(
204
+ attachments: [
205
+ fallback: fallback,
206
+ pretext: fallback,
207
+ ],
208
+ )
209
+ end
210
+
211
+ # @param event [Qiita::Team::Services::Events::MemberRemoved]
212
+ # @return [void]
213
+ # @raise [Qiita::Team::Services::DeliveryError]
214
+ def team_member_removed(event)
215
+ fallback = "#{event.member.name} was removed from the #{team_link(event.team)} team"
216
+ send_message(
217
+ attachments: [
218
+ fallback: fallback,
219
+ pretext: fallback,
220
+ ],
221
+ )
222
+ end
223
+
224
+ # @param event [Qiita::Team::Services::Events::ProjectCreated]
225
+ # @return [void]
226
+ # @raise [Qiita::Team::Services::DeliveryError]
227
+ def project_created(event)
228
+ fallback = "#{user_link(event.user)} created #{project_link(event.project)} project"
229
+ send_message(
230
+ attachments: [
231
+ fallback: fallback,
232
+ pretext: fallback,
233
+ ],
234
+ )
235
+ end
236
+
237
+ # @param event [Qiita::Team::Services::Events::ProjectUpdated]
238
+ # @return [void]
239
+ # @raise [Qiita::Team::Services::DeliveryError]
240
+ def project_updated(event)
241
+ fallback = "#{user_link(event.user)} updated #{project_link(event.project)} project"
242
+ send_message(
243
+ attachments: [
244
+ fallback: fallback,
245
+ pretext: fallback,
246
+ ],
247
+ )
248
+ end
249
+
250
+ # @param event [Qiita::Team::Services::Events::ProjectDestroyed]
251
+ # @return [void]
252
+ # @raise [Qiita::Team::Services::DeliveryError]
253
+ def project_destroyed(event)
254
+ fallback = "#{user_link(event.user)} deleted #{project_link(event.project)} project"
255
+ send_message(
256
+ attachments: [
257
+ fallback: fallback,
258
+ pretext: fallback,
259
+ ],
260
+ )
261
+ end
262
+
263
+ # @param event [Qiita::Team::Services::Events::ProjectArchived]
264
+ # @return [void]
265
+ # @raise [Qiita::Team::Services::DeliveryError]
266
+ def project_archived(event)
267
+ fallback = "#{user_link(event.user)} archived #{project_link(event.project)} project"
268
+ send_message(
269
+ attachments: [
270
+ fallback: fallback,
271
+ pretext: fallback,
272
+ ],
273
+ )
274
+ end
275
+
276
+ # @param event [Qiita::Team::Services::Events::ProjectActivated]
277
+ # @return [void]
278
+ # @raise [Qiita::Team::Services::DeliveryError]
279
+ def project_activated(event)
280
+ fallback = "#{user_link(event.user)} activated #{project_link(event.project)} project"
281
+ send_message(
282
+ attachments: [
283
+ fallback: fallback,
284
+ pretext: fallback,
285
+ ],
286
+ )
287
+ end
288
+
289
+ private
290
+
291
+ # @param request_body [Hash]
292
+ # @return [void]
293
+ def send_message(request_body)
294
+ http_post(user_hash.merge(request_body))
295
+ end
296
+
297
+ # @return [Hash]
298
+ def user_hash
299
+ if icon_emoji.blank?
300
+ { username: username, icon_url: DEFAULT_ICON_URL }
301
+ else
302
+ { username: username, icon_emoji: icon_emoji }
303
+ end
304
+ end
305
+
306
+ # @param user [Qiita::Team::Services::Resources::User]
307
+ # @return [String]
308
+ def user_link(user)
309
+ "<#{user.url}|#{user.name}>"
310
+ end
311
+
312
+ # @param item [Qiita::Team::Services::Resources::Item]
313
+ # @return [String]
314
+ def item_link(item)
315
+ "<#{item.url}|#{item.title}>"
316
+ end
317
+
318
+ # @param comment [Qiita::Team::Services::Resources::Comment]
319
+ # @return [String]
320
+ def comment_link(comment)
321
+ "<#{comment.url}|comment>"
322
+ end
323
+
324
+ # @param project [Qiita::Team::Services::Resources::Project]
325
+ # @return [String]
326
+ def project_link(project)
327
+ "<#{project.url}|#{project.name}>"
328
+ end
329
+
330
+ # @param team [Qiita::Team::Services::Resources::Team]
331
+ # @return [String]
332
+ def team_link(team)
333
+ "<#{team.url}|#{team.name}>"
334
+ end
335
+ end
336
+ end
337
+ end
338
+ end