functions/github/Connect-GitHubOrg.ps1

function Connect-GitHubOrg
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string] $OrgName
    )

    # When running in GitHub Actions we will need ensure the GitHub App is
    # authenticated for the current GitHub Org
    if ($env:SSH_PRIVATE_KEY -and $env:GITHUB_APP_ID) {
        Write-Information "Getting access token for organisation: '$OrgName'"
        $accessToken = New-GitHubAppInstallationAccessToken -AppId $env:GITHUB_APP_ID `
                                                            -AppPrivateKey $env:SSH_PRIVATE_KEY `
                                                            -OrgName $OrgName
        
        if ($accessToken) {
            $env:GITHUB_TOKEN = $accessToken
        }
        else {
            throw "There was a problem obtaining an access token for '$OrgName' (GitHubAppId=$($env:GITHUB_APP_ID)"
        }
    }
    else {
        Write-Information "The environment variables SSH_PRIVATE_KEY and GITHUB_APP_ID were not set - interactive authentication may be attempted when required"
    }
}