-
Notifications
You must be signed in to change notification settings - Fork 13
/
GetNextBuildVersion.ps1
109 lines (90 loc) · 2.49 KB
/
GetNextBuildVersion.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
98
99
100
101
102
103
104
105
106
107
108
109
Param(
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory = $true)]
[String] $ProductName,
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory = $true)]
[String] $ReleaseName,
[ValidateNotNull()]
[Parameter(Mandatory = $false)]
[Uri] $VersionManagementApiUrl = 'https://tfs.gmgmt.dimensiondata.com:8443/version-management/'
)
If ($VersionManagementApiUrl.AbsoluteUri -notlike '*/')
{
Throw 'Invalid base URI for the build-versioning API (must have a trailing '/').'
}
Add-Type -LiteralPath .\Common\Tasks\LibGit2Sharp.dll
$workingDirectory = Resolve-Path '.'
If (![LibGit2Sharp.Repository]::IsValid($workingDirectory))
{
Throw "Current directory is not a Git repository"
}
Try
{
[LibGit2Sharp.Repository] $repository = New-Object LibGit2Sharp.Repository $workingDirectory
$commitId = $repository.Head.Tip.Sha
}
Finally
{
If ($repository)
{
$repository.Dispose()
}
}
Write-Host "Requesting version for release '$ReleaseName' of product '$ProductName' (commit '$commitId')..."
$webClient = New-Object System.Net.WebClient
Try
{
# TODO: Escape product and release names.
$targetUrl = "$($VersionManagementApiUrl.AbsoluteUri)api/v1/product/$($ProductName)/release/$($ReleaseName)/version/$commitId"
$webClient.UseDefaultCredentials = $true;
$version = $webClient.UploadString(
$targetUrl,
'' # POST body
)
Write-Host "Version is '$version'"
Return $version
}
Catch
{
$exception = [System.Exception] $_.Exception
$webException = $null
While ($exception)
{
If ($exception -is [System.Net.WebException])
{
$webException = [System.Net.WebException] $exception
Break
}
$exception = $exception.InnerException
}
If (!$webException -or !$webException.Response.ContentLength) # There may not be a response body.
{
Throw $_ # No, Powershell doesn't retain the original exception context either way
}
Try
{
$responseStream = $webException.Response.GetResponseStream()
$responseReader = New-Object System.IO.StreamReader $responseStream
$responseText = $responseReader.ReadToEnd()
Write-Error "Error calling version management API: $responseText"
}
Catch
{
Write-Error $webException
}
Finally
{
If ($responseStream)
{
$responseStream.Dispose()
}
}
}
Finally
{
If ($webClient)
{
$webClient.Dispose()
}
}