fastlane-plugin-mackerel_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cbe5875464d547d2277a5e03c38b604814bad5b1ed029d94e884c2ee854f48cf
4
+ data.tar.gz: d3e855c46a80244909a0b34a835e4e0de369c9ef373598b0feaff400fd693f1d
5
+ SHA512:
6
+ metadata.gz: b1c0f9796dce60017b6b7829006744f9f08c043bcf599ca26a60941a0de5817dc811a65d737b1352493fbd341923e1d6c812b26963928569d85dcc47322ee75d
7
+ data.tar.gz: 6a067fb6a499b732ea5a2c2ea4ffab4d493641063848f95036f463733537d87e389b51983b85b1da115e41f6a345706d89c5c1cdbb23a74707c3e5a446c145bb
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 yutailang0119 <muta.yutaro@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # mackerel_api plugin
2
+
3
+ [![fastlane Plugin Badge](https://rawcdn.githack.com/fastlane/fastlane/master/fastlane/assets/plugin-badge.svg)](https://rubygems.org/gems/fastlane-plugin-mackerel_api)
4
+ ![Test](https://github.com/yutailang0119/fastlane-plugin-mackerel_api/workflows/Test/badge.svg)
5
+
6
+ ## Getting Started
7
+
8
+ This project is a [_fastlane_](https://github.com/fastlane/fastlane) plugin. To get started with `fastlane-plugin-mackerel_api`, add it to your project by running:
9
+
10
+ ```bash
11
+ fastlane add_plugin mackerel_api
12
+ ```
13
+
14
+ ## About mackerel_api
15
+
16
+ Call a [Mackerel](https://mackerel.io) API endpoint and get the resulting JSON response.
17
+
18
+ Documentation: [Mackerel API Documents](https://mackerel.io/api-docs).
19
+
20
+ ## Example
21
+
22
+ Check out the [example `Fastfile`](fastlane/Fastfile) to see how to use this plugin. Try it by cloning the repo, running `fastlane install_plugins` and `bundle exec fastlane test`.
23
+
24
+
25
+ ### Usage
26
+
27
+ ```ruby
28
+ result = mackerel_api(
29
+ server_url: "https://api.mackerelio.com",
30
+ api_key: ENV["MACKEREL_API_KEY"],
31
+ http_method: "POST",
32
+ path: "api/v0/services",
33
+ body: { "name": "ExampleService", "memo": "This is an example." }
34
+ )
35
+ ```
36
+
37
+ ```ruby
38
+ # Alternatively call directly with optional error handling or block usage
39
+ MackerelApiAction.run(
40
+ server_url: "https://api.mackerelio.com",
41
+ api_key: ENV["MACKEREL_API_KEY"],
42
+ http_method: "POST",
43
+ path: "api/v0/services",
44
+ body: { "name": "ExampleService", "memo": "This is an example." },
45
+ error_handlers: {
46
+ 404 => proc do |result|
47
+ UI.message("Something went wrong - I couldn\'t find it...")
48
+ end,
49
+ '*' => proc do |result|
50
+ UI.message("Handle all error codes other than 404")
51
+ end
52
+ }
53
+ ) do |result|
54
+ UI.message("JSON returned: #{result[:json]}")
55
+ end
56
+ ```
57
+
58
+ ## Run tests for this plugin
59
+
60
+ To run both the tests, and code style validation, run
61
+
62
+ ```
63
+ rake
64
+ ```
65
+
66
+ To automatically fix many of the styling issues, use
67
+ ```
68
+ rubocop -a
69
+ ```
70
+
71
+ ## Issues and Feedback
72
+
73
+ For any other issues and feedback about this plugin, please submit it to this repository.
74
+
75
+ ## Troubleshooting
76
+
77
+ If you have trouble using plugins, check out the [Plugins Troubleshooting](https://docs.fastlane.tools/plugins/plugins-troubleshooting/) guide.
78
+
79
+ ## Using _fastlane_ Plugins
80
+
81
+ For more information about how the `fastlane` plugin system works, check out the [Plugins documentation](https://docs.fastlane.tools/plugins/create-plugin/).
82
+
83
+ ## About _fastlane_
84
+
85
+ _fastlane_ is the easiest way to automate beta deployments and releases for your iOS and Android apps. To learn more, check out [fastlane.tools](https://fastlane.tools).
@@ -0,0 +1,201 @@
1
+ require 'fastlane/action'
2
+ require_relative '../helper/mackerel_api_helper'
3
+
4
+ module Fastlane
5
+ module Actions
6
+ module SharedValues
7
+ MACKEREL_API_STATUS_CODE = :MACKEREL_API_STATUS_CODE
8
+ MACKEREL_API_RESPONSE = :MACKEREL_API_RESPONSE
9
+ MACKEREL_API_JSON = :MACKEREL_API_JSON
10
+ end
11
+
12
+ class MackerelApiAction < Action
13
+ def self.run(params)
14
+ require 'json'
15
+
16
+ http_method = (params[:http_method] || 'GET').to_s.upcase
17
+ url = Helper::MackerelApiHelper.construct_url(params[:server_url], params[:path], params[:url])
18
+ headers = Helper::MackerelApiHelper.construct_headers(params[:api_key], params[:headers])
19
+ payload = Helper::MackerelApiHelper.construct_body(params[:body], params[:raw_body])
20
+ error_handlers = params[:error_handlers] || {}
21
+ secure = params[:secure]
22
+
23
+ response = Helper::MackerelApiHelper.call_endpoint(
24
+ url,
25
+ http_method,
26
+ headers,
27
+ payload,
28
+ secure
29
+ )
30
+
31
+ status_code = response[:status]
32
+ result = {
33
+ status: status_code,
34
+ body: response.body || "",
35
+ json: Helper::MackerelApiHelper.parse_json(response.body) || {}
36
+ }
37
+
38
+ if status_code.between?(200, 299)
39
+ UI.verbose("Response:")
40
+ UI.verbose(response.body)
41
+ UI.verbose("---")
42
+ yield(result) if block_given?
43
+ else
44
+ handled_error = error_handlers[status_code] || error_handlers['*']
45
+ if handled_error
46
+ handled_error.call(result)
47
+ else
48
+ UI.error("---")
49
+ UI.error("Request failed:\n#{http_method}: #{url}")
50
+ UI.error("Headers:\n#{headers}")
51
+ UI.error("---")
52
+ UI.error("Response:")
53
+ UI.error(response.body)
54
+ UI.user_error!("Mackerel responded with #{status_code}\n---\n#{response.body}")
55
+ end
56
+ end
57
+
58
+ Actions.lane_context[SharedValues::MACKEREL_API_STATUS_CODE] = result[:status]
59
+ Actions.lane_context[SharedValues::MACKEREL_API_RESPONSE] = result[:body]
60
+ Actions.lane_context[SharedValues::MACKEREL_API_JSON] = result[:json]
61
+
62
+ return result
63
+ end
64
+
65
+ def self.description
66
+ "Call a Mackerel API endpoint and get the resulting JSON response"
67
+ end
68
+
69
+ def self.details
70
+ [
71
+ "Call a [Mackerel](https://mackerel.io) API endpoint and get the resulting JSON response.",
72
+ "You must provide your Mackerel API key (get one from [Dashboard](https://mackerel.io/my?tab=apikeys)).",
73
+ "Documentation: [Mackerel API Documents](https://mackerel.io/api-docs)."
74
+ ].join("\n")
75
+ end
76
+
77
+ def self.available_options
78
+ [
79
+ FastlaneCore::ConfigItem.new(key: :server_url,
80
+ env_name: "FL_MACKEREL_API_SERVER_URL",
81
+ description: "The server url. e.g. 'https://api.mackerelio.com'",
82
+ default_value: "https://api.mackerelio.com",
83
+ optional: true,
84
+ verify_block: proc do |value|
85
+ UI.user_error!("Please include the protocol in the server url, e.g. https://api.mackerelio.com") unless value.include?("//")
86
+ end),
87
+ FastlaneCore::ConfigItem.new(key: :api_key,
88
+ env_name: "FL_MACKEREL_API_KEY",
89
+ description: "API key for Mackerel - generate one at https://mackerel.io/my?tab=apikeys",
90
+ sensitive: true,
91
+ code_gen_sensitive: true,
92
+ is_string: true,
93
+ default_value_dynamic: true,
94
+ optional: false),
95
+ FastlaneCore::ConfigItem.new(key: :http_method,
96
+ env_name: "FL_MACKEREL_API_HTTP_METHOD",
97
+ description: "The HTTP method. e.g. GET / POST",
98
+ default_value: "GET",
99
+ optional: true,
100
+ verify_block: proc do |value|
101
+ unless %w(GET POST PUT DELETE HEAD CONNECT PATCH).include?(value.to_s.upcase)
102
+ UI.user_error!("Unrecognised HTTP method")
103
+ end
104
+ end),
105
+ FastlaneCore::ConfigItem.new(key: :body,
106
+ env_name: "FL_MACKEREL_API_REQUEST_BODY",
107
+ description: "The request body in JSON or hash format",
108
+ is_string: false,
109
+ default_value: {},
110
+ optional: true),
111
+ FastlaneCore::ConfigItem.new(key: :raw_body,
112
+ env_name: "FL_MACKEREL_API_REQUEST_RAW_BODY",
113
+ description: "The request body taken verbatim instead of as JSON, useful for file uploads",
114
+ is_string: true,
115
+ optional: true),
116
+ FastlaneCore::ConfigItem.new(key: :path,
117
+ env_name: "FL_MACKEREL_API_PATH",
118
+ description: "The endpoint path. e.g. '/api/v0/hosts/<hostId>/metadata/<namespace>'",
119
+ optional: true),
120
+ FastlaneCore::ConfigItem.new(key: :url,
121
+ env_name: "FL_MACKEREL_API_URL",
122
+ description: "The complete full url - used instead of path. e.g. 'https://api.mackerelio.com/api/v0/services'",
123
+ optional: true,
124
+ verify_block: proc do |value|
125
+ UI.user_error!("Please include the protocol in the url, e.g. https://api.mackerelio.com") unless value.include?("//")
126
+ end),
127
+ FastlaneCore::ConfigItem.new(key: :error_handlers,
128
+ description: "Optional error handling hash based on status code, or pass '*' to handle all errors",
129
+ is_string: false,
130
+ default_value: {},
131
+ optional: true),
132
+ FastlaneCore::ConfigItem.new(key: :headers,
133
+ description: "Optional headers to apply",
134
+ is_string: false,
135
+ default_value: {},
136
+ optional: true),
137
+ FastlaneCore::ConfigItem.new(key: :secure,
138
+ env_name: "FL_MACKEREL_API_SECURE",
139
+ description: "Optionally disable secure requests (ssl_verify_peer)",
140
+ type: Boolean,
141
+ default_value: true,
142
+ optional: true)
143
+ ]
144
+ end
145
+
146
+ def self.output
147
+ [
148
+ ['MACKEREL_API_STATUS_CODE', 'The status code returned from the request'],
149
+ ['MACKEREL_API_RESPONSE', 'The full response body'],
150
+ ['MACKEREL_API_JSON', 'The parsed json returned from Mackerel']
151
+ ]
152
+ end
153
+
154
+ def self.return_value
155
+ "A hash including the HTTP status code (:status), the response body (:body), and if valid JSON has been returned the parsed JSON (:json)."
156
+ end
157
+
158
+ def example_code
159
+ [
160
+ 'result = mackerel_api(
161
+ server_url: "https://api.mackerelio.com",
162
+ api_key: ENV["MACKEREL_API_KEY"],
163
+ http_method: "POST",
164
+ path: "api/v0/services",
165
+ body: { "name": "ExampleService", "memo": "This is an example." }
166
+ )',
167
+ '# Alternatively call directly with optional error handling or block usage
168
+ MackerelApiAction.run(
169
+ server_url: "https://api.mackerelio.com",
170
+ api_key: ENV["MACKEREL_API_KEY"],
171
+ http_method: "POST",
172
+ path: "api/v0/services",
173
+ error_handlers: {
174
+ 404 => proc do |result|
175
+ UI.message("Something went wrong - I couldn\'t find it...")
176
+ end,
177
+ \'*\' => proc do |result|
178
+ UI.message("Handle all error codes other than 404")
179
+ end
180
+ }
181
+ ) do |result|
182
+ UI.message("JSON returned: #{result[:json]}")
183
+ end
184
+ '
185
+ ]
186
+ end
187
+
188
+ def self.authors
189
+ ["yutailang0119"]
190
+ end
191
+
192
+ def self.is_supported?(platform)
193
+ true
194
+ end
195
+
196
+ def category
197
+ :source_control
198
+ end
199
+ end
200
+ end
201
+ end
@@ -0,0 +1,53 @@
1
+ module Fastlane
2
+ module Helper
3
+ class MackerelApiHelper
4
+ def self.construct_headers(api_key, overrides)
5
+ headers = { 'User-Agent' => 'fastlane-mackerel_api' }
6
+ headers['X-Api-Key'] = api_key if api_key
7
+ headers.merge(overrides || {})
8
+ end
9
+
10
+ def self.construct_url(server_url, path, url)
11
+ return_url = (server_url && path) ? File.join(server_url, path) : url
12
+ UI.user_error!("Please provide either `server_url` (e.g. https://api.mackerelio.com) and 'path' or full 'url' for Mackerel API endpoint") unless return_url
13
+ return_url
14
+ end
15
+
16
+ def self.construct_body(body, raw_body)
17
+ body ||= {}
18
+ if raw_body
19
+ raw_body
20
+ elsif body.kind_of?(Hash)
21
+ body.to_json
22
+ elsif body.kind_of?(Array)
23
+ body.to_json
24
+ else
25
+ UI.user_error!("Please provide valid JSON, or a hash as request body") unless parse_json(body)
26
+ body
27
+ end
28
+ end
29
+
30
+ def self.parse_json(value)
31
+ JSON.parse(value)
32
+ rescue JSON::ParserError
33
+ nil
34
+ end
35
+
36
+ def self.call_endpoint(url, http_method, headers, body, secure)
37
+ require 'excon'
38
+ Excon.defaults[:ssl_verify_peer] = secure
39
+ middlewares = Excon.defaults[:middlewares] + [Excon::Middleware::RedirectFollower] # allow redirect in case of repo renames
40
+ UI.verbose("#{http_method} : #{url}")
41
+ connection = Excon.new(url)
42
+ connection.request(
43
+ method: http_method,
44
+ headers: headers,
45
+ middlewares: middlewares,
46
+ body: body,
47
+ debug_request: FastlaneCore::Globals.verbose?,
48
+ debug_response: FastlaneCore::Globals.verbose?
49
+ )
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ module Fastlane
2
+ module MackerelApi
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ require 'fastlane/plugin/mackerel_api/version'
2
+
3
+ module Fastlane
4
+ module MackerelApi
5
+ # Return all .rb files inside the "actions" and "helper" directory
6
+ def self.all_classes
7
+ Dir[File.expand_path('**/{actions,helper}/*.rb', File.dirname(__FILE__))]
8
+ end
9
+ end
10
+ end
11
+
12
+ # By default we want to import all available actions and helpers
13
+ # A plugin can contain any number of actions and plugins
14
+ Fastlane::MackerelApi.all_classes.each do |current|
15
+ require current
16
+ end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fastlane-plugin-mackerel_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yutailang0119
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec_junit_formatter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 0.49.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.49.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-require_tools
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: fastlane
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: 2.146.1
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: 2.146.1
139
+ description:
140
+ email: muta.yutaro@gmail.com
141
+ executables: []
142
+ extensions: []
143
+ extra_rdoc_files: []
144
+ files:
145
+ - LICENSE
146
+ - README.md
147
+ - lib/fastlane/plugin/mackerel_api.rb
148
+ - lib/fastlane/plugin/mackerel_api/actions/mackerel_api_action.rb
149
+ - lib/fastlane/plugin/mackerel_api/helper/mackerel_api_helper.rb
150
+ - lib/fastlane/plugin/mackerel_api/version.rb
151
+ homepage: https://github.com/yutailang0119/fastlane-plugin-mackerel_api
152
+ licenses:
153
+ - MIT
154
+ metadata: {}
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubygems_version: 3.0.3
171
+ signing_key:
172
+ specification_version: 4
173
+ summary: Call a Mackerel API endpoint and get the resulting JSON response
174
+ test_files: []