Skip to content

Commit

Permalink
Add LogRequestBody configuration option to make development debuggi…
Browse files Browse the repository at this point in the history
…ng easier (#60)

* Add `LogRequestBody` configuration option to make development debugging easier

If enabled, the JSON-serialized version of the body of any REST request will
be written to verbose output, so that you can see exactly what is being sent
to GitHub.
  • Loading branch information
HowardWolosky authored Dec 5, 2018
1 parent 2ee3316 commit 98aec29
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Looking for information on how to use this module? Head on over to [README.md](
* [Coding Guidelines](#coding-guidelines)
* [Adding New Configuration Properties](#adding-new-configuration-properties)
* [Code Comments](#code-comments)
* [Debugging Tips](#debugging-tips)
* [Testing](#testing)
* [Installing Pester](#installing-pester)
* [Configuring Your Environment](#configuring-your-environment)
Expand Down Expand Up @@ -270,6 +271,17 @@ code that reads clearly while requiring minimal comments to understand what it's

----------

### Debugging Tips

You may find it useful to configure the module to log the body of all REST requests during
development of a new feature, to make it easier to see exactly what is being sent to GitHub.

```powershell
Set-GitHubConfiguration -LogRequestBody
```

----------

### Testing
[![Build status](https://ci.appveyor.com/api/projects/status/vsfq8kxo2et2dn7i?svg=true
)](https://ci.appveyor.com/project/HowardWolosky/powershellforgithub)
Expand Down
8 changes: 8 additions & 0 deletions GitHubConfiguration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ function Set-GitHubConfiguration
log entry. This can be useful if you have concurrent PowerShell sessions all logging
to the same file, as it would then be possible to filter results based on ProcessId.
.PARAMETER LogRequestBody
If specified, the JSON body of the REST request will be logged to verbose output.
This can be helpful for debugging purposes.
.PARAMETER LogTimeAsUtc
If specified, all times logged will be logged as UTC instead of the local timezone.
Expand Down Expand Up @@ -180,6 +184,8 @@ function Set-GitHubConfiguration

[switch] $LogProcessId,

[switch] $LogRequestBody,

[switch] $LogTimeAsUtc,

[int] $RetryDelaySeconds,
Expand Down Expand Up @@ -262,6 +268,7 @@ function Get-GitHubConfiguration
'DisableTelemetry',
'LogPath',
'LogProcessId',
'LogRequestBody',
'LogTimeAsUtc',
'RetryDelaySeconds',
'SuppressNoTokenWarning',
Expand Down Expand Up @@ -593,6 +600,7 @@ function Import-GitHubConfiguration
'defaultRepositoryName' = [String]::Empty
'logPath' = $logPath
'logProcessId' = $false
'logRequestBody' = $false
'logTimeAsUtc' = $false
'retryDelaySeconds' = 30
'suppressNoTokenWarning' = $false
Expand Down
20 changes: 18 additions & 2 deletions GitHubCore.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ function Invoke-GHRestMethod
$bodyAsBytes = [System.Text.Encoding]::UTF8.GetBytes($Body)
$params.Add("Body", $bodyAsBytes)
Write-Log -Message "Request includes a body." -Level Verbose
if (Get-GitHubConfiguration -Name LogRequestBody)
{
Write-Log -Message $Body -Level Verbose
}
}

[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12
Expand All @@ -217,7 +221,7 @@ function Invoke-GHRestMethod
if ($PSCmdlet.ShouldProcess($jobName, "Start-Job"))
{
[scriptblock]$scriptBlock = {
param($Url, $Method, $Headers, $Body, $ValidBodyContainingRequestMethods, $TimeoutSec, $ScriptRootPath)
param($Url, $Method, $Headers, $Body, $ValidBodyContainingRequestMethods, $TimeoutSec, $LogRequestBody, $ScriptRootPath)

# We need to "dot invoke" Helpers.ps1 and GitHubConfiguration.ps1 within
# the context of this script block since we're running in a different
Expand All @@ -239,6 +243,10 @@ function Invoke-GHRestMethod
$bodyAsBytes = [System.Text.Encoding]::UTF8.GetBytes($Body)
$params.Add("Body", $bodyAsBytes)
Write-Log -Message "Request includes a body." -Level Verbose
if ($LogRequestBody)
{
Write-Log -Message $Body -Level Verbose
}
}

try
Expand Down Expand Up @@ -272,7 +280,15 @@ function Invoke-GHRestMethod
}
}

$null = Start-Job -Name $jobName -ScriptBlock $scriptBlock -Arg @($url, $Method, $headers, $Body, $ValidBodyContainingRequestMethods, (Get-GitHubConfiguration -Name WebRequestTimeoutSec), $PSScriptRoot)
$null = Start-Job -Name $jobName -ScriptBlock $scriptBlock -Arg @(
$url,
$Method,
$headers,
$Body,
$ValidBodyContainingRequestMethods,
(Get-GitHubConfiguration -Name WebRequestTimeoutSec),
(Get-GitHubConfiguration -Name LogRequestBody),
$PSScriptRoot)

if ($PSCmdlet.ShouldProcess($jobName, "Wait-JobWithAnimation"))
{
Expand Down

0 comments on commit 98aec29

Please sign in to comment.