Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

GitReleases stream support #105

Merged
merged 1 commit into from
Oct 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 80 additions & 44 deletions AU/Plugins/GitReleases.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ param(
# The text that should be used in the description of the release.
[string]$releaseDescription = $null,

# The text that should be used in the description when a release is created for a stream (by default it uses the latest commit message).
[string]$streamReleaseDescription = '',

# The formatting to use when replacing <date> in release header/description and on date based releases.
[string]$dateFormat = '{0:yyyy-MM-dd}',

Expand Down Expand Up @@ -55,13 +58,39 @@ function GetOrCreateRelease() {
"prerelease" = $false
} | ConvertTo-Json -Compress

Write-Verbose "Trying to create the new release $tagName..."
Write-Host "Creating the new release $tagName..."
return Invoke-RestMethod -UseBasicParsing -Method Post -Uri "https://api.github.com/repos/$repository/releases" -Body $json -Headers $headers
}

[array]$packages = if ($Force) { $Info.result.updated } else { $Info.result.pushed }

if ($packages.Length -eq 0) { Write-Host "No package updated, skipping"; return }

$packagesToRelease = New-Object 'System.Collections.Generic.List[hashtable]'

$packages | % {
if ($_.Streams) {
$pkg = $_
$data = ConvertFrom-Json ($pkg.Streams -replace '@', '' -replace '\s*=\s*', '":"' -replace '{\s*', '{"' -replace '\s*}', '"}' -replace '\s*;\s*', '","')
($data | Get-Member -MemberType NoteProperty).Name | % {
$value = $data.$_
$packagesToRelease.Add(@{
Name = $pkg.Name
RemoteVersion = $value
NuFile = Resolve-Path ("$($pkg.Path)/*.$($value).nupkg")
})
}
}
else {
$packagesToRelease.Add(@{
Name = $_.Name
NuspecVersion = $_.NuspecVersion
RemoteVersion = $_.RemoteVersion
NuFile = Resolve-Path ("$($_.Path)/$($_.Name).$($_.RemoteVersion).nupkg")
})
}
}

$origin = git config --get remote.origin.url

if (!($origin -match "github.com\/([^\/]+\/[^\/\.]+)")) {
Expand All @@ -71,64 +100,71 @@ if (!($origin -match "github.com\/([^\/]+\/[^\/\.]+)")) {
$repository = $Matches[1]

$headers = @{
Authorization = "token $ApiToken"
Authorization = "token $ApiToken"
}

if ($releaseType -eq 'date' -and !$releaseHeader) {
$releaseHeader = 'Packages updated on <date>'
} elseif (!$releaseHeader) {
$releaseHeader = '<PackageName> <RemoteVersion>'
$releaseHeader = 'Packages updated on <date>'
}
elseif (!$releaseHeader) {
$releaseHeader = '<PackageName> <RemoteVersion>'
}

if ($releaseType -eq 'date' -and !$releaseDescription) {
$releaseDescription = 'We had packages that was updated on <date>'
} elseif (!$releaseDescription) {
$releaseDescription = '<PackageName> was updated from version <NuspecVersion> to <RemoteVersion>'
$releaseDescription = 'We had packages that was updated on <date>'
}
elseif (!$releaseDescription) {
$releaseDescription = '<PackageName> was updated from version <NuspecVersion> to <RemoteVersion>'
}

$date = Get-Date -UFormat $dateFormat

if ($releaseType -eq 'date') {
$release = GetOrCreateRelease `
-tagName $date `
-releaseName ($releaseHeader -replace '<date>',$date) `
-releaseDescription ($releaseDescription -replace '<date>',$date) `
-repository $repository `
-headers $headers

if (!$release) {
Write-Error "Unable to create a new release, please check your permissions..."
return
}
$release = GetOrCreateRelease `
-tagName $date `
-releaseName ($releaseHeader -replace '<date>', $date) `
-releaseDescription ($releaseDescription -replace '<date>', $date) `
-repository $repository `
-headers $headers

if (!$release) {
Write-Error "Unable to create a new release, please check your permissions..."
return
}
}

$uploadHeaders = $headers.Clone()
$uploadHeaders['Content-Type'] = 'application/zip'

$packages | % {
if ($releaseType -eq 'package') {
$releaseName = $releaseHeader -replace '<PackageName>',$_.Name -replace '<RemoteVersion>',$_.RemoteVersion -replace '<NuspecVersion>',$_.NuspecVersion -replace '<date>',$date
$packageDesc = $releaseDescription -replace '<PackageName>',$_.Name -replace '<RemoteVersion>',$_.RemoteVersion -replace '<NuspecVersion>',$_.NuspecVersion -replace '<date>',$date
$packagesToRelease | % {
if ($releaseType -eq 'package') {
$releaseName = $releaseHeader -replace '<PackageName>', $_.Name -replace '<RemoteVersion>', $_.RemoteVersion -replace '<NuspecVersion>', $_.NuspecVersion -replace '<date>', $date
if ($_.NuspecVersion) {
$packageDesc = $releaseDescription
}
else {
$packageDesc = $streamReleaseDescription
}
$packageDesc = $packageDesc -replace '<PackageName>', $_.Name -replace '<RemoteVersion>', $_.RemoteVersion -replace '<NuspecVersion>', $_.NuspecVersion -replace '<date>', $date

$release = GetOrCreateRelease `
-tagName "$($_.Name)-$($_.RemoteVersion)" `
-releaseName $releaseName `
-releaseDescription $packageDesc `
-repository $repository `
-headers $headers
}

$release = GetOrCreateRelease `
-tagName "$($_.Name)-$($_.RemoteVersion)" `
-releaseName $releaseName `
-releaseDescription $packageDesc `
-repository $repository `
-headers $headers
}

$path = Resolve-Path "$($_.Path)\*.nupkg"
$fileName = [System.IO.Path]::GetFileName($path)

$existing = $release.assets | ? name -eq $fileName
if ($existing) {
Write-Verbose "Removing existing $fileName asset..."
Invoke-RestMethod -UseBasicParsing -Uri $existing.url -method Delete -Headers $headers | Out-Null
}

$uploadUrl = $release.upload_url -replace '\{.*\}$',''
$rawContent = [System.IO.File]::ReadAllBytes($path)
Write-Host "Uploading $fileName asset..."
Invoke-RestMethod -UseBasicParsing -Uri "${uploadUrl}?name=${fileName}&label=$($_.Name) v$($_.RemoteVersion)" -Body $rawContent -Headers $uploadHeaders -Method Post | Out-Null
$fileName = [System.IO.Path]::GetFileName($_.NuFile)

$existing = $release.assets | ? name -eq $fileName
if ($existing) {
Write-Verbose "Removing existing $fileName asset..."
Invoke-RestMethod -UseBasicParsing -Uri $existing.url -method Delete -Headers $headers | Out-Null
}

$uploadUrl = $release.upload_url -replace '\{.*\}$', ''
$rawContent = [System.IO.File]::ReadAllBytes($_.NuFile)
Write-Host "Uploading $fileName asset..."
Invoke-RestMethod -UseBasicParsing -Uri "${uploadUrl}?name=${fileName}&label=$($_.Name) v$($_.RemoteVersion)" -Body $rawContent -Headers $uploadHeaders -Method Post | Out-Null
}