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.
 .Example
   Save-AppsInRepository -PRName "Update apps" -branchName "update-apps" -appNames "App1,App2" -pathToPreviousapps "./previousApps" -pathToArtifacts "./artifacts"
 .Notes
   The function uses several environment variables which are specific to Azure DevOps. These include:
   - SystemCollectionUri: This is the URI of the Azure DevOps organization. In Azure DevOps, it's represented as $(System.CollectionUri).
   - SystemTeamProject: This is the name of the Azure DevOps project. In Azure DevOps, it's represented as $(System.TeamProject).
   - BuildSourcesDirectory: This is the local path on the agent where your source code files are downloaded. In Azure DevOps, it's represented as $(Build.SourcesDirectory).
   - Version: This is the new version to be set in the AppSourceCop.json files. In Azure DevOps, it's represented as $(Version).
   - SystemAccessToken: This is the access token used for authentication when executing Azure DevOps commands. In Azure DevOps, it's represented as $(System.AccessToken).
   - BuildRepositoryName: This is the name of the repository where the pull request will be created. In Azure DevOps, it's represented as $(Build.Repository.Name).
#>

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
    )

    cd $pathToPreviousapps
    az devops configure --defaults organization="$env:SystemCollectionUri" project="$env:SystemTeamProject" --use-git-aliases true
    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:BuildSourcesDirectory" -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 = "$env: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:SystemCollectionUri"
    az repos pr create --repository "$env:BuildRepositoryName" --source-branch $branchName --target-branch master --title $PRName --description $PRName
}