|
| 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 | +} |
0 commit comments