Functions/Link-AzDoItems.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
Function Link-AzDoItems
{
<#
    .SYNOPSIS
        Links two Azure Devops item in a parent/child relationship
 
    .DESCRIPTION
        Links two Azure Devops item in a parent/child relationship
 
    .EXAMPLE
         
 
    .PARAMETER PersonalAccessToken
        This is your personal access token from Azure Devops.
 
    .PARAMETER OrganizationName
        The name of your Azure Devops Organisation
 
    .PARAMETER ProjectName
        The name of your Azure Devops Project or Team
 
    .PARAMETER ParentItemID
        The name of your Azure Devops Project or Team
 
    .PARAMETER ChildItemID
        The name of your Azure Devops Project or Team
 
    .INPUTS
        Input is from command line or called from a script.
 
    .OUTPUTS
        This will output the logfile.
 
    .NOTES
        Version: 0.1
        Author: Lars Panzerbjørn
        Creation Date: 2020.07.31
        Purpose/Change: Initial script development
#>

    [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
    param
    (
        [Parameter(Mandatory)]
        [Alias('PAT')]
        [string]$PersonalAccessToken,

        [Parameter(Mandatory)]
        [Alias('Company')]
        [string]$Organisation,

        [Parameter(Mandatory)]
        [Alias('TeamName')]
        [string]$Project,

        [Parameter(Mandatory)][string]$ParentItemID,
        [Parameter(Mandatory)][string]$ChildItemID
    )

    BEGIN
    {
        Write-Verbose "Beginning $($MyInvocation.Mycommand)"
        $JsonContentType = 'application/json-patch+json'
        $BaseUri = "https://dev.azure.com/$($Organisation)/"
        $Uri = $BaseUri + "$Project/_apis/wit/workitems/$ChildItemID`?api-version=6.1-preview.3"
        

        $Token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PersonalAccessToken)"))
        $Header = @{Authorization = 'Basic ' + $Token;accept=$JsonContentType}
    }

    PROCESS
    {
        Write-Verbose "Processing $($MyInvocation.Mycommand)"
        $Value = @{
            rel = "System.LinkTypes.Hierarchy-Reverse"
            url = "https://dev.azure.com/$Organisation/$Project/_apis/wit/workItems/$ParentItemID"
            }
        $Attributes = @{
            isLocked = $False
            name = "Parent"
        }


        $Body = @([pscustomobject]@{
                op = "add"
                path = '/relations/-'
                value = $Value
                Attributes = $Attributes
            }
        )

        $Body = ConvertTo-Json $Body
        $Body
        $Result = Invoke-RestMethod -Uri $uri -Method PATCH -Headers $Header -ContentType $JsonContentType -Body $Body

    }
    END
    {
        Write-Verbose "Ending $($MyInvocation.Mycommand)"
        #$Body
        $Result
    }
}