@@ -16,6 +16,14 @@ if (-not ([System.Management.Automation.PSTypeName]'WikiExampleBlockType').Type)
16
16
Add-Type - TypeDefinition $typeDefinition
17
17
}
18
18
19
+ $projectRootPath = Split-Path - Path $PSScriptRoot - Parent
20
+ $testHelperPath = Join-Path - Path $projectRootPath - ChildPath ' TestHelper.psm1'
21
+ Import-Module - Name $testHelperPath - Force
22
+
23
+ $script :localizedData = Get-LocalizedData - ModuleName ' WikiPages' - ModuleRoot $PSScriptRoot
24
+
25
+ $appVeyorApiUrl = ' https://ci.appveyor.com/api'
26
+
19
27
<#
20
28
. SYNOPSIS
21
29
New-DscResourceWikiSite generates wiki pages that can be uploaded to GitHub to use as
@@ -293,4 +301,244 @@ function Get-DscResourceWikiExampleContent
293
301
return $exampleStringBuilder.ToString ()
294
302
}
295
303
296
- Export-ModuleMember - Function New-DscResourceWikiSite
304
+ <#
305
+ . SYNOPSIS
306
+ Publishes the Wiki Content from an AppVeyor job artifact.
307
+
308
+ . DESCRIPTION
309
+ This function adds the content pages from the Wiki Content artifact of a specified
310
+ AppVeyor job to the Wiki of a specified GitHub repository.
311
+
312
+ . PARAMETER RepoName
313
+ The name of the Github Repo, in the format <account>/<repo>.
314
+
315
+ . PARAMETER JobId
316
+ The AppVeyor job id that contains the wiki artifact to publish.
317
+
318
+ . PARAMETER ResourceModuleName
319
+ The name of the Dsc Resource Module.
320
+
321
+ . PARAMETER BuildVersion
322
+ The build version number to tag the Wiki Github commit with.
323
+
324
+ . PARAMETER GithubAccessToken
325
+ The GitHub access token to allow a push to the GitHub Wiki.
326
+
327
+ . PARAMETER GitUserEmail
328
+ The email address to use for the Git commit.
329
+
330
+ . PARAMETER GitUserName
331
+ The user name to use for the Git commit.
332
+
333
+ . EXAMPLE
334
+ Publish-WikiContent -RepoName 'PowerShell/xActiveDirectory' -JobId 'imy2wgp1ylo9bcpb' -ResourceModuleName 'xActiveDirectory' `
335
+ -BuildVersion 'v1.0.0'
336
+
337
+ Adds the Content pages from the AppVeyor Job artifact to the Wiki for the specified GitHub repository.
338
+
339
+ . NOTES
340
+ Appveyor - Push to remote Git repository from a build: https://www.appveyor.com/docs/how-to/git-push/
341
+ #>
342
+ function Publish-WikiContent
343
+ {
344
+ [CmdletBinding ()]
345
+ param
346
+ (
347
+ [Parameter ()]
348
+ [System.String ]
349
+ $RepoName = $env: APPVEYOR_REPO_NAME ,
350
+
351
+ [Parameter ()]
352
+ [System.String ]
353
+ $JobId = $env: APPVEYOR_JOB_ID ,
354
+
355
+ [Parameter ()]
356
+ [System.String ]
357
+ $ResourceModuleName = (($env: APPVEYOR_REPO_NAME -split ' /' )[1 ]),
358
+
359
+ [Parameter ()]
360
+ [System.String ]
361
+ $BuildVersion = $env: APPVEYOR_BUILD_VERSION ,
362
+
363
+ [Parameter ()]
364
+ [System.String ]
365
+ $GithubAccessToken = $env: github_access_token ,
366
+
367
+ [Parameter ()]
368
+ [System.String ]
369
+ $GitUserEmail = $env: APPVEYOR_REPO_COMMIT_AUTHOR_EMAIL ,
370
+
371
+ [Parameter ()]
372
+ [System.String ]
373
+ $GitUserName = $env: APPVEYOR_REPO_COMMIT_AUTHOR
374
+ )
375
+
376
+ $ErrorActionPreference = ' Stop'
377
+
378
+ $headers = @ {
379
+ ' Content-type' = ' application/json'
380
+ }
381
+
382
+ Write-Verbose - Message $script :localizedData.CreateTempDirMessage
383
+ $path = New-TempFolder
384
+
385
+ try
386
+ {
387
+ Write-Verbose - Message $script :localizedData.ConfigGlobalGitMessage
388
+ Invoke-Git config -- global core.autocrlf true
389
+
390
+ $wikiRepoName = " https://github.com/$RepoName .wiki.git"
391
+ Write-Verbose - Message ($script :localizedData.CloneWikiGitRepoMessage -f $WikiRepoName )
392
+ Invoke-Git clone $wikiRepoName $path -- quiet
393
+
394
+ $jobArtifactsUrl = " $appVeyorApiUrl /buildjobs/$JobId /artifacts"
395
+ Write-Verbose - Message ($localizedData.DownloadAppVeyorArtifactDetailsMessage -f $JobId , $jobArtifactsUrl )
396
+
397
+ try
398
+ {
399
+ $artifacts = Invoke-RestMethod - Method Get - Uri $jobArtifactsUrl - Headers $headers - Verbose:$false
400
+ }
401
+ catch
402
+ {
403
+ switch (($_ | ConvertFrom-Json ).Message)
404
+ {
405
+ ' Job not found.'
406
+ {
407
+ throw ($script :localizedData.NoAppVeyorJobFoundError -f $JobId )
408
+ }
409
+
410
+ default
411
+ {
412
+ throw $_
413
+ }
414
+ }
415
+ }
416
+
417
+ $wikiContentArtifact = $artifacts | Where-Object - Property fileName -like " $ResourceModuleName_ *_wikicontent.zip"
418
+
419
+ if ($null -eq $wikiContentArtifact )
420
+ {
421
+ throw ($LocalizedData.NoWikiContentArtifactError -f $JobId )
422
+ }
423
+
424
+ $artifactUrl = " $appVeyorApiUrl /buildjobs/$JobId /artifacts/$ ( $wikiContentArtifact.fileName ) "
425
+
426
+ Write-Verbose - Message ($localizedData.DownloadAppVeyorWikiContentArtifactMessage -f $artifactUrl )
427
+ $wikiContentArtifactPath = Join-Path - Path $Path - ChildPath $wikiContentArtifact.filename
428
+ Invoke-RestMethod - Method Get - Uri $artifactUrl - OutFile $wikiContentArtifactPath - Headers $headers `
429
+ - Verbose:$false
430
+
431
+ Write-Verbose - Message ($localizedData.UnzipWikiContentArtifactMessage -f $wikiContentArtifact.filename )
432
+ Expand-Archive - Path $wikiContentArtifactPath - DestinationPath $path - Force
433
+ Remove-Item - Path $wikiContentArtifactPath
434
+
435
+ Push-Location
436
+ Set-Location - Path $path
437
+
438
+ Write-Verbose - Message $script :localizedData.ConfigLocalGitMessage
439
+ Invoke-Git config -- local user.email $GitUserEmail
440
+ Invoke-Git config -- local user.name $GitUserName
441
+ Invoke-Git remote set-url origin " https://$ ( $GitUserName ) :$ ( $GithubAccessToken ) @github.com/$RepoName .wiki.git"
442
+
443
+ Write-Verbose - Message $localizedData.AddWikiContentToGitRepoMessage
444
+ Invoke-Git add *
445
+
446
+ Write-Verbose - Message ($localizedData.CommitAndTagRepoChangesMessage -f $BuildVersion )
447
+ Invoke-Git commit -- message ($localizedData.UpdateWikiCommitMessage -f $JobId ) -- quiet
448
+ Invoke-Git tag -- annotate $BuildVersion -- message $BuildVersion
449
+
450
+ Write-Verbose - Message $localizedData.PushUpdatedRepoMessage
451
+ Invoke-Git push origin -- quiet
452
+ Invoke-Git push origin $BuildVersion -- quiet
453
+
454
+ Pop-Location
455
+ }
456
+ finally
457
+ {
458
+ Remove-Item - Path $path - Recurse - Force
459
+ Write-Verbose - Message $localizedData.PublishWikiContentCompleteMessage
460
+ }
461
+ }
462
+
463
+ <#
464
+ . SYNOPSIS
465
+ Invokes the git command.
466
+
467
+ . PARAMETER Arguments
468
+ The arguments to pass to the Git executable.
469
+
470
+ . EXAMPLE
471
+ Invoke-Git clone https://github.com/X-Guardian/xActiveDirectory.wiki.git --quiet
472
+
473
+ Invokes the Git executable to clone the specified repository to the current working directory.
474
+ #>
475
+
476
+ function Invoke-Git
477
+ {
478
+ [CmdletBinding ()]
479
+ param
480
+ (
481
+ [parameter (ValueFromRemainingArguments = $true )]
482
+ [System.String []]
483
+ $Arguments
484
+ )
485
+
486
+ Write-Debug - Message ($localizedData.InvokingGitMessage -f $Arguments )
487
+
488
+ try
489
+ {
490
+ & git.exe @Arguments 2> $null
491
+ }
492
+ catch
493
+ {
494
+ if ($LASTEXITCODE -ne 0 )
495
+ {
496
+ throw $_
497
+ }
498
+ }
499
+ }
500
+
501
+ <#
502
+ . SYNOPSIS
503
+ Creates a new temporary folder with a random name.
504
+
505
+ . EXAMPLE
506
+ New-TempFolder
507
+
508
+ This command creates a new temporary folder with a random name.
509
+
510
+ . PARAMETER MaximumRetries
511
+ Specifies the maximum number of time to retry creating the temp folder.
512
+
513
+ . OUTPUTS
514
+ System.IO.DirectoryInfo
515
+ #>
516
+ function New-TempFolder
517
+ {
518
+ [CmdletBinding ()]
519
+ [OutputType ([System.IO.DirectoryInfo ])]
520
+ param (
521
+ [Parameter ()]
522
+ [Int ]
523
+ $MaximumRetries = 10
524
+ )
525
+
526
+ $tempPath = [System.IO.Path ]::GetTempPath()
527
+
528
+ $retries = 0
529
+ do
530
+ {
531
+ $retries ++
532
+ if ($Retries -gt $MaximumRetries )
533
+ {
534
+ throw ($localizedData.NewTempFolderCreationError -f $tempPath )
535
+ }
536
+ $name = [System.IO.Path ]::GetRandomFileName()
537
+ $path = New-Item - Path $tempPath - Name $name - ItemType Directory - ErrorAction SilentlyContinue
538
+ }
539
+ while (-not $path )
540
+
541
+ return $path
542
+ }
543
+
544
+ Export-ModuleMember - Function New-DscResourceWikiSite , Publish-WikiContent
0 commit comments