2
2
import sys
3
3
import json
4
4
import requests
5
+ import logging
5
6
6
7
API_NAMESPACE = 409723
7
8
env_file_path = "/meta/env_vars_to_export"
8
9
9
10
def getBaseUrl (instance ):
10
11
baseUrl = "%s/api" % (instance );
11
- if DEBUG :
12
- print ("baseUrl: " + baseUrl )
12
+ logging .debug ("baseUrl: " + baseUrl )
13
13
return baseUrl
14
14
15
15
def processCallbackResponse (response ):
16
- print ("Processing answer from CR creation REST call" )
17
- print ("Callback returned code %s" % (response .status_code ))
16
+ logging . info ("Processing answer from CR creation REST call" )
17
+ logging . debug ("Callback returned code %s" % (response .status_code ))
18
18
if (response .status_code != 200 and response .status_code != 201 ):
19
- print ("Callback creation failed with code %s" % (response .status_code ))
20
- print ("Error: " + response .text )
19
+ logging . error ("Callback creation failed with code %s" % (response .status_code ))
20
+ logging . error ("Error: " + response .text )
21
21
sys .exit (response .status_code )
22
22
23
- print ("Callback creation successful" )
23
+ logging . info ("Callback creation successful" )
24
24
25
25
26
26
def processCreateChangeRequestResponse (response ):
27
- if DEBUG :
28
- print ("Processing answer from CR creation REST call" )
29
- print (" Change Request returned code %s" % (response .status_code ))
27
+ logging .debug ("Processing answer from CR creation REST call" )
28
+ logging .debug (" Change Request returned code %s" % (response .status_code ))
30
29
if (response .status_code != 200 and response .status_code != 201 ):
31
- print (" Change Request creation failed with code %s" % (response .status_code ))
32
- print (" ERROR: " + response .text )
30
+ logging . error (" Change Request creation failed with code %s" % (response .status_code ))
31
+ logging . error (" ERROR: " + response .text )
33
32
sys .exit (response .status_code )
34
33
35
- print (" Change Request creation successful" )
34
+ logging . info (" Change Request creation successful" )
36
35
data = response .json () # json.loads(response.text)
37
36
CR_NUMBER = data ["result" ]["number" ]
38
37
CR_SYSID = data ["result" ]["sys_id" ]
39
38
FULL_JSON = json .dumps (data , indent = 2 )
40
39
41
- print (f" Change Request Number: { CR_NUMBER } " )
42
- print (f" Change Request sys_id: { CR_SYSID } " )
43
- if DEBUG :
44
- print ( " Change Request full answer:\n " + FULL_JSON )
40
+ logging .info (f" Change Request Number: { CR_NUMBER } " )
41
+ logging .info (f" Change Request sys_id: { CR_SYSID } " )
42
+ logging .debug ( " Change Request full answer:\n " + FULL_JSON )
45
43
46
44
if os .path .exists (env_file_path ):
47
45
env_file = open (env_file_path , "a" )
@@ -59,26 +57,22 @@ def processCreateChangeRequestResponse(response):
59
57
# Fields required are pasted in the data
60
58
def createChangeRequest (user , password , baseUrl , data ):
61
59
62
- if DEBUG :
63
- print ("Entering createChangeRequest:" )
60
+ logging .debug ("Entering createChangeRequest:" )
64
61
65
62
if (bool (data )):
66
63
crBody = json .loads (data )
67
- if DEBUG :
68
- print ("Data: " + data )
64
+ logging .debug ("Data: " + data )
69
65
else :
70
66
crBody = {}
71
- if DEBUG :
72
- print (" Data: None" )
67
+ logging .debug (" Data: None" )
73
68
crBody ["cf_build_id" ] = os .getenv ('CF_BUILD_ID' )
74
69
75
70
76
71
url = "%s/now/table/change_request" % (baseUrl )
77
72
78
- if DEBUG :
79
- print (f" URL: { url } " )
80
- print (f" User: { user } " )
81
- print (f" Body: { crBody } " )
73
+ logging .debug (f" URL: { url } " )
74
+ logging .debug (f" User: { user } " )
75
+ logging .debug (f" Body: { crBody } " )
82
76
83
77
resp = requests .post (url ,
84
78
json = crBody ,
@@ -88,16 +82,18 @@ def createChangeRequest(user, password, baseUrl, data):
88
82
89
83
def processModifyChangeRequestResponse (response , action ):
90
84
91
- if DEBUG :
92
- print ("Processing answer from CR %s REST call" % (action ))
93
- print (" %s Change Request returned code %s" % (action ,response .status_code ))
85
+ logging .debug ("Processing answer from CR %s REST call" % (action ))
86
+ logging .debug (" %s Change Request returned code %s" % (action ,response .status_code ))
94
87
if (response .status_code != 200 and response .status_code != 201 ):
95
- print (" %s Change Request creation failed with code %s" % (action , response .status_code ))
96
- print (" ERROR: " + response .text )
88
+ logging . error (" %s Change Request creation failed with code %s" % (action , response .status_code ))
89
+ logging . error (" ERROR: " + response .text )
97
90
sys .exit (response .status_code )
98
91
99
- print (" %s Change Request successful" % (action ))
92
+ logging . info (" %s Change Request successful" % (action ))
100
93
data = response .json () # json.loads(response.text)
94
+ CR_NUMBER = data ["result" ]["number" ]
95
+ CR_SYSID = data ["result" ]["sys_id" ]
96
+
101
97
FULL_JSON = json .dumps (data , indent = 2 )
102
98
103
99
if (action == "close" ):
@@ -111,6 +107,9 @@ def processModifyChangeRequestResponse(response, action):
111
107
if os .path .exists (env_file_path ):
112
108
env_file = open (env_file_path , "a" )
113
109
env_file .write (f"{ jsonVar } =/codefresh/volume/servicenow-cr-close.json\n " )
110
+ env_file .write (f"CR_NUMBER={ CR_NUMBER } \n " )
111
+ env_file .write (f"CR_SYSID={ CR_SYSID } \n " )
112
+
114
113
env_file .close ()
115
114
116
115
json_file = open ("/codefresh/volume/servicenow-cr-close.json" , "w" )
@@ -120,9 +119,8 @@ def processModifyChangeRequestResponse(response, action):
120
119
# Call SNow REST API to close a CR
121
120
# Fields required are pasted in the data
122
121
def closeChangeRequest (user , password , baseUrl , sysid , code , notes , data ):
123
- if DEBUG :
124
- print ("Entering closeChangeRequest:" )
125
- print (f"DATA: { data } " )
122
+ logging .debug ("Entering closeChangeRequest:" )
123
+ logging .debug (f"DATA: { data } " )
126
124
if (bool (data )):
127
125
crBody = json .loads (data )
128
126
else :
@@ -140,18 +138,16 @@ def closeChangeRequest(user, password, baseUrl, sysid, code, notes, data):
140
138
# Call SNow REST API to update a CR
141
139
# Fields required are pasted in the data
142
140
def updateChangeRequest (user , password , baseUrl , sysid , data ):
143
- if DEBUG :
144
- print ("Entering updateChangeRequest:" )
145
- print (f"DATA: { data } " )
141
+ logging .debug ("Entering updateChangeRequest:" )
142
+ logging .debug (f"DATA: { data } " )
146
143
if (bool (data )):
147
144
crBody = json .loads (data )
148
145
else :
149
146
crBody = {}
150
- print ("WARNING: CR_DATA is empty. What are you updating exactly?" )
147
+ logging . error ("WARNING: CR_DATA is empty. What are you updating exactly?" )
151
148
152
149
url = "%s/now/table/change_request/%s" % (baseUrl , sysid )
153
- if DEBUG :
154
- print (f" update CR URL: { url } " )
150
+ logging .debug (f" update CR URL: { url } " )
155
151
resp = requests .patch (url ,
156
152
json = crBody ,
157
153
headers = {"content-type" :"application/json" },
@@ -162,10 +158,9 @@ def updateChangeRequest(user, password, baseUrl, sysid, data):
162
158
#
163
159
def callback (user , password , baseUrl , number , cf_build_id , token , policy ):
164
160
165
- if DEBUG :
166
- print ("Entering callback:" )
167
- print ("CR Number: " + number )
168
- print ("CF Build ID: " + cf_build_id )
161
+ logging .debug ("Entering callback:" )
162
+ logging .debug ("CR Number: " + number )
163
+ logging .debug ("CF Build ID: " + cf_build_id )
169
164
170
165
url = "%s/%s/codefresh/callback" % (baseUrl , API_NAMESPACE )
171
166
@@ -176,9 +171,8 @@ def callback(user, password, baseUrl, number, cf_build_id, token, policy):
176
171
"cf_url" : os .getenv ("CF_URL" ),
177
172
"cr_policy" : policy
178
173
}
179
- if DEBUG :
180
- print ("Calling POST on " + url )
181
- print ("Data: " + json .dumps (body ))
174
+ logging .debug ("Calling POST on " + url )
175
+ logging .debug ("Data: " + json .dumps (body ))
182
176
183
177
resp = requests .post (url ,
184
178
json = body ,
@@ -187,32 +181,29 @@ def callback(user, password, baseUrl, number, cf_build_id, token, policy):
187
181
processCallbackResponse (response = resp )
188
182
189
183
def checkSysid (sysid ):
190
- if DEBUG :
191
- print ("Entering checkSysid: " )
192
- print (" CR_SYSID: %s" % (sysid ))
184
+ logging .debug ("Entering checkSysid: " )
185
+ logging .debug (" CR_SYSID: %s" % (sysid ))
193
186
194
187
if ( sysid == None ):
195
188
print ("FATAL: CR_SYSID is not defined." )
196
189
sys .exit (1 )
197
190
198
191
def checkToken (token ):
199
- if DEBUG :
200
- print ("Entering checkToken: " )
201
- print (" TOKEN: %s" % (token ))
192
+ logging .debug ("Entering checkToken: " )
193
+ logging .debug (" TOKEN: %s" % (token ))
202
194
203
195
if ( token == None ):
204
- print ("FATAL: TOKEN is not defined." )
196
+ logging . error ("FATAL: TOKEN is not defined." )
205
197
sys .exit (1 )
206
198
207
199
def checkConflictPolicy (policy ):
208
- if DEBUG :
209
- print ("Entering checkConflictPolicy: " )
210
- print (" CR_CONFLICT_POLICY: %s" % (policy ))
200
+ logging .debug ("Entering checkConflictPolicy: " )
201
+ logging .debug (" CR_CONFLICT_POLICY: %s" % (policy ))
211
202
212
203
if policy == "ignore" or policy == "reject" or policy == "wait" :
213
204
return
214
205
else :
215
- print ("FATAL: CR_CONFLICT_POLICY invalid value. Accepted values are ignore, reject or wait." )
206
+ logging . error ("FATAL: CR_CONFLICT_POLICY invalid value. Accepted values are ignore, reject or wait." )
216
207
sys .exit (1 )
217
208
218
209
def main ():
@@ -226,13 +217,19 @@ def main():
226
217
DEBUG = True if os .getenv ('DEBUG' , "false" ).lower () == "true" else False
227
218
TOKEN = os .getenv ('TOKEN' )
228
219
POLICY = os .getenv ('CR_CONFLICT_POLICY' )
229
-
230
220
if DEBUG :
231
- print ("Starting ServiceNow plugin for Codefresh" )
232
- print (f" ACTION: { ACTION } " )
233
- print (f" DATA: { DATA } " )
234
- print (" SYSID: %s" % (os .getenv ('CR_SYSID' )))
235
- print ("---" )
221
+ LOG_LEVEL = "debug"
222
+ else :
223
+ LOG_LEVEL = os .getenv ('LOG_LEVEL' , "info" )
224
+
225
+ log_format = "%(asctime)s:%(levelname)s:%(name)s.%(funcName)s: %(message)s"
226
+ logging .basicConfig (format = log_format , level = LOG_LEVEL .upper ())
227
+
228
+ logging .info ("Starting ServiceNow plugin for Codefresh" )
229
+ logging .debug (f" ACTION: { ACTION } " )
230
+ logging .debug (f" DATA: { DATA } " )
231
+ logging .debug (" SYSID: %s" % (os .getenv ('CR_SYSID' )))
232
+
236
233
237
234
if ACTION == "createcr" :
238
235
# Used only later in the callback but eant to check for error early
@@ -280,7 +277,7 @@ def main():
280
277
data = DATA
281
278
)
282
279
else :
283
- printf ("FATAL: Unknown action: {ACTION}. Allowed values are createCR, closeCR or updateCR." )
280
+ logging . error ("FATAL: Unknown action: {ACTION}. Allowed values are createCR, closeCR or updateCR." )
284
281
sys .exit (1 )
285
282
286
283
0 commit comments