|
| 1 | +module Fastlane |
| 2 | + module Actions |
| 3 | + module SharedValues |
| 4 | + ONE_SIGNAL_APP_ID = :ONE_SIGNAL_APP_ID |
| 5 | + ONE_SIGNAL_APP_AUTH_KEY = :ONE_SIGNAL_APP_AUTH_KEY |
| 6 | + end |
| 7 | + |
| 8 | + class OnesignalAction < Action |
| 9 | + def self.run(params) |
| 10 | + require 'net/http' |
| 11 | + require 'uri' |
| 12 | + require 'base64' |
| 13 | + |
| 14 | + UI.message("OneSignal App Name: #{params[:app_name]}") |
| 15 | + UI.message("OneSignal App ID: #{params[:app_id]}") |
| 16 | + auth_token = params[:auth_token] |
| 17 | + app_name = params[:app_name] |
| 18 | + app_id = params[:app_id] |
| 19 | + apns_p12_password = params[:apns_p12_password] |
| 20 | + android_token = params[:android_token] |
| 21 | + android_gcm_sender_id = params[:android_gcm_sender_id] |
| 22 | + |
| 23 | + payload = {} |
| 24 | + payload['name'] = app_name |
| 25 | + |
| 26 | + unless params[:apns_p12].nil? |
| 27 | + data = File.read(params[:apns_p12]) |
| 28 | + apns_p12 = Base64.encode64(data) |
| 29 | + payload["apns_env"] = params[:apns_env] |
| 30 | + payload["apns_p12"] = apns_p12 |
| 31 | + # we need to have something for the p12 password, even if it's an empty string |
| 32 | + payload["apns_p12_password"] = apns_p12_password || "" |
| 33 | + end |
| 34 | + |
| 35 | + payload["gcm_key"] = android_token unless android_token.nil? |
| 36 | + payload["android_gcm_sender_id"] = android_gcm_sender_id unless android_gcm_sender_id.nil? |
| 37 | + |
| 38 | + # here's the actual lifting - POST/PUT to OneSignal |
| 39 | + |
| 40 | + headers = { |
| 41 | + 'Content-Type' => 'application/json', |
| 42 | + 'Authorization' => "Basic #{auth_token}", |
| 43 | + } |
| 44 | + |
| 45 | + if app_id |
| 46 | + uri = URI.parse("https://onesignal.com/api/v1/apps/#{app_id}") |
| 47 | + http = Net::HTTP.new(uri.host, uri.port) |
| 48 | + http.use_ssl = true |
| 49 | + response = http.put(uri.path, payload.to_json, headers) |
| 50 | + else |
| 51 | + uri = URI.parse("https://onesignal.com/api/v1/apps") |
| 52 | + http = Net::HTTP.new(uri.host, uri.port) |
| 53 | + http.use_ssl = true |
| 54 | + response = http.post(uri.path, payload.to_json, headers) |
| 55 | + end |
| 56 | + |
| 57 | + case response.code.to_i |
| 58 | + when 200, 204 |
| 59 | + response_body = JSON.parse(response.body) |
| 60 | + |
| 61 | + Actions.lane_context[SharedValues::ONE_SIGNAL_APP_ID] = response_body["id"] |
| 62 | + Actions.lane_context[SharedValues::ONE_SIGNAL_APP_AUTH_KEY] = response_body["basic_auth_key"] |
| 63 | + |
| 64 | + if app_id |
| 65 | + puts("Successfully updated OneSignal app".green) |
| 66 | + else |
| 67 | + puts("Successfully created new OneSignal app".green) |
| 68 | + end |
| 69 | + else |
| 70 | + UI.user_error!("Unexpected #{response.code} with response: #{response.body}") |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + def self.description |
| 75 | + "Create a new OneSignal application" |
| 76 | + end |
| 77 | + |
| 78 | + def self.details |
| 79 | + "You can use this action to automatically create a OneSignal application. You can also upload a `.p12` with password, a GCM key, or both." |
| 80 | + end |
| 81 | + |
| 82 | + def self.available_options |
| 83 | + [ |
| 84 | + FastlaneCore::ConfigItem.new(key: :auth_token, |
| 85 | + env_name: "ONE_SIGNAL_AUTH_KEY", |
| 86 | + sensitive: true, |
| 87 | + description: "OneSignal Authorization Key", |
| 88 | + verify_block: proc do |value| |
| 89 | + unless value.to_s.length > 0 |
| 90 | + UI.error("Please add 'ENV[\"ONE_SIGNAL_AUTH_KEY\"] = \"your token\"' to your Fastfile's `before_all` section.") |
| 91 | + UI.user_error!("No ONE_SIGNAL_AUTH_KEY given.") |
| 92 | + end |
| 93 | + end), |
| 94 | + |
| 95 | + FastlaneCore::ConfigItem.new(key: :app_name, |
| 96 | + env_name: "ONE_SIGNAL_APP_NAME", |
| 97 | + description: "OneSignal App Name", |
| 98 | + verify_block: proc do |value| |
| 99 | + unless value.to_s.length > 0 |
| 100 | + UI.error("Please add 'ENV[\"ONE_SIGNAL_APP_NAME\"] = \"Your app name\"' to your Fastfile's `before_all` section.") |
| 101 | + UI.user_error!("No ONE_SIGNAL_APP_NAME given.") |
| 102 | + end |
| 103 | + end), |
| 104 | + |
| 105 | + FastlaneCore::ConfigItem.new(key: :app_id, |
| 106 | + env_name: "ONE_SIGNAL_APP_ID", |
| 107 | + description: "Existing OneSignal App ID", |
| 108 | + optional: true, |
| 109 | + verify_block: proc do |value| |
| 110 | + unless value.to_s.length > 0 |
| 111 | + UI.error("Please add 'ENV[\"ONE_SIGNAL_APP_ID\"] = \"Your existing OneSignal ID\"' to your Fastfile's `before_all` section.") |
| 112 | + UI.user_error!("No ONE_SIGNAL_APP_ID given.") |
| 113 | + end |
| 114 | + end), |
| 115 | + |
| 116 | + FastlaneCore::ConfigItem.new(key: :android_token, |
| 117 | + env_name: "ANDROID_TOKEN", |
| 118 | + description: "ANDROID GCM KEY", |
| 119 | + sensitive: true, |
| 120 | + optional: true), |
| 121 | + |
| 122 | + FastlaneCore::ConfigItem.new(key: :android_gcm_sender_id, |
| 123 | + env_name: "ANDROID_GCM_SENDER_ID", |
| 124 | + description: "GCM SENDER ID", |
| 125 | + sensitive: true, |
| 126 | + optional: true), |
| 127 | + |
| 128 | + FastlaneCore::ConfigItem.new(key: :apns_p12, |
| 129 | + env_name: "APNS_P12", |
| 130 | + description: "APNS P12 File (in .p12 format)", |
| 131 | + optional: true), |
| 132 | + |
| 133 | + FastlaneCore::ConfigItem.new(key: :apns_p12_password, |
| 134 | + env_name: "APNS_P12_PASSWORD", |
| 135 | + sensitive: true, |
| 136 | + description: "APNS P12 password", |
| 137 | + optional: true), |
| 138 | + |
| 139 | + FastlaneCore::ConfigItem.new(key: :apns_env, |
| 140 | + env_name: "APNS_ENV", |
| 141 | + description: "APNS environment", |
| 142 | + optional: true, |
| 143 | + default_value: 'production') |
| 144 | + ] |
| 145 | + end |
| 146 | + |
| 147 | + def self.output |
| 148 | + [ |
| 149 | + ['ONE_SIGNAL_APP_ID', 'The OneSignal app ID of the newly created app'], |
| 150 | + ['ONE_SIGNAL_APP_AUTH_KEY', 'The auth token for the newly created OneSignal app'] |
| 151 | + ] |
| 152 | + end |
| 153 | + |
| 154 | + def self.authors |
| 155 | + ["timothybarraclough", "smartshowltd", "hawkrives"] |
| 156 | + end |
| 157 | + |
| 158 | + def self.is_supported?(platform) |
| 159 | + [:ios, :android].include?(platform) |
| 160 | + end |
| 161 | + |
| 162 | + def self.example_code |
| 163 | + [ |
| 164 | + 'onesignal( |
| 165 | + auth_token: "Your OneSignal Auth Token", |
| 166 | + app_name: "Name for new OneSignal App", |
| 167 | + app_id: "ID of an existing OneSignal App", |
| 168 | + android_token: "Your Android GCM key (optional)", |
| 169 | + android_gcm_sender_id: "Your Android GCM Sender ID (optional)", |
| 170 | + apns_p12: "Path to Apple .p12 file (optional)", |
| 171 | + apns_p12_password: "Password for .p12 file (optional)", |
| 172 | + apns_env: "production/sandbox (defaults to production)" |
| 173 | + )' |
| 174 | + ] |
| 175 | + end |
| 176 | + |
| 177 | + def self.category |
| 178 | + :push |
| 179 | + end |
| 180 | + end |
| 181 | + end |
| 182 | +end |
0 commit comments