-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathHasNewCommitsAfterLastProductionRelease.ps1
More file actions
82 lines (65 loc) · 3.51 KB
/
HasNewCommitsAfterLastProductionRelease.ps1
File metadata and controls
82 lines (65 loc) · 3.51 KB
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
Param(
[string]$owner,
[string]$repo,
[string]$previewBranchName
)
Write-Host "Checking whether there exists new commits after last production release:" -ForegroundColor Magenta;
$releasesUrl = "https://api.github.com/repos/$($owner)/$($repo)/releases";
Write-Host "Getting list of releases with '$($releasesUrl)'" -ForegroundColor Blue;
$releasesJSON = Invoke-RestMethod -Uri $releasesUrl -Method Get;
if ($releasesJSON.Count -eq 0) {
Write-Host "Unable to get releases list with '$($releasesUrl)'" -ForegroundColor Red;
Write-Host "NOTE: This Script cannot handle the first release" -ForegroundColor Cyan;
EXIT 1;
}
$lastReleaseJSON = $releasesJSON[0];
$isPreRelease = $lastReleaseJSON.prerelease;
if ([string]::IsNullOrEmpty($isPreRelease)) {
Write-Host "Unable read the last release is pre-release or not" -ForegroundColor Red;
Write-Host "Last Release Data:" -ForegroundColor Cyan;
Write-Host -Object $lastReleaseJSON -ForegroundColor Cyan;
EXIT 1;
}
$isPreRelease = $isPreRelease -as [bool];
if (-Not ($isPreRelease)) {
Write-Host "Preview is not released for the latest changes in '$($previewBranchName)' branch" -ForegroundColor Red;
Write-Host "Latest Release Data:" -ForegroundColor Cyan;
Write-Host -Object $latestReleaseJSON -ForegroundColor Cyan;
EXIT 1;
}
Write-Host "Checking for availability of new commits after last production release:" -ForegroundColor Magenta;
$latestReleaseUrl = "https://api.github.com/repos/$($owner)/$($repo)/releases/latest";
Write-Host "Getting latest production release with '$($latestReleaseUrl)'" -ForegroundColor Blue;
$latestReleaseJSON = Invoke-RestMethod -Uri $latestReleaseUrl -Method Get;
if ($latestReleaseJSON.Count -eq 0) {
Write-Host "Unable to get latest production release with '$($latestReleaseUrl)'" -ForegroundColor Red;
Write-Host "NOTE: This Script cannot handle the first release!" -ForegroundColor Cyan;
EXIT 1;
}
$publishedTime = $latestReleaseJSON.published_at;
$latestReleaseTag = $latestReleaseJSON.tag_name;
if ([string]::IsNullOrEmpty($publishedTime)) {
Write-Host "Unable read the latest production release published time" -ForegroundColor Red;
Write-Host "Latest Release Data:" -ForegroundColor Red;
Write-Host -Object $latestReleaseJSON -ForegroundColor Red;
EXIT 1;
}
if ([string]::IsNullOrEmpty($latestReleaseTag)) {
Write-Host "Unable read the latest production release tag name" -ForegroundColor Red;
Write-Host "Latest Release Data:" -ForegroundColor Red;
Write-Host -Object $latestReleaseJSON -ForegroundColor Red;
EXIT 1;
}
$newCommitsUrl = "https://api.github.com/repos/$($owner)/$($repo)/commits?sha=$($previewBranchName)&since=$($publishedTime)";
Write-Host "Getting commits after latest production release with '$($newCommitsUrl)'" -ForegroundColor Blue;
$newCommitsJSON = Invoke-RestMethod -Uri $newCommitsUrl -Method Get;
if ($newCommitsJSON.Count -gt 0) {
Write-Host "There are atleast '$($newCommitsJSON.Length)' Commits in '$($previewBranchName)' branch after last production release with tag $($latestReleaseTag)" -ForegroundColor Green;
EXIT 0;
}
else {
Write-Host "Unable to get commits after latest production release with ." -ForegroundColor Red;
Write-Host "NOTE: Possibly, there are no commits in '$($previewBranchName)' branch after last production release with tag $($latestReleaseTag)" -ForegroundColor Cyan;
Write-Host "To verify there are not commits, make a request by pasting '$($newCommitsUrl)' in your browser" -ForegroundColor Cyan;
EXIT 1;
}