Sat, 09 Oct 2021 17:46:12 +0200
add three emoji sequences (experimental feature)
1 /*
2 * Copyright 2021 Mike Becker. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
26 package de.uapcore.lightpit.viewmodel
28 import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension
29 import com.vladsch.flexmark.ext.tables.TablesExtension
30 import com.vladsch.flexmark.html.HtmlRenderer
31 import com.vladsch.flexmark.parser.Parser
32 import com.vladsch.flexmark.util.data.MutableDataSet
33 import com.vladsch.flexmark.util.data.SharedDataKeys
34 import de.uapcore.lightpit.entities.*
35 import de.uapcore.lightpit.types.IssueCategory
36 import de.uapcore.lightpit.types.IssueStatus
37 import de.uapcore.lightpit.types.IssueStatusPhase
38 import de.uapcore.lightpit.types.VersionStatus
39 import kotlin.math.roundToInt
41 class IssueSummary {
42 var open = 0
43 var active = 0
44 var done = 0
46 val total get() = open + active + done
48 val openPercent get() = 100 - activePercent - donePercent
49 val activePercent get() = if (total > 0) (100f * active / total).roundToInt() else 0
50 val donePercent get() = if (total > 0) (100f * done / total).roundToInt() else 100
52 /**
53 * Adds the specified issue to the summary by incrementing the respective counter.
54 * @param issue the issue
55 */
56 fun add(issue: Issue) {
57 when (issue.status.phase) {
58 IssueStatusPhase.Open -> open++
59 IssueStatusPhase.WorkInProgress -> active++
60 IssueStatusPhase.Done -> done++
61 }
62 }
63 }
65 class IssueDetailView(
66 val issue: Issue,
67 val comments: List<IssueComment>,
68 val project: Project,
69 val version: Version? = null,
70 val component: Component? = null
71 ) : View() {
72 private val parser: Parser
73 private val renderer: HtmlRenderer
75 init {
76 val options = MutableDataSet()
77 .set(SharedDataKeys.EXTENSIONS, listOf(TablesExtension.create(), StrikethroughExtension.create()))
78 parser = Parser.builder(options).build()
79 renderer = HtmlRenderer.builder(options
80 .set(HtmlRenderer.ESCAPE_HTML, true)
81 ).build()
83 issue.description = formatMarkdown(issue.description ?: "")
84 for (comment in comments) {
85 comment.commentFormatted = formatMarkdown(comment.comment)
86 }
87 }
89 private fun formatEmojis(text: String) = text
90 .replace("(/)", "✅")
91 .replace("(x)", "❌")
92 .replace("(!)", "⚡")
94 private fun formatMarkdown(text: String) =
95 renderer.render(parser.parse(formatEmojis(text)))
96 }
98 class IssueEditView(
99 val issue: Issue,
100 val versions: List<Version>,
101 val components: List<Component>,
102 val users: List<User>,
103 val project: Project, // TODO: allow null values to create issues from the IssuesServlet
104 val version: Version? = null,
105 val component: Component? = null
106 ) : EditView() {
108 val versionsUpcoming: List<Version>
109 val versionsRecent: List<Version>
111 val issueStatus = IssueStatus.values()
112 val issueCategory = IssueCategory.values()
114 init {
115 val recent = mutableListOf<Version>()
116 issue.affected?.let { recent.add(it) }
117 val upcoming = mutableListOf<Version>()
118 issue.resolved?.let { upcoming.add(it) }
120 for (v in versions) {
121 if (v.status.isReleased) {
122 if (v.status != VersionStatus.Deprecated) recent.add(v)
123 } else {
124 upcoming.add(v)
125 }
126 }
127 versionsRecent = recent.distinct()
128 versionsUpcoming = upcoming.distinct()
129 }
130 }