Skip to content
Open
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,20 @@
A powershell module that will hold Powershell functionality.

> Module generated with [TestingHelper Powershell Module](https://www.powershellgallery.com/packages/TestingHelper/)

## Issue dependencies

Mark one project item as blocked by another:

```powershell
Add-ProjectItemBlocked -ItemId PVTI_blocked -BlockingItemId PVTI_blocker -Owner my-org -ProjectNumber 123
```

`ItemId` is the blocked item; `BlockingItemId` is its blocker. Both must be
GitHub issues in the selected project (not drafts or pull requests). Owner and
project number can also come from the current project context. Pipe blocker item
IDs to create multiple relationships. The function returns `$true` for each
successful relationship and logs an error and returns `$null` on failure.

Uses GitHub's documented [`addBlockedBy` GraphQL mutation](https://docs.github.com/en/graphql/reference/issues#addblockedby---mutation)
with the underlying issue node IDs, not the project item IDs.
8 changes: 8 additions & 0 deletions Test/mockfiles.log
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,13 @@
{
"FileName": "invoke-GitHubOrgProjectWithFields-octodemo-700-query-field-text.json",
"Command": "Invoke-GitHubOrgProjectWithFields -Owner octodemo -ProjectNumber 700 -afterFields \"\" -afterItems \"\" -query \"updated:>=2025-03-15\""
},
{
"Command": "Invoke-AddBlockedBy -IssueId I_kwDOPrRnkc7KkwSq -BlockingIssueId I_kwDOPrRnkc7KJAku",
"FileName": "invoke-addBlockedBy-success.json"
},
{
"Command": "Invoke-AddBlockedBy -IssueId I_kwDOPrRnkc7KkwSq -BlockingIssueId I_kwDOPrRnkc7KJAvo",
"FileName": "invoke-addBlockedBy-second-blocker.json"
}
]
8 changes: 8 additions & 0 deletions Test/private/mocks/invoke-addBlockedBy-second-blocker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"data": {
"addBlockedBy": {
"issue": { "id": "I_kwDOPrRnkc7KkwSq" },
"blockingIssue": { "id": "I_kwDOPrRnkc7KJAvo" }
}
}
}
8 changes: 8 additions & 0 deletions Test/private/mocks/invoke-addBlockedBy-success.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"data": {
"addBlockedBy": {
"issue": { "id": "I_kwDOPrRnkc7KkwSq" },
"blockingIssue": { "id": "I_kwDOPrRnkc7KJAku" }
}
}
}
20 changes: 20 additions & 0 deletions Test/public/driver/issue/Invoke-AddBlockedBy.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function Test_InvokeAddBlockedBy_SUCCESS {
# Arrange
Reset-InvokeCommandMock

# Act
$request = Invoke-PrivateContext {
function Invoke-GraphQL {
param($Query, $Variables)
return @{ Query = $Query; Variables = $Variables }
}

Invoke-AddBlockedBy -IssueId I_blocked -BlockingIssueId I_blocker
} -ModulePath (Get-Module ProjectHelper).Path

# Assert
Assert-AreEqual -Expected "I_blocked" -Presented $request.Variables.input.issueId
Assert-AreEqual -Expected "I_blocker" -Presented $request.Variables.input.blockingIssueId
Assert-IsTrue -Condition ($request.Query.Contains('addBlockedBy(input: $input)'))
Assert-IsTrue -Condition ($request.Query.Contains('AddBlockedByInput!'))
}
108 changes: 108 additions & 0 deletions Test/public/issues/Add-ProjectItemBlocked.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
function Test_AddProjectItemBlocked_SUCCESS_Pipeline {
# Arrange
Reset-InvokeCommandMock
Mock_DatabaseRoot
$p = Get-Mock_Project_700
MockCall_GetProject $p -Cache
$item = $p.issue
$blocker = Get-ProjectItem -ItemId $p.subIssueToShow.id -Owner $p.owner -ProjectNumber $p.number
$secondBlocker = Get-ProjectItemByUrl -Url $p.subIssuesToAdd[0].url -Owner $p.owner -ProjectNumber $p.number -PassThru
MockCallJson -Command "Invoke-AddBlockedBy -IssueId $($item.contentId) -BlockingIssueId $($blocker.contentId)" -File "invoke-addBlockedBy-success.json"
MockCallJson -Command "Invoke-AddBlockedBy -IssueId $($item.contentId) -BlockingIssueId $($secondBlocker.contentId)" -File "invoke-addBlockedBy-second-blocker.json"

# Act
$result = @($blocker.id, $secondBlocker.id) | Add-ProjectItemBlocked -ItemId $item.id -Owner $p.owner -ProjectNumber $p.number

# Assert
Assert-Count -Expected 2 -Presented $result
Assert-IsTrue -Condition $result[0]
Assert-IsTrue -Condition $result[1]
}

function Test_AddProjectItemBlocked_NotFound {
# Arrange
Reset-InvokeCommandMock
Mock_DatabaseRoot
$p = Get-Mock_Project_700
MockCall_GetProject $p -Cache
MockCallToNull -Command "Invoke-GetItem -ItemId missing"

# Act
$missingItem = Add-ProjectItemBlocked -ItemId missing -BlockingItemId $p.issue.id -Owner $p.owner -ProjectNumber $p.number -InformationVariable itemMessages
$missingBlocker = Add-ProjectItemBlocked -ItemId $p.issue.id -BlockingItemId missing -Owner $p.owner -ProjectNumber $p.number -InformationVariable blockerMessages

# Assert
Assert-IsNull -Object $missingItem
Assert-IsNull -Object $missingBlocker
Assert-IsTrue -Condition (($itemMessages.MessageData.Message -join "`n").Contains("ItemId [missing] not found on project"))
Assert-IsTrue -Condition (($blockerMessages.MessageData.Message -join "`n").Contains("BlockingItemId [missing] not found on project"))
}

function Test_AddProjectItemBlocked_InvalidInput {
# Arrange
Reset-InvokeCommandMock
Mock_DatabaseRoot
$p = Get-Mock_Project_700
MockCall_GetProject $p -Cache
$params = @{ Owner = $p.owner; ProjectNumber = $p.number }

# Act
$draft = Add-ProjectItemBlocked -ItemId $p.draftissue.id -BlockingItemId $p.issue.id @params -InformationVariable draftMessages
$pullRequest = Add-ProjectItemBlocked -ItemId $p.issue.id -BlockingItemId $p.pullrequest.id @params -InformationVariable pullRequestMessages
$self = Add-ProjectItemBlocked -ItemId $p.issue.id -BlockingItemId $p.issue.id @params -InformationVariable selfMessages

# Assert
Assert-IsNull -Object $draft
Assert-IsNull -Object $pullRequest
Assert-IsNull -Object $self
Assert-IsTrue -Condition (($draftMessages.MessageData.Message -join "`n").Contains("must be a GitHub Issue"))
Assert-IsTrue -Condition (($pullRequestMessages.MessageData.Message -join "`n").Contains("must be a GitHub Issue"))
Assert-IsTrue -Condition (($selfMessages.MessageData.Message -join "`n").Contains("cannot be blocked by itself"))
}

function Test_AddProjectItemBlocked_ERROR {
# Arrange
Reset-InvokeCommandMock
Mock_DatabaseRoot
$p = Get-Mock_Project_700
MockCall_GetProject $p -Cache
$blocker = Get-ProjectItem -ItemId $p.subIssueToShow.id -Owner $p.owner -ProjectNumber $p.number
$command = "Invoke-AddBlockedBy -IssueId $($p.issue.contentId) -BlockingIssueId $($blocker.contentId)"
$params = @{ ItemId = $p.issue.id; BlockingItemId = $blocker.id; Owner = $p.owner; ProjectNumber = $p.number }
MockCallThrow -Command $command -ExceptionMessage "Relationship would create a cycle"

# Act
$result = Add-ProjectItemBlocked @params -InformationVariable messages

# Assert
Assert-IsNull -Object $result
Assert-IsTrue -Condition (($messages.MessageData.Message -join "`n").Contains("Relationship would create a cycle"))
}

function Test_AddProjectItemBlocked_ERROR_Response {
# Arrange
Reset-InvokeCommandMock
Mock_DatabaseRoot
$p = Get-Mock_Project_700
MockCall_GetProject $p -Cache
$blocker = Get-ProjectItem -ItemId $p.subIssueToShow.id -Owner $p.owner -ProjectNumber $p.number
$command = "Invoke-AddBlockedBy -IssueId $($p.issue.contentId) -BlockingIssueId $($blocker.contentId)"
$params = @{ ItemId = $p.issue.id; BlockingItemId = $blocker.id; Owner = $p.owner; ProjectNumber = $p.number }
$success = Get-MockFileContentJson -FileName "invoke-addBlockedBy-success.json" -AsHashtable
$responses = @(
$null,
@{ data = @{ addBlockedBy = @{ issue = @{ id = $p.issue.contentId } } } },
@{ data = @{ addBlockedBy = @{ issue = @{ id = $blocker.contentId }; blockingIssue = @{ id = $p.issue.contentId } } } },
@{ data = $success.data; errors = @(@{ message = "Permission denied" }) }
)

foreach ($response in $responses) {
MockCallToObject -Command $command -OutObject $response

# Act
$result = Add-ProjectItemBlocked @params

# Assert
Assert-IsNull -Object $result
}
}
3 changes: 3 additions & 0 deletions Test/traceInvoke.log
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,6 @@ Invoke-ProjectHelperOpenUrl -Url "https://github.com/octodemo/rulasg-dev-1/issue
Invoke-ProjectHelperOpenUrl -Url "https://github.com/orgs/octodemo/projects/700/views/1?pane=issue&itemId=128098775"
ProjectHelper_EditFile
ProjectHelper_EditFileCode
Invoke-AddBlockedBy -IssueId I_kwDOPrRnkc7KkwSq -BlockingIssueId I_kwDOPrRnkc7KJAku
Invoke-GetItem -ItemId missing
Invoke-AddBlockedBy -IssueId I_kwDOPrRnkc7KkwSq -BlockingIssueId I_kwDOPrRnkc7KJAvo
21 changes: 21 additions & 0 deletions public/driver/issue/Invoke-AddBlockedBy.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<#
.DESCRIPTION
Integration function not intended for direct user use.
#>
function Invoke-AddBlockedBy {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$IssueId,
[Parameter(Mandatory)][string]$BlockingIssueId
)

$query = Get-GraphQLString "addBlockedBy.mutant"
$variables = @{
input = @{
issueId = $IssueId
blockingIssueId = $BlockingIssueId
}
}

return Invoke-GraphQL -Query $query -Variables $variables
} Export-ModuleMember -Function Invoke-AddBlockedBy
6 changes: 6 additions & 0 deletions public/graphql/addBlockedBy.mutant
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mutation AddBlockedBy($input: AddBlockedByInput!) {
addBlockedBy(input: $input) {
issue { id }
blockingIssue { id }
}
}
78 changes: 78 additions & 0 deletions public/issues/Add-ProjectItemBlocked.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
Set-MyInvokeCommandAlias -Alias AddBlockedBy -Command 'Invoke-AddBlockedBy -IssueId {issueid} -BlockingIssueId {blockingissueid}'

<#
.SYNOPSIS
Mark a project item as blocked by another project item.
.DESCRIPTION
Both items must be backed by GitHub issues in the selected project.
ItemId is the blocked item; BlockingItemId identifies its blocker.
Returns true when the relationship is created, or null on failure.
.EXAMPLE
Add-ProjectItemBlocked -ItemId PVTI_blocked -BlockingItemId PVTI_blocker -Owner octodemo -ProjectNumber 700
.LINK
https://docs.github.com/en/graphql/reference/issues#addblockedby---mutation
#>
function Add-ProjectItemBlocked {
[CmdletBinding()]
param(
[Parameter(Mandatory, Position = 0)][string]$ItemId,
[Parameter(Mandatory, ValueFromPipeline, Position = 1)][string]$BlockingItemId,
[Parameter()][string]$Owner,
[Parameter()][string]$ProjectNumber
)

begin {
($Owner, $ProjectNumber) = Resolve-ProjectParameters -Owner $Owner -ProjectNumber $ProjectNumber
}

process {
$item = Get-ProjectItem -ItemId $ItemId -Owner $Owner -ProjectNumber $ProjectNumber
if (!$item) {
"ItemId [$ItemId] not found on project $Owner/$ProjectNumber" | Write-MyError
return $null
}

$blockingItem = Get-ProjectItem -ItemId $BlockingItemId -Owner $Owner -ProjectNumber $ProjectNumber
if (!$blockingItem) {
"BlockingItemId [$BlockingItemId] not found on project $Owner/$ProjectNumber" | Write-MyError
return $null
}

foreach ($issueItem in @($item, $blockingItem)) {
if ($issueItem.type -ne "Issue" -or [string]::IsNullOrWhiteSpace($issueItem.contentId)) {
"ItemId [$($issueItem.id)] must be a GitHub Issue. Type is [$($issueItem.type)]" | Write-MyError
return $null
}
}

if ($item.contentId -ceq $blockingItem.contentId) {
"ItemId [$ItemId] cannot be blocked by itself" | Write-MyError
return $null
}

$parameters = @{
issueid = $item.contentId
blockingissueid = $blockingItem.contentId
}

try {
$response = Invoke-MyCommand -Command AddBlockedBy -Parameters $parameters
} catch {
"Failed to block ItemId [$ItemId] by BlockingItemId [$BlockingItemId] - $($_.Exception.Message)" | Write-MyError
return $null
}

if ($response.errors) {
"Failed to block ItemId [$ItemId] by BlockingItemId [$BlockingItemId] - $($response.errors.message -join '; ')" | Write-MyError
return $null
}

if ($response.data.addBlockedBy.issue.id -cne $item.contentId -or
$response.data.addBlockedBy.blockingIssue.id -cne $blockingItem.contentId) {
"Failed to block ItemId [$ItemId] by BlockingItemId [$BlockingItemId]: unexpected mutation response" | Write-MyError
return $null
}

return $true
Comment thread
rulasg marked this conversation as resolved.
Resolved
}
} Export-ModuleMember -Function Add-ProjectItemBlocked
Loading