Co-authored-by: Armaan Mcleod <armaan_mcleod@outlook.com>
This commit is contained in:
Bernie White 2022-03-23 20:42:13 +10:00 коммит произвёл GitHub
Родитель 12c4e686c5
Коммит 0069506050
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 276 добавлений и 0 удалений

47
.github/workflows/dependencies.yaml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,47 @@
#
# Automated dependency updates
#
# NOTES:
# This automatically bumps PowerShell dependency versions.
name: Dependencies
on:
schedule:
- cron: '50 1 * * 1' # At 01:50 AM, on Monday each week
workflow_dispatch:
env:
WORKING_BRANCH: dependencies/powershell-bump
jobs:
dependencies:
name: Bump dependencies
runs-on: ubuntu-latest
if: github.repository == 'PSRule-pipelines'
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Configure
run: |
git config user.name github-actions
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
- name: Get working branch
run: |
git checkout -B ${{ env.WORKING_BRANCH }} --force
- name: Check dependencies
run: |
Import-Module ./scripts/dependencies.psm1;
Update-Dependencies -Path ./modules.json;
shell: pwsh
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

21
modules.json Normal file
Просмотреть файл

@ -0,0 +1,21 @@
{
"dependencies": {
"PSRule": {
"version": "1.11.1"
},
"VstsTaskSdk": {
"version": "0.11.0"
},
"PowerShellGet": {
"version": "2.2.3"
}
},
"devDependencies": {
"Pester": {
"version": "5.3.1"
},
"PSScriptAnalyzer": {
"version": "1.20.0"
}
}
}

Просмотреть файл

@ -1,5 +1,12 @@
# PSRule options for QA
requires:
PSRule.Rules.MSFT.OSS: '@pre >=1.0.1'
include:
module:
- PSRule.Rules.MSFT.OSS
input:
pathIgnore:
- '.vscode/'

201
scripts/dependencies.psm1 Normal file
Просмотреть файл

@ -0,0 +1,201 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Note:
# Handles dependencies updates.
function Update-Dependencies {
[CmdletBinding()]
param (
[Parameter(Mandatory = $True)]
[String]$Path,
[Parameter(Mandatory = $False)]
[String]$Repository = 'PSGallery'
)
process {
$modules = Get-Content -Path $Path -Raw | ConvertFrom-Json -AsHashtable;
$dependencies = CheckVersion $modules.dependencies -Repository $Repository;
$devDependencies = CheckVersion $modules.devDependencies -Repository $Repository -Dev;
$modules = [Ordered]@{
dependencies = $dependencies
devDependencies = $devDependencies
}
$modules | ConvertTo-Json -Depth 10 | Set-Content -Path $Path;
$updates = @(git status --porcelain);
if ($Null -ne $Env:WORKING_BRANCH -and $Null -ne $updates -and $updates.Length -gt 0) {
git add modules.json;
git commit -m "Update $path";
git push --force -u origin $Env:WORKING_BRANCH;
$existingBranch = @(gh pr list --head $Env:WORKING_BRANCH --state open --json number | ConvertFrom-Json);
if ($Null -eq $existingBranch -or $existingBranch.Length -eq 0) {
gh pr create -B 'main' -H $Env:WORKING_BRANCH -l 'dependencies' -t 'Bump PowerShell dependencies' -F 'out/updates.txt';
}
else {
$pr = $existingBranch[0].number
gh pr edit $pr -F 'out/updates.txt';
}
}
}
}
function Install-Dependencies {
[CmdletBinding()]
param (
[Parameter(Mandatory = $True)]
[String]$Path,
[Parameter(Mandatory = $False)]
[String]$Repository = 'PSGallery'
)
process {
$modules = Get-Content -Path $Path -Raw | ConvertFrom-Json;
InstallVersion $modules.dependencies -Repository $Repository;
InstallVersion $modules.devDependencies -Repository $Repository -Dev;
}
}
function Save-Dependencies {
[CmdletBinding()]
param (
[Parameter(Mandatory = $True)]
[String]$Path,
[Parameter(Mandatory = $True)]
[String]$OutputPath,
[Parameter(Mandatory = $False)]
[String]$Repository = 'PSGallery'
)
process {
$modules = Get-Content -Path $Path -Raw | ConvertFrom-Json;
SaveVersion $modules.dependencies -Repository $Repository -OutputPath $OutputPath;
}
}
function CheckVersion {
[CmdletBinding()]
[OutputType([System.Collections.Specialized.OrderedDictionary])]
param (
[Parameter(Mandatory = $True)]
[AllowNull()]
[Hashtable]$InputObject,
[Parameter(Mandatory = $True)]
[String]$Repository,
[Parameter(Mandatory = $False)]
[Switch]$Dev,
[Parameter(Mandatory = $False)]
[String]$OutputPath = 'out/'
)
begin {
$group = 'Dependencies';
if ($Dev) {
$group = 'DevDependencies';
}
if (!(Test-Path -Path $OutputPath)) {
$Null = New-Item -Path $OutputPath -ItemType Directory -Force;
}
$changeNotes = Join-Path -Path $OutputPath -ChildPath 'updates.txt';
}
process {
$dependencies = [Ordered]@{ };
if ($Null -eq $InputObject) {
return $dependencies;
}
$InputObject.GetEnumerator() | Sort-Object -Property Name | ForEach-Object {
$dependencies[$_.Name] = $_.Value
}
foreach ($module in $dependencies.GetEnumerator()) {
Write-Host -Object "[$group] -- Checking $($module.Name)";
$installParams = @{}
$installParams += $module.Value;
$installParams.MinimumVersion = $installParams.version;
$installParams.Remove('version');
$available = @(Find-Module -Repository $Repository -Name $module.Name @installParams -ErrorAction Ignore);
foreach ($found in $available) {
if (([Version]$found.Version) -gt ([Version]$module.Value.version)) {
Write-Host -Object "[$group] -- Newer version found $($found.Version)";
$dependencies[$module.Name].version = $found.Version;
$Null = Add-Content -Path $changeNotes -Value "Bump $($module.Name) to $($found.Version).";
}
else {
Write-Host -Object "[$group] -- Already up to date.";
}
}
}
return $dependencies;
}
}
function InstallVersion {
[CmdletBinding()]
[OutputType([void])]
param (
[Parameter(Mandatory = $True)]
[PSObject]$InputObject,
[Parameter(Mandatory = $True)]
[String]$Repository,
[Parameter(Mandatory = $False)]
[Switch]$Dev
)
begin {
$group = 'Dependencies';
if ($Dev) {
$group = 'DevDependencies';
}
}
process {
foreach ($module in $InputObject.PSObject.Properties.GetEnumerator()) {
Write-Host -Object "[$group] -- Installing $($module.Name) v$($module.Value.version)";
$installParams = @{ MinimumVersion = $module.Value.version };
if ($Null -eq (Get-InstalledModule -Name $module.Name @installParams -ErrorAction Ignore)) {
Install-Module -Name $module.Name @installParams -Force -Repository $Repository;
}
}
}
}
function SaveVersion {
[CmdletBinding()]
[OutputType([void])]
param (
[Parameter(Mandatory = $True)]
[PSObject]$InputObject,
[Parameter(Mandatory = $True)]
[String]$Repository,
[Parameter(Mandatory = $True)]
[String]$OutputPath,
[Parameter(Mandatory = $False)]
[Switch]$Dev
)
begin {
$group = 'Dependencies';
if ($Dev) {
$group = 'DevDependencies';
}
}
process {
foreach ($module in $InputObject.PSObject.Properties.GetEnumerator()) {
Write-Host -Object "[$group] -- Saving $($module.Name) v$($module.Value.version)";
$saveParams = @{ MinimumVersion = $module.Value.version };
Save-Module -Name $module.Name @saveParams -Force -Repository $Repository -Path $OutputPath;
}
}
}
Export-ModuleMember -Function @(
'Update-Dependencies'
'Install-Dependencies'
'Save-Dependencies'
)