Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🩹 [Patch]: Adjust git config for GitHub App Installations #263

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
[string[]] $Events

# The target type of the installation.
[string] $TargetType
[string] $InstallationType

# The target login of the installation.
[string] $TargetName
# The target login or slug of the installation.
[string] $InstallationName

# Simple parameterless constructor
InstallationGitHubContext() {}
Expand Down
18 changes: 9 additions & 9 deletions src/functions/private/Auth/Context/Set-GitHubContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ function Set-GitHubContext {
}
if ($script:GitHub.EnvironmentType -eq 'GHA') {
$gitHubEvent = Get-Content -Path $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json
$targetType = $gitHubEvent.repository.owner.type
$targetName = $gitHubEvent.repository.owner.login
$installationType = $gitHubEvent.repository.owner.type
$installationName = $gitHubEvent.repository.owner.login
$enterprise = $gitHubEvent.enterprise.slug
$organization = $gitHubEvent.organization.login
$owner = $gitHubEvent.repository.owner.login
Expand All @@ -98,7 +98,7 @@ function Set-GitHubContext {
Write-Debug "Organization: $organization"
Write-Debug "Repository: $repo"
Write-Debug "Repository Owner: $owner"
Write-Debug "Repository Owner Type: $targetType"
Write-Debug "Repository Owner Type: $installationType"
Write-Debug "Sender: $gh_sender"
if ([string]::IsNullOrEmpty($contextObj['Enterprise'])) {
$contextObj['Enterprise'] = [string]$enterprise
Expand All @@ -109,17 +109,17 @@ function Set-GitHubContext {
if ([string]::IsNullOrEmpty($contextObj['Repo'])) {
$contextObj['Repo'] = [string]$repo
}
if ([string]::IsNullOrEmpty($contextObj['TargetType'])) {
$contextObj['TargetType'] = [string]$targetType
if ([string]::IsNullOrEmpty($contextObj['InstallationType'])) {
$contextObj['InstallationType'] = [string]$installationType
}
if ([string]::IsNullOrEmpty($contextObj['TargetName'])) {
$contextObj['TargetName'] = [string]$targetName
if ([string]::IsNullOrEmpty($contextObj['InstallationName'])) {
$contextObj['InstallationName'] = [string]$installationName
}
$contextObj['Name'] = "$($contextObj['HostName'])/$($contextObj['Username'])/" +
"$($contextObj['TargetType'])/$($contextObj['TargetName'])"
"$($contextObj['InstallationType'])/$($contextObj['InstallationName'])"
} else {
$contextObj['Name'] = "$($contextObj['HostName'])/$($contextObj['Username'])/" +
"$($contextObj['TargetType'])/$($contextObj['TargetName'])"
"$($contextObj['InstallationType'])/$($contextObj['InstallationName'])"
}
}
'App' {
Expand Down
10 changes: 5 additions & 5 deletions src/functions/public/Auth/Connect-GitHubApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,20 @@
InstallationID = [string]$installation.id
Permissions = [pscustomobject]$installation.permissions
Events = [string[]]$installation.events
TargetType = [string]$installation.target_type
InstallationType = [string]$installation.target_type
Token = [securestring]$token.Token
TokenExpirationDate = [datetime]$token.ExpiresAt
}

switch ($installation.target_type) {
$contextParams['InstallationName'] = switch ($installation.target_type) {
'User' {
$contextParams['TargetName'] = [string]$installation.account.login
[string]$installation.account.login
}
'Organization' {
$contextParams['TargetName'] = [string]$installation.account.login
[string]$installation.account.login
}
'Enterprise' {
$contextParams['TargetName'] = [string]$installation.account.slug
[string]$installation.account.slug
}
}
Write-Verbose 'Logging in using a managed installation access token...'
Expand Down
22 changes: 14 additions & 8 deletions src/functions/public/Git/Set-GitHubGitConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,22 @@
$id = $Context.DatabaseID
$token = $Context.Token | ConvertFrom-SecureString -AsPlainText
$hostName = $Context.HostName
$installationName = $Context.InstallationName

if ($PSCmdlet.ShouldProcess("$Name", 'Set Git configuration')) {
Write-Verbose "git config --local user.name '$username'"
git config --local user.name "$username"

Write-Verbose "git config --local user.email '[email protected]'"
git config --local user.email "[email protected]"

Write-Verbose "git config --local 'url.https://oauth2:$token@$hostName.insteadOf' 'https://$hostName'"
git config --local "url.https://oauth2:$token@$hostName.insteadOf" "https://$hostName"
$git = 'git'
@(
@('config', '--local', 'user.name', "$username"),
@('config', '--local', 'user.email', "[email protected]"),
@('config', '--local', "url.https://oauth2:$token@$hostName/$installationName.insteadOf",
"https://$hostName/$installationName")
) | ForEach-Object {
Write-Verbose "$git $($_ -join ' ')"
& $git $_
if ($LASTEXITCODE -ne 0) {
throw "Failed to run git command. ($LASTEXITCODE)"
}
}
}
} catch {
throw $_
Expand Down
Loading