Skip to content

Commit 5795251

Browse files
committed
add method patch() and class GitHub
1 parent 4187158 commit 5795251

File tree

2 files changed

+158
-2
lines changed

2 files changed

+158
-2
lines changed

sc.GitHub.cls

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
Class sc.GitHub
2+
{
3+
4+
ClassMethod Get(Output Items, Owner As %String = "", Repository As %String = "", Username As %String = "clminstaller", Password As %String = "clminstaller2016", CommitFrom As %Integer = "", CommitTo As %Integer = "") As %Status
5+
{
6+
#dim req As %Net.HttpRequest
7+
set req = ..CreateRequest(Username, Password)
8+
set req.Location = "repos/" _ Owner _ "/" _ Repository _ "/commits" // as described in https://developer.github.com/v3/repos/
9+
10+
set links = ##class(%ListOfDataTypes).%New()
11+
set sc = ..GetSHAs(.req, CommitFrom, CommitTo,.Items)
12+
13+
return sc
14+
}
15+
16+
ClassMethod GetSHAs(Request As %Net.HttpRequest, CommitFrom As %Integer, CommitTo As %Integer, Output Items) As %Status
17+
{
18+
set st = Request.Get()
19+
20+
return:$$$ISERR(st) st
21+
return:(Request.HttpResponse.StatusCode = 404) $$$ERROR($$$GeneralError,"Repository doesn't exist OR you don't have access")
22+
return:((Request.HttpResponse.StatusCode = 403) && (Request.HttpResponse.GetHeader("X-RATELIMIT-REMAINING")=0)) $$$ERROR($$$GeneralError,"API rate limit exceeded. Try logging in.")
23+
return:(Request.HttpResponse.StatusCode '= 200) $$$ERROR($$$GeneralError,"Received " _ Request.HttpResponse.StatusCode _ " expected 200")
24+
25+
#dim objects As List of %ZEN.proxyObject
26+
set st = ##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject(Request.HttpResponse.Data,,.objects,1)
27+
28+
set:CommitFrom="" CommitFrom=1
29+
set:CommitTo="" CommitTo=1
30+
if (CommitFrom > CommitTo) {
31+
w "CommitFrom can't be more than CommitTo",!
32+
return $$$NO
33+
}
34+
35+
if ((CommitFrom >= 1) && (CommitTo <= objects.Size)) {
36+
set list = ""
37+
for i=CommitFrom:1:CommitTo
38+
{
39+
set sha = objects.GetAt(i).%data("sha")
40+
set list = list _ $lb(sha)
41+
}
42+
}else {
43+
w "CommitTo can't be more than 30"
44+
return $$$NO
45+
}
46+
47+
do ..GetFileNameForReleaseBySHAs(Request,list,.Items)
48+
49+
return $$$OK
50+
}
51+
52+
ClassMethod GetFileNameForReleaseBySHAs(Request As %Net.HttpRequest, SHAsList, Output filesForRelease) As %Status
53+
{
54+
#dim objects As List of %ZEN.proxyObject
55+
#dim files As List of %ZEN.proxyObject
56+
set location = Request.Location
57+
for i=1:1:$ll(SHAsList)
58+
{
59+
set Request.Location = location_"/"_$lg(SHAsList, i)
60+
61+
set st = Request.Get()
62+
Return:$$$ISERR(st) st
63+
64+
set st = ##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject(Request.HttpResponse.Data,,.objects,1)
65+
return:$$$ISERR(st) st
66+
67+
set files = objects.%data("files")
68+
69+
for j=1:1:files.Size
70+
{
71+
set fileStatus = files.GetAt(j).%data("status")
72+
continue:(fileStatus'="modified")&&(fileStatus'="added")
73+
set fileName = files.GetAt(j).%data("filename")
74+
set:$L(fileName,".xml")'=1 fileName = $extract(fileName,1,*-4)
75+
continue:..IsCacheFile(fileName)=0
76+
set filesForRelease(..ToCacheName(fileName)) = ""
77+
}
78+
}
79+
}
80+
81+
ClassMethod CreateRequest(Username As %String, Password As %String) As %Net.HttpRequest
82+
{
83+
set namespace = $Namespace
84+
set SSLConfig = "GitHub"
85+
86+
zn "%SYS"
87+
do:'##class(Security.SSLConfigs).Exists(SSLConfig) ##class(Security.SSLConfigs).Create(SSLConfig)
88+
zn namespace
89+
90+
set req=##class(%Net.HttpRequest).%New()
91+
set req.Https=1
92+
set req.SSLConfiguration=SSLConfig
93+
set req.Server="api.github.com"
94+
do req.SetHeader("Accept","application/vnd.github.v3+json") // we want 3rd version of api
95+
96+
if ($d(Username) && $d(Password) && (Username'="") && (Password'="")) { // supply Username and Password, if both are provided. GitHub accept Basic Auth
97+
set req.Username = Username // https://developer.github.com/v3/auth/
98+
set req.Password = Password
99+
}
100+
101+
return req
102+
}
103+
104+
/// Check that incoming file is the one you need.
105+
ClassMethod IsCacheFile(FileName As %String) As %Boolean
106+
{
107+
set extensions = "xml,cls,csp,csr,mac,int,bas,inc,gbl,prj,obj,pkg,gof,dfi,pivot,dashboard," //html,css,js,ts,scss,"
108+
return:($L(FileName,".")=1) 0 //no extension
109+
set fileExtension = $P(FileName,".",*)
110+
return $F(extensions,","_$ZCVT(fileExtension,"l")_",")
111+
}
112+
113+
ClassMethod ToCacheName(FileName) As %String
114+
{
115+
set:$L(FileName,".xml")'=1 FileName = $extract(FileName,1,*-4)
116+
set str = $Select($L(FileName,"cls/")>1:$Replace($P(FileName,"cls/",2),"/","."),
117+
$L(FileName,"csp/")>1:$Replace($P(FileName,"csp/",2),"/","."),
118+
$L(FileName,"dfi/")>1:$Replace($P(FileName,"dfi/",2),"/","."),
119+
$L(FileName,"inc/")>1:$Replace($P(FileName,"inc/",2),"/","."),
120+
$L(FileName,"web/")>1:$Replace($P(FileName,"web/",2),"/","."),
121+
1:$Replace(FileName,"/",".")
122+
)
123+
set str = $P(str,".",1,*-1)_"."_ $ZCVT($P(str,".",*),"U")
124+
quit str
125+
}
126+
127+
}

sc.code.cls

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,22 @@ ClassMethod import(filemask = "*.xml;*.cls;*.mac;*.int;*.inc;*.dfi", qspec = "ck
156156
Q sc
157157
}
158158

159+
ClassMethod patch(filename = "C:\temp\workdir\patch.xml", CommitFrom = "", CommitTo = "") As %Status
160+
{
161+
s gln = ..gln()
162+
163+
set owner = @gln@("owner")
164+
set repository = @gln@("repository")
165+
set user = @gln@("user")
166+
set password = @gln@("password")
167+
168+
do ##class(sc.GitHub).Get(.items, owner, repository, user, password, CommitFrom, CommitTo)
169+
//zw items
170+
set sc = $system.OBJ.Export(.items,filename)
171+
172+
quit sc
173+
}
174+
159175
ClassMethod inFilter(ByRef filtermask, filename) As %Boolean
160176
{
161177
s result=0
@@ -189,7 +205,7 @@ ClassMethod setIgnore(ByRef filtermask)
189205
{
190206
s filtermask($I(filtermask))=file.ReadLine()
191207
}
192-
quit
208+
quit
193209
}
194210

195211
/// get or set working directory for export/import source
@@ -213,11 +229,24 @@ ClassMethod init()
213229
set stream=##class(%Stream.FileCharacter).%New()
214230
set sc=stream.LinkToFile(..workdir()_$$$confile)
215231
s conf={}.%FromJSON(stream.Read($$$MaxCacheInt))
232+
216233
s gln=..gln()
234+
217235
s @gln@("compileList")=conf.compileList
218236
s @gln@("projectName")=conf.projectName
237+
s @gln@("owner")=conf.owner
238+
s @gln@("repository")=conf.repository
239+
s @gln@("user")=conf.user
240+
s @gln@("password")=conf.password
241+
219242
w "compileList="_conf.compileList,!
220-
w "projectName="_conf.projectName
243+
w "projectName="_conf.projectName,!
244+
w "owner="_conf.owner,!
245+
w "repository="_conf.repository,!
246+
w "user="_conf.user,!
247+
w "password="_conf.password,!
248+
249+
quit $$$OK
221250
}
222251

223252
/// export release file for list and project settings

0 commit comments

Comments
 (0)