Skip to content

Instantly share code, notes, and snippets.

@heoelri
Created January 7, 2022 10:08
Show Gist options
  • Save heoelri/e8c1757c80f774ba9d0668cfc52afe35 to your computer and use it in GitHub Desktop.
Save heoelri/e8c1757c80f774ba9d0668cfc52afe35 to your computer and use it in GitHub Desktop.
This PowerShell file scans a repository for components that can be monitored by dependabot and auto-generates a dependabot.yml file for your GitHub repository.
[CmdletBinding()] # indicate that this is advanced function (with additional params automatically added)
param (
[string] $outputFile,
[string] $targetBranch = "main" # default = main
)
$files = Get-Childitem -Recurse
function packageEcosystem() {
param (
[string] $ecosystem,
[string] $relPath,
[string] $targetBranch = "main", # default = main
[string] $interval = "daily" # default = every day at 5:00 UTC
)
$block = @"
- package-ecosystem: "$ecosystem"
directory: "$relPath"
schedule:
interval: "$interval"
target-branch: "$targetBranch"
"@
return $block
}
$output = @"
# This file is auto-generated by .github/scripts/update-dependabot.ps1
version: 2
updates:
"@
foreach ($file in $files) {
$relPath = Resolve-Path -relative $($file.FullName) | Split-Path -Parent
$relPath = $relPath -replace '\./', '/' # replace leading ./ with /
if ($file.Name -eq 'main.tf') {
Write-Host "Found main.tf in $($file.FullName)"
$ecosystem = "terraform"
$block = packageEcosystem -ecosystem $ecosystem `
-relpath $relPath `
-targetBranch "$targetBranch"
$output += "`r`n"+$block
} elseif ($file.Name -eq 'Dockerfile') {
Write-Host "Found Dockerfile in $($file.FullName)"
$ecosystem = "docker"
$block = packageEcosystem -ecosystem $ecosystem `
-relpath $relPath `
-targetBranch "$targetBranch"
$output += "`r`n"+$block
} elseif ($file.Name -eq 'package.json') {
Write-Host "Found package.json in $($file.FullName)"
$ecosystem = "npm"
$block = packageEcosystem -ecosystem $ecosystem `
-relpath $relPath `
-targetBranch "$targetBranch"
# NPM uses a customized package-ecosystem block
$block += "`r`n"+@"
allow:
- dependency-type: direct
- dependency-type: production # check only dependencies, which are going to the compiled app, not supporting tools like @vue-cli
"@
$output += "`r`n"+$block
} elseif ($file.Name -like '*.sln') {
Write-Host "Found *.sln in $($file.FullName)"
$ecosystem = "nuget"
$block = packageEcosystem -ecosystem $ecosystem `
-relpath $relPath `
-targetBranch "$targetBranch"
$output += "`r`n"+$block
}
}
if ($outputFile -ne "") {
Write-Host "*** Writing output to $outputFile"
$output | Out-file -FilePath $outputFile -Encoding UTF8
} else {
Write-Host $output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment