fastlane/lib/fastlane/actions/onesignal.rb in fastlane-2.132.0.beta.20190920200012 vs fastlane/lib/fastlane/actions/onesignal.rb in fastlane-2.132.0.beta.20190921200021

- old
+ new

@@ -9,20 +9,31 @@ def self.run(params) require 'net/http' require 'uri' require 'base64' - UI.message("Parameter App name: #{params[:app_name]}") + app_id = params[:app_id].to_s.strip auth_token = params[:auth_token] - app_name = params[:app_name] + app_name = params[:app_name].to_s apns_p12_password = params[:apns_p12_password] android_token = params[:android_token] android_gcm_sender_id = params[:android_gcm_sender_id] + has_app_id = !app_id.empty? + has_app_name = !app_name.empty? + + is_update = has_app_id + + UI.user_error!('Please specify the `app_id` or the `app_name` parameters!') if !has_app_id && !has_app_name + + UI.message("Parameter App ID: #{app_id}") if has_app_id + UI.message("Parameter App name: #{app_name}") if has_app_name + payload = {} - payload['name'] = app_name + payload['name'] = app_name if has_app_name + unless params[:apns_p12].nil? data = File.read(params[:apns_p12]) apns_p12 = Base64.encode64(data) payload["apns_env"] = params[:apns_env] payload["apns_p12"] = apns_p12 @@ -31,65 +42,74 @@ end payload["gcm_key"] = android_token unless android_token.nil? payload["android_gcm_sender_id"] = android_gcm_sender_id unless android_gcm_sender_id.nil? - # here's the actual lifting - POST to OneSignal + # here's the actual lifting - POST or PUT to OneSignal json_headers = { 'Content-Type' => 'application/json', 'Authorization' => "Basic #{auth_token}" } - uri = URI.parse('https://onesignal.com/api/v1/apps') + url = +'https://onesignal.com/api/v1/apps' + url << '/' + app_id if is_update + uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true - response = http.post(uri.path, payload.to_json, json_headers) + + if is_update + response = http.put(uri.path, payload.to_json, json_headers) + else + response = http.post(uri.path, payload.to_json, json_headers) + end + response_body = JSON.parse(response.body) Actions.lane_context[SharedValues::ONE_SIGNAL_APP_ID] = response_body["id"] Actions.lane_context[SharedValues::ONE_SIGNAL_APP_AUTH_KEY] = response_body["basic_auth_key"] - check_response_code(response) + check_response_code(response, is_update) end - def self.check_response_code(response) + def self.check_response_code(response, is_update) case response.code.to_i when 200, 204 - puts("Successfully created new OneSignal app".green) + UI.success("Successfully #{is_update ? 'updated' : 'created new'} OneSignal app") else UI.user_error!("Unexpected #{response.code} with response: #{response.body}") end end def self.description - "Create a new [OneSignal](https://onesignal.com/) application" + "Create or update a new [OneSignal](https://onesignal.com/) application" end def self.details - "You can use this action to automatically create a OneSignal application. You can also upload a `.p12` with password, a GCM key, or both." + "You can use this action to automatically create or update a OneSignal application. You can also upload a `.p12` with password, a GCM key, or both." end def self.available_options [ - FastlaneCore::ConfigItem.new(key: :auth_token, - env_name: "ONE_SIGNAL_AUTH_KEY", - sensitive: true, - description: "OneSignal Authorization Key", - verify_block: proc do |value| - unless value.to_s.length > 0 - UI.error("Please add 'ENV[\"ONE_SIGNAL_AUTH_KEY\"] = \"your token\"' to your Fastfile's `before_all` section.") - UI.user_error!("No ONE_SIGNAL_AUTH_KEY given.") - end - end), + FastlaneCore::ConfigItem.new(key: :app_id, + env_name: "ONE_SIGNAL_APP_ID", + sensitive: true, + description: "OneSignal App ID. Setting this updates an existing app", + optional: true), - FastlaneCore::ConfigItem.new(key: :app_name, - env_name: "ONE_SIGNAL_APP_NAME", - description: "OneSignal App Name", + FastlaneCore::ConfigItem.new(key: :auth_token, + env_name: "ONE_SIGNAL_AUTH_KEY", + sensitive: true, + description: "OneSignal Authorization Key", verify_block: proc do |value| - unless value.to_s.length > 0 - UI.error("Please add 'ENV[\"ONE_SIGNAL_APP_NAME\"] = \"Your app name\"' to your Fastfile's `before_all` section.") - UI.user_error!("No ONE_SIGNAL_APP_NAME given.") + if value.to_s.empty? + UI.error("Please add 'ENV[\"ONE_SIGNAL_AUTH_KEY\"] = \"your token\"' to your Fastfile's `before_all` section.") + UI.user_error!("No ONE_SIGNAL_AUTH_KEY given.") end end), + FastlaneCore::ConfigItem.new(key: :app_name, + env_name: "ONE_SIGNAL_APP_NAME", + description: "OneSignal App Name. This is required when creating an app (in other words, when `:app_id` is not set, and optional when updating an app", + optional: true), + FastlaneCore::ConfigItem.new(key: :android_token, env_name: "ANDROID_TOKEN", description: "ANDROID GCM KEY", sensitive: true, optional: true), @@ -119,12 +139,12 @@ ] end def self.output [ - ['ONE_SIGNAL_APP_ID', 'The OneSignal app ID of the newly created app'], - ['ONE_SIGNAL_APP_AUTH_KEY', 'The auth token for the newly created OneSignal app'] + ['ONE_SIGNAL_APP_ID', 'The app ID of the newly created or updated app'], + ['ONE_SIGNAL_APP_AUTH_KEY', 'The auth token for the newly created or updated app'] ] end def self.authors ["timothybarraclough", "smartshowltd"] @@ -137,9 +157,19 @@ def self.example_code [ 'onesignal( auth_token: "Your OneSignal Auth Token", app_name: "Name for OneSignal App", + android_token: "Your Android GCM key (optional)", + android_gcm_sender_id: "Your Android GCM Sender ID (optional)", + apns_p12: "Path to Apple .p12 file (optional)", + apns_p12_password: "Password for .p12 file (optional)", + apns_env: "production/sandbox (defaults to production)" + )', + 'onesignal( + app_id: "Your OneSignal App ID", + auth_token: "Your OneSignal Auth Token", + app_name: "New Name for OneSignal App", android_token: "Your Android GCM key (optional)", android_gcm_sender_id: "Your Android GCM Sender ID (optional)", apns_p12: "Path to Apple .p12 file (optional)", apns_p12_password: "Password for .p12 file (optional)", apns_env: "production/sandbox (defaults to production)"