diff --git a/CHAGELOG.md b/CHAGELOG.md index a1aafb1..c677f1c 100644 --- a/CHAGELOG.md +++ b/CHAGELOG.md @@ -1,5 +1,25 @@ # CHANGELOG + +## v0.10.0 (2024-09-16) + +### Changelog + +#### Breaking Changes + +The default value of `drive_keyfile` (`drive_key.json`) is removed. +Now, **you must specify the key file path manually** via +the `drive_keyfile` argument or the `GDRIVE_KEY_FILE` environment variable. +This change applies to all actions. + +#### Other Changes +- Added the argument `drive_key_json` (env: `GDRIVE_KEY_JSON`) to all actions, + allowing you to provide the JSON key file content directly. + +- You can also specify the path to the JSON key file using + the `GOOGLE_APPLICATION_CREDENTIALS` environment variable, + commonly used in other SDKs and libraries. + ## v0.9.0 (2022-12-03) ### Changelog diff --git a/README.md b/README.md index 2177cd6..4092d89 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![fastlane Plugin Badge](https://rawcdn.githack.com/fastlane/fastlane/master/fastlane/assets/plugin-badge.svg)](https://rubygems.org/gems/fastlane-plugin-google_drive) [![Gem Version](https://badge.fury.io/rb/fastlane-plugin-google_drive.svg)](https://badge.fury.io/rb/fastlane-plugin-google_drive) -[![Build Status](https://app.travis-ci.com/bskim45/fastlane-plugin-google_drive.svg?branch=main)](https://app.travis-ci.com/bskim45/fastlane-plugin-google_drive) +![Build Status](https://github.com/bskim45/fastlane-plugin-google_drive/actions/workflows/rubygems.yml/badge.svg) [![Test Coverage](https://api.codeclimate.com/v1/badges/681ab1f5c19ca029dff4/test_coverage)](https://codeclimate.com/github/bskim45/fastlane-plugin-google_drive/test_coverage) ## Getting Started @@ -15,14 +15,15 @@ fastlane add_plugin google_drive ## About google_drive -> Please refer to [this guide](https://github.com/gimite/google-drive-ruby/blob/master/doc/authorization.md) to create an Google Drive credential. +> Please refer to [this guide](https://github.com/gimite/google-drive-ruby/blob/master/doc/authorization.md) to create a Google Drive credential. Upload files to Google Drive folder. -> Aliases for this action - `google_drive_upload` and `upload_google_drive` are both removed in `v0.6.0`. ```ruby upload_to_google_drive( drive_keyfile: 'drive_key.json', + # or you can provide the content of JSON keyfile directly as an argument + # drive_key_json: __KEYFILE_CONTENT__, service_account: true, folder_id: 'folder_id', upload_files: ['file_to_upload', 'another_file_to_upload'] diff --git a/lib/fastlane/plugin/google_drive/actions/create_google_drive_folder_action.rb b/lib/fastlane/plugin/google_drive/actions/create_google_drive_folder_action.rb index e7b9c52..4779d1f 100644 --- a/lib/fastlane/plugin/google_drive/actions/create_google_drive_folder_action.rb +++ b/lib/fastlane/plugin/google_drive/actions/create_google_drive_folder_action.rb @@ -7,12 +7,16 @@ module SharedValues GDRIVE_CREATED_FOLDER_ID = :GDRIVE_CREATED_FOLDER_ID GDRIVE_CREATED_FOLDER_URL = :GDRIVE_CREATED_FOLDER_URL end + class CreateGoogleDriveFolderAction < Action def self.run(params) - UI.message("Using credential file: #{params[:drive_keyfile]}") + unless params[:drive_keyfile].nil? + UI.message("Using credential file: #{params[:drive_keyfile]}") + end session = Helper::GoogleDriveHelper.setup( keyfile: params[:drive_keyfile], + key_json: params[:drive_key_json], service_account: params[:service_account] ) @@ -47,36 +51,62 @@ def self.details def self.available_options [ - FastlaneCore::ConfigItem.new(key: :drive_keyfile, - env_name: 'GDRIVE_KEY_FILE', - description: 'Json config file', - type: String, - default_value: 'drive_key.json', - verify_block: proc do |value| - UI.user_error!("Couldn't find config keyfile at path '#{value}'") unless File.exist?(value) - end), - FastlaneCore::ConfigItem.new(key: :service_account, - env_name: 'GDRIVE_SERVICE_ACCOUNT', - description: 'Credential is service account', - optional: true, - is_string: false, - default_value: false), - FastlaneCore::ConfigItem.new(key: :folder_id, - env_name: "GDRIVE_UPLOAD_FOLDER_ID", - description: "Upload target folder id", - optional: false, - type: String, - verify_block: proc do |value| - UI.user_error!("No target folder_id given, pass using `folder_id: 'some_id'`") unless value and !value.empty? - end), - FastlaneCore::ConfigItem.new(key: :folder_title, - env_name: "GDRIVE_FOLDER_NAME", - description: "Folder title of new one", - optional: false, - type: String, - verify_block: proc do |value| - UI.user_error!("No folder title given") if value.nil? || value.empty? - end) + FastlaneCore::ConfigItem.new( + key: :drive_keyfile, + env_names: ['GDRIVE_KEY_FILE', 'GOOGLE_APPLICATION_CREDENTIALS'], + description: 'Path to the JSON keyfile', + conflicting_options: [:drive_key_json], + type: String, + optional: true, + verify_block: proc do |value| + UI.user_error!("Couldn't find config keyfile at path '#{value}'") unless File.exist?(value) + UI.user_error!("'#{value}' doesn't seem to be a valid JSON keyfile") unless FastlaneCore::Helper.json_file?(value) + end + ), + FastlaneCore::ConfigItem.new( + key: :drive_key_json, + env_name: 'GDRIVE_KEY_JSON', + description: 'Credential key in stringified JSON format', + optional: true, + conflicting_options: [:drive_keyfile], + type: String, + sensitive: true, + verify_block: proc do |value| + begin + JSON.parse(value) + rescue JSON::ParserError + UI.user_error!("Provided credential key is not a valid JSON") + end + end + ), + FastlaneCore::ConfigItem.new( + key: :service_account, + env_name: 'GDRIVE_SERVICE_ACCOUNT', + description: 'true if the credential is for a service account, false otherwise', + optional: true, + is_string: false, + default_value: false + ), + FastlaneCore::ConfigItem.new( + key: :folder_id, + env_name: "GDRIVE_UPLOAD_FOLDER_ID", + description: "Upload target folder id", + optional: false, + type: String, + verify_block: proc do |value| + UI.user_error!("No target `folder_id` was provided. Pass it using `folder_id: 'some_id'`") unless value and !value.empty? + end + ), + FastlaneCore::ConfigItem.new( + key: :folder_title, + env_name: "GDRIVE_FOLDER_NAME", + description: "Folder title of new one", + optional: false, + type: String, + verify_block: proc do |value| + UI.user_error!("No folder title given") if value.nil? || value.empty? + end + ) ] end @@ -92,7 +122,7 @@ def self.return_value end def self.authors - ['Kohki Miki (@giginet)'] + ['Bumsoo Kim (@bskim45)', 'Kohki Miki (@giginet)'] end def self.is_supported?(platform) diff --git a/lib/fastlane/plugin/google_drive/actions/find_google_drive_file_by_id_action.rb b/lib/fastlane/plugin/google_drive/actions/find_google_drive_file_by_id_action.rb index 6d9622a..cd2fff5 100644 --- a/lib/fastlane/plugin/google_drive/actions/find_google_drive_file_by_id_action.rb +++ b/lib/fastlane/plugin/google_drive/actions/find_google_drive_file_by_id_action.rb @@ -11,10 +11,13 @@ module SharedValues class FindGoogleDriveFileByIdAction < Action def self.run(params) - UI.message("Using credential file: #{params[:drive_keyfile]}") + unless params[:drive_keyfile].nil? + UI.message("Using credential file: #{params[:drive_keyfile]}") + end session = Helper::GoogleDriveHelper.setup( keyfile: params[:drive_keyfile], + key_json: params[:drive_key_json], service_account: params[:service_account] ) @@ -53,18 +56,36 @@ def self.available_options [ FastlaneCore::ConfigItem.new( key: :drive_keyfile, - env_name: 'GDRIVE_KEY_FILE', - description: 'The path to the json keyfile', + env_names: ['GDRIVE_KEY_FILE', 'GOOGLE_APPLICATION_CREDENTIALS'], + description: 'Path to the JSON keyfile', + conflicting_options: [:drive_key_json], type: String, - default_value: 'drive_key.json', + optional: true, verify_block: proc do |value| UI.user_error!("Couldn't find config keyfile at path '#{value}'") unless File.exist?(value) + UI.user_error!("'#{value}' doesn't seem to be a valid JSON keyfile") unless FastlaneCore::Helper.json_file?(value) + end + ), + FastlaneCore::ConfigItem.new( + key: :drive_key_json, + env_name: 'GDRIVE_KEY_JSON', + description: 'Credential key in stringified JSON format', + optional: true, + conflicting_options: [:drive_keyfile], + type: String, + sensitive: true, + verify_block: proc do |value| + begin + JSON.parse(value) + rescue JSON::ParserError + UI.user_error!("Provided credential key is not a valid JSON") + end end ), FastlaneCore::ConfigItem.new( key: :service_account, env_name: 'GDRIVE_SERVICE_ACCOUNT', - description: 'Whether the credential file is for the google service account', + description: 'true if the credential is for a service account, false otherwise', optional: true, is_string: false, default_value: false diff --git a/lib/fastlane/plugin/google_drive/actions/find_google_drive_file_by_title_action.rb b/lib/fastlane/plugin/google_drive/actions/find_google_drive_file_by_title_action.rb index 7cdd22c..b51cec8 100644 --- a/lib/fastlane/plugin/google_drive/actions/find_google_drive_file_by_title_action.rb +++ b/lib/fastlane/plugin/google_drive/actions/find_google_drive_file_by_title_action.rb @@ -8,10 +8,13 @@ module SharedValues class FindGoogleDriveFileByTitleAction < Action def self.run(params) - UI.message("Using credential file: #{params[:drive_keyfile]}") + unless params[:drive_keyfile].nil? + UI.message("Using credential file: #{params[:drive_keyfile]}") + end session = Helper::GoogleDriveHelper.setup( keyfile: params[:drive_keyfile], + key_json: params[:drive_key_json], service_account: params[:service_account] ) @@ -57,18 +60,36 @@ def self.available_options [ FastlaneCore::ConfigItem.new( key: :drive_keyfile, - env_name: 'GDRIVE_KEY_FILE', - description: 'The path to the json keyfile', + env_names: ['GDRIVE_KEY_FILE', 'GOOGLE_APPLICATION_CREDENTIALS'], + description: 'Path to the JSON keyfile', + conflicting_options: [:drive_key_json], type: String, - default_value: 'drive_key.json', + optional: true, verify_block: proc do |value| UI.user_error!("Couldn't find config keyfile at path '#{value}'") unless File.exist?(value) + UI.user_error!("'#{value}' doesn't seem to be a valid JSON keyfile") unless FastlaneCore::Helper.json_file?(value) + end + ), + FastlaneCore::ConfigItem.new( + key: :drive_key_json, + env_name: 'GDRIVE_KEY_JSON', + description: 'Credential key in stringified JSON format', + optional: true, + conflicting_options: [:drive_keyfile], + type: String, + sensitive: true, + verify_block: proc do |value| + begin + JSON.parse(value) + rescue JSON::ParserError + UI.user_error!("Provided credential key is not a valid JSON") + end end ), FastlaneCore::ConfigItem.new( key: :service_account, env_name: 'GDRIVE_SERVICE_ACCOUNT', - description: 'Whether the credential file is for the google service account', + description: 'true if the credential is for a service account, false otherwise', optional: true, is_string: false, default_value: false diff --git a/lib/fastlane/plugin/google_drive/actions/update_google_drive_file_action.rb b/lib/fastlane/plugin/google_drive/actions/update_google_drive_file_action.rb index b5a8d3b..7ba5136 100644 --- a/lib/fastlane/plugin/google_drive/actions/update_google_drive_file_action.rb +++ b/lib/fastlane/plugin/google_drive/actions/update_google_drive_file_action.rb @@ -7,12 +7,16 @@ module SharedValues GDRIVE_UPDATED_FILE_NAME = :GDRIVE_UPDATED_FILE_NAME GDRIVE_UPDATED_FILE_URL = :GDRIVE_UPDATED_FILE_URL end + class UpdateGoogleDriveFileAction < Action def self.run(params) - UI.message("Using credential file: #{params[:drive_keyfile]}") + unless params[:drive_keyfile].nil? + UI.message("Using credential file: #{params[:drive_keyfile]}") + end session = Helper::GoogleDriveHelper.setup( keyfile: params[:drive_keyfile], + key_json: params[:drive_key_json], service_account: params[:service_account] ) @@ -46,37 +50,63 @@ def self.details def self.available_options [ - FastlaneCore::ConfigItem.new(key: :drive_keyfile, - env_name: 'GDRIVE_KEY_FILE', - description: 'Json config file', - type: String, - default_value: 'drive_key.json', - verify_block: proc do |value| - UI.user_error!("Couldn't find config keyfile at path '#{value}'") unless File.exist?(value) - end), - FastlaneCore::ConfigItem.new(key: :service_account, - env_name: 'GDRIVE_SERVICE_ACCOUNT', - description: 'Credential is service account', - optional: true, - is_string: false, - default_value: false), - FastlaneCore::ConfigItem.new(key: :file_id, - env_name: "GDRIVE_UPDATE_FILE_ID", - description: "Target file id to update the content", - optional: false, - type: String, - verify_block: proc do |value| - UI.user_error!("No target file id given, pass using `file_id: 'some_id'`") unless value and !value.empty? - end), - FastlaneCore::ConfigItem.new(key: :upload_file, - env_name: "GDRIVE_UPLOAD_FILE", - description: "Path to a file to be uploaded", - optional: false, - is_string: false, - verify_block: proc do |value| - UI.user_error!("No upload file is given, pass using `upload_file: 'some/path/a.txt'`") unless value and !value.empty? - UI.user_error!("Couldn't find upload file at path '#{value}'") unless File.exist?(value) - end) + FastlaneCore::ConfigItem.new( + key: :drive_keyfile, + env_names: ['GDRIVE_KEY_FILE', 'GOOGLE_APPLICATION_CREDENTIALS'], + description: 'Path to the JSON keyfile', + conflicting_options: [:drive_key_json], + type: String, + optional: true, + verify_block: proc do |value| + UI.user_error!("Couldn't find config keyfile at path '#{value}'") unless File.exist?(value) + UI.user_error!("'#{value}' doesn't seem to be a valid JSON keyfile") unless FastlaneCore::Helper.json_file?(value) + end + ), + FastlaneCore::ConfigItem.new( + key: :drive_key_json, + env_name: 'GDRIVE_KEY_JSON', + description: 'Credential key in stringified JSON format', + optional: true, + conflicting_options: [:drive_keyfile], + type: String, + sensitive: true, + verify_block: proc do |value| + begin + JSON.parse(value) + rescue JSON::ParserError + UI.user_error!("Provided credential key is not a valid JSON") + end + end + ), + FastlaneCore::ConfigItem.new( + key: :service_account, + env_name: 'GDRIVE_SERVICE_ACCOUNT', + description: 'true if the credential is for a service account, false otherwise', + optional: true, + is_string: false, + default_value: false + ), + FastlaneCore::ConfigItem.new( + key: :file_id, + env_name: "GDRIVE_UPDATE_FILE_ID", + description: "Target file id to update the content", + optional: false, + type: String, + verify_block: proc do |value| + UI.user_error!("No target `file_id` is provided. Pass it using `file_id: 'some_id'`") unless value and !value.empty? + end + ), + FastlaneCore::ConfigItem.new( + key: :upload_file, + env_name: "GDRIVE_UPLOAD_FILE", + description: "Path to a file to be uploaded", + optional: false, + is_string: false, + verify_block: proc do |value| + UI.user_error!("No upload file is given, pass using `upload_file: 'some/path/a.txt'`") unless value and !value.empty? + UI.user_error!("Couldn't find upload file at path '#{value}'") unless File.exist?(value) + end + ) ] end diff --git a/lib/fastlane/plugin/google_drive/actions/upload_to_google_drive_action.rb b/lib/fastlane/plugin/google_drive/actions/upload_to_google_drive_action.rb index 35395f5..cbc0972 100644 --- a/lib/fastlane/plugin/google_drive/actions/upload_to_google_drive_action.rb +++ b/lib/fastlane/plugin/google_drive/actions/upload_to_google_drive_action.rb @@ -7,12 +7,16 @@ module SharedValues GDRIVE_UPLOADED_FILE_NAMES = :GDRIVE_UPLOADED_FILE_NAMES GDRIVE_UPLOADED_FILE_URLS = :GDRIVE_UPLOADED_FILE_URLS end + class UploadToGoogleDriveAction < Action def self.run(params) - UI.message("Using credential file: #{params[:drive_keyfile]}") + unless params[:drive_keyfile].nil? + UI.message("Using credential file: #{params[:drive_keyfile]}") + end session = Helper::GoogleDriveHelper.setup( keyfile: params[:drive_keyfile], + key_json: params[:drive_key_json], service_account: params[:service_account] ) @@ -56,46 +60,74 @@ def self.details def self.available_options [ - FastlaneCore::ConfigItem.new(key: :drive_keyfile, - env_name: 'GDRIVE_KEY_FILE', - description: 'Json config file', - type: String, - default_value: 'drive_key.json', - verify_block: proc do |value| - UI.user_error!("Couldn't find config keyfile at path '#{value}'") unless File.exist?(value) - end), - FastlaneCore::ConfigItem.new(key: :service_account, - env_name: 'GDRIVE_SERVICE_ACCOUNT', - description: 'Credential is service account', - optional: true, - is_string: false, - default_value: false), - FastlaneCore::ConfigItem.new(key: :folder_id, - env_name: "GDRIVE_UPLOAD_FOLDER_ID", - description: "Upload target folder id", - optional: false, - type: String, - verify_block: proc do |value| - UI.user_error!("No target folder id given, pass using `folder_id: 'some_id'`") unless value and !value.empty? - end), - FastlaneCore::ConfigItem.new(key: :upload_files, - env_name: "GDRIVE_UPLOAD_FILES", - description: "Path to files to be uploaded", - optional: false, - is_string: false, - verify_block: proc do |value| - UI.user_error!("upload_files must be an Array of paths to files") unless value.kind_of?(Array) - UI.user_error!("No upload file is given, pass using `upload_files: ['a', 'b']`") unless value and !value.empty? - value.each do |path| - UI.user_error!("Couldn't find upload file at path '#{path}'") unless File.exist?(path) - end - end), - FastlaneCore::ConfigItem.new(key: :public_links, - env_name: 'GDRIVE_PUBLIC_LINKS', - description: 'Uploaded file links should be public', - optional: true, - default_value: false, - is_string: false) + FastlaneCore::ConfigItem.new( + key: :drive_keyfile, + env_names: ['GDRIVE_KEY_FILE', 'GOOGLE_APPLICATION_CREDENTIALS'], + description: 'Path to the JSON keyfile', + conflicting_options: [:drive_key_json], + type: String, + optional: true, + verify_block: proc do |value| + UI.user_error!("Couldn't find config keyfile at path '#{value}'") unless File.exist?(value) + UI.user_error!("'#{value}' doesn't seem to be a valid JSON keyfile") unless FastlaneCore::Helper.json_file?(value) + end + ), + FastlaneCore::ConfigItem.new( + key: :drive_key_json, + env_name: 'GDRIVE_KEY_JSON', + description: 'Credential key in stringified JSON format', + optional: true, + conflicting_options: [:drive_keyfile], + type: String, + sensitive: true, + verify_block: proc do |value| + begin + JSON.parse(value) + rescue JSON::ParserError + UI.user_error!("Provided credential key is not a valid JSON") + end + end + ), + FastlaneCore::ConfigItem.new( + key: :service_account, + env_name: 'GDRIVE_SERVICE_ACCOUNT', + description: 'true if the credential is for a service account, false otherwise', + optional: true, + is_string: false, + default_value: false + ), + FastlaneCore::ConfigItem.new( + key: :folder_id, + env_name: "GDRIVE_UPLOAD_FOLDER_ID", + description: "Upload target folder id", + optional: false, + type: String, + verify_block: proc do |value| + UI.user_error!("No target `folder_id` was provided. Pass it using `folder_id: 'some_id'`") unless value and !value.empty? + end + ), + FastlaneCore::ConfigItem.new( + key: :upload_files, + env_name: "GDRIVE_UPLOAD_FILES", + description: "Path to files to be uploaded", + optional: false, + is_string: false, + verify_block: proc do |value| + UI.user_error!("upload_files must be an Array of paths to files") unless value.kind_of?(Array) + UI.user_error!("No upload file is given, pass using `upload_files: ['a', 'b']`") unless value and !value.empty? + value.each do |path| + UI.user_error!("Couldn't find upload file at path '#{path}'") unless File.exist?(path) + end + end + ), + FastlaneCore::ConfigItem.new( + key: :public_links, + env_name: 'GDRIVE_PUBLIC_LINKS', + description: 'Uploaded file links should be public', + optional: true, + default_value: false, + is_string: false + ) ] end diff --git a/lib/fastlane/plugin/google_drive/helper/google_drive_helper.rb b/lib/fastlane/plugin/google_drive/helper/google_drive_helper.rb index d7c84af..2f4ecee 100644 --- a/lib/fastlane/plugin/google_drive/helper/google_drive_helper.rb +++ b/lib/fastlane/plugin/google_drive/helper/google_drive_helper.rb @@ -1,15 +1,18 @@ require 'fastlane_core/ui/ui' require 'google_drive' + module Fastlane UI = FastlaneCore::UI unless Fastlane.const_defined?("UI") module Helper class GoogleDriveHelper - def self.setup(keyfile: nil, service_account: false) + def self.setup(keyfile: nil, key_json: nil, service_account: false) + keyfile_or_io = keyfile || StringIO.new(key_json || '') + if service_account - ::GoogleDrive::Session.from_service_account_key(keyfile) + ::GoogleDrive::Session.from_service_account_key(keyfile_or_io) else - ::GoogleDrive::Session.from_config(keyfile) + ::GoogleDrive::Session.from_config(keyfile_or_io) end rescue Exception => e UI.error(e.message) diff --git a/spec/create_google_drive_folder_action_spec.rb b/spec/create_google_drive_folder_action_spec.rb index 3904eb7..76de731 100644 --- a/spec/create_google_drive_folder_action_spec.rb +++ b/spec/create_google_drive_folder_action_spec.rb @@ -28,7 +28,7 @@ Fastlane::FastFile.new.parse("lane :test do create_google_drive_folder(drive_keyfile: '#{@key_path}') end").runner.execute(:test) - end.to raise_error(FastlaneCore::Interface::FastlaneError, "No target folder_id given, pass using `folder_id: 'some_id'`") + end.to raise_error(FastlaneCore::Interface::FastlaneError, "No target `folder_id` was provided. Pass it using `folder_id: 'some_id'`") end it 'raise an error if no folder_title was given' do diff --git a/spec/update_google_drive_file_action_spec.rb b/spec/update_google_drive_file_action_spec.rb index 99dd26c..691db6b 100644 --- a/spec/update_google_drive_file_action_spec.rb +++ b/spec/update_google_drive_file_action_spec.rb @@ -28,7 +28,7 @@ Fastlane::FastFile.new.parse("lane :test do update_google_drive_file(drive_keyfile: '#{@key_path}') end").runner.execute(:test) - end.to raise_error(FastlaneCore::Interface::FastlaneError, "No target file id given, pass using `file_id: 'some_id'`") + end.to raise_error(FastlaneCore::Interface::FastlaneError, "No target `file_id` is provided. Pass it using `file_id: 'some_id'`") end it 'raise an error if no upload file was given' do diff --git a/spec/upload_to_google_drive_action_spec.rb b/spec/upload_to_google_drive_action_spec.rb index 2789d15..0ae92c3 100644 --- a/spec/upload_to_google_drive_action_spec.rb +++ b/spec/upload_to_google_drive_action_spec.rb @@ -13,6 +13,8 @@ after(:context) do ENV.delete('GDRIVE_SERVICE_ACCOUNT') + ENV.delete('GDRIVE_KEY_JSON') + ENV.delete('GDRIVE_KEY_FILE') Fastlane::Actions.clear_lane_context end @@ -24,12 +26,28 @@ end.to raise_error(FastlaneCore::Interface::FastlaneError, "Couldn't find config keyfile at path 'test.json'") end + it 'raise an error if given json key is invalid' do + expect do + Fastlane::FastFile.new.parse("lane :test do + upload_to_google_drive(drive_key_json: '') + end").runner.execute(:test) + end.to raise_error(FastlaneCore::Interface::FastlaneError, "Provided credential key is not a valid JSON") + end + + it 'raise an error if json key and keyfile are both provided' do + expect do + Fastlane::FastFile.new.parse("lane :test do + upload_to_google_drive(drive_keyfile: '#{@key_path}', drive_key_json: '{}') + end").runner.execute(:test) + end.to raise_error(FastlaneCore::Interface::FastlaneError, "Unresolved conflict between options: 'drive_keyfile' and 'drive_key_json'") + end + it 'raise an error if no folder id was given' do expect do Fastlane::FastFile.new.parse("lane :test do upload_to_google_drive(drive_keyfile: '#{@key_path}') end").runner.execute(:test) - end.to raise_error(FastlaneCore::Interface::FastlaneError, "No target folder id given, pass using `folder_id: 'some_id'`") + end.to raise_error(FastlaneCore::Interface::FastlaneError, "No target `folder_id` was provided. Pass it using `folder_id: 'some_id'`") end it 'raise an error if no upload file was given' do @@ -56,7 +74,7 @@ end.to raise_error(FastlaneCore::Interface::FastlaneError, "File with id 'some_id' not found in Google Drive") end - it "raise an error if file upload fails" do + it "raise an error if file upload fails (auth using keyfile - argument)" do folder_id = ENV['TEST_UPLOAD_FOLDER_ID'] Fastlane::FastFile.new.parse("lane :test do @@ -67,6 +85,60 @@ expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::GDRIVE_UPLOADED_FILE_URLS].length).to eq(1) end + it "raise an error if file upload fails (auth using keyfile - GDRIVE_KEY_FILE)" do + folder_id = ENV['TEST_UPLOAD_FOLDER_ID'] + ENV['GDRIVE_KEY_FILE'] = @key_path + + Fastlane::FastFile.new.parse("lane :test do + upload_to_google_drive(folder_id: '#{folder_id}', upload_files: ['#{@upload_file}']) + end").runner.execute(:test) + + expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::GDRIVE_UPLOADED_FILE_NAMES]).to eq(['test_file.txt']) + expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::GDRIVE_UPLOADED_FILE_URLS].length).to eq(1) + + ENV.delete('GDRIVE_KEY_FILE') + end + + it "raise an error if file upload fails (auth using keyfile - GOOGLE_APPLICATION_CREDENTIALS)" do + folder_id = ENV['TEST_UPLOAD_FOLDER_ID'] + ENV['GOOGLE_APPLICATION_CREDENTIALS'] = @key_path + + Fastlane::FastFile.new.parse("lane :test do + upload_to_google_drive(folder_id: '#{folder_id}', upload_files: ['#{@upload_file}']) + end").runner.execute(:test) + + expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::GDRIVE_UPLOADED_FILE_NAMES]).to eq(['test_file.txt']) + expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::GDRIVE_UPLOADED_FILE_URLS].length).to eq(1) + + ENV.delete('GOOGLE_APPLICATION_CREDENTIALS') + end + + it "raise an error if file upload fails (auth using json key - argument)" do + folder_id = ENV['TEST_UPLOAD_FOLDER_ID'] + drive_key_json = File.read(@key_path) + + Fastlane::FastFile.new.parse("lane :test do + upload_to_google_drive(drive_key_json: '#{drive_key_json}', folder_id: '#{folder_id}', upload_files: ['#{@upload_file}']) + end").runner.execute(:test) + + expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::GDRIVE_UPLOADED_FILE_NAMES]).to eq(['test_file.txt']) + expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::GDRIVE_UPLOADED_FILE_URLS].length).to eq(1) + end + + it "raise an error if file upload fails (auth using json key - env)" do + folder_id = ENV['TEST_UPLOAD_FOLDER_ID'] + ENV['GDRIVE_KEY_JSON'] = File.read(@key_path) + + Fastlane::FastFile.new.parse("lane :test do + upload_to_google_drive(folder_id: '#{folder_id}', upload_files: ['#{@upload_file}']) + end").runner.execute(:test) + + expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::GDRIVE_UPLOADED_FILE_NAMES]).to eq(['test_file.txt']) + expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::GDRIVE_UPLOADED_FILE_URLS].length).to eq(1) + + ENV.delete('GDRIVE_KEY_JSON') + end + it "raise an error if file upload fails or public link generation fails" do folder_id = ENV['TEST_UPLOAD_FOLDER_ID']