Class: Fastlane::Helper::GithubApiHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/github_api/helper/github_api_helper.rb

Class Method Summary collapse

Class Method Details

.github_api_request(token: nil, path:, params: nil, method: :get, server_url: nil, headers: {}) ⇒ Hash

Make a request to the GitHub API

Parameters:

  • token (String) (defaults to: nil)

    GitHub API token

  • path (String)

    API endpoint path

  • params (Hash) (defaults to: nil)

    Query parameters for GET or body parameters for POST/PUT/PATCH

  • method (Symbol) (defaults to: :get)

    HTTP method (:get, :post, :put, :patch, :delete)

  • server_url (String) (defaults to: nil)

    GitHub API server URL

  • headers (Hash) (defaults to: {})

    Additional headers to include in the request

Returns:

  • (Hash)

    Response from the GitHub API with :status, :body, and :json keys



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
64
65
66
67
68
69
70
71
72
73
74
75
76
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
# File 'lib/fastlane/plugin/github_api/helper/github_api_helper.rb', line 18

def github_api_request(token: nil, path:, params: nil, method: :get, server_url: nil, headers: {})
  require 'json'
  
  # Validate parameters
  UI.user_error!("No GitHub API token given, pass using `token: 'token'`") if token.to_s.empty?
  UI.user_error!("GitHub API path cannot be empty") if path.to_s.empty?
  UI.user_error!("GitHub API server URL cannot be empty") if server_url.to_s.empty?
  
  # Set up headers
  request_headers = headers.clone || {}
  request_headers['User-Agent'] = 'fastlane-github_api'
  request_headers['Authorization'] = "token #{token}" if token
  request_headers['Accept'] = 'application/vnd.github.v3+json' unless request_headers.key?('Accept')
  
  # Handle query parameters for GET requests
  if method == :get && params && !params.empty?
    query_string = URI.encode_www_form(params)
    path = "#{path}?#{query_string}"
    params = nil
  end
  
  # Prepare the URL
  url = "#{server_url}#{path}"
  
  begin
    UI.verbose("#{method.to_s.upcase} : #{url}")
    
    # Set up Excon options
    options = {
      headers: request_headers,
      middlewares: Excon.defaults[:middlewares] + [Excon::Middleware::RedirectFollower],
      debug_request: FastlaneCore::Globals.verbose?,
      debug_response: FastlaneCore::Globals.verbose?
    }
    
    # Add body for non-GET requests if params are provided
    if method != :get && params
      if params.is_a?(Hash) || params.is_a?(Array)
        options[:body] = params.to_json
        request_headers['Content-Type'] = 'application/json'
      else
        options[:body] = params
      end
    end
    
    # Make the request
    connection = Excon.new(url)
    response = connection.request(
      method: method,
      **options
    )
    
    status_code = response.status
    response_body = response.body.to_s
    
    # Parse JSON response if available
    json_response = nil
    if !response_body.empty? && response.headers['Content-Type'] && response.headers['Content-Type'].include?('application/json')
      json_response = parse_json(response_body) || {}
    end
    
    # Create the response hash with both new format and backward compatibility
    result = {
      status: status_code,
      body: response_body,
      json: json_response
    }
    
    # Add backward compatibility for error handling
    # Copy json values to top level for backwards compatibility
    if json_response.is_a?(Hash)
      json_response.each do |key, value|
        result[key] = value
      end
    end
    
    # Add status code at the top level for backward compatibility
    result['status'] = status_code
    
    return result
  rescue => ex
    error_result = {
      status: 0,
      body: ex.message,
      json: { 'error' => "Network Error: #{ex.message}" }
    }
    
    # Add backward compatibility for error handling
    error_result['error'] = "Network Error: #{ex.message}"
    error_result['status'] = 0
    
    return error_result
  end
end