Skip to content

Commit a9e6155

Browse files
committed
bugs fixed, add update mirroring
1 parent 78b9f3f commit a9e6155

File tree

1 file changed

+68
-31
lines changed

1 file changed

+68
-31
lines changed

src/cls/GitHub/API.cls

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -201,50 +201,89 @@ Method GetLastCommit(Owner As %String, Repository As %String, Branch As %String
201201
Return st
202202
}
203203

204-
Method Mirror(Directory As %String = {..#Directory}) As %Status
204+
Method MirrorAll(Directory As %String = {..#Directory}) As %Status
205205
{
206206

207207
#Dim conf As %DynamicObject
208208
#Dim repos As %DynamicArray
209-
Set github = "https://github.com/"
209+
#Dim mirrors As %DynamicArray
210+
#Dim st As %Status
210211

211-
Set stream=##class(%Stream.FileCharacter).%New()
212-
Set sc=stream.LinkToFile(Directory_"repos.json")
213-
Set conf={}.%FromJSON(stream.Read($$$MaxCacheInt))
212+
Set stream = ##class(%Stream.FileCharacter).%New()
213+
Set st = stream.LinkToFile(Directory_"repos.json")
214+
Set conf = {}.%FromJSON(stream.Read($$$MaxCacheInt))
214215

215-
Set ownerfrom = conf.from
216-
Set ownerto = conf.to
217-
Set org = conf.org
218-
Set repos = conf.repos
216+
set mirrors = conf.mirrors
219217

220-
for i=0:1:repos.%Size()-1
218+
for i=0:1:mirrors.%Size()-1
221219
{
222-
Set repo = repos.%Get(i)_".git"
220+
set mirror = mirrors.%Get(i)
221+
Set ownerfrom = mirror.from
222+
Set ownerto = mirror.to
223+
Set org = mirror.org
224+
Set repos = mirror.repos
223225

224-
if (##class(%File).Exists(Directory_repo)=1)
226+
for j=0:1:repos.%Size()-1
225227
{
226-
Do ..FetchAndPush(Directory_repo)
227-
Continue
228+
Set repo = repos.%Get(j)_".git"
229+
230+
if (##class(%File).Exists(Directory_repo)=1)
231+
{
232+
Do ..FetchAndPush(Directory_repo)
233+
Continue
234+
}
235+
236+
Set st1 = ..MirrorOne(repo, ownerfrom, ownerto, org)
237+
Set st = $$$ADDSC(st, st1)
238+
228239
}
229-
230-
Do ..CreateRepo(repo, $select(org=0:"",1:ownerto))
231-
Do ..Clone(github_ownerfrom_"/"_repo, Directory)
232-
Do ..Mirroring(Directory_repo, github_ownerto_"/"_repo)
233-
Do ..FetchAndPush(Directory_repo)
234240
}
241+
Return st
242+
}
243+
244+
Method MirrorOne(Repo As %String, OwnerFrom As %String, OwnerTo As %String, Org As %String, Directory As %String = {..#Directory}) As %Status
245+
{
246+
Set st = $$$OK
247+
248+
Set github = "https://github.com/"
249+
Set sourceRepo = github_OwnerFrom_"/"_Repo
250+
Set mirrorRepo = github_OwnerTo_"/"_Repo
251+
252+
set description = "This is a read-only mirror of "_sourceRepo_". Put you PR there."
253+
254+
w !,"Create repo for mirroring: "_mirrorRepo
255+
Set st1 = ..CreateRepo(Repo, description, $select(Org=0:"",1:OwnerTo))
256+
257+
w " "_st1,!,!,"Clone bare repo: "_sourceRepo
258+
Set st2 = ..Clone(sourceRepo, Directory)
259+
Set st = $$$ADDSC(st1, st2)
260+
261+
w " "_st,!,!,"Mirroring: "_sourceRepo_" -> "_mirrorRepo
262+
Set st3 = ..Mirroring(Directory_Repo, mirrorRepo)
263+
Set st = $$$ADDSC(st, st3)
264+
265+
w " "_st,!,!,"Fetch and push... "
266+
Set st4 = ..FetchAndPush(Directory_Repo)
267+
Set st = $$$ADDSC(st, st4)
268+
269+
Return st
235270
}
236271

237-
Method CreateRepo(Name As %String, Org As %String, Private As %String = "false") As %Status
272+
Method CreateRepo(Name As %String, Description As %String = "empty", Org As %String, Private As %String = "false") As %Status
238273
{
239274
if $d(Org) && (Org '= "") {
240275
Set ..Request.Location = "orgs/" _ Org _ "/repos"
241276
}else {
242277
Set ..Request.Location = "user/repos"
243278
}
244279

245-
Do ..Request.EntityBody.Write("{""name"":"""_Name_""" ,""private"":"_Private_"}")
280+
set json = "{""name"":"""_Name_""" ,""description"":"""_Description_""","_
281+
"""private"":"_Private_", ""has_issues"":false}"
282+
283+
Do ..Request.EntityBody.Write(json)
246284

247285
Set st = ..Request.Post()
286+
248287
Return st
249288
}
250289

@@ -253,9 +292,9 @@ ClassMethod Clone(Repo As %String, Directory As %String, Parameter As %String =
253292
Do $system.Process.CurrentDirectory(Directory)
254293
Set cmd = "git clone " _ Parameter _" "_ Repo
255294

256-
Do ##class(GitHub.Utils).execute(cmd,1)
295+
Set st = ##class(GitHub.Utils).execute(cmd)
257296

258-
Return $$$OK
297+
Return st
259298
}
260299

261300
ClassMethod Mirroring(Directory As %String, RepoForMirroring) As %Status
@@ -264,24 +303,22 @@ ClassMethod Mirroring(Directory As %String, RepoForMirroring) As %Status
264303

265304
Set cmd = "git remote set-url --push origin " _ RepoForMirroring
266305

267-
Do ##class(GitHub.Utils).execute(cmd,1)
306+
Set st = ##class(GitHub.Utils).execute(cmd)
268307

269-
Return $$$OK
308+
Return st
270309
}
271310

272311
ClassMethod FetchAndPush(Directory As %String) As %Status
273312
{
274313
Do $system.Process.CurrentDirectory(Directory)
275-
276-
Set st = $$$OK
277-
314+
w !!,"directory: "_Directory,!
278315
Set cmd = "git fetch -p origin"
279-
Set st = ##class(GitHub.Utils).execute(cmd,1)
316+
Set st1 = ##class(GitHub.Utils).execute(cmd,1)
280317

281318
Set cmd = "git push --mirror"
282-
Set st = ##class(GitHub.Utils).execute(cmd,1)
319+
Set st2 = ##class(GitHub.Utils).execute(cmd,1)
283320

284-
Return st
321+
Return $$$ADDSC(st1, st2)
285322
}
286323

287324
ClassMethod UpdateMirrors(Directory As %String = {..#Directory}) As %Status

0 commit comments

Comments
 (0)