138 lines
4.5 KiB
PowerShell
138 lines
4.5 KiB
PowerShell
################START SCRIPT################
|
|
|
|
#rilevo time per log inizio analisi
|
|
$startTime = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
|
|
|
|
# avvio stopwatch
|
|
$mainStopWatch = [system.diagnostics.stopwatch]::StartNew()
|
|
|
|
#access token per autenticazione
|
|
#creato da profilo marco.locatelli@egalware.com su gitlab.steamware.net
|
|
#creazione 04 gennaio 2024, scadenza 31 dicembre 2024
|
|
$gitlabHead = @{"PRIVATE-TOKEN"="glpat-VjT_SAsBk3s-yWE1LDUF"}
|
|
|
|
#access token per autenticazione
|
|
#creato da profilo replica su gitea2.steamware.net
|
|
$giteaHead = @{"Authorization"="token 21c3e674ae369eda349d0e1fadaddd526f90419d"}
|
|
|
|
#contatore ciclo do while
|
|
$projectCount = 1
|
|
|
|
#numero massimo di progetti da analizzare
|
|
$projectNumber = 200
|
|
|
|
#livello di log: 0=log sintetico, 1=log errori, 2=log full, 3=log ampolloso
|
|
$logLevel = 2
|
|
|
|
#output a terminale: 0=disattivo, 1=abilitato
|
|
$terminalOutput = 1
|
|
|
|
#cartella file di log
|
|
$GitLogFolder="C:\Steamware\Logs\Gitlab\"
|
|
|
|
#nome file di log
|
|
$logFile = Join-Path $GitLogFolder "GiteaReposCreation.log"
|
|
|
|
#dichiaro funzione per scrittura output
|
|
Function WriteLogOutput
|
|
{
|
|
Param ($logType, [string]$logString)
|
|
#scrivo su file la stringa se il tipo di log è > o = al livello di log richieeso
|
|
if($logType -le $logLevel)
|
|
{
|
|
Add-content $logFile -value "$logString"
|
|
#scrivo su terminale la stringa se $terminalOutput=1
|
|
if($terminalOutput -eq 1)
|
|
{
|
|
Write-Output($logString)
|
|
}
|
|
}
|
|
}
|
|
|
|
#creazione folder di Log se non già esistente
|
|
if (Test-Path $GitLogFolder)
|
|
{
|
|
}
|
|
else
|
|
{
|
|
New-Item $GitLogFolder -ItemType Directory
|
|
}
|
|
|
|
#scrivo intestazione e inizio analisi
|
|
WriteLogOutput 0 "--------------------"
|
|
Switch ($logLevel)
|
|
{
|
|
0 {WriteLogOutput 0 "LOG SINTETICO GITEA"}
|
|
1 {WriteLogOutput 0 "LOG ERRORI GITEA"}
|
|
2 {WriteLogOutput 0 "LOG FULL GITEA"}
|
|
3 {WriteLogOutput 0 "LOG AMPOLLOSO GITEA"}
|
|
}
|
|
WriteLogOutput 0 ""
|
|
WriteLogOutput 0 "Percorso log: $GitLogFolder"
|
|
WriteLogOutput 0 ""
|
|
WriteLogOutput 0 "Inizio Esecuzione Script: $startTime"
|
|
|
|
#ciclo principale do/while che cicla da 1 a N projectNumber
|
|
do
|
|
{
|
|
$callGitlabUrl = "https://gitlab.steamware.net/api/v4/projects/" + $projectCount
|
|
try
|
|
{
|
|
$gitlabResponse = Invoke-WebRequest -URI $callGitlabUrl -Headers $gitlabHead -ContentType "application/json" -UseBasicParsing
|
|
$parsedGitlabResponse = $gitlabResponse.Content | ConvertFrom-Json
|
|
#scrivo il numero del progetto nel terminale e su file
|
|
WriteLogOutput 1 ""
|
|
WriteLogOutput 1 "--------------------"
|
|
WriteLogOutput 1 "**PROGETTO $projectCount**"
|
|
foreach($gitlabItem in $parsedGitlabResponse)
|
|
{
|
|
WriteLogOutput 1 "Gitlab Project Name: $($gitlabItem.name)"
|
|
$callGiteaUrl = "https://gitea.steamware.net/api/v1/orgs/Egalware/repos"
|
|
#dichiaro un body da convertire in JSON con il nome del repo da creare
|
|
$giteaBody =
|
|
@{
|
|
name = $($gitlabItem.name);
|
|
}
|
|
# Converting my hash to json format
|
|
$GiteaJSON = $giteaBody | ConvertTo-Json
|
|
try
|
|
{
|
|
$giteaResponse = Invoke-WebRequest -URI $callGiteaUrl -Method POST -Headers $giteaHead -ContentType "application/json" -Body $GiteaJSON
|
|
$parsedGiteaResponse = $giteaResponse.Content | ConvertFrom-Json
|
|
foreach($giteaItem in $parsedGiteaResponse)
|
|
{
|
|
WriteLogOutput 1 "Gitea New Project Name: $($giteaItem.name)"
|
|
WriteLogOutput 1 "Status: $($giteaResponse.StatusDescription)"
|
|
WriteLogOutput 1 "When: $($giteaItem.created_at)"
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
}
|
|
#scrivo se trovo un errore (in particolare progetto non trovato) durante il try/catch (solo se loglevel è = 3)
|
|
catch
|
|
{
|
|
WriteLogOutput 3 ""
|
|
WriteLogOutput 3 "--------------------"
|
|
WriteLogOutput 3 "**PROGETTO $projectCount NON ESISTENTE**"
|
|
}
|
|
$projectCount=$projectCount+1
|
|
}
|
|
#fine ciclo principale
|
|
while($projectCount -le $projectNumber)
|
|
|
|
# fermo stopwatch e calcolo durata script
|
|
$mainStopWatch.Stop()
|
|
$durataScript = $mainStopWatch.Elapsed.TotalSeconds
|
|
|
|
#rilevo time per log fine analisi
|
|
$endTime = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
|
|
|
|
#scrivo a video le statistiche
|
|
WriteLogOutput 0 ""
|
|
WriteLogOutput 0 "Fine Esecuzione Script: $endTime"
|
|
WriteLogOutput 0 ""
|
|
WriteLogOutput 0 "Durata Esecuzione Script: $durataScript secondi"
|
|
WriteLogOutput 0 "" |