functions/Get-MetadataObjectFromRelease.ps1

<#
    .NOTES
        TBD
 
    .DESCRIPTION
        TBD
 
    .PARAMETER ArtifactsDirectory
        TBD
 
    .EXAMPLE
        Get-MetadataObjectFromRelease
 
        Example usage Get-MetadataObjectFromRelease
#>

function Get-MetadataObjectFromRelease {
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]$ArtifactsDirectory
    )

    begin {
        $CurrentTime = Get-Date -Format "MM-dd-yyyy-hh-mm-ss"
    }

    process {

        $MetadataObject = [Ordered]@{
            attemptnumber                             = $Env:RELEASE_ATTEMPTNUMBER
            definitionenvironmentid                   = $Env:RELEASE_DEFINITIONENVIRONMENTID
            definitionid                              = $Env:RELEASE_DEFINITIONID
            definitionname                            = $Env:RELEASE_DEFINITIONNAME
            deployment_requestedfor                   = $Env:RELEASE_DEPLOYMENT_REQUESTEDFOR
            deployment_requestedforemail              = $Env:RELEASE_DEPLOYMENT_REQUESTEDFOREMAIL
            deployment_requestedforid                 = $Env:RELEASE_DEPLOYMENT_REQUESTEDFORID
            deployment_starttime                      = $Env:RELEASE_DEPLOYMENT_STARTTIME
            deploymentid                              = $Env:RELEASE_DEPLOYMENTID
            deployphaseid                             = $Env:RELEASE_DEPLOYPHASEID
            environmentid                             = $Env:RELEASE_ENVIRONMENTID
            environmentname                           = $Env:RELEASE_ENVIRONMENTNAME
            environments_createreleasemetadata_status = $Env:RELEASE_ENVIRONMENTS_CREATERELEASEMETADATA_STATUS
            environmenturi                            = $Env:RELEASE_ENVIRONMENTURI
            primaryartifactsourcealias                = $Env:RELEASE_PRIMARYARTIFACTSOURCEALIAS
            reason                                    = $Env:RELEASE_REASON
            releasedescription                        = $Env:RELEASE_RELEASEDESCRIPTION
            releaseid                                 = $Env:RELEASE_RELEASEID
            releasename                               = $Env:RELEASE_RELEASENAME
            releaseuri                                = $Env:RELEASE_RELEASEURI
            requestedfor                              = $Env:RELEASE_REQUESTEDFOR
            requestedforemail                         = $Env:RELEASE_REQUESTEDFOREMAIL
            requestedforid                            = $Env:RELEASE_REQUESTEDFORID
            skipartifactsdownload                     = $Env:RELEASE_SKIPARTIFACTSDOWNLOAD
            teamproject                               = $env:SYSTEM_TEAMPROJECT
            teamfoundationcollectionuri               = $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI
            resources                                 = [Ordered] @{ }
            currentTime                               = $CurrentTime
        }

        #test
        (Get-ChildItem -Path $ArtifactsDirectory | `
                    Where-Object { $_.PSIsContainer }).Name | ForEach-Object {

            $aliastName = (($_ -replace '[\.\-\s\b ]', '_').ToUpper()).replace('[', '`[').replace(']', '`]')

            $buildId = [environment]::GetEnvironmentVariable(('RELEASE_ARTIFACTS_{0}_BUILDID' -f $aliastName), "Process")
            If ((-not [string]::IsNullOrEmpty($buildId))) {

                $buildUrl = ("{0}/{1}/_build/results?buildId={2}" -f $MetadataObject.teamfoundationcollectionuri, [uri]::EscapeDataString($MetadataObject.teamproject), $buildId)
                $repositoryID = [environment]::GetEnvironmentVariable(('RELEASE_ARTIFACTS_{0}_REPOSITORY_ID' -f $aliastName), "Process")
                $commitId = [environment]::GetEnvironmentVariable(('RELEASE_ARTIFACTS_{0}_SOURCEVERSION' -f $aliastName), "Process")
                $gitCommitUrl = ("{0}/{1}/_git/{2}/commit/{3}" -f $MetadataObject.teamfoundationcollectionuri, [uri]::EscapeDataString($MetadataObject.teamproject), $repositoryID, $commitId)
                $buildName = [environment]::GetEnvironmentVariable(('RELEASE_ARTIFACTS_{0}_BUILDNUMBER' -f $aliastName), "Process")

                $repositoryName = [environment]::GetEnvironmentVariable(('RELEASE_ARTIFACTS_{0}_REPOSITORY_NAME' -f $aliastName), "Process")
                $branch = [environment]::GetEnvironmentVariable(('RELEASE_ARTIFACTS_{0}_SOURCEBRANCHNAME' -f $aliastName), "Process")
                $triggeredBy = [environment]::GetEnvironmentVariable(('RELEASE_ARTIFACTS_{0}_REQUESTEDFOR' -f $aliastName), "Process")

                $artifactObject = [Ordered]@{
                    Version        = $buildName
                    repositoryName = $repositoryName
                    branch         = $branch
                    buildUrl       = $buildUrl
                    buildName      = $buildName
                    commitId       = $commitId.substring(0, 8)
                    commitUrl      = $gitCommitUrl
                    triggeredBy    = $triggeredBy
                    alias          = $aliastName
                }

                $MetadataObject.resources.add($_, $artifactObject)
            }
        }

        return $MetadataObject
    }
}