File: markWorkItemAsStale.ps1

package info (click to toggle)
azure-devops-cli-extension 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,680 kB
  • sloc: python: 160,797; xml: 198; sh: 61; makefile: 56
file content (177 lines) | stat: -rw-r--r-- 6,910 bytes parent folder | download | duplicates (2)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177


$staleThresholdInDays = Get-VstsInput -Name staleThresholdInDays -Require

$organization = Get-VstsInput -Name Organization -Require

$areaPath = Get-VstsInput -Name AreaPath -Require

$PatToken = Get-VstsInput -Name PatToken -Require



$exemptTags = @('security', 'pinned')

$witTypeSupported = "('Feature','Task','User Story', 'Bug')"

$staleTagText = "ProAct : stale"

$staleCommentText = "This work item has been automatically marked as stale (tag => $staleTagText) because there has been no activity in last $staleThresholdInDays days. This work item will be closed if tag => $staleTagText is not removed within $daysUntilClose days."



# variable reading above this line



function MarkWitAsStale {

    param (

        $organization,

        $staleWits,

        $exemptTags,

        $staleTagText,

        $staleCommentText

    )

    $updatedStaleWits = @()

    foreach ($staleWit in $staleWits) {

        # get current tags in staleWit

        [String]$staleWitId = $staleWit.fields | Select-Object -ExpandProperty System.Id

        [String]$staleWitTags = ""



        # check to verify if System.Tags is present in the staleWit.fields

        if ([bool]($staleWit.fields.PSobject.Properties.name -match "System.Tags")) {

            [String]$staleWitTags = $staleWit.fields | Select-Object -ExpandProperty System.Tags

            $staleWitTags += "; "

        }



        # We do not update a staleWit if it contains any of the exempted tags

        $exemptTagFlag = $false;

        foreach ($exemptTag in $exemptTags) {

            if ($staleWitTags -like "*$($exemptTag)*") {

                $exemptTagFlag = $true

                break;

            }

        }



        if ($exemptTagFlag) {

            continue;

        }



        # Add staleTagText if staleWitTags doesn't already contain the staleTag

        if (-Not ($staleWitTags -like "*$($staleTagText)*" )) {

            $staleWitTags += "$($staleTagText)"

            # update WIT with new tags and comment

            $ignoreOutput = az boards work-item update --id $staleWitId --fields -f System.Tags=$staleWitTags --discussion $staleCommentText --org $organization

            $updatedStaleWits += $staleWit

        }

    }



    return $updatedStaleWits

}



# region install devops extension if missing



$extensions = az extension list -o json | ConvertFrom-Json



$devopsFound = $False

foreach($extension in $extensions)

{

    if($extension.name -eq 'azure-devops'){

        $devopsFound = $True

    }

}



if ($devopsFound -eq $False){

    az extension add -n azure-devops

}



$Env:AZURE_DEVOPS_EXT_PAT = $PatToken



# end region install extension



# region mark work items as stale



$staleWitWiqlQuery = "select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.ChangedDate], [System.State], [System.Tags] from WorkItems where [System.WorkItemType] IN $($witTypeSupported) and not [System.State] in ('Closed', 'Resolved', 'Completed', 'Cut') AND [System.Tags] NOT CONTAINS '$staleTagText' AND [System.AreaPath] = '$areaPath' AND [System.ChangedDate] < @today - $staleThresholdInDays ORDER BY [System.ChangedDate] ASC"

Write-Host "Query used"

Write-Host $staleWitWiqlQuery

$staleWits = az boards query --wiql $staleWitWiqlQuery --org $organization -o json | ConvertFrom-Json



MarkWitAsStale $organization $staleWits $exemptTags $staleTagText $staleCommentText





# endregion