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

Sat, 27 Nov 2021 10:44:17 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 27 Nov 2021 10:44:17 +0100
changeset 239
9365c7fb0240
parent 234
d71bc6db42ef
child 249
6bded7090719
permissions
-rw-r--r--

#109 add assignee filter to rss feed

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@184 34 import de.uapcore.lightpit.entities.*
universe@184 35 import de.uapcore.lightpit.types.IssueCategory
universe@184 36 import de.uapcore.lightpit.types.IssueStatus
universe@184 37 import de.uapcore.lightpit.types.IssueStatusPhase
universe@184 38 import de.uapcore.lightpit.types.VersionStatus
universe@184 39 import kotlin.math.roundToInt
universe@184 40
universe@184 41 class IssueSummary {
universe@184 42 var open = 0
universe@184 43 var active = 0
universe@184 44 var done = 0
universe@184 45
universe@184 46 val total get() = open + active + done
universe@184 47
universe@184 48 val openPercent get() = 100 - activePercent - donePercent
universe@184 49 val activePercent get() = if (total > 0) (100f * active / total).roundToInt() else 0
universe@184 50 val donePercent get() = if (total > 0) (100f * done / total).roundToInt() else 100
universe@184 51
universe@184 52 /**
universe@184 53 * Adds the specified issue to the summary by incrementing the respective counter.
universe@184 54 * @param issue the issue
universe@184 55 */
universe@184 56 fun add(issue: Issue) {
universe@184 57 when (issue.status.phase) {
universe@184 58 IssueStatusPhase.Open -> open++
universe@184 59 IssueStatusPhase.WorkInProgress -> active++
universe@184 60 IssueStatusPhase.Done -> done++
universe@184 61 }
universe@184 62 }
universe@184 63 }
universe@184 64
universe@184 65 class IssueDetailView(
universe@184 66 val issue: Issue,
universe@184 67 val comments: List<IssueComment>,
universe@184 68 val project: Project,
universe@184 69 val version: Version? = null,
universe@184 70 val component: Component? = null
universe@184 71 ) : View() {
universe@234 72 private val parser: Parser
universe@234 73 private val renderer: HtmlRenderer
universe@184 74
universe@184 75 init {
universe@184 76 val options = MutableDataSet()
universe@234 77 .set(SharedDataKeys.EXTENSIONS, listOf(TablesExtension.create(), StrikethroughExtension.create()))
universe@234 78 parser = Parser.builder(options).build()
universe@234 79 renderer = HtmlRenderer.builder(options
universe@234 80 .set(HtmlRenderer.ESCAPE_HTML, true)
universe@234 81 ).build()
universe@184 82
universe@234 83 issue.description = formatMarkdown(issue.description ?: "")
universe@184 84 for (comment in comments) {
universe@234 85 comment.commentFormatted = formatMarkdown(comment.comment)
universe@184 86 }
universe@184 87 }
universe@234 88
universe@234 89 private fun formatEmojis(text: String) = text
universe@234 90 .replace("(/)", "&#9989;")
universe@234 91 .replace("(x)", "&#10060;")
universe@234 92 .replace("(!)", "&#9889;")
universe@234 93
universe@234 94 private fun formatMarkdown(text: String) =
universe@234 95 renderer.render(parser.parse(formatEmojis(text)))
universe@184 96 }
universe@184 97
universe@184 98 class IssueEditView(
universe@184 99 val issue: Issue,
universe@184 100 val versions: List<Version>,
universe@184 101 val components: List<Component>,
universe@184 102 val users: List<User>,
universe@184 103 val project: Project, // TODO: allow null values to create issues from the IssuesServlet
universe@184 104 val version: Version? = null,
universe@184 105 val component: Component? = null
universe@184 106 ) : EditView() {
universe@184 107
universe@184 108 val versionsUpcoming: List<Version>
universe@184 109 val versionsRecent: List<Version>
universe@184 110
universe@184 111 val issueStatus = IssueStatus.values()
universe@184 112 val issueCategory = IssueCategory.values()
universe@184 113
universe@184 114 init {
universe@184 115 val recent = mutableListOf<Version>()
universe@231 116 issue.affected?.let { recent.add(it) }
universe@184 117 val upcoming = mutableListOf<Version>()
universe@231 118 issue.resolved?.let { upcoming.add(it) }
universe@231 119
universe@184 120 for (v in versions) {
universe@184 121 if (v.status.isReleased) {
universe@184 122 if (v.status != VersionStatus.Deprecated) recent.add(v)
universe@184 123 } else {
universe@184 124 upcoming.add(v)
universe@184 125 }
universe@184 126 }
universe@186 127 versionsRecent = recent.distinct()
universe@186 128 versionsUpcoming = upcoming.distinct()
universe@184 129 }
universe@184 130 }
universe@184 131

mercurial