Skip to content

Commit ca2c52a

Browse files
authored
Update variable usage script to handle CaC projects (#2586)
1 parent 980f7ff commit ca2c52a

File tree

1 file changed

+119
-44
lines changed

1 file changed

+119
-44
lines changed

src/shared-content/scripts/find-variable-usage-scripts.include.md

Lines changed: 119 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,81 @@ $space = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Heade
3636
3737
Write-Host "Looking for usages of variable named $variableToFind in space: '$spaceName'"
3838
39+
# Function to process deployment steps
40+
function Process-DeploymentSteps {
41+
param(
42+
$steps,
43+
$project,
44+
$gitRef = $null
45+
)
46+
47+
$results = @()
48+
# Loop through steps
49+
foreach ($step in $steps) {
50+
$props = $step | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" }
51+
foreach ($prop in $props) {
52+
$propName = $prop.Name
53+
$json = $step.$propName | ConvertTo-Json -Compress -Depth 10
54+
if ($null -ne $json -and ($json -like "*$variableToFind*")) {
55+
$result = [pscustomobject]@{
56+
Project = $project.Name
57+
VariableSet = $null
58+
MatchType = "Step"
59+
Context = $step.Name
60+
Property = $propName
61+
AdditionalContext = $null
62+
Link = "$octopusURL$($project.Links.Web)/deployments/process/steps?actionId=$($step.Actions[0].Id)"
63+
}
64+
65+
if ($gitRef) {
66+
$result | Add-Member -MemberType NoteProperty -Name "GitRef" -Value $gitRef
67+
}
68+
69+
$results += $result
70+
}
71+
}
72+
}
73+
return $results
74+
}
75+
76+
# Function to process runbook steps
77+
function Process-RunbookSteps {
78+
param(
79+
$steps,
80+
$project,
81+
$runbook,
82+
$gitRef = $null
83+
)
84+
85+
$results = @()
86+
# Loop through steps
87+
foreach ($step in $steps) {
88+
$props = $step | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" }
89+
foreach ($prop in $props) {
90+
$propName = $prop.Name
91+
$json = $step.$propName | ConvertTo-Json -Compress -Depth 10
92+
if ($null -ne $json -and ($json -like "*$variableToFind*")) {
93+
$result = [pscustomobject]@{
94+
Project = $project.Name
95+
VariableSet = $null
96+
MatchType = "Runbook Step"
97+
Context = $runbook.Name
98+
Property = $propName
99+
AdditionalContext = $step.Name
100+
Link = "$octopusURL$($project.Links.Web)/operations/runbooks/$($runbook.Id)/process/$($runbook.RunbookProcessId)/steps?actionId=$($step.Actions[0].Id)"
101+
}
102+
103+
if ($gitRef) {
104+
$result | Add-Member -MemberType NoteProperty -Name "GitRef" -Value $gitRef
105+
}
106+
107+
$results += $result
108+
}
109+
}
110+
}
111+
return $results
112+
}
113+
39114
# Get all projects
40115
$projects = Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/projects/all" -Headers $header
41116
@@ -45,6 +120,20 @@ foreach ($project in $projects) {
45120
# Get project variables
46121
$projectVariableSet = Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/variables/$($project.VariableSetId)" -Headers $header
47122
123+
# Get all GitRefs for CaC project
124+
if ($project.IsVersionControlled) {
125+
$gitBranches = Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/projects/$($project.Id)/git/branches" -Headers $header
126+
$gitTags = Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/projects/$($project.Id)/git/tags" -Headers $header
127+
128+
$gitRefs = @()
129+
foreach($branch in $gitBranches.Items) {
130+
$gitRefs += $branch.CanonicalName
131+
}
132+
foreach($tag in $gitTags.Items) {
133+
$gitRefs += $tag.CanonicalName
134+
}
135+
}
136+
48137
# Check to see if variable is named in project variables.
49138
$matchingNamedVariables = $projectVariableSet.Variables | Where-Object { $_.Name -ieq "$variableToFind" }
50139
if ($null -ne $matchingNamedVariables) {
@@ -84,30 +173,23 @@ foreach ($project in $projects) {
84173
85174
# Search Deployment process if enabled
86175
if ($searchDeploymentProcesses -eq $True) {
87-
# Get project deployment process
88-
$deploymentProcess = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/deploymentprocesses/$($project.DeploymentProcessId)" -Headers $header)
89-
90-
# Loop through steps
91-
foreach ($step in $deploymentProcess.Steps) {
92-
$props = $step | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" }
93-
foreach ($prop in $props) {
94-
$propName = $prop.Name
95-
$json = $step.$propName | ConvertTo-Json -Compress -Depth 10
96-
if ($null -ne $json -and ($json -like "*$variableToFind*")) {
97-
$result = [pscustomobject]@{
98-
Project = $project.Name
99-
VariableSet = $null
100-
MatchType = "Step"
101-
Context = $step.Name
102-
Property = $propName
103-
AdditionalContext = $null
104-
Link = "$octopusURL$($project.Links.Web)/deployments/process/steps?actionId=$($step.Actions[0].Id)"
105-
}
106-
# Add and de-dupe later
107-
$variableTracking += $result
108-
}
176+
if ($project.IsVersionControlled) {
177+
# For CaC Projects, loop through GitRefs
178+
foreach ($gitRef in $gitRefs) {
179+
$escapedGitRef = [Uri]::EscapeDataString($gitRef)
180+
$processUrl = "$octopusURL/api/$($space.Id)/projects/$($project.Id)/$($escapedGitRef)/deploymentprocesses"
181+
# Get project deployment process
182+
$deploymentProcess = (Invoke-RestMethod -Method Get -Uri $processUrl -Headers $header)
183+
# Add and de-dupe later
184+
$variableTracking += Process-DeploymentSteps -steps $deploymentProcess.Steps -project $project -gitRef $gitRef
109185
}
110186
}
187+
else {
188+
# Get project deployment process
189+
$deploymentProcess = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/deploymentprocesses/$($project.DeploymentProcessId)" -Headers $header)
190+
# Add and de-dupe later
191+
$variableTracking += Process-DeploymentSteps -steps $deploymentProcess.Steps -project $project
192+
}
111193
}
112194
113195
# Search Runbook processes if enabled
@@ -118,30 +200,23 @@ foreach ($project in $projects) {
118200
119201
# Loop through each runbook
120202
foreach ($runbook in $runbooks.Items) {
121-
# Get runbook process
122-
$runbookProcess = (Invoke-RestMethod -Method Get -Uri "$octopusURL$($runbook.Links.RunbookProcesses)" -Headers $header)
123-
124-
# Loop through steps
125-
foreach ($step in $runbookProcess.Steps) {
126-
$props = $step | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" }
127-
foreach ($prop in $props) {
128-
$propName = $prop.Name
129-
$json = $step.$propName | ConvertTo-Json -Compress -Depth 10
130-
if ($null -ne $json -and ($json -like "*$variableToFind*")) {
131-
$result = [pscustomobject]@{
132-
Project = $project.Name
133-
VariableSet = $null
134-
MatchType = "Runbook Step"
135-
Context = $runbook.Name
136-
Property = $propName
137-
AdditionalContext = $step.Name
138-
Link = "$octopusURL$($project.Links.Web)/operations/runbooks/$($runbook.Id)/process/$($runbook.RunbookProcessId)/steps?actionId=$($step.Actions[0].Id)"
139-
}
140-
# Add and de-dupe later
141-
$variableTracking += $result
142-
}
203+
# For CaC Projects, loop through GitRefs
204+
if ($project.IsVersionControlled) {
205+
foreach ($gitRef in $gitRefs) {
206+
$escapedGitRef = [Uri]::EscapeDataString($gitRef)
207+
$processUrl = "$octopusURL/api/$($space.Id)/projects/$($project.Id)/$($escapedGitRef)/runbookprocesses/$($runbook.RunbookProcessId)"
208+
# Get runbook process
209+
$runbookProcess = (Invoke-RestMethod -Method Get -Uri $processUrl -Headers $header)
210+
# Add and de-dupe later
211+
$variableTracking += Process-RunbookSteps -steps $runbookProcess.Steps -project $project -runbook $runbook -gitRef $gitRef
143212
}
144213
}
214+
else {
215+
# Get runbook process
216+
$runbookProcess = (Invoke-RestMethod -Method Get -Uri "$octopusURL$($runbook.Links.RunbookProcesses)" -Headers $header)
217+
# Add and de-dupe later
218+
$variableTracking += Process-RunbookSteps -steps $runbookProcess.Steps -project $project -runbook $runbook
219+
}
145220
}
146221
}
147222
}

0 commit comments

Comments
 (0)