functions/Get-MetadataObjectFromRelease.ps1

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
<#
    .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
    }
}