Skip to content
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
1 change: 1 addition & 0 deletions Test/private/MockCall_Project.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function Get-Mock_Project_625 {
$project.id = $p.id
$project.owner = $p.owner.login
$project.number = $p.number
$project.title = $p.title
$project.url = $p.url

# Add Items to mock
Expand Down
5 changes: 5 additions & 0 deletions Test/private/MockCall_Project700.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function Get-Mock_Project_700 {
# Project info
$project.id = $pActual.id
$project.owner = $pActual.owner.login
$project.title = $pActual.title
$project.number = $pActual.number
$project.url = $pActual.url
$project.cacheFileName = "$($pActual.owner.login)_$($pActual.number).json"
Expand All @@ -69,6 +70,8 @@ function Get-Mock_Project_700 {
$project.issueToCreateAddAndRemove= @{
id = $id
number = 46
title = "Issue for development"
body = "Body of issue for development"
url = "https://github.com/octodemo/rulasg-dev-1/issues/46"
getIssueOrPullRequestMockFile = "invoke-getissueorpullrequest-46.json"
itemId = $itemId
Expand Down Expand Up @@ -126,7 +129,9 @@ function Get-Mock_Project_700 {
$draftIssue = $pActual.items.nodes | Where-Object { $_.content.title -eq "DraftIssue for development" }
$fss = $draftIssue.fieldValues.nodes | Where-Object { $_.field.id -eq $($fieldsingleselect.id) }
$project.draftissue = @{
order = 11 # order of this item when calling spi
id = $draftIssue.id
url = "https://github.com/orgs/$($project.owner)/projects/$($project.number)/views/1?pane=issue&itemId=$($draftIssue.fullDatabaseId)"
contentId = $draftIssue.content.id
title = $draftIssue.content.title
status = ($draftIssue.fieldValues.nodes | Where-Object { $_.field.name -eq "Status" }).name
Expand Down
20 changes: 20 additions & 0 deletions Test/public/environment/projectParameters.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function Test_SetProjectParameters_SUCCESS{

$p = Get-Mock_Project_700 ; $owner = $p.Owner ; $projectNumber = $p.Number ; $projectTitle = $p.title
MockCall_GetProject $p
$dbPath = Get-Mock_DatabaseRootPath

# Act
Set-ProjectParameters -Owner $owner -ProjectNumber $projectNumber

$v = @{
Owner = @{value =$owner ; file = $($dbPath | Join-Path -Child "EnvironmentCache_Owner.json")}
ProjectNumber = @{value =$projectNumber ; file = $($dbPath | Join-Path -Child "EnvironmentCache_ProjectNumber.json")}
ProjectTitle = @{value =$projectTitle ; file = $($dbPath | Join-Path -Child "EnvironmentCache_ProjectTitle.json")}
}

$v.keys | ForEach-Object {
$key = $_
Assert-AreEqual -Expected $v[$key].value -Presented (Get-Content -Path $v[$key].file | ConvertFrom-Json)
}
}
43 changes: 28 additions & 15 deletions Test/public/getPrompt.test.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
function Test_GetProjecthelperPrompt {

$owner = "octodemo"
$projectNumber = "625"
$s = $ProjecthelperPromoptSettings
$p = Get-Mock_Project_625 ; $owner = $p.owner ; $projectNumber = $p.number ; $projectTitle = $p.title
MockCall_GetProject -MockProject $p

$Mock_ProjectHelperPromptSettingsVariableName = "Mock_ProjectHelperPromptSettingsVariableName"
Clear-Variable -Name $Mock_ProjectHelperPromptSettingsVariableName -Scope Global -ErrorAction SilentlyContinue
MockCallToString -Command "echo ProjecthelperPromoptSettings" -OutString $Mock_ProjectHelperPromptSettingsVariableName

MockCall_GitHubOrgProjectWithFields -Owner $owner -ProjectNumber $projectNumber -FileName "invoke-GitHubOrgProjectWithFields-octodemo-625-skipitems.json" -SkipItems
MockCallJson -Command 'Invoke-GetItem -itemid id1' -FileName "invoke-getitem-id1.json"
Expand All @@ -17,21 +20,27 @@
Assert-IsNull -Object $(($result | select-string -Pattern "^\[$" ).LineNumber)

# Set environment with empty values
Set-ProjectHelperEnvironment -Owner $owner -ProjectNumber $projectNumber
Set-ProjectParameters -Owner $owner -ProjectNumber $projectNumber

# With environment, without new line
$result = Invoke-WriteProjecthelperPrompt
# Find the line with '[' character
$resultLine = ($result | select-string -Pattern "^\[$" ).LineNumber

$s = (Get-Variable -Name $Mock_ProjectHelperPromptSettingsVariableName -Scope Global).Value

Assert-AreEqual -Presented $result[$resultLine - 1] -Expected $s.BeforeStatus.PreText
Assert-AreEqual -Presented $result[$resultLine] -Expected $($($s.OwnerStatus.PreText)+$owner)
Assert-AreEqual -Presented $result[$resultLine + 1] -Expected $s.DelimStatus1.PreText
Assert-AreEqual -Presented $result[$resultLine + 2] -Expected $($($s.NumberStatus.PreText)+$projectNumber)
Assert-AreEqual -Presented $result[$resultLine + 3] -Expected "" # $s.DelimStatus2.PreText is " " that is converted to "" by posh-git"
Assert-AreEqual -Presented $result[$resultLine + 4] -Expected $s.OKStatus.PreText
Assert-AreEqual -Presented $result[$resultLine + 5] -Expected $s.AfterStatus.PreText
Assert-AreEqual -Presented $result[$resultLine + 6] -Expected "" # $s.SpaceStatus.PreText " " that is converted to "" by posh-git

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
Assert-AreEqual -Presented $result[$resultLine + 4] -Expected $($($s.TitleStatus.PreText)+$projectTitle)
Assert-AreEqual -Presented $result[$resultLine + 5] -Expected "" # $s.DelimStatus2.PreText is " " that is converted to "" by posh-git"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
Assert-AreEqual -Presented $result[$resultLine + 6] -Expected $s.OKStatus.PreText
Assert-AreEqual -Presented $result[$resultLine + 7] -Expected $s.AfterStatus.PreText
Assert-AreEqual -Presented $result[$resultLine + 8] -Expected "" # $s.SpaceStatus.PreText " " that is converted to "" by posh-git

# With environment, without new line
$result = Invoke-WriteProjecthelperPrompt -withnewline
Expand All @@ -43,24 +52,28 @@
Assert-AreEqual -Presented $result[$resultLine + 1] -Expected $s.DelimStatus1.PreText
Assert-AreEqual -Presented $result[$resultLine + 2] -Expected $($($s.NumberStatus.PreText)+$projectNumber)
Assert-AreEqual -Presented $result[$resultLine + 3] -Expected "" # $s.DelimStatus2.PreText is " " that is converted to "" by posh-git"
Assert-AreEqual -Presented $result[$resultLine + 4] -Expected $s.OKStatus.PreText
Assert-AreEqual -Presented $result[$resultLine + 5] -Expected $s.AfterStatus.PreText
Assert-AreEqual -Presented $result[$resultLine + 6] -Expected "" # SpaceStatus.PreText
Assert-AreEqual -Presented $result[$resultLine + 7] -Expected $s.NewlineStatus.PreText

Assert-AreEqual -Presented $result[$resultLine + 4] -Expected $($($s.TitleStatus.PreText)+$projectTitle)
Assert-AreEqual -Presented $result[$resultLine + 5] -Expected "" # $s.DelimStatus2.PreText is " " that is converted to "" by posh-git"

Assert-AreEqual -Presented $result[$resultLine + 6] -Expected $s.OKStatus.PreText
Assert-AreEqual -Presented $result[$resultLine + 7] -Expected $s.AfterStatus.PreText
Assert-AreEqual -Presented $result[$resultLine + 8] -Expected "" # SpaceStatus.PreText
Assert-AreEqual -Presented $result[$resultLine + 9] -Expected $s.NewlineStatus.PreText

# Add some staged items
Edit-ProjectItem -Owner $owner -ProjectNumber $projectNumber -ItemId "id1" -FieldName "sf_Text1" -Value "value1"
Edit-ProjectItem -Owner $owner -ProjectNumber $projectNumber -ItemId "id1" -FieldName "sf_Text2" -Value "value2"
# Edit-ProjectItem -Owner $owner -ProjectNumber $projectNumber -ItemId "id1" -FieldName "sf_Text2" -Value "value2"
Edit-ProjectItem -Owner $owner -ProjectNumber $projectNumber -ItemId "id2" -FieldName "sf_Text1" -Value "value1"
Edit-ProjectItem -Owner $owner -ProjectNumber $projectNumber -ItemId "id2" -FieldName "sf_Text2" -Value "value2"
$itemstaged = 4
# Edit-ProjectItem -Owner $owner -ProjectNumber $projectNumber -ItemId "id2" -FieldName "sf_Text2" -Value "value2"
$itemstaged = 2

# With items staged
$result = Invoke-WriteProjecthelperPrompt
# Find the line with '[' character
$resultLine = ($result | select-string -Pattern "^\[$" ).LineNumber

Assert-AreEqual -Presented $result[$resultLine + 4] -Expected $($($s.KOStatus.PreText)+$itemstaged)
Assert-AreEqual -Presented $result[$resultLine + 6] -Expected $($($s.KOStatus.PreText)+$itemstaged)

}

Expand Down
51 changes: 51 additions & 0 deletions Test/public/issues/New-ProjectIssue.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,54 @@ function Test_NewProjectIssue{

Assert-AreEqual -Expected $result -Presented $i.itemId
}

function Test_CopyProjectIssue_SUCCESS {

$p = Get-Mock_Project_700 ; $owner = $p.owner ; $projectNumber = $p.number
$r = $p.repo
$i = $p.issueToCreateAddAndRemove
$sourceItemId = $p.issue.id

MockCall_GetProject $p -cache

$createString = 'Invoke-CreateIssue -RepositoryId {repoid} -Title "{title}" -Body "{body}"'
$createString = $createString -replace "{repoid}", $r.id
$createString = $createString -replace "{title}", $i.title
$createString = $createString -replace "{body}", $i.body

MockCallJson -Command "Invoke-Repository -Owner $($r.owner) -Name $($r.name)" -FileName $r.getRepoMockFile
MockCallToObject -Command $createString -OutObject @{ data = @{ createIssue = @{ issue = @{ url = $i.url } } } }
MockCallJson -Command "Invoke-GetIssueOrPullRequest -Url $($i.url)" -FileName $i.getIssueOrPullRequestMockFile
MockCallJson -Command "Invoke-AddItemToProject -ProjectId $($p.id) -ContentId $($i.id)" -FileName $i.addIssueToOProjectMockFile

# Act
$result = Copy-ProjectIssue -ItemId $sourceItemId -ProjectOwner $owner -ProjectNumber $projectNumber -RepoOwner $r.owner -RepoName $r.name

# Assert
Assert-AreEqual -Expected $i.itemId -Presented $result
}

function Test_CopyProjectIssue_SUCCESS_DoNotAddToProject {

$p = Get-Mock_Project_700 ; $owner = $p.owner ; $projectNumber = $p.number
$r = $p.repo
$i = $p.issueToCreateAddAndRemove
$sourceItemId = $p.issue.id

MockCall_GetProject $p -cache

$createString = 'Invoke-CreateIssue -RepositoryId {repoid} -Title "{title}" -Body "{body}"'
$createString = $createString -replace "{repoid}", $r.id
$createString = $createString -replace "{title}", $i.title
$createString = $createString -replace "{body}", $i.body

MockCallJson -Command "Invoke-Repository -Owner $($r.owner) -Name $($r.name)" -FileName $r.getRepoMockFile
MockCallToObject -Command $createString -OutObject @{ data = @{ createIssue = @{ issue = @{ url = $i.url } } } }

# Act
$result = Copy-ProjectIssue -ItemId $sourceItemId -ProjectOwner $owner -ProjectNumber $projectNumber -RepoOwner $r.owner -RepoName $r.name -DoNotAddToProject

# Assert
Assert-AreEqual -Expected $sourceItemId -Presented $result

}
25 changes: 24 additions & 1 deletion Test/public/items/use_order.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,30 @@ function Test_UserOrder_Success_OpenBrowser{
$p = Get-Mock_Project_700 ; $owner = $p.owner ; $projectNumber = $p.number
$order = $p.issue.order ; $url = $p.issue.url

MockCallToNull -command "Invoke-ProjectHelperOpenUrl -Url $url"
$command = 'Invoke-ProjectHelperOpenUrl -Url "{url}"'
$command = $command -replace "{url}", $url
MockCallToNull -command $command

# We need to have the environment set to get item details in PassThru
Set-ProjectHelperEnvironment -Owner $owner -ProjectNumber $projectNumber
$list = Search-ProjectItem -IncludeDone

# Act
$result = $list | Use-Order $order -OpenInBrowser

# Assert
Assert-IsNull -Object $result
}

function Test_UserOrder_Success_OpenBrowser_DRAFT{
MockCall_GetProject_700

$p = Get-Mock_Project_700 ; $owner = $p.owner ; $projectNumber = $p.number
$order = $p.draftissue.order ; $url = $p.draftissue.url

$command = 'Invoke-ProjectHelperOpenUrl -Url "{url}"'
$command = $command -replace "{url}", $url
MockCallToNull -command $command

# We need to have the environment set to get item details in PassThru
Set-ProjectHelperEnvironment -Owner $owner -ProjectNumber $projectNumber
Expand Down
2 changes: 1 addition & 1 deletion Test/public/project_item.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function Test_GetProjectItemUrl_SUCCESS_FromApiWhenMissingInCache {

# Arrange
Reset-InvokeCommandMock
$p = Get-Mock_Project_700 ; $owner = $p.owner ; $projectNumber = $p.number
$p = Get-Mock_Project_625 ; $owner = $p.owner ; $projectNumber = $p.number
$itemId = "id1"
MockCall_GetProject -MockProject $p -SkipItems
MockCallJson -Command "Invoke-GetItem -itemid $itemId" -FileName "invoke-getitem-id1.json"
Expand Down
2 changes: 2 additions & 0 deletions Test/traceInvoke.log
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,5 @@ Invoke-UpdateProjectV2Collaborators -ProjectId PVT_kwDOAlIw4c4BCe3V -collaborato
Invoke-GitHubOrgProjectWithFields -Owner octodemo -ProjectNumber 700 -afterFields "" -afterItems "" -query "updated:>=2024-02-18"
Invoke-GitHubOrgProjectWithFields -Owner octodemo -ProjectNumber 700 -afterFields "" -afterItems "" -query "updated:>=2025-03-15"
Invoke-ProjectHelperOpenUrl -Url https://github.com/octodemo/rulasg-dev-1/issues/26
echo ProjecthelperPromoptSettings
Invoke-CreateIssue -RepositoryId R_kgDOPrRnkQ -Title "Issue for development" -Body "Body of issue for development"
2 changes: 1 addition & 1 deletion include/openFilesUrls.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Provides controls to open files and URLs in the default system applications.
# Use $MODULE_NAME variable to set up functions names

Set-MyInvokeCommandAlias -Alias OpenUrl -Command "Invoke-$($MODULE_NAME)OpenUrl -Url {url}"
Set-MyInvokeCommandAlias -Alias OpenUrl -Command $('Invoke-{modulename}OpenUrl -Url "{url}"' -replace "{modulename}", $MODULE_NAME)

function Invoke-ModuleNameOpenUrl{
[CmdletBinding()]
Expand Down
22 changes: 15 additions & 7 deletions private/projectDatabase/project_database.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ function Save-ProjectV2toDatabase{
param(
[Parameter(Position = 0)][object]$ProjectV2,
[Parameter(Position = 1)][hashtable]$Items,
[Parameter(Position = 2)][Object[]]$Fields,
[Parameter()][switch]$ItemsUpdate
[Parameter(Position = 1)][hashtable]$QueryItems,
[Parameter(Position = 2)][Object[]]$Fields
)

$owner = $ProjectV2.owner.login
Expand All @@ -96,14 +96,15 @@ function Save-ProjectV2toDatabase{
$db.owner = $owner
$db.number = $projectnumber

if($ItemsUpdate){
# Full list update
$db.items = $Items

# Update just a few items
if($QueryItems){
# Update each of the items to avoid replacing all
foreach ($item in $items.Values){
foreach ($item in $QueryItems.Values){
Set-Item $db $item
}
} else {
# Add items
$db.items = $Items
}

# Update fields
Expand Down Expand Up @@ -162,6 +163,13 @@ function Get-DatabaseKey{
[Parameter(Position = 1)][int]$ProjectNumber
)

if([string]::IsNullOrWhiteSpace($Owner)){
throw "Owner is null or empty"
}
if($ProjectNumber -le 0){
throw "ProjectNumber is null or not a positive integer"
}

$ret = "$($owner)_$($projectnumber)"

return $ret
Expand Down
6 changes: 6 additions & 0 deletions private/projectDatabase/project_database_Item.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ function Set-Item{
$db = New-HashTable
}

# Add Sanity check for item
# Ensure that we are adding an item to the proper project database
if($Database.ProjectId -ne $Item.projectId){
Wait-Debugger
}

$items = $db | AddHashLink items

$items.$($Item.id) = $Item
Expand Down
15 changes: 8 additions & 7 deletions private/projectDatabase/project_database_update.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ function Update-ProjectDatabase {

$params = @{ owner = $Owner ; projectnumber = $ProjectNumber ; afterFields = "" ; afterItems = "" ; query = "$query" }

# We new if this is a partial update
$isQuery = -Not [string]::IsNullOrEmpty($Query)

# This means that the ProjectNumber has a empty string value
if($ProjectNumber -eq 0){
throw "ProjectNumber invalid. Please specify a valid ProjectNumber"
Expand Down Expand Up @@ -86,16 +89,14 @@ function Update-ProjectDatabase {

# If query is set we are updating just a few items from the database.
# update just this items
if( -Not [string]::IsNullOrEmpty($Query)){
if( $isQuery ){
$queryItems = $items

$actualprj = Get-ProjectFromDatabase -Owner $Owner -ProjectNumber $ProjectNumber

# Check if project has no items or the project is not cached yet
$actualItems = $actualprj.items ?? $(New-HashTable)
$items = $actualprj.items ?? $(New-HashTable)

foreach($itemKey in $items.Keys){
$actualItems.$itemKey = $items.$itemKey
}
$items = $actualItems
}

# If we are Skiping items on the update we need to keep the existing items in the database and just update the fields
Expand All @@ -109,7 +110,7 @@ function Update-ProjectDatabase {
}

# Save ProjectV2 object to ProjectDatabase
Save-ProjectV2toDatabase $projectV2 -Items $items -Fields $fields
Save-ProjectV2toDatabase $projectV2 -Items $items -Fields $fields -QueryItems $queryItems

return $true
} Export-ModuleMember -Function Update-ProjectDatabase
Expand Down
12 changes: 12 additions & 0 deletions public/environment/environmentCache.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@

# Last Known Good Owner
Owner = Get-EnvItem -Name "EnvironmentCache_Owner"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Last Known Good Project Number
ProjectNumber = Get-EnvItem -Name "EnvironmentCache_ProjectNumber"

# Last known good Project Name
ProjectTitle = Get-EnvItem -Name "EnvironmentCache_ProjectTitle"

# List of fields to display on Items display commands. Useful with ConvertToItemDisplay
# TODO : Consider if its worth keeping this setting
DisplayFields = Get-EnvItem -Name "EnvironmentCache_Display_Fields"
Expand All @@ -26,6 +31,7 @@

Set-EnvItem -Name "EnvironmentCache_Owner" -Value $null
Set-EnvItem -Name "EnvironmentCache_ProjectNumber" -Value $null
Set-EnvItem -Name "EnvironmentCache_ProjectTitle" -Value $null
Set-EnvItem -Name "EnvironmentCache_Display_Fields" -Value $null

} Export-ModuleMember -Function Reset-ProjectHelperEnvironment
Expand All @@ -35,6 +41,7 @@
param(
[Parameter(ValueFromPipelineByPropertyName)][string]$Owner,
[Parameter(ValueFromPipelineByPropertyName)][string]$ProjectNumber,
[Parameter(ValueFromPipelineByPropertyName)][string]$ProjectTitle,

Check warning

Code scanning / PSScriptAnalyzer

Command accepts pipeline input but has not defined a process block. Warning

Command accepts pipeline input but has not defined a process block.
[Parameter(ValueFromPipelineByPropertyName)][string[]]$DisplayFields
)

Expand All @@ -45,6 +52,11 @@
if(! [string]::IsNullOrWhiteSpace($ProjectNumber)) {
Set-EnvItem -Name "EnvironmentCache_ProjectNumber" -Value $ProjectNumber
}

if(! [string]::IsNullOrWhiteSpace($ProjectTitle)) {
Set-EnvItem -Name "EnvironmentCache_ProjectTitle" -Value $ProjectTitle
}

if($DisplayFields) {
Set-EnvItem -Name "EnvironmentCache_Display_Fields" -Value $DisplayFields
}
Expand Down
Loading
Loading