Class: Fastlane::Actions::GithubApiAction

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

Constant Summary

Constants inherited from Fastlane::Action

Fastlane::Action::AVAILABLE_CATEGORIES, Fastlane::Action::RETURN_TYPES

Documentation collapse

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, author, deprecated_notes, lane_context, method_missing, other_action, return_type, sample_return_value, shell_out_should_use_bundle_exec?, step_text

Class Method Details

.authorsObject



161
162
163
# File 'fastlane/lib/fastlane/actions/github_api.rb', line 161

def authors
  ["tommeier"]
end

.available_optionsObject



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
146
147
# File 'fastlane/lib/fastlane/actions/github_api.rb', line 79

def available_options
  [
    FastlaneCore::ConfigItem.new(key: :server_url,
                                 env_name: "FL_GITHUB_API_SERVER_URL",
                                 description: "The server url. e.g. 'https://your.internal.github.host/api/v3' (Default: 'https://api.github.com')",
                                 default_value: "https://api.github.com",
                                 optional: true,
                                 verify_block: proc do |value|
                                   UI.user_error!("Please include the protocol in the server url, e.g. https://your.github.server/api/v3") unless value.include?("//")
                                 end),
    FastlaneCore::ConfigItem.new(key: :api_token,
                                 env_name: "FL_GITHUB_API_TOKEN",
                                 description: "Personal API Token for GitHub - generate one at https://github.com/settings/tokens",
                                 sensitive: true,
                                 code_gen_sensitive: true,
                                 is_string: true,
                                 default_value: ENV["GITHUB_API_TOKEN"],
                                 default_value_dynamic: true,
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :http_method,
                                 env_name: "FL_GITHUB_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_GITHUB_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_GITHUB_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_GITHUB_API_PATH",
                                 description: "The endpoint path. e.g. '/repos/:owner/:repo/readme'",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :url,
                                 env_name: "FL_GITHUB_API_URL",
                                 description: "The complete full url - used instead of path. e.g. 'https://uploads.github.com/repos/fastlane...'",
                                 optional: true,
                                 verify_block: proc do |value|
                                   UI.user_error!("Please include the protocol in the url, e.g. https://uploads.github.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_GITHUB_API_SECURE",
                                 description: "Optionally disable secure requests (ssl_verify_peer)",
                                 type: Boolean,
                                 default_value: true,
                                 optional: true)
  ]
end

.categoryObject



199
200
201
# File 'fastlane/lib/fastlane/actions/github_api.rb', line 199

def category
  :source_control
end

.descriptionObject



67
68
69
# File 'fastlane/lib/fastlane/actions/github_api.rb', line 67

def description
  "Call a GitHub API endpoint and get the resulting JSON response"
end

.detailsObject



71
72
73
74
75
76
77
# File 'fastlane/lib/fastlane/actions/github_api.rb', line 71

def details
  [
    "Calls any GitHub API endpoint. You must provide your GitHub Personal token (get one from [https://github.com/settings/tokens/new](https://github.com/settings/tokens/new)).",
    "Out parameters provide the status code and the full response JSON if valid, otherwise the raw response body.",
    "Documentation: [https://developer.github.com/v3](https://developer.github.com/v3)."
  ].join("\n")
end

.example_codeObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'fastlane/lib/fastlane/actions/github_api.rb', line 165

def example_code
  [
    'result = github_api(
    server_url: "https://api.github.com",
    api_token: ENV["GITHUB_TOKEN"],
    http_method: "GET",
    path: "/repos/:owner/:repo/readme",
    body: { ref: "master" }
  )',
    '# Alternatively call directly with optional error handling or block usage
    GithubApiAction.run(
      server_url: "https://api.github.com",
      api_token: ENV["GITHUB_TOKEN"],
      http_method: "GET",
      path: "/repos/:owner/:repo/readme",
      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

.is_supported?(platform) ⇒ Boolean

Returns:



195
196
197
# File 'fastlane/lib/fastlane/actions/github_api.rb', line 195

def is_supported?(platform)
  true
end

.outputObject



149
150
151
152
153
154
155
# File 'fastlane/lib/fastlane/actions/github_api.rb', line 149

def output
  [
    ['GITHUB_API_STATUS_CODE', 'The status code returned from the request'],
    ['GITHUB_API_RESPONSE', 'The full response body'],
    ['GITHUB_API_JSON', 'The parsed json returned from GitHub']
  ]
end

.return_valueObject



157
158
159
# File 'fastlane/lib/fastlane/actions/github_api.rb', line 157

def 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



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
61
# File 'fastlane/lib/fastlane/actions/github_api.rb', line 11

def run(params)
  require 'json'

  http_method = (params[:http_method] || 'GET').to_s.upcase
  url = construct_url(params[:server_url], params[:path], params[:url])
  headers = construct_headers(params[:api_token], params[:headers])
  payload = construct_body(params[:body], params[:raw_body])
  error_handlers = params[:error_handlers] || {}
  secure = params[:secure]

  response = call_endpoint(
    url,
    http_method,
    headers,
    payload,
    secure
  )

  status_code = response[:status]
  result = {
    status: status_code,
    body: response.body || "",
    json: 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}")
      UI.error("---")
      UI.error("Response:")
      UI.error(response.body)
      UI.user_error!("GitHub responded with #{status_code}\n---\n#{response.body}")
    end
  end

  Actions.lane_context[SharedValues::GITHUB_API_STATUS_CODE] = result[:status]
  Actions.lane_context[SharedValues::GITHUB_API_RESPONSE] = result[:body]
  Actions.lane_context[SharedValues::GITHUB_API_JSON] = result[:json]

  return result
end