Class: Fastlane::Actions::HipchatAction

Inherits:
Fastlane::Action show all
Defined in:
lib/fastlane/actions/hipchat.rb

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, details, output, sh

Class Method Details

.authorObject



119
120
121
# File 'lib/fastlane/actions/hipchat.rb', line 119

def self.author
  "jingx23"
end

.available_optionsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fastlane/actions/hipchat.rb', line 83

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :message,
                                 env_name: "FL_HIPCHAT_MESSAGE",
                                 description: "The message to post on HipChat",
                                 default_value: ''),
    FastlaneCore::ConfigItem.new(key: :channel,
                                 env_name: "FL_HIPCHAT_CHANNEL",
                                 description: "The room or @username"),
    FastlaneCore::ConfigItem.new(key: :api_token,
                                 env_name: "HIPCHAT_API_TOKEN",
                                 description: "Hipchat API Token",
                                 verify_block: Proc.new do |value|
                                  unless value.to_s.length > 0
                                    Helper.log.fatal "Please add 'ENV[\"HIPCHAT_API_TOKEN\"] = \"your token\"' to your Fastfile's `before_all` section.".red
                                    raise 'No HIPCHAT_API_TOKEN given.'.red
                                  end
                                 end),
    FastlaneCore::ConfigItem.new(key: :success,
                                 env_name: "FL_HIPCHAT_SUCCESS",
                                 description: "Was this build successful? (true/false)",
                                 optional: true,
                                 default_value: true,
                                 is_string: false),
    FastlaneCore::ConfigItem.new(key: :version,
                                 env_name: "HIPCHAT_API_VERSION",
                                 description: "Version of the Hipchat API. Must be 1 or 2",
                                 verify_block: Proc.new do |value|
                                  if value.nil? || ![1, 2].include?(value.to_i)
                                    Helper.log.fatal "Please add 'ENV[\"HIPCHAT_API_VERSION\"] = \"1 or 2\"' to your Fastfile's `before_all` section.".red
                                    raise 'No HIPCHAT_API_VERSION given.'.red
                                  end
                                 end)
    ]
end

.check_response_code(response, channel) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fastlane/actions/hipchat.rb', line 66

def self.check_response_code(response, channel)
  case response.code.to_i
    when 200, 204
      true
    when 404
      raise "Channel `#{channel}` not found".red
    when 401
      raise "Access denied for channel `#{channel}`".red
    else
      raise "Unexpected #{response.code} for `#{channel}` with response: #{response.body}".red
  end
end

.descriptionObject



79
80
81
# File 'lib/fastlane/actions/hipchat.rb', line 79

def self.description
  "Send a error/success message to HipChat"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/fastlane/actions/hipchat.rb', line 123

def self.is_supported?(platform)
  true
end

.run(options) ⇒ Object



7
8
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
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fastlane/actions/hipchat.rb', line 7

def self.run(options)
  require 'net/http'
  require 'uri'

  api_token = options[:api_token]
  api_version = options[:version]

  channel = options[:channel]
  color = (options[:success] ? 'green' : 'red')
  
  the_message = options[:message]
  message = "<table><tr><td><img src='https://s3-eu-west-1.amazonaws.com/fastlane.tools/fastlane.png' width='50' height='50'></td><td>#{the_message[0..9999]}</td></tr></table>"

  if api_version.to_i == 1
    ########## running on V1 ##########
    if user?(channel)
      raise 'HipChat private message not working with API V1 please use API V2 instead'.red
    else
      uri = URI.parse('https://api.hipchat.com/v1/rooms/message')
      response = Net::HTTP.post_form(uri, { 'from' => 'fastlane',
                                            'auth_token' => api_token,
                                            'color' => color,
                                            'message_format' => 'html',
                                            'room_id' => channel,
                                            'message' => message })

      check_response_code(response, channel)
    end
  else
    ########## running on V2 ##########
    if user?(channel)
      channel.slice!(0)
      params = { 'message' => message, 'message_format' => 'html' }
      json_headers = { 'Content-Type' => 'application/json',
                       'Accept' => 'application/json', 'Authorization' => "Bearer #{api_token}" }

      uri = URI.parse("https://api.hipchat.com/v2/user/#{channel}/message")
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true

      response = http.post(uri.path, params.to_json, json_headers)
      check_response_code(response, channel)
    else
      uri = URI.parse("https://api.hipchat.com/v2/room/#{channel}/notification")
      response = Net::HTTP.post_form(uri, { 'from' => 'fastlane',
                                            'auth_token' => api_token,
                                            'color' => color,
                                            'message_format' => 'html',
                                            'message' => message })

      check_response_code(response, channel)
    end
  end
end

.user?(channel) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/fastlane/actions/hipchat.rb', line 62

def self.user?(channel)
  channel.to_s.start_with?('@')
end