forked from lokalise/lokalise-fastlane-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_keys_to_lokalise.rb
104 lines (94 loc) Β· 4.88 KB
/
add_keys_to_lokalise.rb
1
2
3
4
5
6
7
8
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require 'net/http'
module Fastlane
module Actions
class AddKeysToLokaliseAction < Action
class << self
def run(params)
token = params[:api_token]
project_id = params[:project_identifier]
raw_keys = params[:keys]
platforms = params[:platforms]
keys = {keys: []}
raw_keys.each do |key|
keys[:keys] << {
key_name: key,
platforms: platforms.map(&:downcase)
}
end
uri = URI("https://api.lokalise.com/api2/projects/#{project_id}/keys")
request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
request.body = keys.to_json
request.add_field("x-api-token", token)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
response = http.request(request)
jsonResponse = JSON.parse(response.body)
raise "Bad response π\n#{response.body}".red unless jsonResponse.kind_of? Hash
if response.kind_of? Net::HTTPSuccess
inserted = jsonResponse["keys"].count
UI.success "Keys uploaded. #{inserted} inserted π"
elsif jsonResponse["response"]["status"] == "error"
code = jsonResponse["response"]["code"]
message = jsonResponse["response"]["message"]
raise "Response error code #{code} (#{message}) π".red
else
raise "Bad response π\n#{jsonResponse}".red
end
end
#####################################################
# @!group Documentation
#####################################################
def description
"Add keys to lokalise"
end
def available_options
[
FastlaneCore::ConfigItem.new(key: :api_token,
env_name: "LOKALISE_API_TOKEN",
description: "API Token for Lokalise",
is_string: true,
verify_block: proc do |value|
raise "No API token for Lokalise given, pass using `api_token: 'token'`".red unless (value and not value.empty?)
end),
FastlaneCore::ConfigItem.new(key: :project_identifier,
env_name: "LOKALISE_PROJECT_ID",
description: "Lokalise Project Identifier",
is_string: true,
verify_block: proc do |value|
raise "No Project Identifier for Lokalise given, pass using `project_identifier: 'identifier'`".red unless (value and not value.empty?)
end),
FastlaneCore::ConfigItem.new(key: :platforms,
description: "Platforms array. Supported values: ios, web, android, other",
optional: true,
is_string: false,
default_value: %w[ios],
verify_block: proc do |value|
raise "Keys must be passed as array of strings".red unless (value.is_a? Array and !value.empty?)
value.each.with_index do |key, index|
raise "Key at index #{index} must be string".red unless key.is_a? String
raise "Key at index #{index} must be ios, android, web, or other" unless %w[ios android other web].include?(key.downcase)
end
end),
FastlaneCore::ConfigItem.new(key: :keys,
description: "Keys to add",
optional: false,
is_string: false,
verify_block: proc do |value|
raise "Keys must be passed as array of strings".red unless (value.kind_of? Array and !value.empty?)
value.each_with_index do |key, index|
raise "Key at index #{index} must be string".red unless key.kind_of? String
raise "Key at index #{index} can't be empty".red if key.empty?
end
end)
]
end
def authors
"Fedya-L"
end
def is_supported?(platform)
[:ios, :mac].include? platform
end
end
end
end
end