1 <#
2 .SYNOPSIS
3 Uploads the release asset and returns the resulting object from the upload
4 
5 .PARAMETER ReleaseTag
6 Tag to look up release
7 
8 .PARAMETER AssetPath
9 Location of the asset file to upload
10 
11 .PARAMETER GitHubRepo
12 Name of the GitHub repo to search (of the form Azure/azure-sdk-for-cpp)
13 
14 #>
15 
16 param (
17     [Parameter(Mandatory = $true)]
18     [ValidateNotNullOrEmpty()]
19     [string] $ReleaseTag,
20 
21     [Parameter(Mandatory = $true)]
22     [ValidateNotNullOrEmpty()]
23     [string] $AssetPath,
24 
25     [Parameter(Mandatory = $true)]
26     [ValidateNotNullOrEmpty()]
27     [string] $GitHubRepo,
28 
29     [Parameter(Mandatory = $true)]
30     [ValidateNotNullOrEmpty()]
31     [string] $GitHubPat
32 )
33 
34 # Get information about release at $ReleaseTag
35 $releaseInfoUrl = "https://api.github.com/repos/$GitHubRepo/releases/tags/$ReleaseTag"
36 Write-Verbose "Requesting release info from $releaseInfoUrl"
37 $release = Invoke-RestMethod `
38     -Uri  $releaseInfoUrl `
39     -Method GET
40 
41 $assetFilename = Split-Path $AssetPath -Leaf
42 
43 # Upload URL comes in the literal form (yes, those curly braces) of:
44 # https://uploads.github.com/repos/Azure/azure-sdk-for-cpp/releases/123/assets{?name,label}
45 # Converts to something like:
46 # https://uploads.github.com/repos/Azure/azure-sdk-for-cpp/releases/123/assets?name=foo.tar.gz
47 # Docs: https://docs.github.com/en/rest/reference/repos#get-a-release-by-tag-name
48 $uploadUrl = $release.upload_url.Split('{')[0] + "?name=$assetFilename"
49 
50 Write-Verbose "Uploading $assetFilename to $uploadUrl"
51 
52 $asset = Invoke-RestMethod `
53     -Uri $uploadUrl `
54     -Method POST `
55     -InFile $AssetPath `
56     -Credential $credentials `
57     -Headers @{ Authorization = "token $GitHubPat" } `
58     -ContentType "application/gzip"
59 
60 Write-Verbose "Upload complete. Browser download URL: $($asset.browser_download_url)"
61 
62 return $asset
63