Save/Save-AppsInRepository.ps1
<#
.Synopsis Save Apps in Repository .Description The SaveAppsInRepository function is used to save apps in a repository using Azure DevOps and Git. It performs the following actions: - Configures Azure DevOps - Performs Git actions to create a new branch and add changes - Creates a pull request in Azure DevOps .Parameter prName The name of the pull request that will be used when creating the pull request and Git commit. .Parameter branchName The name of the Git branch that will be created for adding changes. .Parameter filterName The name of the filter that will be used when removing old apps. .Parameter pathToPreviousapps The path to the directory where previous apps are stored. .Parameter pathToArtifacts The path to the directory where artifacts are stored for copying. .Parameter SystemCollectionUri The URI of the Azure DevOps system collection. .Parameter SystemTeamProject The Azure DevOps system team project. .Parameter SystemAccessToken The Azure DevOps system access token. .Parameter BuildRepositoryName The name of the repository for creating the pull request. .Parameter folderNames An array of folder names. .Parameter version Version of the current build. #> function Save-AppsInRepository { param ( [Parameter(Mandatory=$true)] [string]$prName, [Parameter(Mandatory=$true)] [string]$branchName, [Parameter(Mandatory=$true)] [string]$filterName, [Parameter(Mandatory=$true)] [string]$pathToPreviousapps, [Parameter(Mandatory=$true)] [string]$pathToArtifacts, [Parameter(Mandatory=$true)] $SystemCollectionUri, [Parameter(Mandatory=$true)] $SystemTeamProject, [Parameter(Mandatory=$true)] $SystemAccessToken, [Parameter(Mandatory=$true)] $BuildRepositoryName, [Parameter(Mandatory=$true)] $folderNames [Parameter(Mandatory=$true)] $version ) try { az devops configure --defaults organization="$SystemCollectionUri" project="$SystemTeamProject" --use-git-aliases true } catch { Write-Error "An error occurred while configuring Azure DevOps: $_" } try { git fetch origin master git checkout master git checkout -b $branchName Get-ChildItem -Path "$pathToPreviousapps" -Filter $filterName | Remove-Item Copy-Item -Path $pathToArtifacts -Destination "$pathToPreviousapps" -Recurse foreach ($folderName in $folderNames) { $jsonFilePath = Join-Path $folderName 'app\AppSourceCop.json' if (Test-Path $jsonFilePath) { try { $jsonContent = Get-Content -Path $jsonFilePath -Raw | ConvertFrom-Json if ($null -eq $jsonContent.version) { throw "Field 'version' not found in the JSON file: $jsonFilePath" } $jsonContent.version = $version $jsonContent | ConvertTo-Json -Depth 32 | Set-Content -Path $jsonFilePath } catch { Write-Output "Error processing file '$jsonFilePath': $_" } } else { Write-Output "File not found: $jsonFilePath" } } git add . git commit -m $prName git push origin $branchName } catch { Write-Error "An error occurred while working with Git: $_" } try { $env:AZURE_DEVOPS_EXT_PAT = "$SystemAccessToken" az devops login --organization "$SystemCollectionUri" az repos pr create --repository "$BuildRepositoryName" --source-branch $branchName --target-branch master --title $prName --description $prName } catch { Write-Error "An error occurred while creating the pull request: $_" } } |