Files
powershell-scripts/ApiGit/ApiGitResources/ApiScriptsFunctions.ps1
T
marco.locatelli@steamware.net 674ce894b5 minor fix
2026-05-13 16:01:06 +02:00

82 lines
2.8 KiB
PowerShell

######### FUNZIONI COMUNI PER SCRIPT API GIT #########
#scrittura output & log
Function WriteLogOutput {
Param ($logFile, $logType, [string]$logString)
#compongo path per file di log
$logPath = Join-Path $logFolder $logFile
#scrivo su file la stringa se il tipo di log è > o uguale al livello richiesto
if ($logType -le $logLevel) {
Add-content $logPath -value "$logString"
#scrivo su terminale la stringa se $terminalOutput=1
if ($terminalOutput -eq 1) {
Write-Output($logString)
}
}
}
#verifica esistenza folder di log
Function CheckLogFolder {
if (Test-Path $logFolder) {
}
else {
New-Item $logFolder -ItemType Directory
}
}
#rotazione file .log in .old
Function RotateOldLog {
$logPath = Join-Path $logFolder $logFile
$oldLogPath = Join-Path $logFolder $oldLogFile
#se file .old esiste sposto contenuto di attuale .log in coda a attuale .old e cancello attuale .log
if (Test-Path $logPath){
if (Test-Path $oldLogPath) {
$from = Get-Content -Path $logPath
Add-Content -Path $oldLogPath -Value $from
Remove-Item -Path $logPath
}
#se file .old non esiste rinomino attuale file.log in file.old
else {
Get-ChildItem $logPath | Rename-Item -NewName { $_.Name -replace '.log','.old' }
}
}
}
#creazione nuovo mirror
Function FreshMirrorCreation {
Param ($projectNumber, $user, $auth, $destination, $path)
#compongo url da chiamare per creazione nuovo mirror
$callUrlCreateMirror = "https://gitlab.steamware.net/api/v4/projects/" + $projectNumber + "/remote_mirrors"
#creo url del nuovo mirror con username e token relativi a gitlab
$newMirror = "http://" + $user + ":" + $auth + "@" + $destination + "/" + $path
#creo body da convertire in json
$body =
@{
url = $newMirror
enabled = 1
}
#converto body in json prima di passarlo alla chiamata POST
$jsonBody = ConvertTo-Json -InputObject $body
#chiamata api POST che crea mirror con url e body specificati
$rebuildResponse = Invoke-WebRequest -Method Post -URI $callUrlCreateMirror -Headers $gitlabHead -ContentType "application/json" -Body $jsonBody -UseBasicParsing
#conversione da Json della risposta alla chiamata POST
$parsedRebuild = $rebuildResponse.Content | ConvertFrom-Json
#rilevo ID e URL nuovo mirror per scriverli nel log
foreach ($item in $parsedRebuild) {
$mirrorId = $($item.id)
$mirrorUrl = $($item.url)
}
#scrivo ID e URL nuovo mirror
WriteLogOutput $logFile 1 "NEW ID: $mirrorId - URL: $mirrorUrl - Mirror creato con successo"
}
Function TryParse-Json {
param([string]$InputString)
try {
return $InputString | ConvertFrom-Json -ErrorAction Stop
}
catch {
return $null
}
}