Update/Update-TranslationFiles.ps1

<#
 .Synopsis
   Update-TranslationFiles is a function that updates translation files in a repository.
 .Description
   This function downloads translation files, extracts them, copies them to the appropriate locations in the repository, and creates a pull request in Azure DevOps.
 .Parameter authContext
   A hashtable containing authentication context, including the AccessToken.
 .Parameter pathToRepo
   The path to the repository where the translation files will be copied. Default is the build sources directory.
 .Parameter configPath
   The path to the configuration file (JSON) that contains repository and application information.
   Example:
    {
        "repositories": [
            {
                "name": "SA Extensions",
                "applications": [
                    {
                        "name": "SMART eReporting for Ukraine",
                        "path": "SMART eReporting for Ukraine/app/Translations"
                    }
                ]
            }
        ]
    }
 .Parameter branchName
   The name of the branch where changes will be made.
 .Parameter translationApps
   A string containing the names of the translation apps to be processed.
 .Parameter targetBranch
   (Optional) The name of the target branch for the pull request. Defaults to "master".
 .Example
   Update-TranslationFiles -authContext $authContext -configPath "./config.json" -branchName "update-translations" -translationApps "App1,App2"
#>


function Update-TranslationFiles {
    param (
        [Parameter(Mandatory=$true)]
        [hashtable]$authContext,
        [Parameter(Mandatory=$false)]
        [string]$pathToRepo = "$env:BUILD_SOURCESDIRECTORY",
        [Parameter(Mandatory=$true)]
        [string]$configPath,
        [Parameter(Mandatory=$true)]
        [string]$branchName,
        [Parameter(Mandatory=$true)]
        [string]$translationApps,
        [string]$worksItemID,
        [string]$targetBranch = "master"   # optional, defaults to master
    )

    $appPaths = @()
    $token = $authContext.AccessToken

    $headers = @{
        "Content-Type"  = "application/json"
        "Authorization" = "Bearer $token"
    }
    $body = @{ "projects" = $translationApps } | ConvertTo-Json

    $tempPath = [System.IO.Path]::GetTempPath()
    $filePath = [System.IO.Path]::Combine($tempPath, "file.zip")

    try {
        $response = Invoke-RestMethod -Uri 'https://navdev.smartcloud.com.ua:7148/translationtool/ODataV4/translationapi_getTranslationFiles' -Method Post -Headers $headers -Body $body
        [System.IO.File]::WriteAllBytes($filePath, [System.Convert]::FromBase64String($response.value))
    } catch {
        Write-Error "Error downloading translation files: $_"
        return
    }

    $newTempDir = [System.IO.Path]::Combine($tempPath, [System.Guid]::NewGuid().ToString())
    try {
        Add-Type -AssemblyName System.IO.Compression.FileSystem
        [System.IO.Directory]::CreateDirectory($newTempDir) | Out-Null
        [System.IO.Compression.ZipFile]::ExtractToDirectory($filePath, $newTempDir)
        Write-Host "Archive extracted to $newTempDir"
    } catch {
        Write-Error "Error during archive extraction: $_"
        return
    }

    $config = Get-Content -Raw -Path $configPath | ConvertFrom-Json

    Write-Host "##[group]Copying translation files"
    Get-ChildItem -Path $newTempDir -Filter *.xlf | ForEach-Object {
        $file = $_
        $appName = ($file.Name -split '\.')[0]
        Write-Host "Processing app: $appName"

        $appPath = $config.repositories | ForEach-Object {
            $_.applications | Where-Object { $_.name -eq $appName } | ForEach-Object {
                Join-Path -Path $pathToRepo -ChildPath $_.path
            }
        } | Select-Object -First 1

        if ($appPath) {
            $destinationPath = Join-Path -Path $appPath -ChildPath $file.Name
            try {
                Copy-Item -Path $file.FullName -Destination $destinationPath -Force
                if (Test-Path -Path $destinationPath) {
                    $appPaths += $destinationPath
                    Write-Host "Copied $file.Name to $destinationPath"
                }
            } catch {
                Write-Error "Error copying $file.Name to $destinationPath : $_"
            }
        } else {
            Write-Warning "No matching app found for $file.Name"
        }
    }
    Write-Host "##[endgroup]"

    Set-Location -Path $pathToRepo

    Write-Host "##[group]Updating translation files in Git"
    try {
        git config user.email "hosted.agent@dev.azure.com"
        git config user.name "Azure Pipeline"
        git fetch origin $targetBranch
        git checkout -b $branchName origin/$targetBranch
        git add .
        git commit -m "Updated translation files for $branchName"
        git push origin $branchName
        $env:AZURE_DEVOPS_EXT_PAT = "$env:SystemAccessToken"
        az devops login --organization "$env:SYSTEM_COLLECTIONURI"
        az repos pr create `
          --repository "$env:BUILD_REPOSITORY_NAME" `
          --source-branch $branchName `
          --target-branch $targetBranch `
          --title $branchName `
          --description "Update translations for $branchName" `
          --work-items $worksItemID
    } catch {
        Write-Error "Error updating Git repository: $_"
    }
    Write-Host "##[endgroup]"
}