Sun, 08 Jan 2023 17:07:26 +0100
#15 add issue filters
universe@184 | 1 | /* |
universe@184 | 2 | * Copyright 2021 Mike Becker. All rights reserved. |
universe@184 | 3 | * |
universe@184 | 4 | * Redistribution and use in source and binary forms, with or without |
universe@184 | 5 | * modification, are permitted provided that the following conditions are met: |
universe@184 | 6 | * |
universe@184 | 7 | * 1. Redistributions of source code must retain the above copyright |
universe@184 | 8 | * notice, this list of conditions and the following disclaimer. |
universe@184 | 9 | * |
universe@184 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@184 | 11 | * notice, this list of conditions and the following disclaimer in the |
universe@184 | 12 | * documentation and/or other materials provided with the distribution. |
universe@184 | 13 | * |
universe@184 | 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@184 | 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@184 | 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
universe@184 | 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
universe@184 | 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
universe@184 | 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
universe@184 | 20 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
universe@184 | 21 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
universe@184 | 22 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
universe@184 | 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
universe@184 | 24 | */ |
universe@184 | 25 | |
universe@184 | 26 | package de.uapcore.lightpit.servlet |
universe@184 | 27 | |
universe@247 | 28 | import de.uapcore.lightpit.AbstractServlet |
universe@247 | 29 | import de.uapcore.lightpit.HttpRequest |
universe@247 | 30 | import de.uapcore.lightpit.boolValidator |
universe@184 | 31 | import de.uapcore.lightpit.dao.DataAccessObject |
universe@247 | 32 | import de.uapcore.lightpit.dateOptValidator |
universe@184 | 33 | import de.uapcore.lightpit.entities.* |
universe@263 | 34 | import de.uapcore.lightpit.types.* |
universe@184 | 35 | import de.uapcore.lightpit.viewmodel.* |
universe@254 | 36 | import jakarta.servlet.annotation.WebServlet |
universe@184 | 37 | import java.sql.Date |
universe@184 | 38 | |
universe@184 | 39 | @WebServlet(urlPatterns = ["/projects/*"]) |
universe@184 | 40 | class ProjectServlet : AbstractServlet() { |
universe@184 | 41 | |
universe@184 | 42 | init { |
universe@184 | 43 | get("/", this::projects) |
universe@184 | 44 | get("/%project", this::project) |
universe@184 | 45 | get("/%project/issues/%version/%component/", this::project) |
universe@184 | 46 | get("/%project/edit", this::projectForm) |
universe@184 | 47 | get("/-/create", this::projectForm) |
universe@184 | 48 | post("/-/commit", this::projectCommit) |
universe@184 | 49 | |
universe@184 | 50 | get("/%project/versions/", this::versions) |
universe@184 | 51 | get("/%project/versions/%version/edit", this::versionForm) |
universe@184 | 52 | get("/%project/versions/-/create", this::versionForm) |
universe@184 | 53 | post("/%project/versions/-/commit", this::versionCommit) |
universe@184 | 54 | |
universe@184 | 55 | get("/%project/components/", this::components) |
universe@184 | 56 | get("/%project/components/%component/edit", this::componentForm) |
universe@184 | 57 | get("/%project/components/-/create", this::componentForm) |
universe@184 | 58 | post("/%project/components/-/commit", this::componentCommit) |
universe@184 | 59 | |
universe@184 | 60 | get("/%project/issues/%version/%component/%issue", this::issue) |
universe@184 | 61 | get("/%project/issues/%version/%component/%issue/edit", this::issueForm) |
universe@186 | 62 | post("/%project/issues/%version/%component/%issue/comment", this::issueComment) |
universe@263 | 63 | post("/%project/issues/%version/%component/%issue/relation", this::issueRelation) |
universe@263 | 64 | get("/%project/issues/%version/%component/%issue/removeRelation", this::issueRemoveRelation) |
universe@184 | 65 | get("/%project/issues/%version/%component/-/create", this::issueForm) |
universe@186 | 66 | post("/%project/issues/%version/%component/-/commit", this::issueCommit) |
universe@184 | 67 | } |
universe@184 | 68 | |
universe@193 | 69 | private fun projects(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 70 | val projects = dao.listProjects() |
universe@184 | 71 | val projectInfos = projects.map { |
universe@184 | 72 | ProjectInfo( |
universe@184 | 73 | project = it, |
universe@184 | 74 | versions = dao.listVersions(it), |
universe@184 | 75 | components = emptyList(), // not required in this view |
universe@184 | 76 | issueSummary = dao.collectIssueSummary(it) |
universe@184 | 77 | ) |
universe@184 | 78 | } |
universe@184 | 79 | |
universe@184 | 80 | with(http) { |
universe@184 | 81 | view = ProjectsView(projectInfos) |
universe@184 | 82 | navigationMenu = projectNavMenu(projects) |
universe@184 | 83 | styleSheets = listOf("projects") |
universe@184 | 84 | render("projects") |
universe@184 | 85 | } |
universe@184 | 86 | } |
universe@184 | 87 | |
universe@184 | 88 | private fun activeProjectNavMenu( |
universe@184 | 89 | projects: List<Project>, |
universe@184 | 90 | projectInfo: ProjectInfo, |
universe@184 | 91 | selectedVersion: Version? = null, |
universe@184 | 92 | selectedComponent: Component? = null |
universe@184 | 93 | ) = |
universe@184 | 94 | projectNavMenu( |
universe@184 | 95 | projects, |
universe@184 | 96 | projectInfo.versions, |
universe@184 | 97 | projectInfo.components, |
universe@184 | 98 | projectInfo.project, |
universe@184 | 99 | selectedVersion, |
universe@184 | 100 | selectedComponent |
universe@184 | 101 | ) |
universe@184 | 102 | |
universe@210 | 103 | private sealed interface LookupResult<T> |
universe@210 | 104 | private class NotFound<T> : LookupResult<T> |
universe@210 | 105 | private data class Found<T>(val elem: T?) : LookupResult<T> |
universe@184 | 106 | |
universe@184 | 107 | private fun <T : HasNode> HttpRequest.lookupPathParam(paramName: String, list: List<T>): LookupResult<T> { |
universe@184 | 108 | val node = pathParams[paramName] |
universe@184 | 109 | return if (node == null || node == "-") { |
universe@210 | 110 | Found(null) |
universe@184 | 111 | } else { |
universe@184 | 112 | val result = list.find { it.node == node } |
universe@184 | 113 | if (result == null) { |
universe@210 | 114 | NotFound() |
universe@184 | 115 | } else { |
universe@210 | 116 | Found(result) |
universe@184 | 117 | } |
universe@184 | 118 | } |
universe@184 | 119 | } |
universe@184 | 120 | |
universe@184 | 121 | private fun obtainProjectInfo(http: HttpRequest, dao: DataAccessObject): ProjectInfo? { |
universe@184 | 122 | val project = dao.findProjectByNode(http.pathParams["project"] ?: "") ?: return null |
universe@184 | 123 | |
universe@184 | 124 | val versions: List<Version> = dao.listVersions(project) |
universe@184 | 125 | val components: List<Component> = dao.listComponents(project) |
universe@184 | 126 | |
universe@184 | 127 | return ProjectInfo( |
universe@184 | 128 | project, |
universe@184 | 129 | versions, |
universe@184 | 130 | components, |
universe@184 | 131 | dao.collectIssueSummary(project) |
universe@184 | 132 | ) |
universe@184 | 133 | } |
universe@184 | 134 | |
universe@184 | 135 | private fun sanitizeNode(name: String): String { |
universe@184 | 136 | val san = name.replace(Regex("[/\\\\]"), "-") |
universe@184 | 137 | return if (san.startsWith(".")) { |
universe@184 | 138 | "v$san" |
universe@184 | 139 | } else { |
universe@184 | 140 | san |
universe@184 | 141 | } |
universe@184 | 142 | } |
universe@184 | 143 | |
universe@198 | 144 | private fun feedPath(project: Project) = "feed/${project.node}/issues.rss" |
universe@198 | 145 | |
universe@210 | 146 | private data class PathInfos( |
universe@184 | 147 | val projectInfo: ProjectInfo, |
universe@184 | 148 | val version: Version?, |
universe@184 | 149 | val component: Component? |
universe@184 | 150 | ) { |
universe@198 | 151 | val project = projectInfo.project |
universe@198 | 152 | val issuesHref by lazyOf("projects/${project.node}/issues/${version?.node ?: "-"}/${component?.node ?: "-"}/") |
universe@184 | 153 | } |
universe@184 | 154 | |
universe@184 | 155 | private fun withPathInfo(http: HttpRequest, dao: DataAccessObject): PathInfos? { |
universe@184 | 156 | val projectInfo = obtainProjectInfo(http, dao) |
universe@184 | 157 | if (projectInfo == null) { |
universe@184 | 158 | http.response.sendError(404) |
universe@184 | 159 | return null |
universe@184 | 160 | } |
universe@184 | 161 | |
universe@184 | 162 | val version = when (val result = http.lookupPathParam("version", projectInfo.versions)) { |
universe@210 | 163 | is NotFound -> { |
universe@184 | 164 | http.response.sendError(404) |
universe@184 | 165 | return null |
universe@184 | 166 | } |
universe@210 | 167 | is Found -> { |
universe@184 | 168 | result.elem |
universe@184 | 169 | } |
universe@184 | 170 | } |
universe@184 | 171 | val component = when (val result = http.lookupPathParam("component", projectInfo.components)) { |
universe@210 | 172 | is NotFound -> { |
universe@184 | 173 | http.response.sendError(404) |
universe@184 | 174 | return null |
universe@184 | 175 | } |
universe@210 | 176 | is Found -> { |
universe@184 | 177 | result.elem |
universe@184 | 178 | } |
universe@184 | 179 | } |
universe@184 | 180 | |
universe@184 | 181 | return PathInfos(projectInfo, version, component) |
universe@184 | 182 | } |
universe@184 | 183 | |
universe@193 | 184 | private fun project(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 185 | withPathInfo(http, dao)?.run { |
universe@193 | 186 | |
universe@268 | 187 | val filter = IssueFilter(http) |
universe@268 | 188 | |
universe@268 | 189 | val needRelationsMap = filter.onlyBlocker |
universe@268 | 190 | |
universe@268 | 191 | val relationsMap = if (needRelationsMap) dao.getIssueRelationMap(project, filter.includeDone) else emptyMap() |
universe@268 | 192 | |
universe@268 | 193 | val issues = dao.listIssues(project, filter.includeDone, version, component) |
universe@249 | 194 | .sortedWith( |
universe@249 | 195 | IssueSorter( |
universe@267 | 196 | IssueSorter.Criteria(IssueSorter.Field.DONE), |
universe@249 | 197 | IssueSorter.Criteria(IssueSorter.Field.ETA), |
universe@249 | 198 | IssueSorter.Criteria(IssueSorter.Field.UPDATED, false) |
universe@249 | 199 | ) |
universe@249 | 200 | ) |
universe@268 | 201 | .filter { |
universe@268 | 202 | (!filter.onlyMine || (it.assignee?.username ?: "") == (http.remoteUser ?: "<Anonymous>")) && |
universe@268 | 203 | (!filter.onlyBlocker || (relationsMap[it.id]?.any { (_,type) -> type.blocking }?:false)) && |
universe@268 | 204 | (filter.status.isEmpty() || filter.status.contains(it.status)) && |
universe@268 | 205 | (filter.category.isEmpty() || filter.category.contains(it.category)) |
universe@268 | 206 | } |
universe@184 | 207 | |
universe@184 | 208 | with(http) { |
universe@205 | 209 | pageTitle = project.name |
universe@268 | 210 | view = ProjectDetails(projectInfo, issues, filter, version, component) |
universe@198 | 211 | feedPath = feedPath(project) |
universe@184 | 212 | navigationMenu = activeProjectNavMenu( |
universe@184 | 213 | dao.listProjects(), |
universe@184 | 214 | projectInfo, |
universe@184 | 215 | version, |
universe@184 | 216 | component |
universe@184 | 217 | ) |
universe@184 | 218 | styleSheets = listOf("projects") |
universe@266 | 219 | javascript = "project-details" |
universe@184 | 220 | render("project-details") |
universe@184 | 221 | } |
universe@184 | 222 | } |
universe@184 | 223 | } |
universe@184 | 224 | |
universe@193 | 225 | private fun projectForm(http: HttpRequest, dao: DataAccessObject) { |
universe@200 | 226 | if (!http.pathParams.containsKey("project")) { |
universe@200 | 227 | http.view = ProjectEditView(Project(-1), dao.listUsers()) |
universe@200 | 228 | http.navigationMenu = projectNavMenu(dao.listProjects()) |
universe@200 | 229 | } else { |
universe@200 | 230 | val projectInfo = obtainProjectInfo(http, dao) |
universe@200 | 231 | if (projectInfo == null) { |
universe@200 | 232 | http.response.sendError(404) |
universe@200 | 233 | return |
universe@200 | 234 | } |
universe@200 | 235 | http.view = ProjectEditView(projectInfo.project, dao.listUsers()) |
universe@200 | 236 | http.navigationMenu = activeProjectNavMenu( |
universe@184 | 237 | dao.listProjects(), |
universe@184 | 238 | projectInfo |
universe@184 | 239 | ) |
universe@184 | 240 | } |
universe@200 | 241 | http.styleSheets = listOf("projects") |
universe@200 | 242 | http.render("project-form") |
universe@184 | 243 | } |
universe@184 | 244 | |
universe@193 | 245 | private fun projectCommit(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 246 | val project = Project(http.param("id")?.toIntOrNull() ?: -1).apply { |
universe@184 | 247 | name = http.param("name") ?: "" |
universe@184 | 248 | node = http.param("node") ?: "" |
universe@184 | 249 | description = http.param("description") ?: "" |
universe@184 | 250 | ordinal = http.param("ordinal")?.toIntOrNull() ?: 0 |
universe@184 | 251 | repoUrl = http.param("repoUrl") ?: "" |
universe@184 | 252 | owner = (http.param("owner")?.toIntOrNull() ?: -1).let { |
universe@184 | 253 | if (it < 0) null else dao.findUser(it) |
universe@184 | 254 | } |
universe@184 | 255 | // intentional defaults |
universe@184 | 256 | if (node.isBlank()) node = name |
universe@184 | 257 | // sanitizing |
universe@184 | 258 | node = sanitizeNode(node) |
universe@184 | 259 | } |
universe@184 | 260 | |
universe@184 | 261 | if (project.id < 0) { |
universe@184 | 262 | dao.insertProject(project) |
universe@184 | 263 | } else { |
universe@184 | 264 | dao.updateProject(project) |
universe@184 | 265 | } |
universe@184 | 266 | |
universe@184 | 267 | http.renderCommit("projects/${project.node}") |
universe@184 | 268 | } |
universe@184 | 269 | |
universe@193 | 270 | private fun versions(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 271 | val projectInfo = obtainProjectInfo(http, dao) |
universe@184 | 272 | if (projectInfo == null) { |
universe@184 | 273 | http.response.sendError(404) |
universe@184 | 274 | return |
universe@184 | 275 | } |
universe@184 | 276 | |
universe@184 | 277 | with(http) { |
universe@205 | 278 | pageTitle = "${projectInfo.project.name} - ${i18n("navmenu.versions")}" |
universe@184 | 279 | view = VersionsView( |
universe@184 | 280 | projectInfo, |
universe@184 | 281 | dao.listVersionSummaries(projectInfo.project) |
universe@184 | 282 | ) |
universe@198 | 283 | feedPath = feedPath(projectInfo.project) |
universe@184 | 284 | navigationMenu = activeProjectNavMenu( |
universe@184 | 285 | dao.listProjects(), |
universe@184 | 286 | projectInfo |
universe@184 | 287 | ) |
universe@184 | 288 | styleSheets = listOf("projects") |
universe@266 | 289 | javascript = "project-details" |
universe@184 | 290 | render("versions") |
universe@184 | 291 | } |
universe@184 | 292 | } |
universe@184 | 293 | |
universe@193 | 294 | private fun versionForm(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 295 | val projectInfo = obtainProjectInfo(http, dao) |
universe@184 | 296 | if (projectInfo == null) { |
universe@184 | 297 | http.response.sendError(404) |
universe@184 | 298 | return |
universe@184 | 299 | } |
universe@184 | 300 | |
universe@184 | 301 | val version: Version |
universe@184 | 302 | when (val result = http.lookupPathParam("version", projectInfo.versions)) { |
universe@210 | 303 | is NotFound -> { |
universe@184 | 304 | http.response.sendError(404) |
universe@184 | 305 | return |
universe@184 | 306 | } |
universe@210 | 307 | is Found -> { |
universe@184 | 308 | version = result.elem ?: Version(-1, projectInfo.project.id) |
universe@184 | 309 | } |
universe@184 | 310 | } |
universe@184 | 311 | |
universe@184 | 312 | with(http) { |
universe@184 | 313 | view = VersionEditView(projectInfo, version) |
universe@198 | 314 | feedPath = feedPath(projectInfo.project) |
universe@184 | 315 | navigationMenu = activeProjectNavMenu( |
universe@184 | 316 | dao.listProjects(), |
universe@184 | 317 | projectInfo, |
universe@184 | 318 | selectedVersion = version |
universe@184 | 319 | ) |
universe@184 | 320 | styleSheets = listOf("projects") |
universe@184 | 321 | render("version-form") |
universe@184 | 322 | } |
universe@184 | 323 | } |
universe@184 | 324 | |
universe@249 | 325 | private fun obtainIdAndProject(http: HttpRequest, dao: DataAccessObject): Pair<Int, Project>? { |
universe@184 | 326 | val id = http.param("id")?.toIntOrNull() |
universe@184 | 327 | val projectid = http.param("projectid")?.toIntOrNull() ?: -1 |
universe@184 | 328 | val project = dao.findProject(projectid) |
universe@210 | 329 | return if (id == null || project == null) { |
universe@184 | 330 | http.response.sendError(400) |
universe@210 | 331 | null |
universe@210 | 332 | } else { |
universe@210 | 333 | Pair(id, project) |
universe@184 | 334 | } |
universe@210 | 335 | } |
universe@184 | 336 | |
universe@210 | 337 | private fun versionCommit(http: HttpRequest, dao: DataAccessObject) { |
universe@210 | 338 | val idParams = obtainIdAndProject(http, dao) ?: return |
universe@210 | 339 | val (id, project) = idParams |
universe@210 | 340 | |
universe@210 | 341 | val version = Version(id, project.id).apply { |
universe@184 | 342 | name = http.param("name") ?: "" |
universe@184 | 343 | node = http.param("node") ?: "" |
universe@184 | 344 | ordinal = http.param("ordinal")?.toIntOrNull() ?: 0 |
universe@184 | 345 | status = http.param("status")?.let(VersionStatus::valueOf) ?: VersionStatus.Future |
universe@225 | 346 | // TODO: process error messages |
universe@249 | 347 | eol = http.param("eol", ::dateOptValidator, null, mutableListOf()) |
universe@249 | 348 | release = http.param("release", ::dateOptValidator, null, mutableListOf()) |
universe@184 | 349 | // intentional defaults |
universe@184 | 350 | if (node.isBlank()) node = name |
universe@184 | 351 | // sanitizing |
universe@184 | 352 | node = sanitizeNode(node) |
universe@184 | 353 | } |
universe@184 | 354 | |
universe@225 | 355 | // sanitize eol and release date |
universe@225 | 356 | if (version.status.isEndOfLife) { |
universe@225 | 357 | if (version.eol == null) version.eol = Date(System.currentTimeMillis()) |
universe@225 | 358 | } else if (version.status.isReleased) { |
universe@225 | 359 | if (version.release == null) version.release = Date(System.currentTimeMillis()) |
universe@225 | 360 | } |
universe@225 | 361 | |
universe@184 | 362 | if (id < 0) { |
universe@184 | 363 | dao.insertVersion(version) |
universe@184 | 364 | } else { |
universe@184 | 365 | dao.updateVersion(version) |
universe@184 | 366 | } |
universe@184 | 367 | |
universe@184 | 368 | http.renderCommit("projects/${project.node}/versions/") |
universe@184 | 369 | } |
universe@184 | 370 | |
universe@193 | 371 | private fun components(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 372 | val projectInfo = obtainProjectInfo(http, dao) |
universe@184 | 373 | if (projectInfo == null) { |
universe@184 | 374 | http.response.sendError(404) |
universe@184 | 375 | return |
universe@184 | 376 | } |
universe@184 | 377 | |
universe@184 | 378 | with(http) { |
universe@205 | 379 | pageTitle = "${projectInfo.project.name} - ${i18n("navmenu.components")}" |
universe@184 | 380 | view = ComponentsView( |
universe@184 | 381 | projectInfo, |
universe@184 | 382 | dao.listComponentSummaries(projectInfo.project) |
universe@184 | 383 | ) |
universe@198 | 384 | feedPath = feedPath(projectInfo.project) |
universe@184 | 385 | navigationMenu = activeProjectNavMenu( |
universe@184 | 386 | dao.listProjects(), |
universe@184 | 387 | projectInfo |
universe@184 | 388 | ) |
universe@184 | 389 | styleSheets = listOf("projects") |
universe@266 | 390 | javascript = "project-details" |
universe@184 | 391 | render("components") |
universe@184 | 392 | } |
universe@184 | 393 | } |
universe@184 | 394 | |
universe@193 | 395 | private fun componentForm(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 396 | val projectInfo = obtainProjectInfo(http, dao) |
universe@184 | 397 | if (projectInfo == null) { |
universe@184 | 398 | http.response.sendError(404) |
universe@184 | 399 | return |
universe@184 | 400 | } |
universe@184 | 401 | |
universe@184 | 402 | val component: Component |
universe@184 | 403 | when (val result = http.lookupPathParam("component", projectInfo.components)) { |
universe@210 | 404 | is NotFound -> { |
universe@184 | 405 | http.response.sendError(404) |
universe@184 | 406 | return |
universe@184 | 407 | } |
universe@210 | 408 | is Found -> { |
universe@184 | 409 | component = result.elem ?: Component(-1, projectInfo.project.id) |
universe@184 | 410 | } |
universe@184 | 411 | } |
universe@184 | 412 | |
universe@184 | 413 | with(http) { |
universe@184 | 414 | view = ComponentEditView(projectInfo, component, dao.listUsers()) |
universe@198 | 415 | feedPath = feedPath(projectInfo.project) |
universe@184 | 416 | navigationMenu = activeProjectNavMenu( |
universe@184 | 417 | dao.listProjects(), |
universe@184 | 418 | projectInfo, |
universe@184 | 419 | selectedComponent = component |
universe@184 | 420 | ) |
universe@184 | 421 | styleSheets = listOf("projects") |
universe@184 | 422 | render("component-form") |
universe@184 | 423 | } |
universe@184 | 424 | } |
universe@184 | 425 | |
universe@193 | 426 | private fun componentCommit(http: HttpRequest, dao: DataAccessObject) { |
universe@210 | 427 | val idParams = obtainIdAndProject(http, dao) ?: return |
universe@210 | 428 | val (id, project) = idParams |
universe@184 | 429 | |
universe@210 | 430 | val component = Component(id, project.id).apply { |
universe@184 | 431 | name = http.param("name") ?: "" |
universe@184 | 432 | node = http.param("node") ?: "" |
universe@184 | 433 | ordinal = http.param("ordinal")?.toIntOrNull() ?: 0 |
universe@184 | 434 | color = WebColor(http.param("color") ?: "#000000") |
universe@184 | 435 | description = http.param("description") |
universe@227 | 436 | // TODO: process error message |
universe@227 | 437 | active = http.param("active", ::boolValidator, true, mutableListOf()) |
universe@184 | 438 | lead = (http.param("lead")?.toIntOrNull() ?: -1).let { |
universe@184 | 439 | if (it < 0) null else dao.findUser(it) |
universe@184 | 440 | } |
universe@184 | 441 | // intentional defaults |
universe@184 | 442 | if (node.isBlank()) node = name |
universe@184 | 443 | // sanitizing |
universe@184 | 444 | node = sanitizeNode(node) |
universe@184 | 445 | } |
universe@184 | 446 | |
universe@184 | 447 | if (id < 0) { |
universe@184 | 448 | dao.insertComponent(component) |
universe@184 | 449 | } else { |
universe@184 | 450 | dao.updateComponent(component) |
universe@184 | 451 | } |
universe@184 | 452 | |
universe@184 | 453 | http.renderCommit("projects/${project.node}/components/") |
universe@184 | 454 | } |
universe@184 | 455 | |
universe@193 | 456 | private fun issue(http: HttpRequest, dao: DataAccessObject) { |
universe@263 | 457 | val issue = http.pathParams["issue"]?.toIntOrNull()?.let(dao::findIssue) |
universe@263 | 458 | if (issue == null) { |
universe@263 | 459 | http.response.sendError(404) |
universe@263 | 460 | return |
universe@263 | 461 | } |
universe@263 | 462 | renderIssueView(http, dao, issue) |
universe@263 | 463 | } |
universe@263 | 464 | |
universe@263 | 465 | private fun renderIssueView( |
universe@263 | 466 | http: HttpRequest, |
universe@263 | 467 | dao: DataAccessObject, |
universe@263 | 468 | issue: Issue, |
universe@263 | 469 | relationError: String? = null |
universe@263 | 470 | ) { |
universe@184 | 471 | withPathInfo(http, dao)?.run { |
universe@184 | 472 | val comments = dao.listComments(issue) |
universe@184 | 473 | |
universe@184 | 474 | with(http) { |
universe@205 | 475 | pageTitle = "${projectInfo.project.name}: #${issue.id} ${issue.subject}" |
universe@263 | 476 | view = IssueDetailView( |
universe@263 | 477 | issue, |
universe@263 | 478 | comments, |
universe@263 | 479 | project, |
universe@263 | 480 | version, |
universe@263 | 481 | component, |
universe@268 | 482 | dao.listIssues(project, true), |
universe@263 | 483 | dao.listIssueRelations(issue), |
universe@263 | 484 | relationError |
universe@263 | 485 | ) |
universe@198 | 486 | feedPath = feedPath(projectInfo.project) |
universe@184 | 487 | navigationMenu = activeProjectNavMenu( |
universe@184 | 488 | dao.listProjects(), |
universe@184 | 489 | projectInfo, |
universe@184 | 490 | version, |
universe@184 | 491 | component |
universe@184 | 492 | ) |
universe@184 | 493 | styleSheets = listOf("projects") |
universe@207 | 494 | javascript = "issue-editor" |
universe@184 | 495 | render("issue-view") |
universe@184 | 496 | } |
universe@184 | 497 | } |
universe@184 | 498 | } |
universe@184 | 499 | |
universe@193 | 500 | private fun issueForm(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 501 | withPathInfo(http, dao)?.run { |
universe@263 | 502 | val issue = http.pathParams["issue"]?.toIntOrNull()?.let(dao::findIssue) ?: Issue( |
universe@184 | 503 | -1, |
universe@198 | 504 | project, |
universe@184 | 505 | ) |
universe@184 | 506 | |
universe@215 | 507 | // for new issues set some defaults |
universe@215 | 508 | if (issue.id < 0) { |
universe@215 | 509 | // pre-select component, if available in the path info |
universe@215 | 510 | issue.component = component |
universe@184 | 511 | |
universe@215 | 512 | // pre-select version, if available in the path info |
universe@215 | 513 | if (version != null) { |
universe@215 | 514 | if (version.status.isReleased) { |
universe@231 | 515 | issue.affected = version |
universe@215 | 516 | } else { |
universe@231 | 517 | issue.resolved = version |
universe@215 | 518 | } |
universe@191 | 519 | } |
universe@191 | 520 | } |
universe@191 | 521 | |
universe@184 | 522 | with(http) { |
universe@184 | 523 | view = IssueEditView( |
universe@184 | 524 | issue, |
universe@184 | 525 | projectInfo.versions, |
universe@184 | 526 | projectInfo.components, |
universe@184 | 527 | dao.listUsers(), |
universe@198 | 528 | project, |
universe@184 | 529 | version, |
universe@184 | 530 | component |
universe@184 | 531 | ) |
universe@198 | 532 | feedPath = feedPath(projectInfo.project) |
universe@184 | 533 | navigationMenu = activeProjectNavMenu( |
universe@184 | 534 | dao.listProjects(), |
universe@184 | 535 | projectInfo, |
universe@184 | 536 | version, |
universe@184 | 537 | component |
universe@184 | 538 | ) |
universe@184 | 539 | styleSheets = listOf("projects") |
universe@207 | 540 | javascript = "issue-editor" |
universe@184 | 541 | render("issue-form") |
universe@184 | 542 | } |
universe@184 | 543 | } |
universe@184 | 544 | } |
universe@184 | 545 | |
universe@193 | 546 | private fun issueComment(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 547 | withPathInfo(http, dao)?.run { |
universe@263 | 548 | val issue = http.pathParams["issue"]?.toIntOrNull()?.let(dao::findIssue) |
universe@184 | 549 | if (issue == null) { |
universe@184 | 550 | http.response.sendError(404) |
universe@184 | 551 | return |
universe@184 | 552 | } |
universe@184 | 553 | |
universe@207 | 554 | val commentId = http.param("commentid")?.toIntOrNull() ?: -1 |
universe@207 | 555 | if (commentId > 0) { |
universe@207 | 556 | val comment = dao.findComment(commentId) |
universe@232 | 557 | if (comment == null) { |
universe@232 | 558 | http.response.sendError(404) |
universe@232 | 559 | return |
universe@232 | 560 | } |
universe@232 | 561 | val originalAuthor = comment.author?.username |
universe@207 | 562 | if (originalAuthor != null && originalAuthor == http.remoteUser) { |
universe@232 | 563 | val newComment = http.param("comment") |
universe@232 | 564 | if (!newComment.isNullOrBlank()) { |
universe@232 | 565 | comment.comment = newComment |
universe@232 | 566 | dao.updateComment(comment) |
universe@242 | 567 | dao.insertHistoryEvent(issue, comment) |
universe@232 | 568 | } else { |
universe@247 | 569 | logger.debug("Not updating comment ${comment.id} because nothing changed.") |
universe@232 | 570 | } |
universe@207 | 571 | } else { |
universe@207 | 572 | http.response.sendError(403) |
universe@207 | 573 | return |
universe@207 | 574 | } |
universe@207 | 575 | } else { |
universe@207 | 576 | val comment = IssueComment(-1, issue.id).apply { |
universe@207 | 577 | author = http.remoteUser?.let { dao.findUserByName(it) } |
universe@207 | 578 | comment = http.param("comment") ?: "" |
universe@207 | 579 | } |
universe@232 | 580 | val newId = dao.insertComment(comment) |
universe@242 | 581 | dao.insertHistoryEvent(issue, comment, newId) |
universe@184 | 582 | } |
universe@184 | 583 | |
universe@184 | 584 | http.renderCommit("${issuesHref}${issue.id}") |
universe@184 | 585 | } |
universe@184 | 586 | } |
universe@184 | 587 | |
universe@193 | 588 | private fun issueCommit(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 589 | withPathInfo(http, dao)?.run { |
universe@184 | 590 | val issue = Issue( |
universe@184 | 591 | http.param("id")?.toIntOrNull() ?: -1, |
universe@198 | 592 | project |
universe@184 | 593 | ).apply { |
universe@184 | 594 | component = dao.findComponent(http.param("component")?.toIntOrNull() ?: -1) |
universe@184 | 595 | category = IssueCategory.valueOf(http.param("category") ?: "") |
universe@184 | 596 | status = IssueStatus.valueOf(http.param("status") ?: "") |
universe@184 | 597 | subject = http.param("subject") ?: "" |
universe@184 | 598 | description = http.param("description") ?: "" |
universe@184 | 599 | assignee = http.param("assignee")?.toIntOrNull()?.let { |
universe@184 | 600 | when (it) { |
universe@184 | 601 | -1 -> null |
universe@184 | 602 | -2 -> component?.lead |
universe@184 | 603 | else -> dao.findUser(it) |
universe@184 | 604 | } |
universe@184 | 605 | } |
universe@225 | 606 | // TODO: process error messages |
universe@225 | 607 | eta = http.param("eta", ::dateOptValidator, null, mutableListOf()) |
universe@184 | 608 | |
universe@231 | 609 | affected = http.param("affected")?.toIntOrNull()?.takeIf { it > 0 }?.let { Version(it, project.id) } |
universe@231 | 610 | resolved = http.param("resolved")?.toIntOrNull()?.takeIf { it > 0 }?.let { Version(it, project.id) } |
universe@184 | 611 | } |
universe@184 | 612 | |
universe@186 | 613 | val openId = if (issue.id < 0) { |
universe@232 | 614 | val id = dao.insertIssue(issue) |
universe@232 | 615 | dao.insertHistoryEvent(issue, id) |
universe@232 | 616 | id |
universe@186 | 617 | } else { |
universe@232 | 618 | val reference = dao.findIssue(issue.id) |
universe@232 | 619 | if (reference == null) { |
universe@232 | 620 | http.response.sendError(404) |
universe@232 | 621 | return |
universe@232 | 622 | } |
universe@232 | 623 | |
universe@232 | 624 | if (issue.hasChanged(reference)) { |
universe@232 | 625 | dao.updateIssue(issue) |
universe@232 | 626 | dao.insertHistoryEvent(issue) |
universe@232 | 627 | } else { |
universe@247 | 628 | logger.debug("Not updating issue ${issue.id} because nothing changed.") |
universe@232 | 629 | } |
universe@232 | 630 | |
universe@214 | 631 | val newComment = http.param("comment") |
universe@214 | 632 | if (!newComment.isNullOrBlank()) { |
universe@232 | 633 | val comment = IssueComment(-1, issue.id).apply { |
universe@214 | 634 | author = http.remoteUser?.let { dao.findUserByName(it) } |
universe@214 | 635 | comment = newComment |
universe@232 | 636 | } |
universe@232 | 637 | val commentid = dao.insertComment(comment) |
universe@242 | 638 | dao.insertHistoryEvent(issue, comment, commentid) |
universe@214 | 639 | } |
universe@186 | 640 | issue.id |
universe@186 | 641 | } |
universe@186 | 642 | |
universe@186 | 643 | if (http.param("more") != null) { |
universe@185 | 644 | http.renderCommit("${issuesHref}-/create") |
universe@185 | 645 | } else { |
universe@186 | 646 | http.renderCommit("${issuesHref}${openId}") |
universe@185 | 647 | } |
universe@184 | 648 | } |
universe@184 | 649 | } |
universe@263 | 650 | |
universe@263 | 651 | private fun issueRelation(http: HttpRequest, dao: DataAccessObject) { |
universe@263 | 652 | withPathInfo(http, dao)?.run { |
universe@263 | 653 | val issue = http.pathParams["issue"]?.toIntOrNull()?.let(dao::findIssue) |
universe@263 | 654 | if (issue == null) { |
universe@263 | 655 | http.response.sendError(404) |
universe@263 | 656 | return |
universe@263 | 657 | } |
universe@263 | 658 | |
universe@263 | 659 | // determine the relation type |
universe@263 | 660 | val type: Pair<RelationType, Boolean>? = http.param("type")?.let { |
universe@263 | 661 | try { |
universe@263 | 662 | if (it.startsWith("!")) { |
universe@263 | 663 | Pair(RelationType.valueOf(it.substring(1)), true) |
universe@263 | 664 | } else { |
universe@263 | 665 | Pair(RelationType.valueOf(it), false) |
universe@263 | 666 | } |
universe@263 | 667 | } catch (_: IllegalArgumentException) { |
universe@263 | 668 | null |
universe@263 | 669 | } |
universe@263 | 670 | } |
universe@263 | 671 | |
universe@263 | 672 | // if the relation type was invalid, send HTTP 500 |
universe@263 | 673 | if (type == null) { |
universe@263 | 674 | http.response.sendError(500) |
universe@263 | 675 | return |
universe@263 | 676 | } |
universe@263 | 677 | |
universe@263 | 678 | // determine the target issue |
universe@263 | 679 | val targetIssue: Issue? = http.param("issue")?.let { |
universe@263 | 680 | if (it.startsWith("#") && it.length > 1) { |
universe@263 | 681 | it.substring(1).split(" ", limit = 2)[0].toIntOrNull() |
universe@263 | 682 | ?.let(dao::findIssue) |
universe@263 | 683 | ?.takeIf { target -> target.project.id == issue.project.id } |
universe@263 | 684 | } else { |
universe@263 | 685 | null |
universe@263 | 686 | } |
universe@263 | 687 | } |
universe@263 | 688 | |
universe@263 | 689 | // check if the target issue is valid |
universe@263 | 690 | if (targetIssue == null) { |
universe@263 | 691 | renderIssueView(http, dao, issue, "issue.relations.target.invalid") |
universe@263 | 692 | return |
universe@263 | 693 | } |
universe@263 | 694 | |
universe@263 | 695 | // commit the result |
universe@263 | 696 | dao.insertIssueRelation(IssueRelation(issue, targetIssue, type.first, type.second)) |
universe@263 | 697 | http.renderCommit("${issuesHref}${issue.id}") |
universe@263 | 698 | } |
universe@263 | 699 | } |
universe@263 | 700 | |
universe@263 | 701 | private fun issueRemoveRelation(http: HttpRequest, dao: DataAccessObject) { |
universe@263 | 702 | withPathInfo(http, dao)?.run { |
universe@263 | 703 | val issue = http.pathParams["issue"]?.toIntOrNull()?.let(dao::findIssue) |
universe@263 | 704 | if (issue == null) { |
universe@263 | 705 | http.response.sendError(404) |
universe@263 | 706 | return |
universe@263 | 707 | } |
universe@263 | 708 | |
universe@263 | 709 | // determine relation |
universe@263 | 710 | val type = http.param("type")?.let { |
universe@263 | 711 | try {RelationType.valueOf(it)} |
universe@263 | 712 | catch (_:IllegalArgumentException) {null} |
universe@263 | 713 | } |
universe@263 | 714 | if (type == null) { |
universe@263 | 715 | http.response.sendError(500) |
universe@263 | 716 | return |
universe@263 | 717 | } |
universe@263 | 718 | val rel = http.param("to")?.toIntOrNull()?.let(dao::findIssue)?.let { |
universe@263 | 719 | IssueRelation( |
universe@263 | 720 | issue, |
universe@263 | 721 | it, |
universe@263 | 722 | type, |
universe@263 | 723 | http.param("reverse")?.toBoolean() ?: false |
universe@263 | 724 | ) |
universe@263 | 725 | } |
universe@263 | 726 | |
universe@263 | 727 | // execute removal, if there is something to remove |
universe@263 | 728 | rel?.run(dao::deleteIssueRelation) |
universe@263 | 729 | |
universe@263 | 730 | // always pretend that the operation was successful - if there was nothing to remove, it's okay |
universe@263 | 731 | http.renderCommit("${issuesHref}${issue.id}") |
universe@263 | 732 | } |
universe@263 | 733 | } |
universe@184 | 734 | } |