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

Mon, 30 Oct 2023 14:44:36 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 30 Oct 2023 14:44:36 +0100
changeset 292
703591e739f4
parent 291
bcf05cccac6f
permissions
-rw-r--r--

add possibility to show issues w/o version or component - fixes #335

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@292 105 val pathInfos: PathInfos,
universe@184 106 val issue: Issue,
universe@184 107 val comments: List<IssueComment>,
universe@184 108 val project: Project,
universe@263 109 projectIssues: List<Issue>,
universe@263 110 val currentRelations: List<IssueRelation>,
universe@263 111 /**
universe@263 112 * Optional resource key to an error message for the relation editor.
universe@263 113 */
universe@284 114 val relationError: String?,
universe@284 115 commitRefs: List<CommitRef>
universe@184 116 ) : View() {
universe@291 117 val relationTypes = RelationType.entries
universe@263 118 val linkableIssues = projectIssues.filterNot { it.id == issue.id }
universe@284 119 val commitLinks: List<CommitLink>
universe@263 120
universe@234 121 private val parser: Parser
universe@234 122 private val renderer: HtmlRenderer
universe@184 123
universe@184 124 init {
universe@184 125 val options = MutableDataSet()
universe@234 126 .set(SharedDataKeys.EXTENSIONS, listOf(TablesExtension.create(), StrikethroughExtension.create()))
universe@234 127 parser = Parser.builder(options).build()
universe@268 128 renderer = HtmlRenderer.builder(
universe@268 129 options
universe@268 130 .set(HtmlRenderer.ESCAPE_HTML, true)
universe@234 131 ).build()
universe@184 132
universe@234 133 issue.description = formatMarkdown(issue.description ?: "")
universe@184 134 for (comment in comments) {
universe@234 135 comment.commentFormatted = formatMarkdown(comment.comment)
universe@184 136 }
universe@284 137
universe@284 138 val commitBaseUrl = project.repoUrl
universe@284 139 commitLinks = (if (commitBaseUrl == null || project.vcs == VcsType.None) emptyList() else commitRefs.map {
universe@284 140 CommitLink(buildCommitUrl(commitBaseUrl, project.vcs, it.hash), it.hash, it.message)
universe@284 141 })
universe@184 142 }
universe@234 143
universe@284 144 private fun buildCommitUrl(baseUrl: String, vcs: VcsType, hash: String): String =
universe@284 145 with (StringBuilder(baseUrl)) {
universe@284 146 if (!endsWith("/")) append('/')
universe@284 147 when (vcs) {
universe@284 148 VcsType.Mercurial -> append("rev/")
universe@284 149 else -> append("commit/")
universe@284 150 }
universe@284 151 append(hash)
universe@284 152 toString()
universe@284 153 }
universe@284 154
universe@234 155 private fun formatEmojis(text: String) = text
universe@234 156 .replace("(/)", "&#9989;")
universe@234 157 .replace("(x)", "&#10060;")
universe@234 158 .replace("(!)", "&#9889;")
universe@234 159
universe@234 160 private fun formatMarkdown(text: String) =
universe@234 161 renderer.render(parser.parse(formatEmojis(text)))
universe@184 162 }
universe@184 163
universe@184 164 class IssueEditView(
universe@184 165 val issue: Issue,
universe@184 166 val versions: List<Version>,
universe@184 167 val components: List<Component>,
universe@184 168 val users: List<User>,
universe@184 169 val project: Project, // TODO: allow null values to create issues from the IssuesServlet
universe@292 170 val pathInfos: PathInfos
universe@184 171 ) : EditView() {
universe@184 172
universe@184 173 val versionsUpcoming: List<Version>
universe@184 174 val versionsRecent: List<Version>
universe@184 175
universe@291 176 val issueStatus = IssueStatus.entries
universe@291 177 val issueCategory = IssueCategory.entries
universe@184 178
universe@184 179 init {
universe@184 180 val recent = mutableListOf<Version>()
universe@231 181 issue.affected?.let { recent.add(it) }
universe@184 182 val upcoming = mutableListOf<Version>()
universe@231 183 issue.resolved?.let { upcoming.add(it) }
universe@231 184
universe@184 185 for (v in versions) {
universe@184 186 if (v.status.isReleased) {
universe@184 187 if (v.status != VersionStatus.Deprecated) recent.add(v)
universe@184 188 } else {
universe@184 189 upcoming.add(v)
universe@184 190 }
universe@184 191 }
universe@186 192 versionsRecent = recent.distinct()
universe@186 193 versionsUpcoming = upcoming.distinct()
universe@184 194 }
universe@184 195 }
universe@184 196
universe@268 197 class IssueFilter(http: HttpRequest) {
universe@268 198
universe@291 199 val issueStatus = IssueStatus.entries
universe@291 200 val issueCategory = IssueCategory.entries
universe@291 201 val sortCriteria = IssueSorter.Field.entries.flatMap { listOf(IssueSorter.Criteria(it, true), IssueSorter.Criteria(it, false)) }
universe@268 202 val flagIncludeDone = "f.0"
universe@268 203 val flagMine = "f.1"
universe@268 204 val flagBlocker = "f.2"
universe@268 205
universe@268 206 val includeDone: Boolean = evalFlag(http, flagIncludeDone)
universe@268 207 val onlyMine: Boolean = evalFlag(http, flagMine)
universe@268 208 val onlyBlocker: Boolean = evalFlag(http, flagBlocker)
universe@268 209 val status: List<IssueStatus> = evalEnum(http, "s")
universe@268 210 val category: List<IssueCategory> = evalEnum(http, "c")
universe@268 211
universe@271 212 val sortPrimary: IssueSorter.Criteria = evalSort(http, "primary", IssueSorter.Criteria(IssueSorter.Field.DONE))
universe@271 213 val sortSecondary: IssueSorter.Criteria = evalSort(http, "secondary", IssueSorter.Criteria(IssueSorter.Field.ETA))
universe@271 214 val sortTertiary: IssueSorter.Criteria = evalSort(http, "tertiary", IssueSorter.Criteria(IssueSorter.Field.UPDATED, false))
universe@271 215
universe@271 216 private fun evalSort(http: HttpRequest, prio: String, defaultValue: IssueSorter.Criteria): IssueSorter.Criteria {
universe@271 217 val param = http.param("sort_$prio")
universe@271 218 if (param != null) {
universe@271 219 http.session.removeAttribute("sort_$prio")
universe@271 220 val p = param.split(".")
universe@271 221 if (p.size > 1) {
universe@271 222 try {
universe@271 223 http.session.setAttribute("sort_$prio", IssueSorter.Criteria(enumValueOf(p[0]), p[1].toBoolean()))
universe@271 224 } catch (_:IllegalArgumentException) {
universe@271 225 // ignore malfored values
universe@271 226 }
universe@271 227 }
universe@271 228 }
universe@271 229 return http.session.getAttribute("sort_$prio") as IssueSorter.Criteria? ?: defaultValue
universe@271 230 }
universe@271 231
universe@268 232 private fun evalFlag(http: HttpRequest, name: String): Boolean {
universe@268 233 val param = http.paramArray("filter")
universe@268 234 if (param.isNotEmpty()) {
universe@268 235 if (param.contains(name)) {
universe@268 236 http.session.setAttribute(name, true)
universe@268 237 } else {
universe@268 238 http.session.removeAttribute(name)
universe@268 239 }
universe@268 240 }
universe@268 241 return http.session.getAttribute(name) != null
universe@268 242 }
universe@268 243
universe@268 244 private inline fun <reified T : Enum<T>> evalEnum(http: HttpRequest, prefix: String): List<T> {
universe@268 245 val sattr = "f.${prefix}"
universe@268 246 val param = http.paramArray("filter")
universe@268 247 if (param.isNotEmpty()) {
universe@268 248 val list = param.filter { it.startsWith("${prefix}.") }
universe@268 249 .map { it.substring(prefix.length + 1) }
universe@268 250 .map {
universe@268 251 try {
universe@268 252 // quick and very dirty validation
universe@268 253 enumValueOf<T>(it)
universe@268 254 } catch (_: IllegalArgumentException) {
universe@268 255 // skip
universe@268 256 }
universe@268 257 }
universe@268 258 if (list.isEmpty()) {
universe@268 259 http.session.removeAttribute(sattr)
universe@268 260 } else {
universe@268 261 http.session.setAttribute(sattr, list.joinToString(","))
universe@268 262 }
universe@268 263 }
universe@268 264
universe@268 265 return http.session.getAttribute(sattr)
universe@268 266 ?.toString()
universe@268 267 ?.split(",")
universe@268 268 ?.map { enumValueOf(it) }
universe@268 269 ?: emptyList()
universe@268 270 }
universe@268 271 }

mercurial