Sorting list of projects by activity?

I have a task to go through and make a change to all projects in Octopus and I would like to prioritize them based upon latest activity (e.g. deployment). Is there a way I can do this? For instance, retrieve a list of all deployment tasks and then (probably client-side) dedupe the list of projects.

Ended up answering my own question. For those that might also be curious, here’s how I did it:

$OctopusURL = "https://octopus/" #url of your octopus server
$OctopusAPIkey = "API-1234" #API Key to authenticate in Octopus.

$RecentDate = (get-date -Hour 0 -Minute 0 -Second 0).AddMonths(-6)
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }

#Projects
$projects = (Invoke-WebRequest ($OctopusURL+'/api/projects/all') -Headers $header -Method Get -UseBasicParsing).content | ConvertFrom-Json

#Deployments
$deployments = @()
$uristem = '/api/deployments'
do {
    Write-Host $uristem
    $result = (Invoke-WebRequest ($OctopusURL+$uristem) -Headers $header -Method Get -UseBasicParsing).content | ConvertFrom-Json
    $uristem = $result.Links.'Page.Next'
    $deployments += $result.items
} while (-not [String]::IsNullOrEmpty($uristem))
$deployments = $deployments | Select-Object -Property *,@{N='CreatedDate';E={Get-Date $_.Created}} | Sort-Object -Property CreatedDate -Descending

#Tasks
$tasks = @()
$uristem = '/api/tasks'
do {
    Write-Host $uristem
    $result = (Invoke-WebRequest ($OctopusURL+$uristem) -Headers $header -Method Get -UseBasicParsing).content | ConvertFrom-Json
    $uristem = $result.Links.'Page.Next'
    $tasks += $result.items
} while (-not [String]::IsNullOrEmpty($uristem))
$tasks = $tasks | Select-Object -Property *,@{N='StartTimeDate';E={Get-Date $_.StartTime}},@{N='CompletedTimeDate';E={Get-Date $_.CompletedTime}},@{N='LastUpdatedTimeDate';E={Get-Date $_.LastUpdatedTime}} | Sort-Object -Property LastUpdatedTimeDate -Descending

$DeploymentTaskIds = $deployments | ForEach-Object { $_.TaskId }
$DeploymentTasks = $tasks | Where-Object { $DeploymentTaskIds -contains $_.Id } | Sort-Object -Property LastUpdatedTimeDate -Descending

$SuccessfulDeploymentTasks = $DeploymentTasks | Where-Object { $_.FinishedSuccessfully }
$SuccessfulDeploymentIds = $SuccessfulDeploymentTasks | ForEach-Object { $_.Arguments.DeploymentId }
$SuccessfulDeployments = $deployments | Where-Object { $SuccessfulDeploymentIds -contains $_.Id }

$RecentSuccessfulDeploymentTasks = $SuccessfulDeploymentTasks | Where-Object { $_.CompletedTimeDate -ge $RecentDate }
$RecentSuccessfulDeploymentIds = $RecentSuccessfulDeploymentTasks | ForEach-Object { $_.Arguments.DeploymentId }
$RecentSuccessfulDeployments = $deployments | Where-Object { $RecentSuccessfulDeploymentIds -contains $_.Id }

$ActiveProjects = $RecentSuccessfulDeployments | Group-Object -Property ProjectId | Sort-Object -Property Count -Descending | ForEach-Object { $_ | Select-Object -Property Name,Count }
$ActiveProjects = $ActiveProjects | ForEach-Object { $p = $_; $projects | Where-Object { $_.Id -eq $p.Name } | Select-Object *,@{N='Count';E={$p.Count}} }
$ActiveProjects = $ActiveProjects | ForEach-Object { Write-Host $_.Id; $p = $_.Id; $_ | Select-Object -Property *,@{N='LatestCompletedTimeDate';E={($RecentSuccessfulDeploymentTasks | Where-Object { ($RecentSuccessfulDeployments | Where-Object { $_.ProjectId -eq $p } | Select -First 1).TaskId -eq $_.Id }).CompletedTimeDate}} }
$ActiveProjects | Select-Object -Property Name,LatestCompletedTimeDate,Count

Hi John,

Thanks for reaching out! That’s great to hear you got it resolved. Thank you for taking the time to let us and the community know your solution!

Don’t hesitate to get back in touch if you have any further questions or concerns :slight_smile:

Kind regards,

Kenny