forked from fruition-partners/sn-sublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceNowBuild.py
191 lines (150 loc) · 6.83 KB
/
serviceNowBuild.py
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
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
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
'''
Copyright (c) 2013 Fruition Partners, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
import sublime
import sublime_plugin
import urllib2
import re
import base64
import json
class ServiceNowBuildListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
view.run_command('service_now_build')
def on_load(self,view):
sublime.set_timeout(syncFileCallback,10)
class ServiceNowBuildCommand(sublime_plugin.TextCommand):
def run(self, edit):
# Get the body of the file
reg = sublime.Region(0, self.view.size())
self.text = self.view.substr(reg)
# Get the Base64 encoded Auth String
authentication = get_authentication(self, edit)
if not authentication:
return
# Get the field name from the comment in the file
fieldname = get_fieldname(self.text)
try:
data = json.dumps({fieldname: self.text})
url = self.url + "&sysparm_action=update&JSON"
url = url.replace("sys_id", "sysparm_query=sys_id")
result = http_call(authentication, url, data)
#Check if success. If error, try with JSONv2 URL
if "\"__status\":\"success\"" not in result:
url = self.url + "&sysparm_action=update&JSONv2"
url = url.replace("sys_id", "sysparm_query=sys_id")
result = http_call(authentication, url, data)
if "\"__status\":\"success\"" not in result:
print "SN-Sublime - File Upload Failed!!"
print "URL used: " + url
else:
print "SN-Sublime - File Successully Uploaded"
return
except (urllib2.HTTPError) as (e):
err = 'SN-Sublime - HTTP Error: %s' % (str(e.code))
except (urllib2.URLError) as (e):
err = 'SN-Sublime - URL Error: %s' % (str(e))
print err
return
class ServiceNowSync(sublime_plugin.TextCommand):
def run(self, edit):
# Get the body of the file
reg = sublime.Region(0, self.view.size())
self.text = self.view.substr(reg)
# Get the Base64 encoded Auth String
authentication = get_authentication(self, edit)
if not authentication:
return
try:
url = self.url + "&sysparm_action=get&JSON"
url = url.replace("sys_id", "sysparm_sys_id")
response_data = json.loads(http_call(authentication,url,{}))
serverText = response_data['records'][0]['script']
if self.text != serverText and sublime.ok_cancel_dialog("File has been updated on server. \nPress OK to Reload."):
self.view.erase(edit, reg)
self.view.begin_edit()
self.view.insert(edit,0,serverText)
self.view.end_edit(edit)
return
except (urllib2.HTTPError) as (e):
err = 'SN-Sublime - HTTP Error %s' % (str(e.code))
except (urllib2.URLError) as (e):
err = 'SN-Sublime - URL Error %s' % (str(e.code))
print err
def http_call(authentication, url, data):
timeout = 5
request = urllib2.Request(url, data)
request.add_header("Authorization", authentication)
request.add_header("Content-type", "application/json")
http_file = urllib2.urlopen(request, timeout=timeout)
result = http_file.read()
return result
def get_authentication(sublimeClass, edit):
# Get the file URL from the comment in the file
sublimeClass.url = get_url(sublimeClass.text)
if not sublimeClass.url:
return False
# Get the instance name from the URL
instance = get_instance(sublimeClass.url)
if not instance:
return False
settings = sublime.load_settings('SN.sublime-settings')
reg = sublime.Region(0, sublimeClass.view.size())
text = sublimeClass.view.substr(reg)
authMatch = re.search(r"__authentication[\W=]*([a-zA-Z0-9:~`\!@#$%\^&*()_\-;,.]*)", text)
if authMatch and authMatch.groups()[0] != "STORED":
user_pass = authMatch.groups()[0]
authentication = store_authentication(sublimeClass, edit, user_pass, instance)
else:
authentication = settings.get(instance)
if authentication:
return "Basic " + authentication
else:
print "SN-Sublime - Auth Error. No authentication header tag found"
return False
def store_authentication(sublimeClass, edit, authentication, instance):
base64string = base64.encodestring(authentication).replace('\n', '')
reg = sublime.Region(0, sublimeClass.view.size())
text = sublimeClass.view.substr(reg)
sublimeClass.text = text.replace(authentication, "STORED")
sublimeClass.view.replace(edit, reg, sublimeClass.text)
settings = sublime.load_settings('SN.sublime-settings')
settings.set(instance, base64string)
settings = sublime.save_settings('SN.sublime-settings')
return base64string
def get_fieldname(text):
fieldname_match = re.search(r"__fieldName[\W=]*([a-zA-Z0-9_]*)", text)
if fieldname_match:
return fieldname_match.groups()[0]
else:
return 'script'
def get_url(text):
url_match = re.search(r"__fileURL[\W=]*([a-zA-Z0-9:/.\-_?&=]*)", text)
if url_match:
return url_match.groups()[0]
else:
print "SN-Sublime - Error. Not a ServiceNow File"
return False
def get_instance(url):
instance_match = re.search(r"//([a-zA-Z0-9]*)\.", url)
if instance_match:
return instance_match.groups()[0]
else:
print "SN-Sublime - Error. No instance info found"
return False
def syncFileCallback():
sublime.active_window().active_view().run_command('service_now_sync')