Skip to content

Commit

Permalink
Update Invoke-Git to use System.Diagnostics.Process
Browse files Browse the repository at this point in the history
  • Loading branch information
phbits committed Jun 9, 2021
1 parent 210424f commit 1db68b7
Show file tree
Hide file tree
Showing 10 changed files with 493 additions and 209 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- If a class-based resource has a parent class that contains DSC resource
properties they will now also be returned as part of the DSC resource
parameters ([issue #62](https://github.com/dsccommunity/DscResource.DocGenerator/issues/62)).
- `Invoke-Git`
- Converted to public function.
- Updated to use `System.Diagnostics.Process` for improved error handling.

### Fixed

Expand Down
54 changes: 0 additions & 54 deletions source/Private/Invoke-Git.ps1

This file was deleted.

90 changes: 90 additions & 0 deletions source/Public/Invoke-Git.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<#
.SYNOPSIS
Invokes the git command.
.PARAMETER WorkingDirectory
The path to the git working directory.
.PARAMETER Timeout
Seconds to wait for process to exit.
.PARAMETER Arguments
The arguments to pass to the Git executable.
.EXAMPLE
Invoke-Git clone https://github.com/X-Guardian/xActiveDirectory.wiki.git --quiet
Invokes the Git executable to clone the specified repository to the current working directory.
#>

function Invoke-Git
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$WorkingDirectory,

[Parameter(Mandatory = $false)]
[System.Int32]
$TimeOut = 120,

[Parameter(ValueFromRemainingArguments = $true)]
[System.String[]]
$Arguments
)

$returnValue = @{
'ExitCode' = -1
'StandardOutput' = ''
'StandardError' = ''
}

$argumentsJoined = $Arguments -join ' '

# Trying to remove any access token from the debug output.
if ($argumentsJoined -match ':[\d|a-f].*@')
{
$argumentsJoined = $argumentsJoined -replace ':[\d|a-f].*@', ':RedactedToken@'
}

Write-Debug -Message ($localizedData.InvokingGitMessage -f $argumentsJoined)

try
{
$process = New-Object -TypeName System.Diagnostics.Process
$process.StartInfo.Arguments = $Arguments
$process.StartInfo.CreateNoWindow = $true
$process.StartInfo.FileName = 'git.exe'
$process.StartInfo.RedirectStandardOutput = $true
$process.StartInfo.RedirectStandardError = $true
$process.StartInfo.UseShellExecute = $false
$process.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
$process.StartInfo.WorkingDirectory = $WorkingDirectory

if ($process.Start() -eq $true)
{
if ($process.WaitForExit($TimeOut) -eq $true)
{
$returnValue.ExitCode = $process.ExitCode
$returnValue.StandardOutput = $process.StandardOutput.ReadToEnd()
$returnValue.StandardError = $process.StandardError.ReadToEnd()
}
}
}
catch
{
throw $_
}
finally
{
if ($process)
{
$process.Dispose()
}
}

return $returnValue
}
96 changes: 57 additions & 39 deletions source/Public/Publish-WikiContent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -93,77 +93,95 @@ function Publish-WikiContent

$ErrorActionPreference = 'Stop'

$headers = @{
'Content-type' = 'application/json'
}

Write-Verbose -Message $script:localizedData.CreateTempDirMessage

$tempPath = New-TempFolder

try
{
Push-Location
$wikiRepoName = "https://github.com/$OwnerName/$RepositoryName.wiki.git"

Write-Verbose -Message $script:localizedData.ConfigGlobalGitMessage
Write-Verbose -Message ($script:localizedData.CloneWikiGitRepoMessage -f $WikiRepoName)

if ($PSBoundParameters.ContainsKey('GlobalCoreAutoCrLf'))
$gitCloneResult = Invoke-Git -WorkingDirectory $tempPath.FullName `
-Arguments @( 'clone', $wikiRepoName, $tempPath, '--quiet' )

if ($gitCloneResult.ExitCode -eq 0)
{
$null = Invoke-Git -Arguments 'config', '--global', 'core.autocrlf', $GlobalCoreAutoCrLf
}
Write-Verbose -Message $script:localizedData.ConfigGlobalGitMessage

$wikiRepoName = "https://github.com/$OwnerName/$RepositoryName.wiki.git"
if ($PSBoundParameters.ContainsKey('GlobalCoreAutoCrLf'))
{
$null = Invoke-Git -WorkingDirectory $tempPath.FullName `
-Arguments @( 'config', '--local', 'core.autocrlf', $GlobalCoreAutoCrLf )
}

Write-Verbose -Message ($script:localizedData.CloneWikiGitRepoMessage -f $WikiRepoName)
$copyWikiFileParameters = @{
Path = $Path
DestinationPath = $tempPath
Force = $true
}

$null = Invoke-Git -Arguments 'clone', $wikiRepoName, $tempPath, '--quiet'
Copy-WikiFolder @copyWikiFileParameters

$copyWikiFileParameters = @{
Path = $Path
DestinationPath = $tempPath
Force = $true
}
New-WikiSidebar -ModuleName $ModuleName -Path $tempPath
New-WikiFooter -Path $tempPath

Copy-WikiFolder @copyWikiFileParameters
Set-Location -Path $tempPath

New-WikiSidebar -ModuleName $ModuleName -Path $tempPath
New-WikiFooter -Path $tempPath
Write-Verbose -Message $script:localizedData.ConfigLocalGitMessage

Set-Location -Path $tempPath
$null = Invoke-Git -WorkingDirectory $tempPath.FullName `
-Arguments @( 'config', '--local', 'user.email', $GitUserEmail )

Write-Verbose -Message $script:localizedData.ConfigLocalGitMessage
$null = Invoke-Git -WorkingDirectory $tempPath.FullName `
-Arguments @( 'config', '--local', 'user.name', $GitUserName )

$null = Invoke-Git -Arguments 'config', '--local', 'user.email', $GitUserEmail
$null = Invoke-Git -Arguments 'config', '--local', 'user.name', $GitUserName
$null = Invoke-Git -Arguments 'remote', 'set-url', 'origin', "https://$($GitUserName):$($GitHubAccessToken)@github.com/$OwnerName/$RepositoryName.wiki.git"
$null = Invoke-Git -WorkingDirectory $tempPath.FullName `
-Arguments @( 'remote', 'set-url', 'origin', "https://$($GitUserName):$($GitHubAccessToken)@github.com/$OwnerName/$RepositoryName.wiki.git" )

Write-Verbose -Message $localizedData.AddWikiContentToGitRepoMessage
Write-Verbose -Message $localizedData.AddWikiContentToGitRepoMessage

$null = Invoke-Git -Arguments 'add', '*'
$null = Invoke-Git -WorkingDirectory $tempPath.FullName `
-Arguments @( 'add', '*' )

Write-Verbose -Message ($localizedData.CommitAndTagRepoChangesMessage -f $ModuleVersion)
Write-Verbose -Message ($localizedData.CommitAndTagRepoChangesMessage -f $ModuleVersion)

$invokeGitResult = Invoke-Git -Arguments 'commit', '--message', ($localizedData.UpdateWikiCommitMessage -f $ModuleVersion), '--quiet'
if ($invokeGitResult -eq 0)
{
$null = Invoke-Git -Arguments 'tag', '--annotate', $ModuleVersion, '--message', $ModuleVersion
$gitCommitResult = Invoke-Git -WorkingDirectory $tempPath.FullName `
-Arguments @( 'commit', '--message', "`"$($localizedData.UpdateWikiCommitMessage -f $ModuleVersion)`"", '--quiet' )

if ($gitCommitResult.ExitCode -eq 0)
{
$null = Invoke-Git -WorkingDirectory $tempPath.FullName `
-Arguments @( 'tag', '--annotate', $ModuleVersion, '--message', $ModuleVersion )

Write-Verbose -Message $localizedData.PushUpdatedRepoMessage
Write-Verbose -Message $localizedData.PushUpdatedRepoMessage

$null = Invoke-Git -Arguments 'push', 'origin', '--quiet'
$null = Invoke-Git -Arguments 'push', 'origin', $ModuleVersion, '--quiet'
$null = Invoke-Git -WorkingDirectory $tempPath.FullName `
-Arguments @( 'push', 'origin', '--quiet' )

Write-Verbose -Message $localizedData.PublishWikiContentCompleteMessage
$null = Invoke-Git -WorkingDirectory $tempPath.FullName `
-Arguments @( 'push', 'origin', $ModuleVersion, '--quiet' )

Write-Verbose -Message $localizedData.PublishWikiContentCompleteMessage
}
else
{
Write-Warning -Message $localizedData.NothingToCommitToWiki
}
}
else
{
Write-Warning -Message $localizedData.NothingToCommitToWiki
Write-Verbose -Message $script:localizedData.WikiGitCloneFailMessage

Write-Debug -Message ($script:localizedData.WikiGitCloneFailMessageDebug -f $wikiRepoName)
Write-Debug -Message ($script:localizedData.InvokeGitStandardOutput -f $gitCloneResult.StandardOutput)
Write-Debug -Message ($script:localizedData.InvokeGitStandardError -f $gitCloneResult.StandardError)
Write-Debug -Message ($script:localizedData.InvokeGitExitCodeMessage -f $gitCloneResult.ExitCode)
}
}
finally
{
Pop-Location

Remove-Item -Path $tempPath -Recurse -Force
}
}
5 changes: 5 additions & 0 deletions source/en-US/DscResource.DocGenerator.strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ ConvertFrom-StringData @'
ClassBasedCommentBasedHelpMessage = Reading comment-based help from source file '{0}'.
FoundResourceExamplesMessage = Found {0} examples.
IgnoreAstParseErrorMessage = Errors was found during parsing of comment-based help. These errors were ignored: {0}
WikiGitCloneFailMessage = Failed to clone wiki. Ensure the feature is enabled and the first page has been created.
WikiGitCloneFailMessageDebug = Wiki clone URL '{0}'
InvokeGitStandardOutputMessage = git standard output: '{0}'
InvokeGitStandardErrorMessage = git standard error: '{0}'
InvokeGitExitCodeMessage = git exit code: '{0}'
'@
8 changes: 7 additions & 1 deletion source/tasks/Publish_GitHub_Wiki_Content.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,13 @@ task Publish_GitHub_Wiki_Content {
}
}

$remoteURL = git remote get-url origin
$gitRemoteResult = Invoke-Git -WorkingDirectory $BuildRoot `
-Arguments @( 'remote', 'get-url', 'origin' )

if ($gitRemoteResult.ExitCode -eq 0)
{
$remoteURL = $gitRemoteResult.StandardOutput
}

# Parse the URL for owner name and repository name.
if ($remoteURL -match 'github')
Expand Down
Loading

0 comments on commit 1db68b7

Please sign in to comment.