Skip to content

Cleanup after failed E2E #105

Cleanup after failed E2E

Cleanup after failed E2E #105

name: Cleanup after failed E2E
on:
workflow_dispatch:
inputs:
githubOwner:
description: GitHub organization set as owner for the temp repositories (default is to use your personal account)
required: false
default: ''
defaults:
run:
shell: pwsh
jobs:
Check:
runs-on: [ ubuntu-latest ]
outputs:
githubOwner: ${{ steps.check.outputs.githubOwner }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Check secrets
id: check
env:
githubOwner: ${{ github.event.inputs.githubOwner }}
run: |
$err = $false
if ('${{ Secrets.E2EPAT }}' -eq '') {
Write-Host "::Error::In order to run end to end tests, you need a Secret called E2EPAT containing a valid Personal Access Token with the following permissions: admin:org, delete_repo, repo, workflow, packages:write"
$err = $true
}
if ('${{ Secrets.adminCenterApiCredentials }}' -eq '') {
Write-Host "::Error::In order to run end to end tests, you need a Secret called adminCenterApiCredentials containing API access credentials for BC Admin Center"
$err = $true
}
if ($err) {
exit 1
}
$githubOwner = $ENV:githubOwner
if (!($githubOwner)) {
$githubOwner = "$ENV:GITHUB_REPOSITORY_OWNER"
}
$orgmap = Get-Content -path (Join-Path "." "e2eTests\orgmap.json") -encoding UTF8 -raw | ConvertFrom-Json
if ($orgmap.PSObject.Properties.Name -eq $githubOwner) {
$githubOwner = $orgmap."$githubOwner"
}
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "githubOwner=$githubOwner"
Write-Host "githubOwner=$githubOwner"
RemoveRepositories:
runs-on: [ ubuntu-latest ]
needs: [ Check ]
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Remove Temp Repositories
run: |
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
Import-Module (Join-Path "." "e2eTests/e2eTestHelper.psm1") -DisableNameChecking
$owner = '${{ needs.Check.outputs.githubowner }}'
$token = '${{ Secrets.E2EPAT }}'
SetTokenAndRepository -github -githubOwner $owner -token $token -repository ''
@(invoke-gh repo list $owner --limit 1000 -silent -returnValue) | ForEach-Object { $_.Split("`t")[0] } | Where-Object { "$_" -like "$owner/tmp*" } | ForEach-Object {
$repo = $_
Write-Host "https://github.com/$repo"
$repoOwner = $repo.Split('/')[0]
@((invoke-gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" "/orgs/$repoOwner/packages?package_type=nuget" -silent -returnvalue -ErrorAction SilentlyContinue | ConvertFrom-Json)) | Where-Object { ($_.PSObject.Properties.Name -eq 'repository') -and ($_.repository.full_name -eq $repo) } | ForEach-Object {
Write-Host "- package $($_.name)"
# Pipe empty string into GH API --METHOD DELETE due to https://github.com/cli/cli/issues/3937
'' | invoke-gh api --method DELETE -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" "/orgs/$repoOwner/packages/nuget/$($_.name)" --input -
}
invoke-gh repo delete "https://github.com/$repo" --yes | Out-Host
}