src/main/kotlin/de/uapcore/lightpit/viewmodel/Issues.kt

Mon, 30 Oct 2023 10:06:22 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 30 Oct 2023 10:06:22 +0100
changeset 291
bcf05cccac6f
parent 284
671c1c8fbf1c
child 292
703591e739f4
permissions
-rw-r--r--

replace Enum.values() with Enum.entries

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.viewmodel
universe@184 27
universe@184 28 import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension
universe@184 29 import com.vladsch.flexmark.ext.tables.TablesExtension
universe@184 30 import com.vladsch.flexmark.html.HtmlRenderer
universe@184 31 import com.vladsch.flexmark.parser.Parser
universe@184 32 import com.vladsch.flexmark.util.data.MutableDataSet
universe@234 33 import com.vladsch.flexmark.util.data.SharedDataKeys
universe@268 34 import de.uapcore.lightpit.HttpRequest
universe@184 35 import de.uapcore.lightpit.entities.*
universe@263 36 import de.uapcore.lightpit.types.*
universe@184 37 import kotlin.math.roundToInt
universe@184 38
universe@249 39 class IssueSorter(private vararg val criteria: Criteria) : Comparator<Issue> {
universe@249 40 enum class Field {
universe@271 41 DONE, PHASE, STATUS, CATEGORY, ETA, UPDATED, CREATED;
universe@271 42
universe@271 43 val resourceKey: String by lazy {
universe@271 44 if (this == DONE) "issue.filter.sort.done"
universe@271 45 else if (this == PHASE) "issue.filter.sort.phase"
universe@271 46 else "issue.${this.name.lowercase()}"
universe@271 47 }
universe@249 48 }
universe@249 49
universe@271 50 data class Criteria(val field: Field, val asc: Boolean = true) {
universe@271 51 override fun toString(): String {
universe@271 52 return "$field.$asc"
universe@271 53 }
universe@271 54 }
universe@249 55
universe@249 56 override fun compare(left: Issue, right: Issue): Int {
universe@249 57 if (left == right) {
universe@260 58 return 0
universe@249 59 }
universe@249 60 for (c in criteria) {
universe@249 61 val result = when (c.field) {
universe@267 62 Field.PHASE -> left.status.phase.compareTo(right.status.phase)
universe@267 63 Field.DONE -> (left.status.phase == IssueStatusPhase.Done).compareTo(right.status.phase == IssueStatusPhase.Done)
universe@265 64 Field.STATUS -> left.status.compareTo(right.status)
universe@265 65 Field.CATEGORY -> left.category.compareTo(right.category)
universe@265 66 Field.ETA -> left.compareEtaTo(right.eta)
universe@249 67 Field.UPDATED -> left.updated.compareTo(right.updated)
universe@265 68 Field.CREATED -> left.created.compareTo(right.created)
universe@249 69 }
universe@249 70 if (result != 0) {
universe@249 71 return if (c.asc) result else -result
universe@249 72 }
universe@249 73 }
universe@249 74 return 0
universe@249 75 }
universe@249 76 }
universe@249 77
universe@184 78 class IssueSummary {
universe@184 79 var open = 0
universe@184 80 var active = 0
universe@184 81 var done = 0
universe@184 82
universe@184 83 val total get() = open + active + done
universe@184 84
universe@184 85 val openPercent get() = 100 - activePercent - donePercent
universe@184 86 val activePercent get() = if (total > 0) (100f * active / total).roundToInt() else 0
universe@184 87 val donePercent get() = if (total > 0) (100f * done / total).roundToInt() else 100
universe@184 88
universe@184 89 /**
universe@184 90 * Adds the specified issue to the summary by incrementing the respective counter.
universe@184 91 * @param issue the issue
universe@184 92 */
universe@184 93 fun add(issue: Issue) {
universe@184 94 when (issue.status.phase) {
universe@184 95 IssueStatusPhase.Open -> open++
universe@184 96 IssueStatusPhase.WorkInProgress -> active++
universe@184 97 IssueStatusPhase.Done -> done++
universe@184 98 }
universe@184 99 }
universe@184 100 }
universe@184 101
universe@284 102 data class CommitLink(val url: String, val hash: String, val message: String)
universe@284 103
universe@184 104 class IssueDetailView(
universe@184 105 val issue: Issue,
universe@184 106 val comments: List<IssueComment>,
universe@184 107 val project: Project,
universe@263 108 val version: Version?,
universe@263 109 val component: Component?,
universe@263 110 projectIssues: List<Issue>,
universe@263 111 val currentRelations: List<IssueRelation>,
universe@263 112 /**
universe@263 113 * Optional resource key to an error message for the relation editor.
universe@263 114 */
universe@284 115 val relationError: String?,
universe@284 116 commitRefs: List<CommitRef>
universe@184 117 ) : View() {
universe@291 118 val relationTypes = RelationType.entries
universe@263 119 val linkableIssues = projectIssues.filterNot { it.id == issue.id }
universe@284 120 val commitLinks: List<CommitLink>
universe@263 121
universe@234 122 private val parser: Parser
universe@234 123 private val renderer: HtmlRenderer
universe@184 124
universe@184 125 init {
universe@184 126 val options = MutableDataSet()
universe@234 127 .set(SharedDataKeys.EXTENSIONS, listOf(TablesExtension.create(), StrikethroughExtension.create()))
universe@234 128 parser = Parser.builder(options).build()
universe@268 129 renderer = HtmlRenderer.builder(
universe@268 130 options
universe@268 131 .set(HtmlRenderer.ESCAPE_HTML, true)
universe@234 132 ).build()
universe@184 133
universe@234 134 issue.description = formatMarkdown(issue.description ?: "")
universe@184 135 for (comment in comments) {
universe@234 136 comment.commentFormatted = formatMarkdown(comment.comment)
universe@184 137 }
universe@284 138
universe@284 139 val commitBaseUrl = project.repoUrl
universe@284 140 commitLinks = (if (commitBaseUrl == null || project.vcs == VcsType.None) emptyList() else commitRefs.map {
universe@284 141 CommitLink(buildCommitUrl(commitBaseUrl, project.vcs, it.hash), it.hash, it.message)
universe@284 142 })
universe@184 143 }
universe@234 144
universe@284 145 private fun buildCommitUrl(baseUrl: String, vcs: VcsType, hash: String): String =
universe@284 146 with (StringBuilder(baseUrl)) {
universe@284 147 if (!endsWith("/")) append('/')
universe@284 148 when (vcs) {
universe@284 149 VcsType.Mercurial -> append("rev/")
universe@284 150 else -> append("commit/")
universe@284 151 }
universe@284 152 append(hash)
universe@284 153 toString()
universe@284 154 }
universe@284 155
universe@234 156 private fun formatEmojis(text: String) = text
universe@234 157 .replace("(/)", "&#9989;")
universe@234 158 .replace("(x)", "&#10060;")
universe@234 159 .replace("(!)", "&#9889;")
universe@234 160
universe@234 161 private fun formatMarkdown(text: String) =
universe@234 162 renderer.render(parser.parse(formatEmojis(text)))
universe@184 163 }
universe@184 164
universe@184 165 class IssueEditView(
universe@184 166 val issue: Issue,
universe@184 167 val versions: List<Version>,
universe@184 168 val components: List<Component>,
universe@184 169 val users: List<User>,
universe@184 170 val project: Project, // TODO: allow null values to create issues from the IssuesServlet
universe@184 171 val version: Version? = null,
universe@184 172 val component: Component? = null
universe@184 173 ) : EditView() {
universe@184 174
universe@184 175 val versionsUpcoming: List<Version>
universe@184 176 val versionsRecent: List<Version>
universe@184 177
universe@291 178 val issueStatus = IssueStatus.entries
universe@291 179 val issueCategory = IssueCategory.entries
universe@184 180
universe@184 181 init {
universe@184 182 val recent = mutableListOf<Version>()
universe@231 183 issue.affected?.let { recent.add(it) }
universe@184 184 val upcoming = mutableListOf<Version>()
universe@231 185 issue.resolved?.let { upcoming.add(it) }
universe@231 186
universe@184 187 for (v in versions) {
universe@184 188 if (v.status.isReleased) {
universe@184 189 if (v.status != VersionStatus.Deprecated) recent.add(v)
universe@184 190 } else {
universe@184 191 upcoming.add(v)
universe@184 192 }
universe@184 193 }
universe@186 194 versionsRecent = recent.distinct()
universe@186 195 versionsUpcoming = upcoming.distinct()
universe@184 196 }
universe@184 197 }
universe@184 198
universe@268 199 class IssueFilter(http: HttpRequest) {
universe@268 200
universe@291 201 val issueStatus = IssueStatus.entries
universe@291 202 val issueCategory = IssueCategory.entries
universe@291 203 val sortCriteria = IssueSorter.Field.entries.flatMap { listOf(IssueSorter.Criteria(it, true), IssueSorter.Criteria(it, false)) }
universe@268 204 val flagIncludeDone = "f.0"
universe@268 205 val flagMine = "f.1"
universe@268 206 val flagBlocker = "f.2"
universe@268 207
universe@268 208 val includeDone: Boolean = evalFlag(http, flagIncludeDone)
universe@268 209 val onlyMine: Boolean = evalFlag(http, flagMine)
universe@268 210 val onlyBlocker: Boolean = evalFlag(http, flagBlocker)
universe@268 211 val status: List<IssueStatus> = evalEnum(http, "s")
universe@268 212 val category: List<IssueCategory> = evalEnum(http, "c")
universe@268 213
universe@271 214 val sortPrimary: IssueSorter.Criteria = evalSort(http, "primary", IssueSorter.Criteria(IssueSorter.Field.DONE))
universe@271 215 val sortSecondary: IssueSorter.Criteria = evalSort(http, "secondary", IssueSorter.Criteria(IssueSorter.Field.ETA))
universe@271 216 val sortTertiary: IssueSorter.Criteria = evalSort(http, "tertiary", IssueSorter.Criteria(IssueSorter.Field.UPDATED, false))
universe@271 217
universe@271 218 private fun evalSort(http: HttpRequest, prio: String, defaultValue: IssueSorter.Criteria): IssueSorter.Criteria {
universe@271 219 val param = http.param("sort_$prio")
universe@271 220 if (param != null) {
universe@271 221 http.session.removeAttribute("sort_$prio")
universe@271 222 val p = param.split(".")
universe@271 223 if (p.size > 1) {
universe@271 224 try {
universe@271 225 http.session.setAttribute("sort_$prio", IssueSorter.Criteria(enumValueOf(p[0]), p[1].toBoolean()))
universe@271 226 } catch (_:IllegalArgumentException) {
universe@271 227 // ignore malfored values
universe@271 228 }
universe@271 229 }
universe@271 230 }
universe@271 231 return http.session.getAttribute("sort_$prio") as IssueSorter.Criteria? ?: defaultValue
universe@271 232 }
universe@271 233
universe@268 234 private fun evalFlag(http: HttpRequest, name: String): Boolean {
universe@268 235 val param = http.paramArray("filter")
universe@268 236 if (param.isNotEmpty()) {
universe@268 237 if (param.contains(name)) {
universe@268 238 http.session.setAttribute(name, true)
universe@268 239 } else {
universe@268 240 http.session.removeAttribute(name)
universe@268 241 }
universe@268 242 }
universe@268 243 return http.session.getAttribute(name) != null
universe@268 244 }
universe@268 245
universe@268 246 private inline fun <reified T : Enum<T>> evalEnum(http: HttpRequest, prefix: String): List<T> {
universe@268 247 val sattr = "f.${prefix}"
universe@268 248 val param = http.paramArray("filter")
universe@268 249 if (param.isNotEmpty()) {
universe@268 250 val list = param.filter { it.startsWith("${prefix}.") }
universe@268 251 .map { it.substring(prefix.length + 1) }
universe@268 252 .map {
universe@268 253 try {
universe@268 254 // quick and very dirty validation
universe@268 255 enumValueOf<T>(it)
universe@268 256 } catch (_: IllegalArgumentException) {
universe@268 257 // skip
universe@268 258 }
universe@268 259 }
universe@268 260 if (list.isEmpty()) {
universe@268 261 http.session.removeAttribute(sattr)
universe@268 262 } else {
universe@268 263 http.session.setAttribute(sattr, list.joinToString(","))
universe@268 264 }
universe@268 265 }
universe@268 266
universe@268 267 return http.session.getAttribute(sattr)
universe@268 268 ?.toString()
universe@268 269 ?.split(",")
universe@268 270 ?.map { enumValueOf(it) }
universe@268 271 ?: emptyList()
universe@268 272 }
universe@268 273 }

mercurial