Skip to content

Commit 04f8fc7

Browse files
authored
Merge pull request #37 from ITS-Unibas/old-concept
Mirroring the logic from the Python version of the Jira Observer
2 parents 162b8a6 + 8beed41 commit 04f8fc7

8 files changed

+120
-165
lines changed

WASP/Private/Compare-JiraState.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ function Compare-JiraState () {
4646
}
4747
}
4848
}
49+
#TODO: Tickets die in $jiraStateFileContent, aber nicht in $IssuesCurrentState sind, müssen gelöscht werden. Anschliessend extra return für diese Tickets
50+
51+
4952
}
5053
catch {
5154
<#Do this if a terminating exception happens#>

WASP/Private/Invoke-JiraObserver.ps1

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ function Invoke-JiraObserver {
2626
$PackageGalleryRepositoryName = $GitFile.Replace(".git", "")
2727
$PackagesGalleryPath = Join-Path -Path $config.Application.BaseDirectory -ChildPath $PackageGalleryRepositoryName
2828

29-
$GitBranchDEV = $Config.GitBranchDEV
30-
$GitBranchTEST = $Config.GitBranchTEST
31-
$GitBranchPROD = $Config.GitBranchPROD
29+
$GitBranchDEV = $Config.Application.GitBranchDEV
30+
$GitBranchTEST = $Config.Application.GitBranchTEST
31+
$GitBranchPROD = $Config.Application.GitBranchPROD
3232
}
3333

3434
process {
@@ -119,58 +119,65 @@ function Invoke-JiraObserver {
119119
# $jiraStateFileContent <> $currentJiraStates: Vergleiche den aktuellen Stand der Tickets mit dem Jira State File und speichere die Unterschiede in einer Liste
120120
$IssuesCompareState, $NewIssues = Compare-JiraState $IssuesCurrentState $jiraStateFileContent
121121

122-
# Der alte Stand des Jira State Files wird geupdated, zuallererst wird der alte Stand kopiert und die neuen Tickets werden angehängt
123-
$UpdatedJiraState = $jiraStateFileContent.Clone()
124-
$UpdatedJiraState += $NewIssues
125122

126123
# Die verfügbaren Branches werden aus der Package Gallery abgerufen
127124
$RemoteBranches = Get-RemoteBranches -Repo $packageGalleryRepo -User $gitHubOrganization
128125

126+
$UpdateJiraStateFile = $true
129127
# jedes Issue, welches vom Stand im neusten JiraState-File abweicht wird einzeln durchgegangen
130128
foreach($key in $IssuesCompareState.keys) {
131-
# Ermittlung des Dev-Branches anhand des Software Namens (mit Eventualität des Repackaging branches)
129+
# Ermittlung des Dev-Branches anhand des Paket Namens (mit Eventualität des Repackaging branches)
132130
$DevBranchPrefix = "$GitBranchDEV$key"
133131
$DevBranch = Get-DevBranch -RemoteBranches $RemoteBranches -DevBranchPrefix $DevBranchPrefix
134-
# dev → test: PR nach test, wenn nicht offen. Falls der PR schon gemerged wurde, wird der Jira State aktualisiert
132+
# dev → test: PR nach test
135133
if ($IssuesCompareState[$key].StatusOld -eq "Development" -and $IssuesCompareState[$key].Status -eq "Testing") {
136-
# Es wird gecheckt ob ein offener oder gemergedter Pull Request nach test existiert, falls nicht wird ein neuer PR erstellt
137-
$UpdateJiraStateFile = Update-PullRequest -SourceBranch $DevBranch -DestinationBranch $GitBranchTEST -Software $key -DestinationName "Testing"
138-
139-
# test → prod: PR nach prod, wenn nicht offen.Falls der PR schon gemerged wurde, wird der Jira State aktualisiert.
134+
$PullRequestTitle = "$key to $GitBranchTEST"
135+
$response = New-PullRequest -SourceRepo $packageGalleryRepo -SourceUser $gitHubOrganization -SourceBranch $DevBranch -DestinationRepo $packageGalleryRepo -DestinationUser $gitHubOrganization -DestinationBranch $GitBranchTEST -PullRequestTitle $PullRequestTitle -ErrorAction Stop
136+
Start-Sleep -Seconds 4
137+
if ($response.Status -ne 201) {
138+
Write-Log -Message "Error creating Pull Request for Package $($key): $($response.Status) - $($response.Message) - $($response.errors.message)" -Severity 3
139+
$UpdateJiraStateFile = $false
140+
} else {
141+
Write-Log -Message "Successfully created new Pull Request from $PullRequestTitle." -Severity 1
142+
}
143+
# test → prod: PR nach prod
140144
} elseif ($IssuesCompareState[$key].StatusOld -eq "Testing" -and $IssuesCompareState[$key].Status -eq "Production") {
141-
# Es wird gecheckt ob ein offener oder gemergedter Pull Request nach prod existiert, falls nicht wird ein neuer PR erstellt
142-
$UpdateJiraStateFile = Update-PullRequest -SourceBranch $DevBranch -DestinationBranch $GitBranchPROD -Software $key -DestinationName "Production"
143-
145+
$PullRequestTitle = "$key to $GitBranchPROD"
146+
$response = New-PullRequest -SourceRepo $packageGalleryRepo -SourceUser $gitHubOrganization -SourceBranch $DevBranch -DestinationRepo $packageGalleryRepo -DestinationUser $gitHubOrganization -DestinationBranch $GitBranchPROD -PullRequestTitle $PullRequestTitle -ErrorAction Stop
147+
Start-Sleep -Seconds 4
148+
if ($response.Status -ne 201) {
149+
Write-Log -Message "Error creating Pull Request for Package $($key): $($response.Status) - $($response.Message) - $($response.errors.message)" -Severity 3
150+
$UpdateJiraStateFile = $false
151+
} else {
152+
Write-Log -Message "Successfully created new Pull Request from $PullRequestTitle." -Severity 1
153+
}
144154
# prod → dev: kein PR, neuer branch mit @ + random hash
145155
} elseif ($IssuesCompareState[$key].StatusOld -eq "Production" -and $IssuesCompareState[$key].Status -eq "Development") {
146-
# Falls kein DevBranch für die Software existiert (weil die Software schon nach Prod gemerged wurde), wird ein repackaging branch mit einer uuid erstellt
156+
# Falls kein DevBranch für das Paket existiert (schon nach Prod gemerged), wird ein repackaging branch mit einer uuid erstellt
147157
if ($DevBranch -notin $RemoteBranches) {
148158
$guid = New-Guid
149159
$RepackagingString = [convert]::ToString($guid).Replace("-","")
150160
$RepackagingBranch = "$DevBranchPrefix@$RepackagingString"
151161
New-RemoteBranch -Repository $packageGalleryRepo -User $gitHubOrganization -BranchName $RepackagingBranch
152162
Write-Log -Message "New Repackaging Branch $RepackagingBranch created" -Severity 0
153163
}
154-
$UpdateJiraStateFile = $true
155-
# test -> dev: änderung wird im Jira State File geschrieben
156-
} elseif ($IssuesCompareState[$key].StatusOld -eq "Testing" -and $IssuesCompareState[$key].Status -eq "Development") {
157-
$UpdateJiraStateFile = $true
158-
# dev → prod: Error Message, da Testing übersprungen wurde
159-
} elseif (($IssuesCompareState[$key].StatusOld -eq "Development" -and $IssuesCompareState[$key].Status -eq "Production")) {
160-
Write-Log -Message "The status of the issue $key has changed from Development to Production without going through Testing. This Action is not allowed." -Severity 3
164+
# Doppelhopping: dev → prod, Use Case nicht erlaubt. Muss Testing durchlaufen.
165+
} elseif ($IssuesCompareState[$key].StatusOld -eq "Development" -and $IssuesCompareState[$key].Status -eq "Production") {
166+
Write-Log -Message "Package $key moved from $($IssuesCompareState[$key].StatusOld) to $($IssuesCompareState[$key].Status): Not allowed! Move ticket to the correct lane." -Severity 2
161167
$UpdateJiraStateFile = $false
162-
}
163-
else {
168+
# test -> dev
169+
} elseif ($IssuesCompareState[$key].StatusOld -eq "Testing" -and $IssuesCompareState[$key].Status -eq "Development") {
170+
Write-Log -Message "Package $key moved from $($IssuesCompareState[$key].StatusOld) to $($IssuesCompareState[$key].Status): No action needed." -Severity 1
171+
# Doppelhopping: prod -> test, Use Case nicht erlaubt. Muss Development durchlaufen.
172+
} elseif ($IssuesCompareState[$key].StatusOld -eq "Production" -and $IssuesCompareState[$key].Status -eq "Testing") {
173+
Write-Log -Message "Package $key moved from $($IssuesCompareState[$key].StatusOld) to $($IssuesCompareState[$key].Status): Not allowed! Move ticket to the correct lane." -Severity 2
164174
$UpdateJiraStateFile = $false
165175
}
166-
if ($UpdateJiraStateFile -eq $true) {
167-
# Der Jira State File wird aktualisiert für den entsprechenden Branch
168-
$UpdatedJiraState[$key] = [PSCustomObject]@{
169-
Assignee = $IssuesCompareState[$key].Assignee
170-
Status = $IssuesCompareState[$key].Status
171-
}
176+
177+
if ($UpdateJiraStateFile -eq $false) {
178+
break
172179
}
173-
}
180+
}
174181

175182
# PHS: Branch in der Package Gallery auf prod setzen (checkout prod)
176183
Write-Log -Message "Checkout prod branch in Package Gallery" -Severity 0
@@ -179,7 +186,9 @@ function Invoke-JiraObserver {
179186

180187
end {
181188
# Aktueller Stand Jira Tickets als neues Jira state file schreiben (Stand wurde schon aktualisiert, kein neuer Request)
182-
Write-JiraStateFile $UpdatedJiraState
189+
if ($UpdateJiraStateFile) {
190+
Write-JiraStateFile $IssuesCurrentState
191+
}
183192
}
184193
}
185194

WASP/Private/Invoke-PostRequest.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ function Invoke-PostRequest {
2727
Headers = @{Authorization = "Token {0}" -f $config.Application.GitHubAPITokenITSUnibasChocoUser}
2828
Body = $Body
2929
}
30-
return Invoke-RestMethod @Splat -ErrorAction Stop
30+
# Github Success Response ist unterschiedlich zur Error Response
31+
$res = Invoke-WebRequest @Splat
32+
$response = [PSCustomObject]@{ Status = $res.StatusCode }
33+
return $response
3134
}
3235
catch {
33-
return $_
36+
$response = $_.ErrorDetails.message | ConvertFrom-Json
37+
return $response
3438
}
3539

3640
}

WASP/Private/New-JiraTicket.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function New-JiraTicket {
6464
} | ConvertTo-Json -Depth 3
6565

6666
# Create the new issue
67-
Write-Log -Message "Creating new jira ticket for pacakge $package with version $version" -Severity 1
67+
Write-Log -Message "Creating new jira ticket for package $package with version $version" -Severity 1
6868

6969
$response = Invoke-WebRequest -Uri $url -Method Post -Headers $header -Body $body
7070

WASP/Private/Update-PackageInboxFiltered.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ function Update-PackageInboxFiltered {
6868
# Is this necessary?
6969
Write-Log ([string](git -C $PackagesInboxRepoPath checkout main 2>&1))
7070

71-
New-PullRequest -SourceRepo $GitRepoInbox -SourceUser $GitHubUser -SourceBranch $DevBranch -DestinationRepo $GitRepoPackageGallery -DestinationUser $GitHubOrganisation -DestinationBranch $DevBranch -ErrorAction Stop
71+
$response = New-PullRequest -SourceRepo $GitRepoInbox -SourceUser $GitHubUser -SourceBranch $DevBranch -DestinationRepo $GitRepoPackageGallery -DestinationUser $GitHubOrganisation -DestinationBranch $DevBranch -ErrorAction Stop
72+
if ($response.Status -ne 201) {
73+
Write-Log -Message "Error creating Pull Request: $($response.Status) - $($response.Message) - $($response.errors.message)" -Severity 3
74+
continue
75+
} else {
76+
Write-Log -Message "New Pull Request created: $DevBranch" -Severity 1
77+
}
7278

7379
$wishlist = Get-Content -Path $wishlistPath | Where-Object { $_ -notlike "#*" }
7480

WASP/Private/Update-PullRequest.ps1

Lines changed: 0 additions & 68 deletions
This file was deleted.

WASP/Private/Write-Log.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ function Write-Log {
8383
$null = New-Item -ItemType directory -Path $LogPath
8484
}
8585
if (-Not (Test-Path $LogFilePath -ErrorAction SilentlyContinue)) {
86-
$numLogFiles = (Get-ChildItem -Path $LogPath -Filter '*.log' | Measure-Object).Count
86+
$LogFiles = Get-ChildItem -Path $LogPath -Filter '*.log'
87+
$numLogFiles = ($LogFiles | Measure-Object).Count
8788
if ($numLogFiles -eq $MaxLogFiles) {
88-
Get-ChildItem $LogPath | Sort-Object CreationTime | Select-Object -First 1 | Remove-Item
89+
$LogFiles | Sort-Object CreationTime | Select-Object -First 1 | Remove-Item
8990
}
9091
elseif ($numLogFiles -gt $MaxLogFiles) {
9192
Get-ChildItem $LogPath | Sort-Object CreationTime | Select-Object -First ($numLogFiles - $MaxLogFiles + 1) | Remove-Item

0 commit comments

Comments
 (0)