Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to add additional headers to Invoke-VSTeamRequest #240

Merged
merged 1 commit into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .docs/Invoke-VSTeamRequest.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ Converts the PowerShell object into JSON and displays in the console.
Type: Switch
```

### -AdditionalHeaders

Adds additional headers to the request

```yaml
Type: Hashtable
```

## INPUTS

### System.String
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 6.4.5

Merged [Pull Request](https://github.com/DarqueWarrior/vsteam/pull/240) from [Michel Zehnder](https://github.com/MichelZ) which included the following:

Add option to add additional headers (-AdditionalHeaders) to the request generated by the `Invoke-VSTeamRequest` call

## 6.4.4

Merged [Pull Request](https://github.com/DarqueWarrior/vsteam/pull/231) from [Dave Neeley](https://github.com/daveneeley) which included the following:
Expand Down
16 changes: 13 additions & 3 deletions Source/Private/common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -597,13 +597,14 @@ function _callAPI {
[string]$ProjectName,
[string]$Team,
[string]$Url,
[object]$QueryString
[object]$QueryString,
[hashtable]$AdditionalHeaders
)

# If the caller did not provide a Url build it.
if (-not $Url) {
$buildUriParams = @{ } + $PSBoundParameters;
$extra = 'method', 'body', 'InFile', 'OutFile', 'ContentType'
$extra = 'method', 'body', 'InFile', 'OutFile', 'ContentType', 'AdditionalHeaders'
foreach ($x in $extra) { $buildUriParams.Remove($x) | Out-Null }
$Url = _buildRequestURI @buildUriParams
}
Expand All @@ -625,6 +626,7 @@ function _callAPI {

if (_useWindowsAuthenticationOnPremise) {
$params.Add('UseDefaultCredentials', $true)
$params.Add('Headers', @{})
}
elseif (_useBearerToken) {
$params.Add('Headers', @{Authorization = "Bearer $env:TEAM_TOKEN" })
Expand All @@ -633,8 +635,16 @@ function _callAPI {
$params.Add('Headers', @{Authorization = "Basic $env:TEAM_PAT" })
}

if ($AdditionalHeaders -and $AdditionalHeaders.PSObject.Properties.name -match "Keys")
{
foreach ($key in $AdditionalHeaders.Keys)
{
$params['Headers'].Add($key, $AdditionalHeaders[$key])
}
}

# We have to remove any extra parameters not used by Invoke-RestMethod
$extra = 'Area', 'Resource', 'SubDomain', 'Id', 'Version', 'JSON', 'ProjectName', 'Team', 'Url', 'QueryString'
$extra = 'Area', 'Resource', 'SubDomain', 'Id', 'Version', 'JSON', 'ProjectName', 'Team', 'Url', 'QueryString', 'AdditionalHeaders'
foreach ($e in $extra) { $params.Remove($e) | Out-Null }

try {
Expand Down
3 changes: 2 additions & 1 deletion Source/Public/Invoke-VSTeamRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ function Invoke-VSTeamRequest {
[string]$OutFile,
[switch]$JSON,
[string]$ContentType,
[string]$Url
[string]$Url,
[hashtable]$AdditionalHeaders
)
DynamicParam {
_buildProjectNameDynamicParam -Mandatory $false
Expand Down
2 changes: 1 addition & 1 deletion Source/VSTeam.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'VSTeam.psm1'

# Version number of this module.
ModuleVersion = '6.4.4'
ModuleVersion = '6.4.5'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
13 changes: 13 additions & 0 deletions unit/test/team.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ InModuleScope VSTeam {
Assert-VerifiableMock
}
}

Context 'Invoke-VSTeamRequest AdditionalHeaders' {
[VSTeamVersions]::Account = 'https://dev.azure.com/test'
Mock Invoke-RestMethod { return @() } -Verifiable -ParameterFilter {
$Headers["Test"] -eq 'Test'
}

Invoke-VSTeamRequest -Area release -Resource releases -Id 1 -SubDomain vsrm -Version '4.1-preview' -ProjectName testproject -JSON -AdditionalHeaders @{Test = "Test"}

It 'Should call API' {
Assert-VerifiableMock
}
}
}

Describe 'Team VSTS' {
Expand Down