Private/Connect-SourceControl.ps1

#
# Connect-SourceControl.ps1
#

function Connect-SourceControl {
    $message = "Configuring Source Control"
    Write-Host $message

    try {
        Configure-SourceControl
    }
    catch {
        Write-Host $_
        $global:devops_projectFile.SourceControlConfigured = "Error"
        pause
    }
    $global:devops_projectFile | ConvertTo-Json | Out-FileUtf8NoBom ("$global:devops_projectLocation\$global:devops_gitRepo.json")  
   
}

function Configure-SourceControl {
    $scOrg = $global:devops_projectFile.OrgName
    $scProject = $global:devops_projectFile.Project
    $scRepo = $global:devops_projectFile.gitRepo.Replace(' ', '')

    try {
        if ($scProject -eq "" -or $scRepo -eq "" -or $scOrg -eq "") {
            $message = "Select a Source Control Provider"
            $options = "Azure DevOps", "Github", "Quit"
            do {
                $sel = Invoke-Menu -MenuTitle "---- $message ------" -MenuOptions $options
            } until ($sel -ge 0)

            try {
                switch ($sel) {
                    '0' { Install-DevOps }
                    '1' { Write-Host "GitHub - Coming Soon !!!!" }
                    '2' { return }
                }     
                pause          
            }
            catch {
                Write-Host $_
                pause
                return
            }
        }
        else {
            Write-Host Source Control is already configured
            pause
        }
    }
    catch {
        Write-Host $_
        $global:devops_projectFile.SourceControlConfigured = "Error"
        $global:devops_projectFile | ConvertTo-Json | Out-FileUtf8NoBom ("$global:devops_projectLocation\$global:devops_gitRepo.json") 
        pause
    } 
}

function Install-DevOps {

    $message = "Connecting to Azure DevOps Organisation"
    
    if ($scOrg -eq "") {
        do {
            Write-Host "Enter the <NAME> of your Azure DevOps Organisation only the NAME - not the full path!"
            $scOrg = Read-Host -Prompt "Depending on your instance, either dev.azure.com/<NAME> or <NAME>.visualstudio.com"
        }until ($scOrg -ne "")   
        
    }

    #Get Azure Accounts / Subscriptions
    Get-AzureAccounts("Azure DevOps")

    #Get Access Token
    $azureDevopsResourceId = "499b84ac-1321-427f-aa17-267ca6975798"
    $token = az account get-access-token --resource $azureDevopsResourceId | ConvertFrom-Json
    $authValue = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":" + $token.accessToken))  
    $env:AZURE_DEVOPS_EXT_PAT = $token.accessToken  

    Write-Host "Azure Subscription - $global:devops_selectedSubscriptionName"
 
    #$global:devops_projectFile.selectedSubscription = $global:devops_selectedSubscription
    #$global:devops_projectFile.selectedSubscriptionName = $global:devops_selectedSubscriptionName
    #az account set --subscription $global:devops_selectedSubscription

    Write-Host ""
    [console]::ForegroundColor = "White"
    if ($scProject -ne "") {
        $azproj = az devops project show --organization https://dev.azure.com/$scOrg --project $scProject | ConvertFrom-Json
        [console]::ForegroundColor = "White"
    }
    
    if ($scProject -eq "" -or !$azproj.url) {
        $msg = "Select existing or Create a new Azure DevOps Project"

        do {
            $sel = Invoke-Menu -MenuTitle "----$msg ------" -MenuOptions "Select", "Create", "Quit"
            
        } until ($sel -ge 0)

        if ($sel -eq 2) {
            return
        }

        if ($sel -eq 1) {
            if ($scProject -eq "") {
                do {
                    $scProject = Read-Host -Prompt "Please enter the Name of the Project you wish to Create"
                }until ($scProject -ne "")   
            }
            $scProject = $scProject.Replace(" ", "")        
        
            $message = "Creating DevOps Project $scProject"
            Write-Host $message
            $output = az devops project create --name $scProject --organization=https://dev.azure.com/$scOrg --process Scrum
            if (!$output) {
                Write-Host "There was a problem creating the Project, please try the process again and Ensure you Organisation URL is correct" -ForegroundColor Red
                $global:devops_projectFile.SourceControlConfigured = "Error"   
                $global:devops_projectFile | ConvertTo-Json | Out-FileUtf8NoBom ("$global:devops_projectLocation\$global:devops_gitRepo.json") 
                return 
            }     
            else {
                $global:devops_projectFile.Project = $scProject  
                $global:devops_projectFile.OrgName = $scOrg
                $global:devops_projectFile.DevOpsProduct = "Azure DevOps"
                $global:devops_projectFile | ConvertTo-Json | Out-FileUtf8NoBom ("$global:devops_projectLocation\$global:devops_gitRepo.json") 
             
                $scCreate = $true
            }           
        }
        else {
            $selection = az devops project list --organization=https://dev.azure.com/$scOrg --query '[value][].{Name:name}' --output json | Out-String | ConvertFrom-Json
            if (!$selection) {
                Write-Host "There was a problem connecting, please try the process again and Ensure you Organisation URL is correct" -ForegroundColor Red
                $global:devops_projectFile.SourceControlConfigured = "Error"   
                $global:devops_projectFile | ConvertTo-Json | Out-FileUtf8NoBom ("$global:devops_projectLocation\$global:devops_gitRepo.json") 
                return 
                
            }
            $options = $selection | ForEach-Object { "$($_.Name)" }
            do {
                $sel = Invoke-Menu -MenuTitle "---- Select the Project you wish to Use ------" -MenuOptions $options
                $scProject = $selection[$sel].Name 
            } until ($scProject -ne "")
            $global:devops_projectFile.Project = $scProject  
            $global:devops_projectFile.OrgName = $scOrg
            $global:devops_projectFile.DevOpsProduct = "Azure DevOps"
        }
        $global:devops_projectFile | ConvertTo-Json | Out-FileUtf8NoBom ("$global:devops_projectLocation\$global:devops_gitRepo.json") 
    }

    if (($scRepo -eq $scProject) -and $scCreate) {
        $repo = az repos show --repository $scRepo --project $scProject --organization=https://dev.azure.com/$scOrg | Out-String | ConvertFrom-Json
    }
    else {
        Write-Host ""

        #Check for blank repo
        if ($scRepo -eq "") {
            do {
                $scRepo = Read-Host -Prompt "Please enter a Name for the Git Repository you wish to Create"
            }until ($scRepo -ne "")
        }

        $message = "Creating Git Repo $scRepo"
        Write-Host $message
      
        $repo = az repos create --name $scRepo --project $scProject --organization=https://dev.azure.com/$scOrg | Out-String | ConvertFrom-Json
        if (!$repo.WebUrl) {
            $global:devops_projectFile.SourceControlConfigured = "Error"
            $global:devops_projectFile | ConvertTo-Json | Out-FileUtf8NoBom ("$global:devops_projectLocation\$global:devops_gitRepo.json") 
            Throw "Error"
        }
        else {
            $global:devops_projectFile.gitRepo = $scRepo
        }
    }

    #Get Access Token
    $azureDevopsResourceId = "499b84ac-1321-427f-aa17-267ca6975798"
    $token = az account get-access-token --resource $azureDevopsResourceId | ConvertFrom-Json
    $authValue = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":" + $token.accessToken))  
    $env:AZURE_DEVOPS_EXT_PAT = $token.accessToken  

    git remote remove origin
    git remote add origin $repo.webUrl
    $REPO_URL = $repo.webUrl

    git config http.$REPO_URL.extraHeader "Authorization:Basic $authValue"

    git add -A
    git commit -m "Initial Commit to Source Control Provider"
    git push --set-upstream origin master

    az repos show --repository $repo.id --open

    $global:devops_projectFile.SourceControlConfigured = "True"
    $global:devops_projectFile | ConvertTo-Json | Out-FileUtf8NoBom ("$global:devops_projectLocation\$global:devops_gitRepo.json") 

}

function Sync-Solution {
    try {
        git add -A
        [string]$result = git status
        if ($result.Contains("nothing to commit") -eq $false) {
            $commitMessage = Read-Host "Enter a description for your Commit"       
            git commit -m $commitMessage  
        }
          
        if ($global:devops_projectFile.SourceControlConfigured -eq "True") {
            if ($global:devops_projectFile.DevOpsProduct -eq "Azure Devops") {
            #Get Access Token
            Write-Host "Getting Updated Access Token"
            $azureDevopsResourceId = "499b84ac-1321-427f-aa17-267ca6975798"
            $token = az account get-access-token --resource $azureDevopsResourceId | ConvertFrom-Json
            $authValue = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":" + $token.accessToken))  
            $env:AZURE_DEVOPS_EXT_PAT = $token.accessToken  
    
            $REPO_URL = git remote get-url origin
            Write-Host "git URL is $REPO_URL"
    
            git config http.$REPO_URL.extraHeader "Authorization:Basic $authValue"
            Write-Host "Updated Auth Token"
            }
            
            Write-Host "Pulling latest from Source"
            git pull
              
            if ($commitMessage) {
                git push origin    
            }            
        }
        pause
            
    }
    catch {
        Write-Host $_
        pause        
    }
}

function Show-SolutionChanges {
    try {
        git status
        pause
            
    }
    catch {
        Write-Host $_
        pause        
    }
}