Skip to content

Commit f33f9dc

Browse files
authored
Merge pull request #159 from rulasg/fix-addcommand-staged
fix(addComment): staged AddComment flow
2 parents 2615c76 + 1ba0330 commit f33f9dc

8 files changed

Lines changed: 162 additions & 53 deletions

Test/private/mocks/invoke-GitHubOrgProjectWithFields-octodemo-700.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4019,7 +4019,7 @@
40194019
"id": "I_kwDOPrRnkc7KkwSq",
40204020
"body": "Body of issue for development",
40214021
"title": "Issue for development",
4022-
"updatedAt": "2025-09-30T05:42:49Z",
4022+
"updatedAt": "2025-10-15T21:30:02Z",
40234023
"createdAt": "2025-09-09T14:01:17Z",
40244024
"number": 26,
40254025
"url": "https://github.com/octodemo/rulasg-dev-1/issues/26",
@@ -4035,29 +4035,29 @@
40354035
"nodes": [
40364036
{
40374037
"createdAt": "2025-09-23T17:51:06Z",
4038-
"updatedAt": "2025-09-23T17:51:06Z",
4038+
"updatedAt": "2025-10-15T21:29:45Z",
40394039
"url": "https://github.com/octodemo/rulasg-dev-1/issues/26#issuecomment-3324995787",
4040-
"body": "sample comment 1",
4040+
"body": "Sample comment 1",
40414041
"fullDatabaseId": "3324995787",
40424042
"author": {
40434043
"login": "rulasg"
40444044
}
40454045
},
40464046
{
40474047
"createdAt": "2025-09-24T08:29:13Z",
4048-
"updatedAt": "2025-09-24T08:29:13Z",
4048+
"updatedAt": "2025-10-15T21:29:55Z",
40494049
"url": "https://github.com/octodemo/rulasg-dev-1/issues/26#issuecomment-3327194303",
4050-
"body": "commetn added",
4050+
"body": "Sample comment 2",
40514051
"fullDatabaseId": "3327194303",
40524052
"author": {
40534053
"login": "rulasg"
40544054
}
40554055
},
40564056
{
40574057
"createdAt": "2025-09-30T05:42:49Z",
4058-
"updatedAt": "2025-09-30T05:42:49Z",
4058+
"updatedAt": "2025-10-15T21:30:02Z",
40594059
"url": "https://github.com/octodemo/rulasg-dev-1/issues/26#issuecomment-3350059109",
4060-
"body": "New comment",
4060+
"body": "Sample comment 3",
40614061
"fullDatabaseId": "3350059109",
40624062
"author": {
40634063
"login": "rulasg"

Test/private/mocks/invoke-getitem-PVTI_lADOAlIw4c4BCe3Vzgeio4o.json

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,51 @@
1313
"id": "I_kwDOPrRnkc7KkwSq",
1414
"body": "Body of issue for development",
1515
"title": "Issue for development",
16-
"updatedAt": "2025-09-11T13:06:24Z",
16+
"updatedAt": "2025-10-15T21:30:02Z",
1717
"createdAt": "2025-09-09T14:01:17Z",
1818
"number": 26,
1919
"url": "https://github.com/octodemo/rulasg-dev-1/issues/26",
2020
"state": "OPEN",
2121
"repository": {
22-
"nameWithOwner": "octodemo/rulasg-dev-1"
22+
"name": "rulasg-dev-1",
23+
"owner": {
24+
"login": "octodemo"
25+
}
26+
},
27+
"comments": {
28+
"totalCount": 3,
29+
"nodes": [
30+
{
31+
"createdAt": "2025-09-23T17:51:06Z",
32+
"updatedAt": "2025-10-15T21:29:45Z",
33+
"url": "https://github.com/octodemo/rulasg-dev-1/issues/26#issuecomment-3324995787",
34+
"body": "Sample comment 1",
35+
"fullDatabaseId": "3324995787",
36+
"author": {
37+
"login": "rulasg"
38+
}
39+
},
40+
{
41+
"createdAt": "2025-09-24T08:29:13Z",
42+
"updatedAt": "2025-10-15T21:29:55Z",
43+
"url": "https://github.com/octodemo/rulasg-dev-1/issues/26#issuecomment-3327194303",
44+
"body": "Sample comment 2",
45+
"fullDatabaseId": "3327194303",
46+
"author": {
47+
"login": "rulasg"
48+
}
49+
},
50+
{
51+
"createdAt": "2025-09-30T05:42:49Z",
52+
"updatedAt": "2025-10-15T21:30:02Z",
53+
"url": "https://github.com/octodemo/rulasg-dev-1/issues/26#issuecomment-3350059109",
54+
"body": "Sample comment 3",
55+
"fullDatabaseId": "3350059109",
56+
"author": {
57+
"login": "rulasg"
58+
}
59+
}
60+
]
2361
}
2462
},
2563
"fieldValues": {
@@ -94,6 +132,18 @@
94132
"dataType": "NUMBER"
95133
}
96134
},
135+
{
136+
"__typename": "ProjectV2ItemFieldIterationValue",
137+
"title": "field-iteration 3",
138+
"startDate": "2025-10-05",
139+
"duration": 14,
140+
"field": {
141+
"__typename": "ProjectV2IterationField",
142+
"id": "PVTIF_lADOAlIw4c4BCe3Vzg0rhqQ",
143+
"name": "field-iteration",
144+
"dataType": "ITERATION"
145+
}
146+
},
97147
{
98148
"__typename": "ProjectV2ItemFieldTextValue",
99149
"text": "text3",
@@ -127,18 +177,6 @@
127177
}
128178
]
129179
}
130-
},
131-
{
132-
"__typename": "ProjectV2ItemFieldIterationValue",
133-
"title": "field-iteration 3",
134-
"startDate": "2025-10-05",
135-
"duration": 14,
136-
"field": {
137-
"__typename": "ProjectV2IterationField",
138-
"id": "PVTIF_lADOAlIw4c4BCe3Vzg0rhqQ",
139-
"name": "field-iteration",
140-
"dataType": "ITERATION"
141-
}
142180
}
143181
]
144182
}

Test/public/edit-sync-projectitem-comments.test.ps1

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,52 @@ function Test_Edit_Sync_ProjectItem_AddComments_Issue {
66
$p = Get-Mock_Project_700 ; $owner = $p.owner ; $projectNumber = $p.number
77
$i = $p.issue
88

9-
$comment = "New comment"
9+
$comment = "New comment"
10+
$comment2 = "Another comment2"
1011

11-
MockCall_GetProject -MockProject $p -skipItems
12-
MockCall_GetItem $i.id
12+
13+
MockCall_GetProject -MockProject $p -SkipItems
14+
MockCall_GetItem $i.id
1315

1416
MockCallJson -Command "Invoke-AddComment -SubjectId $($i.contentId) -Comment ""New comment""" -filename "invoke-addcomment-$($i.contentId).json"
17+
MockCallJson -Command "Invoke-AddComment -SubjectId $($i.contentId) -Comment ""Another comment2""" -filename "invoke-addcomment-$($i.contentId).json"
18+
19+
# Check the status of comments
20+
# $i = Get-projectItem -Owner $owner -ProjectNumber $projectNumber -ItemId $i.id
21+
1522

1623
# Act the edit part
1724
Edit-ProjectItem -Owner $owner -ProjectNumber $projectNumber -ItemId $i.id -FieldName "AddComment" -Value $comment
18-
25+
1926
# Assert the Edit part
2027
$staged = Get-ProjectItemStaged -Owner $owner -ProjectNumber $projectNumber
2128
Assert-AreEqual -Expected $comment -Presented $staged.$($i.id).addcomment.Value
2229

30+
# Confirm that staged values are merged on GetItem
31+
$item = Get-ProjectItem -Owner $owner -ProjectNumber $projectNumber -ItemId $i.id
32+
Assert-AreEqual -Expected $comment -Presented $item.comments[-1].body
33+
Assert-AreEqual -Expected $comment -Presented $item.commentLast.body
34+
2335
# Act the sync part
2436
Sync-ProjectItemStaged -Owner $owner -ProjectNumber $projectNumber
2537

2638
$staged = Get-ProjectItemStaged -Owner $owner -ProjectNumber $projectNumber
2739
Assert-Count -Expected 0 -Presented $staged.Count
40+
41+
# Assert comment is commited to database
42+
$item = Get-ProjectItem -Owner $owner -ProjectNumber $projectNumber -ItemId $i.id
43+
Assert-AreEqual -Expected $comment -Presented $item.comments[-1].body
44+
Assert-AreEqual -Expected $comment -Presented $item.commentLast.body
45+
46+
# Act more comments on a item with already has comments
47+
Edit-ProjectItem -Owner $owner -ProjectNumber $projectNumber -ItemId $i.id -FieldName "AddComment" -Value $comment2
48+
49+
$item = Get-ProjectItem -Owner $owner -ProjectNumber $projectNumber -ItemId $i.id
50+
51+
Sync-ProjectItemStaged -Owner $owner -ProjectNumber $projectNumber
52+
$staged = Get-ProjectItemStaged -Owner $owner -ProjectNumber $projectNumber
53+
Assert-Count -Expected 0 -Presented $staged.Count
54+
$item = Get-ProjectItem -Owner $owner -ProjectNumber $projectNumber -ItemId $i.id
55+
Assert-AreEqual -Expected $comment2 -Presented $item.comments[-1].body
56+
Assert-AreEqual -Expected $comment2 -Presented $item.commentLast.body
2857
}

Test/public/issues/Add-IssuePullRequestComment.test.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ function Test_AddComment_SUCCESS_Using_Cache{
88
$contentId = $i.contentId
99
$comment = "sample comment 1"
1010

11-
MockCall_GetProject_700
11+
MockCall_GetProject -MockProject $p -SkipItems
12+
MockCall_GetItem -ItemId $i.id
1213

1314
Set-ProjectHelperEnvironment -Owner $owner -ProjectNumber $projectNumber -DisplayFields @("Status","FieldText")
1415

Test/public/project_item.test.ps1

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ function Test_GetProjectItem_SUCCESS{
1616
$itemFieldCommentValue = $projectFieldCommentValue + " updated"
1717

1818
# allow get project
19-
MockCall_GetProject_700
19+
MockCall_GetProject -MockProject $p -cache
20+
MockCall_GetProject -MockProject $p -SkipItems
21+
22+
MockCall_GetItem -ItemId $itemId
2023

2124
# Even if id is in project we make a direct call when with Force
2225
MockCallJson -Command "Invoke-GetItem -itemid $itemId" -FileName "invoke-getitem-$itemId-updated.json"
@@ -256,11 +259,12 @@ function Test_ShowProjectItem_SUCCESS{
256259
Reset-InvokeCommandMock
257260
Mock_DatabaseRoot
258261

259-
MockCall_GetProject_700
260-
261262
$p = Get-Mock_Project_700; $Owner = "octodemo" ; $ProjectNumber = 700
262-
263263
$i = $p.issue
264+
265+
MockCall_GetProject -MockProject $p -SkipItems
266+
MockCall_GetItem -ItemId $i.id
267+
264268
$id = $i.Id
265269
$title = $i.title
266270
$status = $i.status

private/projectDatabase/project_database_Item.ps1

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@ function Get-Item{
1414

1515
# Check if is staged
1616
if($database.Staged.$ItemId){
17+
18+
1719
# Update ret with all staged fields values
1820
foreach($fieldKey in $database.Staged.$ItemId.keys){
21+
22+
$value = $database.Staged.$ItemId.$fieldKey.Value
23+
24+
if($fieldKey -eq "AddComment"){
25+
Set-LastComment -Database $database -Item $ret -comment $value
26+
continue
27+
}
28+
29+
# Get fieldname
1930
$fieldname = $database.Staged.$ItemId.$fieldKey.Field.name
2031
# Make type conversions to string
2132
$ret.$fieldname = $database.Staged.$ItemId.$fieldKey.Value
@@ -36,6 +47,29 @@ function Get-Item{
3647
}
3748
}
3849

50+
function Set-LastComment{
51+
[CmdletBinding()]
52+
[OutputType([string])]
53+
param(
54+
[Parameter(Position = 0)][object[]]$Database,
55+
[Parameter(ValueFromPipeline, Position = 1)][hashtable]$Item,
56+
[Parameter()][string]$comment
57+
)
58+
59+
$commentobj = @{
60+
body = $comment
61+
}
62+
63+
# init comments array if needed
64+
if($null -eq $Item.comments){
65+
$Item.comments = @()
66+
}
67+
68+
# Update commentLast field
69+
$Item.commentLast = $commentobj
70+
$Item.comments += $commentobj
71+
}
72+
3973
function Find-Item {
4074
[CmdletBinding()]
4175
param(
@@ -117,6 +151,11 @@ function Set-ItemValue{
117151

118152
$item = $db | AddHashLink items | AddHashLink $ItemId
119153

154+
# Special case for comments
155+
if($FieldName -eq "AddComment"){
156+
Set-LastComment -Database $db -Item $item -comment $Value
157+
}
158+
120159
$item.$FieldName = $Value
121160
}
122161

@@ -148,6 +187,26 @@ function Get-ItemStaged{
148187
}
149188
}
150189

190+
function Remove-ItemStaged{
191+
[CmdletBinding()]
192+
param(
193+
[Parameter(Position = 0)][object]$Database,
194+
[Parameter(Position = 1)][string]$ItemId,
195+
[Parameter(Position = 2)][string]$FieldId
196+
)
197+
198+
$db = $Database
199+
200+
if ($db.Staged.$ItemId.$FieldId) {
201+
$db.Staged.$ItemId.Remove($FieldId)
202+
}
203+
204+
# If no more fields in item remove item
205+
if ($db.Staged.$ItemId.Count -eq 0) {
206+
$db.Staged.Remove($ItemId)
207+
}
208+
}
209+
151210
<#
152211
.SYNOPSIS
153212
Stage a change to the database

private/projectDatabase/project_database_Sync.ps1

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,4 @@ function Sync-Project{
8484
}
8585

8686
return $db
87-
}
88-
89-
function Remove-ItemStaged{
90-
[CmdletBinding()]
91-
param(
92-
[Parameter(Position = 0)][object]$Database,
93-
[Parameter(Position = 1)][string]$ItemId,
94-
[Parameter(Position = 2)][string]$FieldId
95-
)
96-
97-
$db = $Database
98-
99-
if ($db.Staged.$ItemId.$FieldId) {
100-
$db.Staged.$ItemId.Remove($FieldId)
101-
}
102-
103-
# If no more fields in item remove item
104-
if ($db.Staged.$ItemId.Count -eq 0) {
105-
$db.Staged.Remove($ItemId)
106-
}
10787
}

public/items/project_item.ps1

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ function Get-ProjectItem {
2424
($Owner, $ProjectNumber) = Get-OwnerAndProjectNumber -Owner $Owner -ProjectNumber $ProjectNumber
2525
if ([string]::IsNullOrWhiteSpace($owner) -or [string]::IsNullOrWhiteSpace($ProjectNumber)) { "Owner and ProjectNumber are required" | Write-MyError; return $null }
2626

27-
28-
# Get Item from Project database
29-
$db = Get-Project -Owner $Owner -ProjectNumber $ProjectNumber
27+
$db = Get-Project -Owner $Owner -ProjectNumber $ProjectNumber -SkipItems
3028

3129
if(! $db){ "Project not found for Owner [$Owner] and ProjectNumber [$ProjectNumber]" | Write-MyError; return $null}
3230

0 commit comments

Comments
 (0)