snow_sync 2.0.7 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8959b005796890c1ef02a1ea1f96495a3b12d1e3
4
- data.tar.gz: 9115e5849752459463d4eb0b6f1ce6cf25e5c71e
3
+ metadata.gz: 291450da560ad042fd30195c1d5af042c4ff48b4
4
+ data.tar.gz: 1760e8efa915a578052e08cefc5873becf7a128d
5
5
  SHA512:
6
- metadata.gz: 44710ba7a25e35b0e47a377d2e5bfdd926d4aa225c854f683ad131df8ca4f86deadb69771927231302abee383f79ea176a38dbbde70fd44fd3fd739825961598
7
- data.tar.gz: 7c0b1ffa3930496d20d7650928eda35e6f5da708d4a4a7553aade42a58b8cd5651b3db2d78ea08bae94a1f67143e3462611a8d3f65b1411778e7bfb64bfc6602
6
+ metadata.gz: 351fe385067754cccb985ae275e12616e5727dc6753a8c602a75542df3e5cc3a4ae33ffe41f7f85d01e98c1b6940d0a81d31122ccb8bce8feb4d43267e92f5f3
7
+ data.tar.gz: 42568f36c2e279d3dd3cade31af8af0449a3f7483a3f03bfe5b0968ecc65de5d3f9202da209c60fda7536549f5d1d26c8a95c4863475ce96200d072686988a25
@@ -11,5 +11,5 @@ table_map:
11
11
  script_include:
12
12
  name:
13
13
  table:
14
- sysid:
14
+ sys_id:
15
15
  field:
@@ -32,6 +32,14 @@ module SnowSync
32
32
  end
33
33
  end
34
34
 
35
+ # Creates a subdirectory if no directory exists
36
+ # @param [String] name Required directory name
37
+ # @param [Object] &block Optional directory path
38
+
39
+ def create_subdirectory(name, &block)
40
+ create_directory(name, &block)
41
+ end
42
+
35
43
  # Creates a JS file & logs the file creation
36
44
  # @param [String] name Required file name
37
45
  # @param [Object] json Required json object
@@ -81,25 +89,34 @@ module SnowSync
81
89
  end
82
90
  end
83
91
 
84
- # Requests, retrieves, sets up the JS script file locally
92
+ # Creates the dir structure, requests, retrieves & sets up JS files locally
85
93
 
86
- def setup_sync_directories
94
+ def run_setup_and_sync
95
+ # sync directory boolean tracks sync dir creation
96
+ # necessary for iterative subdir creation and re-syncs
97
+ sync_directory = File.directory?("sync")
98
+ directory_name = "sync"
87
99
  @configs["table_map"].each do |key, value|
88
- directory_name = "sync"
89
- create_directory(directory_name)
90
- path = proc { FileUtils.cd(directory_name) }
91
- sub_directory_name = key
92
- create_directory(sub_directory_name, &path)
100
+ subdirectory_name = key
101
+ if sync_directory
102
+ path = proc { FileUtils.cd(directory_name) }
103
+ create_subdirectory(subdirectory_name, &path)
104
+ else
105
+ create_directory(directory_name)
106
+ path = proc { FileUtils.cd(directory_name) }
107
+ create_subdirectory(subdirectory_name, &path)
108
+ sync_directory = "true"
109
+ end
93
110
  begin
94
111
  user = Base64.strict_decode64(@configs["creds"]["user"])
95
112
  pass = Base64.strict_decode64(@configs["creds"]["pass"])
96
113
  response = RestClient.get(
97
114
  "#{@configs['base_url']}#{value["table"]}?sysparm_query=sys_id%3D" +
98
- "#{value["sysid"]}%5Ename%3D#{value["name"]}",
115
+ "#{value["sys_id"]}%5Ename%3D#{value["name"]}",
99
116
  {:authorization => "#{"Basic " + Base64.strict_encode64("#{user}:#{pass}")}",
100
117
  :accept => "application/json"})
101
- path = proc { FileUtils.cd(sub_directory_name) }
102
- @configs[value["table"] + "_response"] = JSON.parse(response)["result"][0]
118
+ path = proc { FileUtils.cd(subdirectory_name) }
119
+ @configs["table_map"][key]["response"] = JSON.parse(response)["result"][0]
103
120
  json = JSON.parse(response)["result"][0][value["field"]]
104
121
  name = value["name"].snakecase
105
122
  create_file(name, json, &path)
@@ -133,12 +150,13 @@ module SnowSync
133
150
 
134
151
  # Merges JS file changes with the encapsulated table response value
135
152
  # @param [String] file JS file path
153
+ # @param [String] type Servicenow script type
136
154
  # @param [Hash] table_hash Configured servicenow table hash
137
155
 
138
- def merge_update(file, table_hash)
156
+ def merge_update(file, type, table_hash)
139
157
  FileUtils.cd(file.split("/")[0..1].join("/"))
140
158
  script_body = File.open(file.split("/").last).read
141
- @configs[table_hash["table"] + "_response"] = script_body
159
+ @configs["table_map"][type]["mod"] = script_body
142
160
  FileUtils.cd("../..")
143
161
  end
144
162
 
@@ -147,7 +165,7 @@ module SnowSync
147
165
  def start_sync
148
166
  check_required_configs
149
167
  encrypt_credentials
150
- setup_sync_directories
168
+ run_setup_and_sync
151
169
  end
152
170
 
153
171
  # Merges all JS file changes & pushes to the configured servicenow instance
@@ -156,15 +174,16 @@ module SnowSync
156
174
  def push_modifications(files)
157
175
  files.each do |file|
158
176
  file.downcase!
177
+ type = file.split("/")[1]
159
178
  table_hash = table_lookup(file)
160
- merge_update(file, table_hash)
179
+ merge_update(file, type, table_hash)
161
180
  begin
162
181
  user = Base64.strict_decode64(@configs["creds"]["user"])
163
182
  pass = Base64.strict_decode64(@configs["creds"]["pass"])
164
183
  request_body_map = {
165
- table_hash["field"].to_sym => @configs[table_hash["table"] + "_response"]
184
+ table_hash["field"].to_sym => @configs["table_map"][type]["mod"]
166
185
  }
167
- response = RestClient.patch("#{@configs['base_url']}#{table_hash["table"]}/#{table_hash["sysid"]}",
186
+ response = RestClient.patch("#{@configs['base_url']}#{table_hash["table"]}/#{table_hash["sys_id"]}",
168
187
  request_body_map.to_json,
169
188
  {:authorization => "#{"Basic " + Base64.strict_encode64("#{user}:#{pass}")}",
170
189
  :content_type => "application/json", :accept => "application/json"})
@@ -1,3 +1,3 @@
1
1
  module SnowSync
2
- VERSION = "2.0.7"
2
+ VERSION = "3.0.0"
3
3
  end
@@ -125,7 +125,7 @@ describe "table_lookup" do
125
125
 
126
126
  it "should return configured SN table" do
127
127
  table_map = util.table_lookup("sync/script_include/test_class.js")
128
- expect(table_map.keys).to eq ["name", "table", "sysid", "field"]
128
+ expect(table_map.keys).to eq ["name", "table", "sys_id", "field"]
129
129
  expect(table_map["table"]).to eq "sys_script_include"
130
130
  end
131
131
 
@@ -137,20 +137,22 @@ describe "merge_update" do
137
137
  SnowSync::SyncUtil.new(opts = "test")
138
138
  end
139
139
 
140
- it "should merge updated script with the configs object" do
140
+ it "should merge script with the configs object" do
141
141
  FileUtils.mkdir_p("sync")
142
142
  FileUtils.mkdir_p("sync/test_sub_dir")
143
- json = { "property" => "value - with update" }
143
+ json_resp = "var test = 'test'; \n" +
144
+ "var testing = function(arg) { \n\tgs.print(arg) \n}; \n" +
145
+ "testing('test');"
144
146
  name = "TestClass".snakecase
145
147
  path = proc do
146
148
  FileUtils.cd("sync/test_sub_dir")
147
149
  end
148
- util.create_file(name, json, &path)
150
+ util.create_file(name, json_resp, &path)
149
151
  FileUtils.cd("../..")
150
152
  file = "sync/test_sub_dir/test_class.js"
151
153
  table_map = util.table_lookup(file)
152
- util.merge_update(file, table_map)
153
- expect(util.configs["sys_script_include_response"] != nil).to eq true
154
+ util.merge_update(file, "script_include", table_map)
155
+ expect(util.configs["table_map"]["script_include"]["mod"] != nil).to eq true
154
156
  end
155
157
 
156
158
  end
@@ -163,7 +165,7 @@ describe "setup_sync_directories" do
163
165
  end
164
166
 
165
167
  it "should setup and synchronize field from the SN instance" do
166
- util.setup_sync_directories
168
+ util.run_setup_and_sync
167
169
  file = File.open("sync/script_include/test_class.js")
168
170
  expect(file.is_a?(Object)).to eq true
169
171
  FileUtils.rm_rf("sync")
@@ -171,14 +173,14 @@ describe "setup_sync_directories" do
171
173
 
172
174
  end
173
175
 
174
- describe "push_modifications" do
176
+ describe "push_modifications - single table configuration" do
175
177
 
176
178
  let! :util do
177
179
  SnowSync::SyncUtil.new(opts = "test")
178
180
  end
179
181
 
180
182
  it "should push modifications to a configured instance" do
181
- util.setup_sync_directories
183
+ util.run_setup_and_sync
182
184
  file = File.open("sync/script_include/test_class.js", "r+")
183
185
  lines = file.readlines
184
186
  file.close
@@ -189,7 +191,7 @@ describe "push_modifications" do
189
191
  end
190
192
  newfile.close
191
193
  util.push_modifications(["sync/script_include/test_class.js"])
192
- util.setup_sync_directories
194
+ util.run_setup_and_sync
193
195
  file = File.open("sync/script_include/test_class.js", "r+")
194
196
  lines = file.readlines
195
197
  file.close
@@ -197,3 +199,41 @@ describe "push_modifications" do
197
199
  end
198
200
 
199
201
  end
202
+
203
+ describe "push_modifications - mutli-table configuration" do
204
+
205
+ let! :util do
206
+ SnowSync::SyncUtil.new(opts = "test")
207
+ end
208
+
209
+ it "should sync, update, queue, push, re-sync mods for a configured instance" do
210
+ def do_edit(file, edit)
211
+ file = File.open(file, "r+")
212
+ lines = file.readlines
213
+ file.close
214
+ lines[0] = edit
215
+ newfile = File.new(file, "w")
216
+ lines.each do |line|
217
+ newfile.write(line)
218
+ end
219
+ newfile.close
220
+ end
221
+ def run_check(file, edit)
222
+ file = File.open(file, "r+")
223
+ lines = file.readlines
224
+ file.close
225
+ expect(lines[0]).to eq edit
226
+ end
227
+ util.run_setup_and_sync
228
+ # sys_script_include
229
+ do_edit("sync/script_include/test_class.js", "// test comment -\n")
230
+ # sys_ui_action
231
+ do_edit("sync/ui_action/test.js", "// test comment -\n")
232
+ # queued mods, push in sequence
233
+ util.push_modifications(["sync/script_include/test_class.js", "sync/ui_action/test.js"])
234
+ util.run_setup_and_sync
235
+ run_check("sync/script_include/test_class.js", "// test comment -\n")
236
+ run_check("sync/ui_action/test.js", "// test comment -\n")
237
+ end
238
+
239
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snow_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.7
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Wallace
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-16 00:00:00.000000000 Z
11
+ date: 2017-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler