Class: Fastlane::Actions::MackerelApiAction
- Inherits:
-
Action
- Object
- Action
- Fastlane::Actions::MackerelApiAction
- Defined in:
- lib/fastlane/plugin/mackerel_api/actions/mackerel_api_action.rb
Class Method Summary collapse
- .authors ⇒ Object
- .available_options ⇒ Object
- .description ⇒ Object
- .details ⇒ Object
- .is_supported?(platform) ⇒ Boolean
- .output ⇒ Object
- .return_value ⇒ Object
- .run(params) ⇒ Object
Instance Method Summary collapse
Class Method Details
.authors ⇒ Object
189 190 191 |
# File 'lib/fastlane/plugin/mackerel_api/actions/mackerel_api_action.rb', line 189 def self. ["yutailang0119"] end |
.available_options ⇒ Object
77 78 79 80 81 82 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/fastlane/plugin/mackerel_api/actions/mackerel_api_action.rb', line 77 def self. [ FastlaneCore::ConfigItem.new(key: :server_url, env_name: "FL_MACKEREL_API_SERVER_URL", description: "The server url. e.g. 'https://api.mackerelio.com'", default_value: "https://api.mackerelio.com", optional: true, verify_block: proc do |value| UI.user_error!("Please include the protocol in the server url, e.g. https://api.mackerelio.com") unless value.include?("//") end), FastlaneCore::ConfigItem.new(key: :api_key, env_name: "FL_MACKEREL_API_KEY", description: "API key for Mackerel - generate one at https://mackerel.io/my?tab=apikeys", sensitive: true, code_gen_sensitive: true, is_string: true, default_value: ENV["MACKEREL_API_KEY"], default_value_dynamic: true, optional: false), FastlaneCore::ConfigItem.new(key: :http_method, env_name: "FL_MACKEREL_API_HTTP_METHOD", description: "The HTTP method. e.g. GET / POST", default_value: "GET", optional: true, verify_block: proc do |value| unless %w(GET POST PUT DELETE HEAD CONNECT PATCH).include?(value.to_s.upcase) UI.user_error!("Unrecognised HTTP method") end end), FastlaneCore::ConfigItem.new(key: :body, env_name: "FL_MACKEREL_API_REQUEST_BODY", description: "The request body in JSON or hash format", is_string: false, default_value: {}, optional: true), FastlaneCore::ConfigItem.new(key: :raw_body, env_name: "FL_MACKEREL_API_REQUEST_RAW_BODY", description: "The request body taken verbatim instead of as JSON, useful for file uploads", is_string: true, optional: true), FastlaneCore::ConfigItem.new(key: :path, env_name: "FL_MACKEREL_API_PATH", description: "The endpoint path. e.g. '/api/v0/hosts/<hostId>/metadata/<namespace>'", optional: true), FastlaneCore::ConfigItem.new(key: :url, env_name: "FL_MACKEREL_API_URL", description: "The complete full url - used instead of path. e.g. 'https://api.mackerelio.com/api/v0/services'", optional: true, verify_block: proc do |value| UI.user_error!("Please include the protocol in the url, e.g. https://api.mackerelio.com") unless value.include?("//") end), FastlaneCore::ConfigItem.new(key: :error_handlers, description: "Optional error handling hash based on status code, or pass '*' to handle all errors", is_string: false, default_value: {}, optional: true), FastlaneCore::ConfigItem.new(key: :headers, description: "Optional headers to apply", is_string: false, default_value: {}, optional: true), FastlaneCore::ConfigItem.new(key: :secure, env_name: "FL_MACKEREL_API_SECURE", description: "Optionally disable secure requests (ssl_verify_peer)", type: Boolean, default_value: true, optional: true) ] end |
.description ⇒ Object
65 66 67 |
# File 'lib/fastlane/plugin/mackerel_api/actions/mackerel_api_action.rb', line 65 def self.description "Call a Mackerel API endpoint and get the resulting JSON response" end |
.details ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/fastlane/plugin/mackerel_api/actions/mackerel_api_action.rb', line 69 def self.details [ "Call a [Mackerel](https://mackerel.io) API endpoint and get the resulting JSON response.", "You must provide your Mackerel API key (get one from [Dashboard](https://mackerel.io/my?tab=apikeys)).", "Documentation: [Mackerel API Documents](https://mackerel.io/api-docs)." ].join("\n") end |
.is_supported?(platform) ⇒ Boolean
193 194 195 |
# File 'lib/fastlane/plugin/mackerel_api/actions/mackerel_api_action.rb', line 193 def self.is_supported?(platform) true end |
.output ⇒ Object
147 148 149 150 151 152 153 |
# File 'lib/fastlane/plugin/mackerel_api/actions/mackerel_api_action.rb', line 147 def self.output [ ['MACKEREL_API_STATUS_CODE', 'The status code returned from the request'], ['MACKEREL_API_RESPONSE', 'The full response body'], ['MACKEREL_API_JSON', 'The parsed json returned from Mackerel'] ] end |
.return_value ⇒ Object
155 156 157 |
# File 'lib/fastlane/plugin/mackerel_api/actions/mackerel_api_action.rb', line 155 def self.return_value "A hash including the HTTP status code (:status), the response body (:body), and if valid JSON has been returned the parsed JSON (:json)." end |
.run(params) ⇒ Object
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 61 62 63 |
# File 'lib/fastlane/plugin/mackerel_api/actions/mackerel_api_action.rb', line 13 def self.run(params) require 'json' http_method = (params[:http_method] || 'GET').to_s.upcase url = Helper::MackerelApiHelper.construct_url(params[:server_url], params[:path], params[:url]) headers = Helper::MackerelApiHelper.construct_headers(params[:api_key], params[:headers]) payload = Helper::MackerelApiHelper.construct_body(params[:body], params[:raw_body]) error_handlers = params[:error_handlers] || {} secure = params[:secure] response = Helper::MackerelApiHelper.call_endpoint( url, http_method, headers, payload, secure ) status_code = response[:status] result = { status: status_code, body: response.body || "", json: Helper::MackerelApiHelper.parse_json(response.body) || {} } if status_code.between?(200, 299) UI.verbose("Response:") UI.verbose(response.body) UI.verbose("---") yield(result) if block_given? else handled_error = error_handlers[status_code] || error_handlers['*'] if handled_error handled_error.call(result) else UI.error("---") UI.error("Request failed:\n#{http_method}: #{url}") UI.error("Headers:\n#{headers.map { |key, value| key == 'X-Api-Key' ? ['X-Api-Key', '********'] : [key, value] }.to_h}") UI.error("---") UI.error("Response:") UI.error(response.body) UI.user_error!("Mackerel responded with #{status_code}\n---\n#{response.body}") end end Actions.lane_context[SharedValues::MACKEREL_API_STATUS_CODE] = result[:status] Actions.lane_context[SharedValues::MACKEREL_API_RESPONSE] = result[:body] Actions.lane_context[SharedValues::MACKEREL_API_JSON] = result[:json] return result end |
Instance Method Details
#category ⇒ Object
197 198 199 |
# File 'lib/fastlane/plugin/mackerel_api/actions/mackerel_api_action.rb', line 197 def category :source_control end |
#example_code ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/fastlane/plugin/mackerel_api/actions/mackerel_api_action.rb', line 159 def example_code [ 'result = mackerel_api( server_url: "https://api.mackerelio.com", api_key: ENV["MACKEREL_API_KEY"], http_method: "POST", path: "api/v0/services", body: { "name": "ExampleService", "memo": "This is an example." } )', '# Alternatively call directly with optional error handling or block usage MackerelApiAction.run( server_url: "https://api.mackerelio.com", api_key: ENV["MACKEREL_API_KEY"], http_method: "POST", path: "api/v0/services", error_handlers: { 404 => proc do |result| UI.message("Something went wrong - I couldn\'t find it...") end, \'*\' => proc do |result| UI.message("Handle all error codes other than 404") end } ) do |result| UI.message("JSON returned: #{result[:json]}") end ' ] end |