Skip to content

Commit

Permalink
Merge pull request #37 from ITS-Unibas/old-concept
Browse files Browse the repository at this point in the history
Mirroring the logic from the Python version of the Jira Observer
  • Loading branch information
TK5-Tim authored Jan 30, 2025
2 parents 162b8a6 + 8beed41 commit 04f8fc7
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 165 deletions.
3 changes: 3 additions & 0 deletions WASP/Private/Compare-JiraState.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ function Compare-JiraState () {
}
}
}
#TODO: Tickets die in $jiraStateFileContent, aber nicht in $IssuesCurrentState sind, müssen gelöscht werden. Anschliessend extra return für diese Tickets


}
catch {
<#Do this if a terminating exception happens#>
Expand Down
75 changes: 42 additions & 33 deletions WASP/Private/Invoke-JiraObserver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ function Invoke-JiraObserver {
$PackageGalleryRepositoryName = $GitFile.Replace(".git", "")
$PackagesGalleryPath = Join-Path -Path $config.Application.BaseDirectory -ChildPath $PackageGalleryRepositoryName

$GitBranchDEV = $Config.GitBranchDEV
$GitBranchTEST = $Config.GitBranchTEST
$GitBranchPROD = $Config.GitBranchPROD
$GitBranchDEV = $Config.Application.GitBranchDEV
$GitBranchTEST = $Config.Application.GitBranchTEST
$GitBranchPROD = $Config.Application.GitBranchPROD
}

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

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

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

$UpdateJiraStateFile = $true
# jedes Issue, welches vom Stand im neusten JiraState-File abweicht wird einzeln durchgegangen
foreach($key in $IssuesCompareState.keys) {
# Ermittlung des Dev-Branches anhand des Software Namens (mit Eventualität des Repackaging branches)
# Ermittlung des Dev-Branches anhand des Paket Namens (mit Eventualität des Repackaging branches)
$DevBranchPrefix = "$GitBranchDEV$key"
$DevBranch = Get-DevBranch -RemoteBranches $RemoteBranches -DevBranchPrefix $DevBranchPrefix
# dev → test: PR nach test, wenn nicht offen. Falls der PR schon gemerged wurde, wird der Jira State aktualisiert
# dev → test: PR nach test
if ($IssuesCompareState[$key].StatusOld -eq "Development" -and $IssuesCompareState[$key].Status -eq "Testing") {
# Es wird gecheckt ob ein offener oder gemergedter Pull Request nach test existiert, falls nicht wird ein neuer PR erstellt
$UpdateJiraStateFile = Update-PullRequest -SourceBranch $DevBranch -DestinationBranch $GitBranchTEST -Software $key -DestinationName "Testing"

# test → prod: PR nach prod, wenn nicht offen.Falls der PR schon gemerged wurde, wird der Jira State aktualisiert.
$PullRequestTitle = "$key to $GitBranchTEST"
$response = New-PullRequest -SourceRepo $packageGalleryRepo -SourceUser $gitHubOrganization -SourceBranch $DevBranch -DestinationRepo $packageGalleryRepo -DestinationUser $gitHubOrganization -DestinationBranch $GitBranchTEST -PullRequestTitle $PullRequestTitle -ErrorAction Stop
Start-Sleep -Seconds 4
if ($response.Status -ne 201) {
Write-Log -Message "Error creating Pull Request for Package $($key): $($response.Status) - $($response.Message) - $($response.errors.message)" -Severity 3
$UpdateJiraStateFile = $false
} else {
Write-Log -Message "Successfully created new Pull Request from $PullRequestTitle." -Severity 1
}
# test → prod: PR nach prod
} elseif ($IssuesCompareState[$key].StatusOld -eq "Testing" -and $IssuesCompareState[$key].Status -eq "Production") {
# Es wird gecheckt ob ein offener oder gemergedter Pull Request nach prod existiert, falls nicht wird ein neuer PR erstellt
$UpdateJiraStateFile = Update-PullRequest -SourceBranch $DevBranch -DestinationBranch $GitBranchPROD -Software $key -DestinationName "Production"

$PullRequestTitle = "$key to $GitBranchPROD"
$response = New-PullRequest -SourceRepo $packageGalleryRepo -SourceUser $gitHubOrganization -SourceBranch $DevBranch -DestinationRepo $packageGalleryRepo -DestinationUser $gitHubOrganization -DestinationBranch $GitBranchPROD -PullRequestTitle $PullRequestTitle -ErrorAction Stop
Start-Sleep -Seconds 4
if ($response.Status -ne 201) {
Write-Log -Message "Error creating Pull Request for Package $($key): $($response.Status) - $($response.Message) - $($response.errors.message)" -Severity 3
$UpdateJiraStateFile = $false
} else {
Write-Log -Message "Successfully created new Pull Request from $PullRequestTitle." -Severity 1
}
# prod → dev: kein PR, neuer branch mit @ + random hash
} elseif ($IssuesCompareState[$key].StatusOld -eq "Production" -and $IssuesCompareState[$key].Status -eq "Development") {
# Falls kein DevBranch für die Software existiert (weil die Software schon nach Prod gemerged wurde), wird ein repackaging branch mit einer uuid erstellt
# Falls kein DevBranch für das Paket existiert (schon nach Prod gemerged), wird ein repackaging branch mit einer uuid erstellt
if ($DevBranch -notin $RemoteBranches) {
$guid = New-Guid
$RepackagingString = [convert]::ToString($guid).Replace("-","")
$RepackagingBranch = "$DevBranchPrefix@$RepackagingString"
New-RemoteBranch -Repository $packageGalleryRepo -User $gitHubOrganization -BranchName $RepackagingBranch
Write-Log -Message "New Repackaging Branch $RepackagingBranch created" -Severity 0
}
$UpdateJiraStateFile = $true
# test -> dev: änderung wird im Jira State File geschrieben
} elseif ($IssuesCompareState[$key].StatusOld -eq "Testing" -and $IssuesCompareState[$key].Status -eq "Development") {
$UpdateJiraStateFile = $true
# dev → prod: Error Message, da Testing übersprungen wurde
} elseif (($IssuesCompareState[$key].StatusOld -eq "Development" -and $IssuesCompareState[$key].Status -eq "Production")) {
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
# Doppelhopping: dev → prod, Use Case nicht erlaubt. Muss Testing durchlaufen.
} elseif ($IssuesCompareState[$key].StatusOld -eq "Development" -and $IssuesCompareState[$key].Status -eq "Production") {
Write-Log -Message "Package $key moved from $($IssuesCompareState[$key].StatusOld) to $($IssuesCompareState[$key].Status): Not allowed! Move ticket to the correct lane." -Severity 2
$UpdateJiraStateFile = $false
}
else {
# test -> dev
} elseif ($IssuesCompareState[$key].StatusOld -eq "Testing" -and $IssuesCompareState[$key].Status -eq "Development") {
Write-Log -Message "Package $key moved from $($IssuesCompareState[$key].StatusOld) to $($IssuesCompareState[$key].Status): No action needed." -Severity 1
# Doppelhopping: prod -> test, Use Case nicht erlaubt. Muss Development durchlaufen.
} elseif ($IssuesCompareState[$key].StatusOld -eq "Production" -and $IssuesCompareState[$key].Status -eq "Testing") {
Write-Log -Message "Package $key moved from $($IssuesCompareState[$key].StatusOld) to $($IssuesCompareState[$key].Status): Not allowed! Move ticket to the correct lane." -Severity 2
$UpdateJiraStateFile = $false
}
if ($UpdateJiraStateFile -eq $true) {
# Der Jira State File wird aktualisiert für den entsprechenden Branch
$UpdatedJiraState[$key] = [PSCustomObject]@{
Assignee = $IssuesCompareState[$key].Assignee
Status = $IssuesCompareState[$key].Status
}

if ($UpdateJiraStateFile -eq $false) {
break
}
}
}

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

end {
# Aktueller Stand Jira Tickets als neues Jira state file schreiben (Stand wurde schon aktualisiert, kein neuer Request)
Write-JiraStateFile $UpdatedJiraState
if ($UpdateJiraStateFile) {
Write-JiraStateFile $IssuesCurrentState
}
}
}

8 changes: 6 additions & 2 deletions WASP/Private/Invoke-PostRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ function Invoke-PostRequest {
Headers = @{Authorization = "Token {0}" -f $config.Application.GitHubAPITokenITSUnibasChocoUser}
Body = $Body
}
return Invoke-RestMethod @Splat -ErrorAction Stop
# Github Success Response ist unterschiedlich zur Error Response
$res = Invoke-WebRequest @Splat
$response = [PSCustomObject]@{ Status = $res.StatusCode }
return $response
}
catch {
return $_
$response = $_.ErrorDetails.message | ConvertFrom-Json
return $response
}

}
Expand Down
2 changes: 1 addition & 1 deletion WASP/Private/New-JiraTicket.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function New-JiraTicket {
} | ConvertTo-Json -Depth 3

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

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

Expand Down
8 changes: 7 additions & 1 deletion WASP/Private/Update-PackageInboxFiltered.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ function Update-PackageInboxFiltered {
# Is this necessary?
Write-Log ([string](git -C $PackagesInboxRepoPath checkout main 2>&1))

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

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

Expand Down
68 changes: 0 additions & 68 deletions WASP/Private/Update-PullRequest.ps1

This file was deleted.

5 changes: 3 additions & 2 deletions WASP/Private/Write-Log.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ function Write-Log {
$null = New-Item -ItemType directory -Path $LogPath
}
if (-Not (Test-Path $LogFilePath -ErrorAction SilentlyContinue)) {
$numLogFiles = (Get-ChildItem -Path $LogPath -Filter '*.log' | Measure-Object).Count
$LogFiles = Get-ChildItem -Path $LogPath -Filter '*.log'
$numLogFiles = ($LogFiles | Measure-Object).Count
if ($numLogFiles -eq $MaxLogFiles) {
Get-ChildItem $LogPath | Sort-Object CreationTime | Select-Object -First 1 | Remove-Item
$LogFiles | Sort-Object CreationTime | Select-Object -First 1 | Remove-Item
}
elseif ($numLogFiles -gt $MaxLogFiles) {
Get-ChildItem $LogPath | Sort-Object CreationTime | Select-Object -First ($numLogFiles - $MaxLogFiles + 1) | Remove-Item
Expand Down
Loading

0 comments on commit 04f8fc7

Please sign in to comment.