functions/github/Connect-GitHubOrg.ps1

function Connect-GitHubOrg {
    [CmdletBinding()]
    param (
    )

    # 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: '$org'"
        $accessToken = New-GitHubAppInstallationAccessToken -AppId $env:GITHUB_APP_ID `
                                                            -AppPrivateKey $env:SSH_PRIVATE_KEY `
                                                            -OrgName $org
        
        if ($accessToken) {
            $env:GITHUB_TOKEN = $accessToken
        }
        else {
            throw "There was a problem obtaining an access token for '$org' (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"
    }
}