Class: Fastlane::Actions::UploadGoogleDataSafetyAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/google_data_safety/actions/upload_google_data_safety_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



133
134
135
# File 'lib/fastlane/plugin/google_data_safety/actions/upload_google_data_safety_action.rb', line 133

def self.authors
  ["Owen Bean"]
end

.available_optionsObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
190
191
# File 'lib/fastlane/plugin/google_data_safety/actions/upload_google_data_safety_action.rb', line 146

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :csv_file,
      env_name: "CSV_FILE",
      description: "Csv file path to upload to Google Play Console Data Safety",
      optional: true,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :csv_content,
      env_name: "CSV_CONTENT",
      description: "Comma delimited list of Google Data Safety form",
      optional: true,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :package_name,
      env_name: "PACKAGE_NAME",
      description: "Package name of project",
      optional: false,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :json_key,
      env_name: "JSON_KEY_FILE",
      description: "Json key file for service account authentication",
      optional: true,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :json_key_data,
      env_name: "JSON_KEY_DATA",
      description: "Json key data for service account authentication",
      optional: true,
      type: Hash
    ),
    FastlaneCore::ConfigItem.new(
      key: :debug,
      env_name: "DEBUG",
      description: "Set to debug mode for troubleshooting",
      optional: true,
      type: Boolean
    ),
  ]
end

.csv_file_content(params: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fastlane/plugin/google_data_safety/actions/upload_google_data_safety_action.rb', line 30

def self.csv_file_content(params: nil)
  unless params[:csv_file] || params[:csv_content]
    if UI.interactive?
      UI.important("To not be asked about this value, you can specify it using 'csv_file'")
      csv_file_path = UI.input("The csv file used for Google data safety sheet: ")
      csv_file_path = File.expand_path(csv_file_path)

      UI.user_error!("Could not find csv file at path '#{csv_file_path}'") unless File.exist?(csv_file_path)
      params[:csv_file]
    else
      UI.user_error!("Could not obtain data safety information as comma separated values. Please have 'csv_file' or 'csv_content' variable set.")
    end
  end

  csv_file_content = ""

  if params[:csv_file]
    csv_file_content = File.read(File.expand_path(params[:csv_file]))
  elsif params[:csv_content]
    csv_file_content = params[:csv_content]
  end

  csv_file_content
end

.descriptionObject



129
130
131
# File 'lib/fastlane/plugin/google_data_safety/actions/upload_google_data_safety_action.rb', line 129

def self.description
  "Google safety data sheet for automation of safety form"
end

.detailsObject



141
142
143
144
# File 'lib/fastlane/plugin/google_data_safety/actions/upload_google_data_safety_action.rb', line 141

def self.details
  # Optional:
  "Uploads and update any data safety sheet on Google Play Console API."
end

.get_google_auth(service_account_json: nil) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/fastlane/plugin/google_data_safety/actions/upload_google_data_safety_action.rb', line 78

def self.get_google_auth(service_account_json: nil)
  scope = 'https://www.googleapis.com/auth/androidpublisher'

  auth = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: , scope: scope)

  token = auth.fetch_access_token!

  token
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
196
197
198
199
# File 'lib/fastlane/plugin/google_data_safety/actions/upload_google_data_safety_action.rb', line 193

def self.is_supported?(platform)
  # Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
  # See: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
  #
  # [:ios, :mac, :android].include?(platform)
  true
end

.return_valueObject



137
138
139
# File 'lib/fastlane/plugin/google_data_safety/actions/upload_google_data_safety_action.rb', line 137

def self.return_value
  # If your method provides a return value, you can describe here what it does
end

.run(params) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fastlane/plugin/google_data_safety/actions/upload_google_data_safety_action.rb', line 10

def self.run(params)

  csv_content = self.csv_file_content(params: params)

   = self.(params: params)

  body = self.get_google_auth(service_account_json: )

  access_token = body["access_token"]

  debug = false

  if !params[:debug].nil?
    debug = params[:debug]
  end

  self.send_data_safety_sheet(package_name: params[:package_name], auth_token: access_token, csv_content: csv_content, debug: debug)

end

.send_data_safety_sheet(package_name: nil, auth_token: nil, csv_content: nil, debug: false) ⇒ Object



88
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
# File 'lib/fastlane/plugin/google_data_safety/actions/upload_google_data_safety_action.rb', line 88

def self.send_data_safety_sheet(package_name: nil, auth_token: nil, csv_content: nil, debug: false)
  if package_name.nil?
    if UI.interactive?
      UI.important("To not be asked package name, you can specify it using 'package_name'.")
      package_name_input = UI.input("The package name to upload Google safety data sheet: ")
      package_name = package_name_input
    else
      UI.Error("Package name is required to upload Google safety data sheet. Please specify package name with 'package_name' variable.")
    end
  end

  uri = URI("https://androidpublisher.googleapis.com/androidpublisher/v3/applications/#{package_name}/dataSafety")

  request = Net::HTTP::Post.new(uri)
  request["Authorization"] = "Bearer #{auth_token}"
  request["Content-Type"] = "application/json"
  request.body = { safetyLabels: csv_content }.to_json

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme = 'https') do | http |
    http.request(request)
  end

  if response.is_a?(Net::HTTPSuccess)
    UI.success("Google Safety Data sheet has been uploaded!")
  else
    if response.is_a?(Net::HTTPUnauthorized)
      UI.error("Unauthorized request to upload Google Data Safety sheet.")
    elsif response.is_a?(Net::HTTPBadRequest)
      UI.error("Bad request to upload Google Data Safety sheet.")
    else
      UI.error("Google Data Safety sheet upload error with API")
    end

    if debug
      UI.important("Google Data Safety API response body")
      UI.important(response.body)
    end
  end

end

.service_account_authentication(params: nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fastlane/plugin/google_data_safety/actions/upload_google_data_safety_action.rb', line 55

def self.(params: nil)
  unless params[:json_key] || params[:json_key_data]
    if UI.interactive?
      UI.important("To not be asked about this value, you can specify it using 'json_key'")
      json_key_path = UI.input("The service account json file used to authenticate with Google: ")
      json_key_path = File.expand_path(json_key_path)

      UI.user_error!("Could not find service account json file at path '#{json_key_path}'") unless File.exist?(json_key_path)
      params[:json_key] = json_key_path
    else
      UI.user_error!("Could not load Google authentication. Make sure it has been added as an environment variable in 'json_key' or 'json_key_data'")
    end
  end

  if params[:json_key]
     = File.open(File.expand_path(params[:json_key]))
  elsif params[:json_key_data]
     = StringIO.new(params[:json_key_data])
  end

  
end