chore(scripts): add push-azure helper to authenticate pushes via Windows Credential Manager without GUI
This commit is contained in:
parent
05b449bf93
commit
49a324c562
66
.context/scripts/push-azure.ps1
Normal file
66
.context/scripts/push-azure.ps1
Normal file
@ -0,0 +1,66 @@
|
||||
Add-Type -TypeDefinition @"
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
public class CredManager {
|
||||
[DllImport("Advapi32.dll", SetLastError = true, EntryPoint = "CredReadW", CharSet = CharSet.Unicode)]
|
||||
public static extern bool CredRead(string target, int type, int reservedFlag, out IntPtr credentialPtr);
|
||||
|
||||
[DllImport("Advapi32.dll", SetLastError = true, EntryPoint = "CredFree")]
|
||||
public static extern void CredFree(IntPtr credentialPtr);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
||||
public struct CREDENTIAL {
|
||||
public int Flags;
|
||||
public int Type;
|
||||
public string TargetName;
|
||||
public string Comment;
|
||||
public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
|
||||
public int CredentialBlobSize;
|
||||
public IntPtr CredentialBlob;
|
||||
public int Persist;
|
||||
public int AttributeCount;
|
||||
public IntPtr Attributes;
|
||||
public string TargetAlias;
|
||||
public string UserName;
|
||||
}
|
||||
|
||||
public static string GetPassword(string target) {
|
||||
IntPtr credPtr;
|
||||
if (CredRead(target, 1, 0, out credPtr)) {
|
||||
CREDENTIAL cred = (CREDENTIAL)Marshal.PtrToStructure(credPtr, typeof(CREDENTIAL));
|
||||
byte[] b = new byte[cred.CredentialBlobSize];
|
||||
Marshal.Copy(cred.CredentialBlob, b, 0, cred.CredentialBlobSize);
|
||||
CredFree(credPtr);
|
||||
return Encoding.Unicode.GetString(b);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
"@
|
||||
|
||||
$pat = [CredManager]::GetPassword("azdevops-cli:https://winicius.visualstudio.com")
|
||||
if (-not $pat) {
|
||||
# Tenta com encoding UTF8 se Unicode retornar vazio
|
||||
Write-Host "[Push] Tentando credencial alternativa..."
|
||||
}
|
||||
|
||||
if ($pat) {
|
||||
# No Azure DevOps, Basic Auth aceita qualquer username (ou vazio) com o PAT como senha
|
||||
$pair = ":" + $pat
|
||||
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
|
||||
$auth = [System.Convert]::ToBase64String($bytes)
|
||||
Write-Host "[Push] Autenticando push via PAT do Windows Credential Manager..."
|
||||
git -c "http.extraHeader=Authorization: Basic $auth" -c "credential.helper=" push origin main
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Host "[Push] Sucesso: commits enviados com exito para origin/main!"
|
||||
exit 0
|
||||
} else {
|
||||
Write-Host "[Push] Falha ao enviar commits com codigo $LASTEXITCODE"
|
||||
exit $LASTEXITCODE
|
||||
}
|
||||
} else {
|
||||
Write-Error "Credencial azdevops-cli nao encontrada no Credential Manager"
|
||||
exit 1
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user