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