Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workflow to automate updating references to Carter NuGet package #304

Merged
merged 14 commits into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
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
47 changes: 47 additions & 0 deletions .github/workflows/update-carter-package-references.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Update Carter NuGet package references

on:
workflow_dispatch:
inputs:
includePrerelease:
type: boolean
default: false
description: Whether to include prerelease versions when searching for the latest version of the Carter NuGet package

commitAndPush:
type: boolean
default: false
description: Whether to commit and push the changes; Useful to test the process by looking at the resulting Git diff

jobs:
update-carter-package-references:
name: Update Carter NuGet package references
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Update package references with latest version from NuGet
run: >-
./update-carter-package-dependencies.ps1
-IncludePrerelease:$${{ github.event.inputs.includePrerelease }}
shell: pwsh

- name: Show Git diff
run: git diff

# https://github.com/marketplace/actions/checkout#push-a-commit-using-the-built-in-token
- if: github.event.inputs.commitAndPush
name: Commit changes and push if necessary
run: |
GIT_CHANGES=$(git status --porcelain)
if [ "$GIT_CHANGES" == "" ]; then
echo "No changes to commit"
else
echo "Committing and pushing changes"
git config user.name Jonathan Channon
git config user.email [email protected]
git add .
git commit -m "Update Carter NuGet package references"
git push
fi
2 changes: 1 addition & 1 deletion template/content/CarterTemplate.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<AssemblyName>CarterTemplate</AssemblyName>
Expand Down
86 changes: 86 additions & 0 deletions update-carter-package-dependencies.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
[CmdletBinding()]
param(
[Parameter()]
[switch]
$IncludePrerelease
)

function Get-LatestCarterPackageVersion {
[CmdletBinding()]
param(
[Parameter()]
[switch]
$IncludePrerelease
)

#
# NuGet Server API docs
# https://learn.microsoft.com/en-us/nuget/api/overview
#

$nugetApiServiceIndexUrl = 'https://api.nuget.org/v3/index.json'
$serviceIndexResponse = Invoke-RestMethod -Method Get -Uri $nugetApiServiceIndexUrl

$nugetApiSearchQueryServiceUrl = $serviceIndexResponse.resources |
Where-Object '@type' -eq 'SearchQueryService' |
Select-Object -First 1 |
Select-Object -ExpandProperty '@id'

#
# https://learn.microsoft.com/en-us/nuget/api/search-query-service-resource#search-for-packages
#
$queryParameters = @{
q = 'Carter'
prerelease = if ($IncludePrerelease) { 'true' } else { 'false' }
semVerLevel = '2.0.0'
}

$searchResponse = Invoke-RestMethod `
-Method Get `
-Uri $nugetApiSearchQueryServiceUrl `
-Body $queryParameters

$latestPackage = $searchResponse.data |
Where-Object 'id' -eq 'Carter' |
Sort-Object -Property 'version' -Descending |
Select-Object -First 1

Write-Output -InputObject $latestPackage.version
}

function Update-CarterPackageReferences {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]
$RootPath,

[Parameter(Mandatory = $true)]
[string]
$PackageVersion
)

$projectFiles = Get-ChildItem `
-Path $RootPath `
-Recurse `
-File `
-Filter '*.csproj'

$regexPattern = '(?<beforeVersion><PackageReference\s+Include="Carter"\s+Version=")[\s\S]+?(?<afterVersion>"\s*/>)'

foreach ($projectFile in $projectFiles) {
$contents = Get-Content -Path $projectFile.FullName -Raw -Encoding utf8

if ([System.Text.RegularExpressions.Regex]::IsMatch($contents, $regexPattern)) {
$newContents = [System.Text.RegularExpressions.Regex]::Replace(
$contents,
$regexPattern,
('${{beforeVersion}}{0}${{afterVersion}}' -f $PackageVersion))

Set-Content -Path $projectFile.FullName -Value $newContents -Encoding utf8 -NoNewline
}
}
}

$targetCarterPackageVersion = Get-LatestCarterPackageVersion -IncludePrerelease:$IncludePrerelease
Update-CarterPackageReferences -RootPath $PSScriptRoot -PackageVersion $targetCarterPackageVersion