Module/DevOps/Initialize-BCSDevOpsRepository.ps1

<#
.SYNOPSIS
  Initialize a BrightCom Repository
 
.DESCRIPTION
  Updates a BrightCom App Repository with default values
  You will need
 
.PARAMETER projectName
  Name of the project where the repository is.
 
.PARAMETER repositoryName
  Name of the repository to create, if left empty repositoryName will be the same as projectName.
  
.PARAMETER projectNameShort
  Short name for the project 3-4 characters.
  
.PARAMETER appIdRangeFrom
  From ID ranges for the app
  
.PARAMETER appIdRangeTo
  From ID ranges for the app
 
.PARAMETER ciTenants
  Tenant names where the app will be deployed, upgraded and installed when the CI pipeline runs. Divide multiple tenants with comma (e.g. Tenant1, Tenant2). Omit to deploy to global scope
 
.PARAMETER cdTenants
  Tenant names where the app will be deployed, upgraded and installed when the Release pipeline runs. Divide multiple tenants with comma (e.g. Tenant1, Tenant2). Omit to deploy to global scope
 
.PARAMETER templateRepoUrl
  Url to template repository if different from the default repository
 
.PARAMETER sourcePat
  Personal Access Token
 
.EXAMPLE
  Initialize-BCSDevOpsRepository -projectName "MyProjectName" -repositoryName "MyRepositoryName" -projectNameShort "MPN" -appIdRangeFrom "50000" -appIdRangeTo "50099" -ciTenants "MyQATenant" -cdTenants "MyProdTenant" -sourcePat (Get-BCSSecureString -InputString "MyDevOpsPat")
 
.NOTES
    Author: Mathias Stjernfelt
    Website: http://www.brightcom.se
#>


function Initialize-BCSDevOpsRepository {
  Param (
    [Parameter(Mandatory = $false)]
    [string]$organisation = 'BrightComSolutions',
    [Parameter(Mandatory = $false)]
    [string]$projectName = "BrightCom Solutions",
    [Parameter(Mandatory = $false)]
    [string]$repositoryName,
    [Parameter(Mandatory = $true)]
    [string]$projectNameShort,
    [Parameter(Mandatory = $true)]
    [string]$appIdRangeFrom,
    [Parameter(Mandatory = $true)]
    [string]$appIdRangeTo,
    [Parameter(Mandatory = $false)]
    [string]$ciTenants,
    [Parameter(Mandatory = $false)]
    [string]$cdTenants,
    [Parameter(Mandatory = $false)]
    [string]$templateRepoUrl,
    [Parameter(Mandatory = $true)]
    [securestring]$sourcePat
  )

  try {
    if ([string]::IsNullOrEmpty($ciTenants)) {
      $ciTenants = ""
    }

    if ([string]::IsNullOrEmpty($cdTenants)) {
      $cdTenants = ""
    }

    if ([string]::IsNullOrEmpty($repositoryName)) {
      $repositoryName = $projectName
    }

    if ([string]::IsNullOrEmpty($templateRepoUrl)) {
      $templateRepoUrl = "https://$($organisation)@dev.azure.com/$($organisation)/BrightCom%20Solutions/_git/BCS%20AL%20Project%20Template"
    }
    
    $templatePathGuid = New-Guid
    $templatePath = "$env:TEMP\$templatePathGuid"

    #Check if repository exists, if not create it.
    $fullOrgUrl = "https://dev.azure.com/$organisation"
    $sourcePatPlainText = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($sourcePat))      
    $sourcePatPlainText | az devops login --org $fullOrgUrl
    $ErrorActionPreference = "continue"
    Write-Host "Checking if repository $repositoryName exists" -ForegroundColor Green
    $repoInfo = az repos show --repository $repositoryName --org $fullOrgUrl --project $projectName

    if (-not [string]::IsNullOrEmpty($repoInfo)) {
      throw "Repository $repositoryName already exists in DevOps project $projectName"
    }

    #Check if powershell-yaml is installed
    if (Get-Module -ListAvailable -Name "powershell-yaml") {
      Write-Host "powershell-yaml is installed." -ForegroundColor Green
    } 
    else {
      Write-Host "powershell-yaml is not installed. Installing it from Powershell Gallery." -ForegroundColor Green
      Install-Module -Name powershell-yaml -force
    }

    Write-Host "Downloading template files from $templateRepoUrl" -ForegroundColor Green
    Invoke-Utility "git" "clone" "$templateRepoUrl" "$templatePath"
    Remove-Item -Path "$templatePath\.git" -Recurse -Force

    $oldLocation = Get-Location
    if (Test-Path -Path $env:TEMP\$projectName) {
      Remove-Item -Path $env:TEMP\$projectName -Recurse -Force
    }

    Write-Host "Creating repository $repositoryName in DevOps project $projectName" -ForegroundColor Green
    az repos create --name $repositoryName --org $fullOrgUrl --project $projectName | Out-Null

    $repoUrl = [uri]::EscapeUriString("https://$organisation@dev.azure.com/$organisation/$projectName/_git/$repositoryName")
    $tempProjectPath = "$env:TEMP\$projectName"

    Write-Host "Downloading target repository" -ForegroundColor Green
    Invoke-Utility "git" "clone" "$repoUrl" "$tempProjectPath"
    Copy-Item -Path "$templatePath\*" -Destination $tempProjectPath -Recurse -ErrorAction Stop
    Set-Location "$tempProjectPath" -ErrorAction Stop

    # Update app\app.json
    $fileName = "App\app.json"
    $filecontent = Get-Content -Path $fileName | ConvertFrom-Json
    $filecontent.id = [guid]::NewGuid();
    $filecontent.name = $repositoryName;
    $filecontent.idRanges[0].from = $appIdRangeFrom;
    $filecontent.idRanges[0].to = $appIdRangeTo;

    Write-Host "Updating App/app.json" -ForegroundColor Green
    Write-Host "id `t`t $($filecontent.id)" -ForegroundColor Yellow
    Write-Host "name `t`t $($filecontent.name)" -ForegroundColor Yellow
    Write-Host "id range `t $appIdRangeFrom..$appIdRangeTo" -ForegroundColor Yellow

    $filecontent | ConvertTo-Json | Format-Json -Indentation 2 | out-file $filename

    #Rename Workspace file
    Write-Host "Renaming VS Code workspace file" -ForegroundColor Green
    Move-Item -Path "BCS AL Project Template.code-workspace" -Destination "$repositoryName.code-workspace"

    # Update test\app.json
    if (Test-Path -Path "Test\app.json") {
      $fileName = "Test\app.json"
      $filecontent = Get-Content -Path $fileName | ConvertFrom-Json

      $filecontent.id = [guid]::NewGuid();
      $filecontent.name = "$repositoryName - Test";
      $filecontent.idRanges[0].from = $testappIdRangeFrom;
      $filecontent.idRanges[0].to = $testAppIdRangeTo;

      Write-Host "Updating Test/app.json" -ForegroundColor Green
      Write-Host "id `t`t $($filecontent.id)" -ForegroundColor Yellow
      Write-Host "name `t`t $($filecontent.name)" -ForegroundColor Yellow
      Write-Host "id range `t $appIdRangeFrom..$appIdRangeTo" -ForegroundColor Yellow

      $filecontent | ConvertTo-Json | Format-Json -Indentation 2 | out-file $filename
    }

    # pte-qa-settings.json
    Write-Host "Updating CI Settings file" -ForegroundColor Green 
    $fileName = ".azureDevOps\pte-qa-settings.json"
    $filecontent = Get-Content -Path $fileName | ConvertFrom-Json
    $filecontent.name = "$projectNameShort";
    if (-not [string]::IsNullOrEmpty($ciTenants)) {
      $filecontent.deployments[0].DeployToTenants = $ciTenants.Split(',');
    }  

    Write-Host "name `t`t $projectNameShort" -ForegroundColor Yellow
    Write-Host "Tenants `t `"$ciTenants`"" -ForegroundColor Yellow
    $filecontent | ConvertTo-Json -Depth 3 | Format-Json -Indentation 2 | out-file $filename

    # pte-prod-settings.json
    Write-Host "Updating Release Settings file" -ForegroundColor Green
    $fileName = ".azureDevOps\pte-prod-settings.json"
    $filecontent = Get-Content -Path $fileName | ConvertFrom-Json
    $filecontent.name = "$projectNameShort";
    if (-not [string]::IsNullOrEmpty($cdTenants)) {
      $filecontent.deployments[0].DeployToTenants = $cdTenants.Split(',');
    }    
    
    Write-Host "name `t`t $projectNameShort" -ForegroundColor Yellow
    Write-Host "Tenants `t `"$cdTenants`"" -ForegroundColor Yellow   
    $filecontent | ConvertTo-Json -Depth 3 | Format-Json -Indentation 2 | out-file $filename

    # pte-prod-release.yml
    Write-Host "Updating Release Pipeline" -ForegroundColor Green
    $fileName = ".azureDevOps\pte-prod-release.yml";
    $filecontent = Get-Content -Path $fileName;
    $content = '';

    foreach ($line in $fileContent) {
      $content = $content + "`n" + $line;
    }

    $yaml = ConvertFrom-YAML $content -Ordered;
    $yaml.resources.pipelines[0].source = "$repositoryName - CI";

    Write-Host "CI Pipeline `t $repositoryName - CI" -ForegroundColor Yellow
    $yaml | ConvertTo-Yaml | out-file $filename;

    #Update DevOps Repo with new setup
    Write-Host "Uploading files to repository branch main" -ForegroundColor Green
    Invoke-Utility git add -A
    Invoke-Utility git checkout -b main
    Invoke-Utility git commit -m 'Initial commit'
    Invoke-Utility git push -u origin main

    Set-Location $oldLocation

    if (Test-Path -Path $env:TEMP\$projectName) {
      Remove-Item -Path $env:TEMP\$projectName -Recurse -Force
    }

    if (Test-Path -Path $templatePath) {
      Remove-Item -Path $templatePath -Recurse -Force
    }
  }
  catch {
    if (Test-Path -Path $env:TEMP\$projectName) {
      Remove-Item -Path $env:TEMP\$projectName -Recurse -Force
    }

    if (Test-Path -Path $templatePath) {
      Remove-Item -Path $templatePath -Recurse -Force
    }

    throw "An error occured: $_";
  }
}

Export-ModuleMember -Function Initialize-BCSDevOpsRepository