Class: Fastlane::Actions::OnesignalAction

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

Constant Summary

Constants inherited from Fastlane::Action

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

Class Method Summary collapse

Methods inherited from Fastlane::Action

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

Class Method Details

.authorsObject



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

def self.authors
  ["timothybarraclough", "smartshowltd"]
end

.available_optionsObject



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
148
# File 'fastlane/lib/fastlane/actions/onesignal.rb', line 89

def self.available_options
  [
    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: :auth_token,
                                 env_name: "ONE_SIGNAL_AUTH_KEY",
                                 sensitive: true,
                                 description: "OneSignal Authorization Key",
                                 verify_block: proc do |value|
                                   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),

    FastlaneCore::ConfigItem.new(key: :android_gcm_sender_id,
                                 env_name: "ANDROID_GCM_SENDER_ID",
                                 description: "GCM SENDER ID",
                                 sensitive: true,
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :apns_p12,
                                 env_name: "APNS_P12",
                                 description: "APNS P12 File (in .p12 format)",
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :apns_p12_password,
                                 env_name: "APNS_P12_PASSWORD",
                                 sensitive: true,
                                 description: "APNS P12 password",
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :apns_env,
                                 env_name: "APNS_ENV",
                                 description: "APNS environment",
                                 optional: true,
                                 default_value: 'production'),

    FastlaneCore::ConfigItem.new(key: :organization_id,
                                 env_name: "ONE_SIGNAL_ORGANIZATION_ID",
                                 sensitive: true,
                                 description: "OneSignal Organization ID",
                                 optional: true)
  ]
end

.categoryObject



191
192
193
# File 'fastlane/lib/fastlane/actions/onesignal.rb', line 191

def self.category
  :push
end

.check_response_code(response, is_update) ⇒ Object



72
73
74
75
76
77
78
79
# File 'fastlane/lib/fastlane/actions/onesignal.rb', line 72

def self.check_response_code(response, is_update)
  case response.code.to_i
  when 200, 204
    UI.success("Successfully #{is_update ? 'updated' : 'created new'} OneSignal app")
  else
    UI.user_error!("Unexpected #{response.code} with response: #{response.body}")
  end
end

.descriptionObject



81
82
83
# File 'fastlane/lib/fastlane/actions/onesignal.rb', line 81

def self.description
  "Create or update a new [OneSignal](https://onesignal.com/) application"
end

.detailsObject



85
86
87
# File 'fastlane/lib/fastlane/actions/onesignal.rb', line 85

def self.details
  "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

.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
# File 'fastlane/lib/fastlane/actions/onesignal.rb', line 165

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)",
      organization_id: "Onesignal organization id (optional)"
    )',
    '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)",
      organization_id: "Onesignal organization id (optional)"
    )'
  ]
end

.is_supported?(platform) ⇒ Boolean

Returns:



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

def self.is_supported?(platform)
  [:ios, :android].include?(platform)
end

.outputObject



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

def self.output
  [
    ['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

.run(params) ⇒ Object



9
10
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
62
63
64
65
66
67
68
69
70
# File 'fastlane/lib/fastlane/actions/onesignal.rb', line 9

def self.run(params)
  require 'net/http'
  require 'uri'
  require 'base64'

  app_id = params[:app_id].to_s.strip
  auth_token = params[:auth_token]
  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]
  organization_id = params[:organization_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 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
    # we need to have something for the p12 password, even if it's an empty string
    payload["apns_p12_password"] = apns_p12_password || ""
  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?
  payload["organization_id"] = organization_id unless organization_id.nil?

  # here's the actual lifting - POST or PUT to OneSignal

  json_headers = { 'Content-Type' => 'application/json', 'Authorization' => "Basic #{auth_token}" }
  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

  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, is_update)
end