Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 0 additions & 54 deletions AlertSender.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,60 +29,6 @@ function Get-Bottleneck {
return $bottleneck
}

class UpdateShouldNotifyResult {
[Parameter(Mandatory)]
[bool]$ShouldNotify
[string]$Message
}

function Get-UpdateShouldNotify {
[CmdletBinding()]
[OutputType([bool])]
param (
[Parameter(Mandatory)]
[PSObject]$UpdateStatus
)

$result = [UpdateShouldNotifyResult]@{
ShouldNotify = $true
}

# If no update is available, no need to notify
if ($UpdateStatus.Status -ne 'Behind') {
$result.ShouldNotify = $false
$result.Message = 'No update available.'
return $result
}

# Define marker file path
$markerFilePath = "$PSScriptRoot\update-notification.marker"

# Check if marker file exists
if (Test-Path $markerFilePath) {
$markerFile = Get-Item $markerFilePath
$timeSinceLastNotification = (Get-Date) - $markerFile.LastWriteTime

# If less than 24 hours have passed since last notification, don't notify
if ($timeSinceLastNotification.TotalHours -lt 24) {
$result.ShouldNotify = $false
$result.Message = "Update notification suppressed. Last notification was $($timeSinceLastNotification.TotalHours.ToString('0.00')) hours ago."

return $result
}
}

# Create or touch the marker file to indicate notification was sent
if (Test-Path $markerFilePath) {
(Get-Item $markerFilePath).LastWriteTime = Get-Date
}
else {
New-Item -Path $markerFilePath -ItemType File -Force | Out-Null
$result.Message = "Created update notification marker file at $markerFilePath"
}

return $result
}

# Convert config from JSON
$Config = $Config | ConvertFrom-Json

Expand Down
163 changes: 107 additions & 56 deletions resources/UpdateInfo.psm1
Original file line number Diff line number Diff line change
@@ -1,73 +1,124 @@
function Get-UpdateStatus {
class UpdateShouldNotifyResult {
[Parameter(Mandatory)]
[bool]$ShouldNotify
[string]$Message
}

process {
# Get currently downloaded version of this project.
$currentVersion = (Get-Content "$PSScriptRoot\version.txt" -Raw).Trim()
function Get-UpdateShouldNotify {
[CmdletBinding()]
[OutputType([UpdateShouldNotifyResult])]
param (
[Parameter(Mandatory)]
[PSObject]$UpdateStatus
)

# Get all releases from GitHub.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
try {
$releases = Invoke-RestMethod -Uri 'https://api.github.com/repos/tigattack/VeeamNotify/releases' -Method Get
}
catch {
$versionStatusCode = $_.Exception.Response.StatusCode.value__
Write-LogMessage -Tag 'WARN' -Message "Failed to query GitHub for the latest version. Please check your internet connection and try again. Status code: $versionStatusCode"
$result = [UpdateShouldNotifyResult]@{
ShouldNotify = $true
}

# If no update is available, no need to notify
if ($UpdateStatus.Status -ne 'Behind') {
$result.ShouldNotify = $false
$result.Message = 'No update available.'
return $result
}

# Define marker file path
$markerFilePath = "$PSScriptRoot\update-notification.marker"

# Check if marker file exists
if (Test-Path $markerFilePath) {
$markerFile = Get-Item $markerFilePath
$timeSinceLastNotification = (Get-Date) - $markerFile.LastWriteTime

# If less than 24 hours have passed since last notification, don't notify
if ($timeSinceLastNotification.TotalHours -lt 24) {
$result.ShouldNotify = $false
$result.Message = "Update notification suppressed. Last notification was $($timeSinceLastNotification.TotalHours.ToString('0.00')) hours ago."

return $result
}
}

if ($releases) {
# Get latest stable
foreach ($i in $releases) {
if (-not $i.prerelease) {
$latestStable = $i.tag_name
break
}
}
# Create or touch the marker file to indicate notification was sent
if (Test-Path $markerFilePath) {
(Get-Item $markerFilePath).LastWriteTime = Get-Date
}
else {
New-Item -Path $markerFilePath -ItemType File -Force | Out-Null
$result.Message = "Created update notification marker file at $markerFilePath"
}

# Get latest prerelease
foreach ($i in $releases) {
if ($i.prerelease) {
$latestPrerelease = $i.tag_name
break
}
}
return $result
}

# Determine if prerelease
$prerelease = $false
foreach ($i in $releases) {
if ($i.tag_name -eq $currentVersion -and $i.prerelease) {
$prerelease = $true
break
}
}
function Get-UpdateStatus {
# Get currently downloaded version of this project.
$currentVersion = (Get-Content "$PSScriptRoot\version.txt" -Raw).Trim()

# Set version status
if ($currentVersion -gt $latestStable) {
$status = 'Ahead'
}
elseif ($currentVersion -lt $latestStable) {
$status = 'Behind'
# Get all releases from GitHub.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
try {
$releases = Invoke-RestMethod -Uri 'https://api.github.com/repos/tigattack/VeeamNotify/releases' -Method Get
}
catch {
$versionStatusCode = $_.Exception.Response.StatusCode.value__
Write-LogMessage -Tag 'WARN' -Message "Failed to query GitHub for the latest version. Please check your internet connection and try again. Status code: $versionStatusCode"
}

if ($releases) {
# Get latest stable
foreach ($i in $releases) {
if (-not $i.prerelease) {
$latestStable = $i.tag_name
break
}
else {
$status = 'Current'
}

# Get latest prerelease
foreach ($i in $releases) {
if ($i.prerelease) {
$latestPrerelease = $i.tag_name
break
}
}

# Create PSObject to return.
$out = New-Object PSObject -Property @{
CurrentVersion = $currentVersion
LatestStable = $latestStable
LatestPrerelease = $latestPrerelease
Prerelease = $prerelease
Status = $status
# Determine if prerelease
$prerelease = $false
foreach ($i in $releases) {
if ($i.tag_name -eq $currentVersion -and $i.prerelease) {
$prerelease = $true
break
}
}

# Set version status
if ($currentVersion -gt $latestStable) {
$status = 'Ahead'
}
elseif ($currentVersion -lt $latestStable) {
$status = 'Behind'
}
else {
# Create PSObject to return.
$out = New-Object PSObject -Property @{
CurrentVersion = $currentVersion
}
$status = 'Current'
}

# Return PSObject.
return $out
# Create PSObject to return.
$out = New-Object PSObject -Property @{
CurrentVersion = $currentVersion
LatestStable = $latestStable
LatestPrerelease = $latestPrerelease
Prerelease = $prerelease
Status = $status
}
}
else {
# Create PSObject to return.
$out = New-Object PSObject -Property @{
CurrentVersion = $currentVersion
}
}

# Return PSObject.
return $out
}
Loading