Save/Save-AppsInRepository.ps1
<#
.Synopsis Save-AppsInRepository is a function that saves apps in a repository. .Description This function takes in several parameters, performs operations like removing old app files, copying new ones, updating the version in AppSourceCop.json files, and creating a pull request in Azure DevOps. .Parameter PRName The name of the pull request to be created. .Parameter branchName The name of the branch where changes will be made. .Parameter appNames A comma-separated string of app names. These names are used to identify the app files to be removed and copied. .Parameter pathToPreviousapps The path to the directory where the previous versions of the apps are stored. .Parameter pathToArtifacts The path to the directory where the new versions of the apps are stored. .Parameter version This is the new version to be set in the AppSourceCop.json files. .Example Save-AppsInRepository -PRName "Update apps" -branchName "update-apps" -appNames "App1,App2" -pathToPreviousapps "./previousApps" -pathToArtifacts "./artifacts" #> function Save-AppsInRepository { param ( [Parameter(Mandatory=$true)] [string]$PRName, [Parameter(Mandatory=$true)] [string]$branchName, [Parameter(Mandatory=$true)] [string]$appNames, [Parameter(Mandatory=$true)] [string]$pathToPreviousapps, [Parameter(Mandatory=$true)] [string]$pathToArtifacts, [Parameter(Mandatory=$true)] [string]$version ) echo "SystemAccessToken: $env:SystemAccessToken" echo "SYSTEM_ACCESSTOKEN $env:SYSTEM_ACCESSTOKEN" cd $env:BUILD_SOURCESDIRECTORY az devops configure --defaults organization="$env:SYSTEM_COLLECTIONURI" project="$env:SYSTEM_TEAMPROJECT" --use-git-aliases true git config user.email "hosted.agent@dev.azure.com" git config user.name "Azure Pipeline" git fetch origin master git checkout master git checkout -b $branchName $appPaths = @($appNames.Split(',').Trim() | Foreach-Object { Get-ChildItem -Path (Join-Path $pathToPreviousapps ("*"+$_+"*.app"))| Remove-Item -Force -Recurse }) $appPaths = @($appNames.Split(',').Trim() | Foreach-Object { Get-ChildItem -Path (Join-Path $pathToArtifacts ("*"+$_+"*.app")) | Copy-Item -Destination $pathToPreviousapps }) $directories = Get-ChildItem -Path "$env:BUILD_SOURCESDIRECTORY" -Filter app -Recurse -Directory -ErrorAction SilentlyContinue foreach ($directory in $directories) { $file = Join-Path -Path $directory.FullName -ChildPath AppSourceCop.json try { $content = Get-Content -Path $file | ConvertFrom-Json if ($content.PSObject.Properties.Name -notcontains 'version') { throw "The 'version' line is missing in the file $file" } $content.version = "$version" $content | ConvertTo-Json | Set-Content -Path $file } catch { Write-Output "Error: $($_.Exception.Message)" } } git add . git commit -m $PRName 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 master --title $PRName --description $PRName } |