RDG.Deployment.Utils.psm1

function Update-RDGDeploymentUtils {
  $moduleName = "RDG.Deployment.Utils"
  $repository = "PSGallery"

  # Check if the module is installed
  $installedModule = Get-Module -Name $moduleName -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1

  if ($installedModule) {
      # Get information about the latest version of the module available in the repository
      $latestModule = Find-Module -Name $moduleName -Repository $repository | Sort-Object Version -Descending | Select-Object -First 1

      if ($latestModule.Version -gt $installedModule.Version) {
          # An update is available
          Write-Host "Updating $moduleName to version $($latestModule.Version)..."
          Update-Module -Name $moduleName -Force -Scope CurrentUser
          Write-Host "$moduleName has been updated to version $($latestModule.Version)."
      } else {
          Write-Host "$moduleName is already up to date (Version $($installedModule.Version))."
      }
  } else {
      # The module is not installed
      Write-Host "$moduleName is not installed. Installing it..."
      Install-Module -Name $moduleName -Repository $repository -Scope CurrentUser -Force
      Write-Host "$moduleName has been installed."
  }
}

function New-AzAuthentication {
  param(
    [string]$subscriptionId
  )


  Show-Title -title "Authenticate with Azure" -color "Yellow"
  if ($null -ne $env:AGENT_ID -and $null -ne $env:SYSTEM_TASKDEFINITIONSURI) {
    # Having the $env:AGENT_ID and $env:SYSTEM_TASKDEFINITIONSURI variables set means we are running in Azure Pipeline.
    # Authentication with service principal is done by the AzureCli task in Azure Pipeline
    Write-Host "Using service principal authentication"
  } else {
    # User login
    Write-Host "Using user authentication"
    $currentSubscriptionId = az account show --query "id" -o tsv
    if ($currentSubscriptionId -ne $subscriptionId) {
      az login | Out-Null
    }
  }

  az account set --subscription $subscriptionId | Out-Null
  Write-Host "Authenticated!" -ForegroundColor "Green"
}

function Get-Scripts {
  param(
    [bool]$delta,
    [string]$manifestPath,
    [string]$retailer,
    [string]$environment
  )

  $manifest = Get-Content -Raw -Path $manifestPath | ConvertFrom-Json
  if ($delta) {
    $changedScripts = Get-Changed -retailer $retailer -environment $environment
    $scripts = $manifest.deploy.scripts | Where-Object { $_ -in $changedScripts }
  }
  else {
    $scripts = $manifest.deploy.scripts
  }

  return $scripts
}

function Get-Changed {
  param(
    [string]$type = 'deploy',
    [string]$retailer,
    [string]$environment
  )
  # Get the root folder of your Git repository
  $repoRoot = git rev-parse --show-toplevel

  # Get the commit hashes for the current and previous commits
  $currentCommit = git rev-parse HEAD
  $previousCommit = git rev-parse HEAD^ # Get previous tag instead of commit -1

  # Define the prefix of the Git tag you want to search for
  $tagPrefix = "$retailer-$environment*"
  # Get a list of all tags that start with the specified prefix
  $matchingTags = git tag --list $tagPrefix

  # If there are matching tags, find the most recent commit hash among them
  if ($matchingTags.Count -gt 0) {
    $previousCommit = $null
    $mostRecentCommitDate = [datetime]::MinValue

    foreach ($tag in $matchingTags) {
      $commitHash = git rev-list -n 1 $tag
      $commitDate = git show -s --format="%ci" $commitHash | Get-Date

      if ($commitDate -gt $mostRecentCommitDate) {
        $mostRecentCommitDate = $commitDate
        $previousCommit = $commitHash
      }
    }
  }
  else {
    Write-Host "No tags found with prefix '$tagPrefix' check if full deploy is needed."
    exit 1
  }

  # Get the list of modified files between the current and previous commits
  $changedFiles = git diff --name-only $previousCommit $currentCommit

  # Get the distinct folder paths from the changed files
  $changedFolders = $changedFiles | ForEach-Object { Split-Path -Parent $_ } | Get-Unique

  # Filter the changed folders to include only the ones containing deploy.ps1
  $folder = $changedFolders | Where-Object { Test-Path (Join-Path -Path $repoRoot -ChildPath ($_ + "\$type.ps1")) }

  # Get the relative paths of deploy.ps1 scripts within the deploy folders and their subfolders
  $changedScripts = $folder | Get-ChildItem -Recurse -Filter "$type.ps1" -File | ForEach-Object {
    $_.FullName.Substring($repoRoot.Length + 1)
  }

  if ($changedScripts) {
    # Normalize slashes in paths between operating systems
    $changedScripts = $changedScripts.Replace('\', '/')
  }

  return $changedScripts
}

function Get-LocationShort {
  param(
    [string]$location
  )

  $locations = @{
    westeurope = 'weu'
  }

  if (!$locations.ContainsKey($location)) {
    Write-Host "Location '$location' does not have a shortname available"
    exit 1
  }

  return $locations[$location];
}

function Show-Title {
  param(
    [string]$title = "",
    [string]$color = "Cyan",
    [string]$edge = "="
  )

  $line = $edge * 75
  Write-Host ""
  Write-Host $line -ForegroundColor $color
  Write-Host " $title" -ForegroundColor $color
  Write-Host $line -ForegroundColor $color
}

function Block-Start {
  param(
    [string]$title = "",
    [string]$color = "Yellow",
    [string]$edge = "="
  )

  $separator = $edge * 75
  Write-Host $separator
  Write-Host " $title" -ForegroundColor $color
  Write-Host ""
}

function Block-End {
  param(
    [string]$title = "",
    [string]$color = "Green",
    [string]$edge = "="
  )

  $separator = $edge * 75
  Write-Host ""
  Write-Host " $title" -ForegroundColor $color
  Write-Host $separator
}

function Invoke-Script {
  param(
    [string]$scriptPath,
    [string]$location,
    [string]$stack,
    [string]$retailer,
    [string]$subscriptionId
  )

  Show-Title -title "Running $scriptPath ($retailer, $stack, $location)" -color "Yellow"

  try {
    & $scriptPath -location $location -stack $stack -retailer $retailer -subscriptionId $subscriptionId
    Write-Host "Success $scriptPath" -ForegroundColor "Green"
  }
  catch {
    Write-Host "Failed $scriptPath with error: $_" -ForegroundColor "Red"
    throw $_
  }
}

function Set-GitTag {
  param(
    [string]$retailer,
    [string]$env
  )
  $commitHash = git rev-parse HEAD
  $timestamp = Get-Date -Format "yy-MM-dd_HH.mm.ss"
  $tagPrefix = "$retailer-$env"
  $tag = "$tagPrefix-$timestamp"

  # Check if a tag already exists on the specified commit
  $existingTags = git tag -l --contains $commitHash

  foreach ($existingTag in $existingTags) {
    if ($existingTag -like "$tagPrefix*") {
      # If a tag exists with the specified prefix, remove it
      git tag -d $existingTag
    }
  }

  # Add the new tag
  git tag -a $tag -m "Tagging version $tag" $commitHash
  git push origin $tag
}