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

Feature: Issue Links #131

Merged
merged 14 commits into from
Jun 23, 2017
95 changes: 43 additions & 52 deletions PSJira/Internal/ConvertTo-JiraIssue.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ function ConvertTo-JiraIssue

# Write-Debug "[ConvertTo-JiraIssue] Processing object: '$i'"

if ($i.errorMessages)
{
# Write-Debug "[ConvertTo-JiraIssue] Detected an errorMessages property. This is an error result."
if ($i.errorMessages) {
# Write-Debug "[ConvertTo-JiraIssue] Detected an errorMessages property. This is an error result."

if ($ReturnError)
{
# Write-Debug "[ConvertTo-JiraIssue] Outputting details about error message"
if ($ReturnError) {
# Write-Debug "[ConvertTo-JiraIssue] Outputting details about error message"
$props = @{
'ErrorMessages' = $i.errorMessages;
}
Expand All @@ -46,101 +44,94 @@ function ConvertTo-JiraIssue

Write-Output $result
}
} else {
}
else {
$server = ($InputObject.self -split 'rest')[0]
$http = "${server}browse/$($i.key)"

# Write-Debug "[ConvertTo-JiraIssue] Defining standard properties"
# Write-Debug "[ConvertTo-JiraIssue] Defining standard properties"
$props = @{
'Key' = $i.key;
'ID' = $i.id;
'RestUrl' = $i.self;
'HttpUrl' = $http;
'Summary' = $i.fields.summary;
'Key' = $i.key;
'ID' = $i.id;
'RestUrl' = $i.self;
'HttpUrl' = $http;
'Summary' = $i.fields.summary;
'Description' = $i.fields.description;
'Status' = $i.fields.status.name;
'Status' = $i.fields.status.name;
}
if ($i.fields.issuelinks) { $props['IssueLinks'] = (ConvertTo-JiraIssueLink $i.fields.issuelinks)}

if ($i.fields.project)
{
# Write-Debug "[ConvertTo-JiraIssue] Obtaining reference to project"
if ($i.fields.project) {
# Write-Debug "[ConvertTo-JiraIssue] Obtaining reference to project"
$props.Project = ConvertTo-JiraProject -InputObject $i.fields.project
}

foreach ($field in $userFields)
{
# Write-Debug "[ConvertTo-JiraIssue] Checking for user field [$field]"
if ($i.fields.$field)
{
# Write-Debug "[ConvertTo-JiraIssue] Adding user field $field"
foreach ($field in $userFields) {
# Write-Debug "[ConvertTo-JiraIssue] Checking for user field [$field]"
if ($i.fields.$field) {
# Write-Debug "[ConvertTo-JiraIssue] Adding user field $field"
$props.$field = ConvertTo-JiraUser -InputObject $i.fields.$field
} elseif ($field -eq 'Assignee') {
# Write-Debug "[ConvertTo-JiraIssue] Adding 'Unassigned' assignee field"
}
elseif ($field -eq 'Assignee') {
# Write-Debug "[ConvertTo-JiraIssue] Adding 'Unassigned' assignee field"
$props.Assignee = 'Unassigned'
} else {
# Write-Debug "[ConvertTo-JiraIssue] Object does not appear to contain property [$field]"
}
else {
# Write-Debug "[ConvertTo-JiraIssue] Object does not appear to contain property [$field]"
}
}

foreach ($field in $dateFields)
{
if ($i.fields.$field)
{
# Write-Debug "[ConvertTo-JiraIssue] Adding date field $field"
foreach ($field in $dateFields) {
if ($i.fields.$field) {
# Write-Debug "[ConvertTo-JiraIssue] Adding date field $field"
$props.$field = Get-Date -Date ($i.fields.$field)
}
}

if ($IncludeDebug)
{
# Write-Debug "[ConvertTo-JiraIssue] Defining debug properties"
if ($IncludeDebug) {
# Write-Debug "[ConvertTo-JiraIssue] Defining debug properties"
$props.Fields = $i.fields
$props.Expand = $i.expand
}

# Write-Debug "[ConvertTo-JiraIssue] Adding transitions"
[void] $transitions.Clear()
foreach ($t in $i.transitions)
{
foreach ($t in $i.transitions) {
[void] $transitions.Add((ConvertTo-JiraTransition -InputObject $t))
}
$props.Transition = ($transitions.ToArray())

# Write-Debug "[ConvertTo-JiraIssue] Adding comments"
# Write-Debug "[ConvertTo-JiraIssue] Adding comments"
[void] $comments.Clear()
if ($i.fields.comment)
{
if ($i.fields.comment.comments)
{
foreach ($c in $i.fields.comment.comments)
{
if ($i.fields.comment) {
if ($i.fields.comment.comments) {
foreach ($c in $i.fields.comment.comments) {
[void] $comments.Add((ConvertTo-JiraComment -InputObject $c))
}
$props.Comment = ($comments.ToArray())
}
}

# Write-Debug "[ConvertTo-JiraIssue] Checking for any additional fields"
# Write-Debug "[ConvertTo-JiraIssue] Checking for any additional fields"
$extraFields = $i.fields.PSObject.Properties | Where-Object -FilterScript { $_.Name -notin $props.Keys }
foreach ($f in $extraFields)
{
foreach ($f in $extraFields) {
$name = $f.Name
# Write-Debug "[ConvertTo-JiraIssue] Adding property [$name] with value [$($f.Value)]"
# Write-Debug "[ConvertTo-JiraIssue] Adding property [$name] with value [$($f.Value)]"
$props[$name] = $f.Value
}

# Write-Debug "[ConvertTo-JiraIssue] Creating PSObject out of properties"
# Write-Debug "[ConvertTo-JiraIssue] Creating PSObject out of properties"
$result = New-Object -TypeName PSObject -Property $props

# Write-Debug "[ConvertTo-JiraIssue] Inserting type name information"
# Write-Debug "[ConvertTo-JiraIssue] Inserting type name information"
$result.PSObject.TypeNames.Insert(0, 'PSJira.Issue')

# Write-Debug "[ConvertTo-JiraProject] Inserting custom toString() method"
# Write-Debug "[ConvertTo-JiraProject] Inserting custom toString() method"
$result | Add-Member -MemberType ScriptMethod -Name "ToString" -Force -Value {
Write-Output "[$($this.Key)] $($this.Summary)"
}

# Write-Debug "[ConvertTo-JiraIssue] Outputting object"
# Write-Debug "[ConvertTo-JiraIssue] Outputting object"
Write-Output $result
}
}
Expand Down
45 changes: 45 additions & 0 deletions PSJira/Internal/ConvertTo-JiraIssueLink.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function ConvertTo-JiraIssueLink {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true,
Position = 0,
ValueFromPipeline = $true)]
[PSObject[]] $InputObject,

[Switch] $ReturnError
)

process {
foreach ($i in $InputObject) {
if ($i.errorMessages) {
if ($ReturnError) {
$props = @{
'ErrorMessages' = $i.errorMessages;
}

$result = New-Object -TypeName PSObject -Property $props
$result.PSObject.TypeNames.Insert(0, 'PSJira.Error')

Write-Output $result
}
}
else {
$props = @{
'ID' = $i.id
'Type' = (ConvertTo-JiraIssueLinkType $i.type)
}
if ($i.inwardIssue) { $props['InwardIssue'] = (ConvertTo-JiraIssue $i.inwardIssue) }
if ($i.outwardIssue) { $props['OutwardIssue'] = (ConvertTo-JiraIssue $i.outwardIssue) }

$result = New-Object -TypeName PSObject -Property $props
$result.PSObject.TypeNames.Insert(0, 'PSJira.IssueLink')

Write-Output $result
}
}
}

end
{
}
}
46 changes: 46 additions & 0 deletions PSJira/Internal/ConvertTo-JiraIssueLinkType.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function ConvertTo-JiraIssueLinkType {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true,
Position = 0,
ValueFromPipeline = $true)]
[PSObject[]] $InputObject,

[Switch] $ReturnError
)

process {
foreach ($i in $InputObject) {
if ($i.errorMessages) {
if ($ReturnError) {
$props = @{
'ErrorMessages' = $i.errorMessages;
}

$result = New-Object -TypeName PSObject -Property $props
$result.PSObject.TypeNames.Insert(0, 'PSJira.Error')

Write-Output $result
}
}
else {
$props = @{
'ID' = $i.id;
'Name' = $i.name;
'InwardText' = $i.inward;
'OutwardText' = $i.outward;
'RestUrl' = $i.self;
}

$result = New-Object -TypeName PSObject -Property $props
$result.PSObject.TypeNames.Insert(0, 'PSJira.IssueLinkType')

Write-Output $result
}
}
}

end
{
}
}
102 changes: 102 additions & 0 deletions PSJira/Public/Add-JiraIssueLink.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
function Add-JiraIssueLink {
<#
.Synopsis
Adds a link between two Issues on Jira
.DESCRIPTION
Creates a new link of the specified type between two Issue.
.EXAMPLE
$link = [PSCustomObject]@{
outwardIssue = [PSCustomObject]@{key = "TEST-10"}
type = [PSCustomObject]@{name = "Composition"}
}
Add-JiraIssueLink -Issue TEST-01 -IssueLink $link
Creates a link "is part of" between TEST-01 and TEST-10
.INPUTS
[PSJira.Issue[]] The JIRA issue that should be linked
[PSJira.IssueLink[]] The JIRA issue link that should be used
#>
[CmdletBinding()]
param(
# Issue key or PSJira.Issue object returned from Get-JiraIssue
[Parameter(
Position = 0,
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[Alias('Key')]
[Object[]] $Issue,

# Issue Link to be created.
[Parameter(Mandatory = $true)]
[Object[]] $IssueLink,

# Write a comment to the issue
[String] $Comment,

# Credentials to use to connect to Jira
[Parameter(Mandatory = $false)]
[System.Management.Automation.PSCredential] $Credential<#,

[Switch] $PassThru#>
)

begin {
Write-Debug "[Add-JiraIssueLink] Reading server from config file"
$server = Get-JiraConfigServer -ConfigFile $ConfigFile -ErrorAction Stop

$issueLinkURL = "$($server)/rest/api/latest/issueLink"
}

process {
# Validate IssueLink object
$objectProperties = $IssueLink | Get-Member -MemberType *Property
if (-not(($objectProperties.Name -contains "type") -and (($objectProperties.Name -contains "outwardIssue") -or ($objectProperties.Name -contains "inwardIssue")))) {
$message = "The IssueLink provided does not contain the information needed."
$exception = New-Object -TypeName System.ArgumentException -ArgumentList $message
Throw $exception
}

# Validate input object from Pipeline
if (($_) -and ($_.PSObject.TypeNames[0] -ne "PSJira.Issue")) {
$message = "Wrong object type provided for Issue. Only PSJira.Issue is accepted"
$exception = New-Object -TypeName System.ArgumentException -ArgumentList $message
Throw $exception
}

foreach ($i in $Issue) {
Write-Debug "[Add-JiraIssueLink] Obtaining reference to issue"
$issueObj = Get-JiraIssue -InputObject $i -Credential $Credential

foreach ($link in $IssueLink) {
if ($link.inwardIssue) {
$inwardIssue = [PSCustomObject]@{key = $link.inwardIssue.key}
}
else {
$inwardIssue = [PSCustomObject]@{key = $issueObj.key}
}

if ($link.outwardIssue) {
$outwardIssue = [PSCustomObject]@{key = $link.outwardIssue.key}
}
else {
$outwardIssue = [PSCustomObject]@{key = $issueObj.key}
}

$body = [PSCustomObject]@{
type = [PSCustomObject]@{name = $link.type.name}
inwardIssue = $inwardIssue
outwardIssue = $outwardIssue
}
if ($Comment) {$body["comment"] = [PSCustomObject]@{body = $Comment}}
$json = ConvertTo-Json $body

$null = Invoke-JiraMethod -Method POST -URI $issueLinkURL -Body $json -Credential $Credential
}
}
}

end {
Write-Debug "[Add-JiraIssueLink] Complete"
}
}
Loading