PSDevOpsAddonsByWiku.ps1


<#PSScriptInfo
 
.VERSION 2.14
 
.GUID dfd5369d-a676-44bb-89dd-a5e29142686a
 
.AUTHOR wzerebecki@plottwist.games
 
.COMPANYNAME Plot Twist
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 Some additional functions for our DevOps, working on top of VSTeam
 
#>
 
Param()

function Set-BenFoxAzureDevOpsPAT {
    $securePassword = Read-host -AsSecureString | ConvertFrom-SecureString
    $securePassword | Out-File -FilePath $env:USERPROFILE"\benFoxDevOpsPat.txt" 
}

function Add-Build {
    Param
    (
        [parameter(Mandatory = $true)]
        [ValidateSet("Ben Fox", "Ethos", "Ethos - Editor", "Anafora Shell", "Anafora Logger", "Game Framework", "Baldur", "Character Core Logic", "Benfox Character Controller", "Game Service Provider", "Nucleus")]
        [String[]]
        $BuildName
    ) 

    $buildId = 0 
    $projectName = 'Unity Packages'

    switch -Exact ($BuildName) {
        'Ben Fox' { $projectName = 'Ben Fox'; $buildId = 141; Break }
        'Anafora Shell' { $buildId = 138; Break }
        'Anafora Logger' { $buildId = 139; Break }
        'Game Framework' { $buildId = 136; Break }
        'Baldur' { $buildId = 137; Break }
        'Character Core Logic' { $buildId = 135; Break }
        'Benfox Character Controller' { $buildId = 130; Break }
        'Game Service Provider' { $buildId = 108; Break }
        'Nucleus' { $buildId = 103; Break }
        'Ethos' { $buildId = 142; Break }
        'Ethos - Editor' { $buildId = 143; Break }
    }
    if (0 -ne $buildId) {
        Add-VSTeamBuild -BuildDefinitionId $buildId -ProjectName $projectName
    }
    else {
        Write-Error "Unknown build"
    }
}


function Get-Builds {
    param(
        [Parameter (Mandatory = $true)] [String]$projectName,
        [Parameter (Mandatory = $false)] [Int]$top = 10
    )
    
    Write-Host $projectName -ForegroundColor Cyan
    Write-Host '-'.PadLeft($projectName.Length, '-') -ForegroundColor Cyan -NoNewline
    Get-VSTeamBuild -ProjectName $projectName -Top $top | Format-Table @{
        Label      = "Build Definition"
        Expression = 
        {
            switch ($_.Result) {
                'failed' { $color = "31"; break }
                'succeeded' { $color = '32'; break }
                'canceled' { $color = "33"; break }
                default { $color = "0" }
            }
            $text = $_.BuildDefinition
            if ('Microsoft.VisualStudio.Services.TFS' -eq $text) { 
                $text = '(DevOps)'
            }

            $e = [char]27
            "$e[${color}m$($text)${e}[0m"
        }
    }, @{
        Label      = "Build"
        Expression = 
        {
            switch ($_.Result) {
                'failed' { $color = "31"; break }
                'succeeded' { $color = '32'; break }
                'canceled' { $color = "33"; break }
                default { $color = "0" }
            }
            $e = [char]27
            "$e[${color}m$($_.BuildNumber)${e}[0m"
        }
    }, @{
        Label      = "Status"
        Expression = 
        {
            switch ($_.Status) {
                'inProgress' { $color = "33"; break }
                'notStarted' { $color = '36'; break }
                default { $color = "0" }
            }
            $e = [char]27
            "$e[${color}m$($_.Status)${e}[0m"
        } 
    }, @{
        Label      = "Result"
        Expression = 
        {
            switch ($_.Result) {
                'failed' { $color = "31"; break }
                'succeeded' { $color = '32'; break }
                'canceled' { $color = "33"; break }
                default { $color = "0" }
            }
            $e = [char]27
            "$e[${color}m$($_.Result)${e}[0m"
        } 
    }, @{
        Label      = "Requested by"
        Expression = 
        {
            switch ($_.Result) {
                'failed' { $color = "31"; break }
                'succeeded' { $color = '32'; break }
                'canceled' { $color = "33"; break }
                default { $color = "0" }
            }
            $text = $_.RequestedBy
            if ('Microsoft.VisualStudio.Services.TFS' -eq $text) { 
                $text = '(DevOps)'
            }
    
            $e = [char]27
            "$e[${color}m$($text)${e}[0m"
        }
    }, @{
        Label      = "Start Time"
        Expression = 
        {
            switch ($_.Result) {
                'failed' { $color = "31"; break }
                'succeeded' { $color = '32'; break }
                'canceled' { $color = "33"; break }
                default { $color = "0" }
            }
            $e = [char]27
            "$e[${color}m$($_.startTime)${e}[0m"
        }
    }
}

function Get-AllBuilds {
    param(
        [Parameter (Mandatory = $false)] [Int]$top = 10
    )
    Write-Host ''
    Get-Builds -ProjectName 'Ben Fox' -Top $top
    Get-Builds -ProjectName 'Unity Packages' -Top $top
}



function Get-PlotTwistPackages {
    Param(
    [Parameter(Mandatory=$false)]
    [Switch]$includeOldFeed
    )

    $pat = Get-Content $env:USERPROFILE"\benFoxDevOpsPat.txt" | ConvertTo-SecureString

    $Organization = 'awesomeind'
    $AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$([System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pat)))")) }
    $UriRootFeeds = "https://feeds.dev.azure.com/$($Organization)/"

    $UriFeeds = $UriRootFeeds + "_apis/packaging/feeds?api-version=6.0-preview.1"
    $FeedResult = Invoke-RestMethod -Uri $UriFeeds -Method get -Headers $AzureDevOpsAuthenicationHeader
 
    Write-Host ''
    Foreach ($feed in $FeedResult.value) {

        if($feed.name -eq 'PlotTweestFeed')
        {
            if($includeOldFeed -eq $false)
            {
                continue;
            }
        }
        if($feed.name -eq 'UnityPkgFeed')
        {
            if($includeOldFeed -eq $false)
            {
                continue;
            }
        }



        # get packages for this feed
        $UriFeedPackages = $UriRootFeeds + "$($feed.project.name)/_apis/packaging/Feeds/$($feed.id)/packages?api-version=6.0-preview.1&includeDescription=true"   
        $FeedPackageResult = Invoke-RestMethod -Uri $UriFeedPackages -Method get -Headers $AzureDevOpsAuthenicationHeader  

        #if there are packages in feed, print them out
        if (0 -ne $FeedPackageResult.value.Count) {
            $nameWithCount = "$($feed.name) ($($FeedPackageResult.value.Count) package"

            if (1 -ne $FeedPackageResult.value.Count) {
                $nameWithCount = $nameWithCount + "s"
            }

            $nameWithCount = $nameWithCount + ")"

            Write-Host $nameWithCount -ForegroundColor Cyan
            Write-Host '-'.PadLeft($nameWithCount.Length, '-') -ForegroundColor Cyan -NoNewline

            $FeedPackageResult.value | ForEach-Object {
                $pkg = $_
                $ver = $pkg.versions | Select-Object -First 1

                New-Object -TypeName psobject -Property @{
                    PublishDate   = $ver.publishDate
                    LatestVersion = $ver.version
                    Listed        = $ver.isListed
                    Name          = $pkg.name
                    Description   = $ver.packageDescription
                    ProtocolType  = $pkg.protocolType
                }
            } 
            | Sort-Object 
            | Format-Table 'Name', 'LatestVersion', 'PublishDate', 'Description'
        }
    }
}

function Get-PlotTwistPackagesWithDownloadCountPerVersion {
    $pat = Get-Content $env:USERPROFILE"\benFoxDevOpsPat.txt" | ConvertTo-SecureString

    $Organization = 'awesomeind'
    $AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$([System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pat)))")) }
    $UriRootFeeds = "https://feeds.dev.azure.com/$($Organization)/"



    $UriFeeds = $UriRootFeeds + "_apis/packaging/feeds?api-version=6.0-preview.1"
    $FeedResult = Invoke-RestMethod -Uri $UriFeeds -Method get -Headers $AzureDevOpsAuthenicationHeader
    Write-Output $unsecurePat  
    
    Foreach ($feed in $FeedResult.value) {
        Write-Host $feed.name


        $UriFeedPackages = $UriRootFeeds + "$($feed.project.name)/_apis/packaging/Feeds/$($feed.id)/packages?api-version=6.0-preview.1"   

        $FeedPackageResult = Invoke-RestMethod -Uri $UriFeedPackages -Method get -Headers $AzureDevOpsAuthenicationHeader  
        Foreach ($feedpackage in $FeedPackageResult.value) {
            Write-Host $feedpackage.name

            $UriFeedPackageVersion = $UriRootFeeds + "$($feed.project.name)/_apis/packaging/Feeds/$($feed.id)/Packages/$($feedpackage.id)/versions?api-version=6.0-preview.1"
            $FeedPackageVersionResult = Invoke-RestMethod -Uri $UriFeedPackageVersion -Method get -Headers $AzureDevOpsAuthenicationHeader
                
            Foreach ($feedpackageversion in $FeedPackageVersionResult.value) {
                Write-Host " " $feedpackageversion.version -NoNewline


                $BodyPackageVersionIds = @{ packageVersionIds = @($feedpackageversion.id) } | ConvertTo-Json     
                $UriFeedPackageVersionUsage = $UriRootFeeds + "$($feed.project.name)/_apis/packaging/Feeds/$($feed.id)/Packages/$($feedpackage.id)/versionmetricsbatch?api-version=6.0-preview.1"   
                $FeedPackageVersionUsageResult = Invoke-RestMethod -Uri $UriFeedPackageVersionUsage -ContentType "application/json" -Method Post -Body $BodyPackageVersionIds -Headers $AzureDevOpsAuthenicationHeader
                foreach ($feedpackageversionusage in $FeedPackageVersionUsageResult.value) {                    
                    Write-Host " - " $feedpackageversionusage.downloadCount                                       
                }
                if (0 -eq $FeedPackageVersionUsageResult.value.Count) {
                    Write-Host ""   
                }
            }

        }

    }
}

# function List-Npm {
# param(
# [Parameter (Mandatory = $true)] [String]$feed
# )
    
# $pkgs = Get-VSTeamPackage -feedName $feed

# if(0 -ne $pkgs.Count)
# {
# Write-Host '--------' $feed -ForegroundColor Blue

# $pkgs | ForEach-Object {
# $pkg = $_
# $ver = Get-VSTeamPackageVersion -feedName $feed -packageId $pkg.Id | Select-Object -First 1
# New-Object -TypeName psobject -Property @{
# PublishDate = $ver.PublishDate
# Version = $ver.Version
# Listed = $ver.IsListed
# Name = $pkg.Name
# Description = $ver.Description
# }
# } | Format-Table 'Name', 'Version', 'PublishDate', 'Description'
# }
# }

# function List-AllNpm {
# List-Npm 'plottwist.all.core'
# List-Npm 'plottwist.all.code'
# List-Npm 'plottwist.all.art'
# List-Npm 'plottwist.all.ld'

# List-Npm 'plottwist.benfox.core'
# List-Npm 'plottwist.benfox.code'
# List-Npm 'plottwist.benfox.art'
# List-Npm 'plottwist.benfox.ld'
# }