Save/SaveAppsInRepository.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.
#>

function SaveAppsInRepository {
    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
    )

    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
        git add "$pathToPreviousapps"
        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: $_"
    }
}