@@ -8,63 +8,91 @@ module Actions
88 class SendBuildToBugsnagAction < Action
99 def self . run ( params )
1010 bugsnag_cli_path = BugsnagCli . get_bugsnag_cli_path ( params )
11- UI . verbose ( "Using bugsnag-cli from path: #{ bugsnag_cli_path } " )
12-
13- payload = { }
11+ verbose = UI . verbose ( "Using bugsnag-cli from path: #{ bugsnag_cli_path } " )
1412
1513 # If a configuration file was found or was specified, load in the options:
14+ config_options = { }
1615 if params [ :config_file ]
1716 UI . message ( "Loading build information from #{ params [ :config_file ] } " )
1817 config_options = load_config_file_options ( params [ :config_file ] )
19-
20- # for each of the config options, if it's not been overriden by any
21- # input to the lane, write it to the payload:
22- payload [ :apiKey ] = params [ :api_key ] || config_options [ :apiKey ]
23- payload [ :versionName ] = params [ :app_version ] || config_options [ :appVersion ]
24- payload [ :versionCode ] = params [ :android_version_code ] || config_options [ :appVersionCode ]
25- payload [ :bundleVersion ] = params [ :ios_bundle_version ] || config_options [ :appBundleVersion ]
26- payload [ :releaseStage ] = params [ :release_stage ] || config_options [ :releaseStage ] || "production"
27- else
28- # No configuration file was found or specified, use the input parameters:
29- payload [ :apiKey ] = params [ :api_key ]
30- payload [ :versionName ] = params [ :app_version ]
31- payload [ :versionCode ] = params [ :android_version_code ]
32- payload [ :bundleVersion ] = params [ :ios_bundle_version ]
33- payload [ :releaseStage ] = params [ :release_stage ] || "production"
3418 end
3519
36- # If builder, or source control information has been provided into
37- # Fastlane, apply it to the payload here.
38- payload [ :builderName ] = params [ :builder ] if params [ :builder ]
39- payload [ :revision ] = params [ :revision ] if params [ :revision ]
40- payload [ :repository ] = params [ :repository ] if params [ :repository ]
41- payload [ :provider ] = params [ :provider ] if params [ :provider ]
42-
43- payload [ :autoAssignRelease ] = params [ :auto_assign_release ] if params [ :auto_assign_release ]
44-
45- # If provided apply metadata to payload.
46- payload [ :metadata ] = params [ :metadata ]
47-
48- payload [ :retries ] = params [ :retries ] if params [ :retries ]
49- payload [ :timeout ] = params [ :timeout ] if params [ :timeout ]
50- payload [ :buildApiRootUrl ] = params [ :endpoint ] if params [ :endpoint ]
51-
52- payload . reject! { |k , v | v == nil || ( v . is_a? ( Hash ) && v . empty? ) }
53-
54- if payload [ :apiKey ] . nil? || !payload [ :apiKey ] . is_a? ( String )
20+ api_key = params [ :api_key ] || config_options [ :apiKey ] unless ( params [ :api_key ] . nil? && config_options [ :apiKey ] . nil? )
21+ version_name = params [ :app_version ] || config_options [ :appVersion ] unless ( params [ :app_version ] . nil? && config_options [ :appVersion ] . nil? )
22+ version_code = params [ :android_version_code ] || config_options [ :appVersionCode ] unless ( params [ :android_version_code ] . nil? && config_options [ :appVersionCode ] . nil? )
23+ bundle_version = params [ :ios_bundle_version ] || config_options [ :appBundleVersion ] unless ( params [ :ios_bundle_version ] . nil? && config_options [ :appBundleVersion ] . nil? )
24+ release_stage = params [ :release_stage ] || config_options [ :releaseStage ] || "production" unless ( params [ :release_stage ] . nil? && config_options [ :releaseStage ] . nil? )
25+ builder = params [ :builder ] unless params [ :builder ] . nil?
26+ revision = params [ :revision ] unless params [ :revision ] . nil?
27+ repository = params [ :repository ] unless params [ :repository ] . nil?
28+ provider = params [ :provider ] unless params [ :provider ] . nil?
29+ auto_assign_release = params [ :auto_assign_release ] unless params [ :auto_assign_release ] . nil?
30+ metadata = params [ :metadata ] unless params [ :metadata ] . nil?
31+ retries = params [ :retries ] unless params [ :retries ] . nil?
32+ timeout = params [ :timeout ] unless params [ :timeout ] . nil?
33+ endpoint = params [ :endpoint ] unless params [ :endpoint ] . nil?
34+
35+
36+ if api_key . nil? || !api_key . is_a? ( String )
5537 UI . user_error! missing_api_key_message ( params )
5638 end
57- if payload [ :versionName ] . nil?
39+ if version_name . nil?
5840 UI . user_error! missing_app_version_message ( params )
5941 end
6042
61- # If verbose flag is enabled (`--verbose`), display the payload debug info
62- UI . verbose ( "Sending build to Bugsnag with payload:" )
63- payload . each do |param |
64- UI . verbose ( " #{ param [ 0 ] . to_s . rjust ( 18 ) } : #{ param [ 1 ] } " )
43+ args = create_build_args (
44+ api_key ,
45+ version_name ,
46+ version_code ,
47+ bundle_version ,
48+ release_stage ,
49+ builder ,
50+ revision ,
51+ repository ,
52+ provider ,
53+ auto_assign_release ,
54+ metadata ,
55+ retries ,
56+ timeout ,
57+ endpoint ,
58+ verbose
59+ )
60+ bugsnag_cli_command = "#{ bugsnag_cli_path } create-build #{ args . join ( ' ' ) } "
61+ UI . verbose ( "Running command: #{ bugsnag_cli_command } " )
62+ success = Kernel . system ( bugsnag_cli_command )
63+ if success
64+ UI . success ( "Build successfully sent to Bugsnag" )
65+ else
66+ UI . user_error! ( "Failed to send build to Bugsnag." )
6567 end
68+ end
6669
67- send_notification ( bugsnag_cli_path , ::JSON . dump ( payload ) )
70+ def self . create_build_args ( api_key , version_name , version_code , bundle_version , release_stage , builder , revision , repository , provider , auto_assign_release , metadata , retries , timeout , endpoint , verbose )
71+ args = [ ]
72+ args += [ "--api-key" , api_key ] unless api_key . nil?
73+ args += [ "--version-name" , version_name ] unless version_name . nil?
74+ args += [ "--version-code" , version_code ] unless version_code . nil?
75+ args += [ "--bundle-version" , bundle_version ] unless bundle_version . nil?
76+ args += [ "--release-stage" , release_stage ] unless release_stage . nil?
77+ args += [ "--builder-name" , builder ] unless builder . nil?
78+ args += [ "--revision" , revision ] unless revision . nil?
79+ args += [ "--repository" , repository ] unless repository . nil?
80+ args += [ "--provider" , provider ] unless provider . nil?
81+ args += [ "--auto-assign-release" ] if auto_assign_release
82+ unless metadata . nil?
83+ if metadata . is_a? ( String )
84+ #
85+ args += [ "--metadata" , metadata ]
86+ elsif metadata . is_a? ( Hash )
87+ formatted_metadata = metadata . map { |k , v | %Q{"#{ k } "="#{ v } "} } . join ( "," )
88+ args += [ "--metadata" , formatted_metadata ]
89+ end
90+ end
91+ args += [ "--retries" , retries ] unless retries . nil?
92+ args += [ "--timeout" , timeout ] unless timeout . nil?
93+ args += [ "--build-api-root-url" , endpoint ] unless endpoint . nil?
94+ args += [ "--verbose" ] if verbose
95+ args
6896 end
6997
7098 def self . missing_api_key_message ( params )
@@ -182,8 +210,8 @@ def self.available_options
182210 end ) ,
183211 FastlaneCore ::ConfigItem . new ( key : :endpoint ,
184212 description : "Bugsnag deployment endpoint" ,
185- optional : true ,
186- default_value : "https://build.bugsnag.com" ) ,
213+ default_value : nil ,
214+ optional : true ) ,
187215 FastlaneCore ::ConfigItem . new ( key : :metadata ,
188216 description : "Metadata" ,
189217 optional :true ,
@@ -221,7 +249,8 @@ def self.load_git_remote_options
221249 git_options [ :repository ] = origin . url
222250 git_options [ :revision ] = repo . revparse ( "HEAD" )
223251 end
224- rescue
252+ rescue => e
253+ UI . verbose ( "Could not load git options: #{ e . message } " )
225254 end
226255 return git_options
227256 end
@@ -343,46 +372,6 @@ def self.map_meta_data(meta_data)
343372 end
344373 output
345374 end
346-
347- def self . parse_response_body ( response )
348- begin
349- JSON . load ( response . body )
350- rescue
351- nil
352- end
353- end
354-
355- def self . to_kebab_case ( str )
356- str . gsub ( /([a-z])([A-Z])/ , '\1-\2' ) . downcase
357- end
358-
359- def self . json_to_cli_args ( json_payload )
360- data = JSON . parse ( json_payload )
361-
362- data . map do |k , v |
363- key = to_kebab_case ( k )
364- if v . is_a? ( Hash )
365- # Convert nested hash into key=value pairs joined by commas
366- nested = v . map { |nk , nv | "#{ to_kebab_case ( nk ) } =#{ nv } " } . join ( "," )
367- "--#{ key } =#{ nested } "
368- else
369- "--#{ key } =#{ v } "
370- end
371- end . join ( " " )
372- end
373-
374- def self . send_notification ( cli_path , body )
375- args = self . json_to_cli_args ( body )
376- bugsnag_cli_command = "#{ cli_path } create-build #{ args } "
377-
378- UI . verbose ( "Running command: #{ bugsnag_cli_command } " )
379- success = Kernel . system ( bugsnag_cli_command )
380- if success
381- UI . success ( "Build successfully sent to Bugsnag" )
382- else
383- UI . user_error! ( "Failed to send build to Bugsnag." )
384- end
385- end
386375 end
387376 end
388377end
0 commit comments