Skip to content

Commit

Permalink
Initial support for vsts-cli
Browse files Browse the repository at this point in the history
- Add all pr commands and short commands
- Add test for filtering params

Implements #549

Not implemented in this PR: 'subgroups' (work-items set-vote policies)
  • Loading branch information
flcdrg committed May 19, 2018
1 parent f9f2b4b commit a5702ff
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
24 changes: 24 additions & 0 deletions src/GitParamTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ $longGitParams = @{
whatchanged = 'since'
}

$shortVstsGlobal = 'h o'
$shortVstsParams = @{
abandon = "i $shortVstsGlobal"
create = "d i p r s t $shortVstsGlobal"
complete = "i $shortVstsGlobal"
list = "i p r s t $shortVstsGlobal"
reactivate = "i $shortVstsGlobal"
'set-vote' = "i $shortVstsGlobal"
show = "i $shortVstsGlobal"
update = "d i $shortVstsGlobal"
}

$longVstsGlobal = 'debug help output query verbose'
$longVstsParams = @{
abandon = "id detect instance $longVstsGlobal"
create = "auto-complete delete-source-branch work-items bypass-policy bypass-policy-reason description detect instance merge-commit-message open project repository reviewers source-branch squash target-branch title $longVstsGlobal"
complete = "id detect instance $longVstsGlobal"
list = " $longVstsGlobal"
reactivate = " $longVstsGlobal"
'set-vote' = " $longVstsGlobal"
show = " $longVstsGlobal"
update = " $longVstsGlobal"
}

# Variable is used in GitTabExpansion.ps1
$gitParamValues = @{
blame = @{
Expand Down
32 changes: 26 additions & 6 deletions src/GitTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ $Global:GitTabSettings = New-Object PSObject -Property @{
$subcommands = @{
bisect = "start bad good skip reset visualize replay log run"
notes = 'add append copy edit get-ref list merge prune remove show'
pr = 'create update show list complete abandon reactivate reviewers work-items set-vote policies'
reflog = "show delete expire"
remote = "
add rename remove set-head set-branches
Expand Down Expand Up @@ -56,6 +57,8 @@ $script:someCommands = @('add','am','annotate','archive','bisect','blame','branc
$script:gitCommandsWithLongParams = $longGitParams.Keys -join '|'
$script:gitCommandsWithShortParams = $shortGitParams.Keys -join '|'
$script:gitCommandsWithParamValues = $gitParamValues.Keys -join '|'
$script:vstsCommandsWithShortParams = $shortVstsParams.Keys -join '|'
$script:vstsCommandsWithLongParams = $longVstsParams.Keys -join '|'

try {
if ($null -ne (git help -a 2>&1 | Select-String flow)) {
Expand Down Expand Up @@ -218,15 +221,15 @@ function script:expandGitAlias($cmd, $rest) {
}
}

function script:expandLongParams($cmd, $filter) {
$longGitParams[$cmd] -split ' ' |
function script:expandLongParams($hash, $cmd, $filter) {
$hash[$cmd] -split ' ' |
Where-Object { $_ -like "$filter*" } |
Sort-Object |
ForEach-Object { -join ("--", $_) }
}

function script:expandShortParams($cmd, $filter) {
$shortGitParams[$cmd] -split ' ' |
function script:expandShortParams($hash, $cmd, $filter) {
$hash[$cmd] -split ' ' |
Where-Object { $_ -like "$filter*" } |
Sort-Object |
ForEach-Object { -join ("-", $_) }
Expand Down Expand Up @@ -264,6 +267,23 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) {

switch -regex ($lastBlock -replace "^$(Get-AliasPattern git) ","") {

# Handles git pr alias
"\!f\(\) \{ exec vsts code pr `"\$\@`"; \}; f\s+(?<op>\S*)$" {
gitCmdOperations $subcommands 'pr' $matches['op']
}

# Handles git pr <cmd> --<param>
"\!f\(\) \{ exec vsts code pr `"\$\@`"; \}; f\s+(?<cmd>$vstsCommandsWithLongParams).*--(?<param>\S*)$"
{
expandLongParams $longVstsParams $matches['cmd'] $matches['param']
}

# Handles git pr <cmd> -<shortparam>
"\!f\(\) \{ exec vsts code pr `"\$\@`"; \}; f\s+(?<cmd>$vstsCommandsWithShortParams).*-(?<shortparam>\S*)$"
{
expandShortParams $shortVstsParams $matches['cmd'] $matches['shortparam']
}

# Handles git <cmd> <op>
"^(?<cmd>$($subcommands.Keys -join '|'))\s+(?<op>\S*)$" {
gitCmdOperations $subcommands $matches['cmd'] $matches['op']
Expand Down Expand Up @@ -400,12 +420,12 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) {

# Handles git <cmd> --<param>
"^(?<cmd>$gitCommandsWithLongParams).* --(?<param>\S*)$" {
expandLongParams $matches['cmd'] $matches['param']
expandLongParams $longGitParams $matches['cmd'] $matches['param']
}

# Handles git <cmd> -<shortparam>
"^(?<cmd>$gitCommandsWithShortParams).* -(?<shortparam>\S*)$" {
expandShortParams $matches['cmd'] $matches['shortparam']
expandShortParams $shortGitParams $matches['cmd'] $matches['shortparam']
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions test/GitParamTabExpansionVsts.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
. $PSScriptRoot\Shared.ps1

Describe 'ParamsTabExpansion VSTS Tests' {
Context 'Push Parameters TabExpansion Tests' {
# Create a git alias for 'pr', as if we'd installed vsts-cli
BeforeEach {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$origPath = Get-Location
$temp = [System.IO.Path]::GetTempPath()
$repoPath = Join-Path $temp ([IO.Path]::GetRandomFileName())

&$gitbin init $repoPath
Set-Location $repoPath

&$gitbin config alias.pr "!f() { exec vsts code pr \`"`$`@\`"; }; f"
}
AfterEach {
Set-Location $origPath
if (Test-Path $repoPath) {
Remove-Item $repoPath -Recurse -Force
}
}

It 'Tab completes git pr create parameters values' {
$result = & $module GitTabExpansionInternal 'git pr create --'
$result -contains '--auto-complete' | Should Be $true
}
It 'Tab completes git pr create auto-complete parameters values' {
$result = & $module GitTabExpansionInternal 'git pr create --auto-complete --'
$result -contains '--delete-source-branch' | Should Be $true
}
It 'Tab completes git pr create all short push parameters' {
$result = & $module GitTabExpansionInternal 'git pr create -'
$result -contains '-d' | Should Be $true
$result -contains '-i' | Should Be $true
$result -contains '-p' | Should Be $true
$result -contains '-r' | Should Be $true
$result -contains '-s' | Should Be $true
$result -contains '-h' | Should Be $true
$result -contains '-o' | Should Be $true
}
}
}
7 changes: 7 additions & 0 deletions test/TabExpansion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ Describe 'TabExpansion Tests' {
}
}

Context 'Vsts' {
It 'Tab completes pr options' {
$result = & $module GitTabExpansionInternal 'git pr '
$result -contains 'abandon' | Should Be $true
}
}

Context 'Add/Reset/Checkout TabExpansion Tests' {
BeforeEach {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
Expand Down

0 comments on commit a5702ff

Please sign in to comment.