generated from IT-Service/PowerShell-GitHub-Action-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvoke-GitHubAction.ps1
103 lines (90 loc) · 2.58 KB
/
Invoke-GitHubAction.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Copyright © 2022 Sergei S. Betke
<#
.SYNOPSIS
Create ReleaseNotes.md from ChangeLog.md
#>
[CmdletBinding()]
Param(
# Relative path to ChangeLog.md file
[Parameter( Mandatory = $False, Position = 0 )]
[Alias( 'ChangeLogPath' )]
[System.String]
$Path = 'CHANGELOG.md',
# Relative path to ReleaseNotes.md file
[Parameter( Mandatory = $False, Position = 1 )]
[Alias( 'ReleaseNotesPath' )]
[System.String]
$Destination = 'RELEASENOTES.md',
# Project version, for which ReleaseNotes.md must be generated
[Parameter( Mandatory = $False )]
[System.String]
$Version
)
Import-Module $PSScriptRoot/lib/GitHubActionsCore -Verbose:$false;
try
{
if ( $Version -ne 'latest' )
{
$LatestVersion = $false;
Write-Verbose "Version $Version";
}
else
{
$LatestVersion = $true;
Write-ActionWarning 'Version does not specified. Used latest version info from change log.';
};
$ReleaseNotesRelativePath = $Destination;
Write-Verbose "Release notes relative path: $ReleaseNotesRelativePath";
$ChangeLogRelativePath = $Path;
Write-Verbose "Changelog relative path: $ChangeLogRelativePath";
$ChangeLog = ( Get-Content -Path $ChangeLogRelativePath -Encoding UTF8 );
$isExpectedSection = $false;
$isFirstVersionSection = $true;
$processedVersion = '';
$releaseNotes = @( $ChangeLog | ForEach-Object {
$isReleaseSectionHeader = ( $_ -match '##\s+(?:(?<version>\d+\.\d+\.\d+)|\[(?<version>\d+\.\d+\.\d+)\])' );
if ( $isReleaseSectionHeader )
{
$releaseVersion = $Matches[ 'version' ];
if ( $LatestVersion )
{
$isExpectedSection = $isFirstVersionSection;
$isFirstVersionSection = $false;
}
else
{
$isExpectedSection = ( $releaseVersion -eq $Version );
};
if ( $isExpectedSection )
{
$processedVersion = $releaseVersion;
};
}
else
{
if ( $isExpectedSection )
{
return $_;
};
};
}
) -join "`r`n";
$releaseNotes | Out-File -Encoding utf8 -FilePath $ReleaseNotesRelativePath -NoNewLine;
if ( [System.String]::IsNullOrEmpty( $releaseNotes ) )
{
Write-ActionWarning `
-Message "Change log does not cotains release notes section for specified version." `
-File $ChangeLogRelativePath;
};
Write-Verbose "Release notes stored in $ReleaseNotesRelativePath";
Set-ActionOutput -Name 'release-notes-path' -Value $ReleaseNotesRelativePath;
Write-Verbose "Actual project version: $processedVersion";
Set-ActionOutput -Name 'actual-version' -Value $processedVersion;
}
catch
{
Set-ActionOutput 'error' $_.ToString();
$ErrorView = 'NormalView';
Set-ActionFailed ($_ | Out-String);
}
exit [System.Environment]::ExitCode;