public/git/Add-PSTSGitRepositoryPush.ps1

function Add-PSTSGitRepositoryPush
{
    param(
         [Parameter(Mandatory=$true)][string] $project,
         [Parameter(Mandatory=$true)][string] $repoName,
         [Parameter(Mandatory=$true)][string] $branch,
         [Parameter(Mandatory=$true)][string] $comments,
         [string] $fileName,

         [Parameter(Mandatory=$true, ParameterSetName="set1")][string] $localPath,

         [Parameter(Mandatory=$true, ParameterSetName="set2")][string] $content,

         [Parameter(Mandatory=$true, ParameterSetName="set3")][string] $sourceRepository,
         [Parameter(Mandatory=$true, ParameterSetName="set3")][string] $sourceProject,
         [Parameter(Mandatory=$true, ParameterSetName="set3")][string] $sourceFolder,
         [Parameter(Mandatory=$true, ParameterSetName="set3")][string] $sourceBranch
    )

    $id = $(Get-PSTSGitRepository -project $project | Where-Object {$_.name -eq $repoName} | Select-Object -ExpandProperty id )

    if ([String]::IsNullOrEmpty($id))
    {
        Write-Error "$repoName doesn't exist in $project !!"
        return 128
    }
    else
    {
        Write-Debug "$repoName exists in $project"
        $newObjectId = Get-PSTSGitRepositoryRef -project $project -id "$id" | Where-Object {$_.name -eq "refs/heads/$branch" }| Select-Object -ExpandProperty objectId

        [GitCommitRef]$commit = [GitCommitRef]::new()
        $commit.comment = $comments

        [GitChange[]]$changes = @()



        if ($PSCmdlet.ParameterSetName -eq "set1" ) {
            $content = Get-content -LiteralPath $localPath -Raw
        }

        if ($PSCmdlet.ParameterSetName -eq "set2" -or $PSCmdlet.ParameterSetName -eq "set1") {
            $change = _buildGitCommitRefChange -fileContent $content -gitFileName $fileName
            $changes+=$change

        }
        else
        {
            $changes= parseFolder -project $sourceProject -repository $sourceRepository -folderPath $sourceFolder -branch $sourceBranch
        }

        $commit.changes = $changes

        $data = [GitPushItem]::new()


        $refUpdate= [GitrefUpdate]::new()
        $refUpdate.name = "refs/heads/$branch"
        $refUpdate.oldObjectId=$newObjectId
        $data.refUpdates = @($refUpdate)

        $data.commits=@($commit)

        $content = `
        _PSTSAPI `
            -resource "git/repositories/$id/pushes" `
            -method "POST" `
            -apiversion "api-version=5.0" `
            -body $($data |ConvertTo-JsonNewtonsoft)  `
            -contentType "application/json"

        return [Object]$content
   }

}

function parseFolder {
    param (
        [string]$project,
        [string]$repository,
        [string]$folderPath,
        [string]$branch )

    $null =@(
    [GitChange[]]$changes = @()

    $resp = Get-PSTSGitRepositoryRefItemBatch -project $project -repository $repository -folderPath $folderPath -branch $branch


    foreach ($item in $resp)  {
        if ($item.path -ne $folderPath) {
            Write-Output $item.path

            if ($item.isFolder) {
                $changes+=ParseFolder -project $project -repository $repository -branch $branch -folderPath $item.path
            }
            else {
                $file = Get-PSTSGitRepositoryRefItemFile -project $project -repository $repository -filePath $item.Path -branch $branch
                $changes+=_buildGitCommitRefChange -fileContent $file.content -gitFileName $item.Path
            }
        }
    }
    )
    return $changes

}

function _buildGitCommitRefChange {
    param(
      [Parameter(Mandatory=$true)] [string] $fileContent,
      [Parameter(Mandatory=$true)] [string] $gitFileName
    )

    $null= @(
        $change = [GitChange]::new()
        $change.changeType = "add"

        $item = [GitCommitRefChangeItem]::new()
        $item.path = $gitFileName

        $change.item = $item

        $gitItemContent = [GitItemContent]::new()
        $gitItemContent.contentType= "rawText"
        $gitItemContent.content = $fileContent -replace [regex]::escape("\"), "\\" -replace [regex]::escape("`""), "\`""
        $change.newContent=$gitItemContent

    )
    return $change
}