Fri, 30 Dec 2022 19:04:34 +0100
#29 add possibility to relate issues
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.*
36 import kotlin.math.roundToInt
38 class IssueSorter(private vararg val criteria: Criteria) : Comparator<Issue> {
39 enum class Field {
40 PHASE, ETA, UPDATED
41 }
43 data class Criteria(val field: Field, val asc: Boolean = true)
45 override fun compare(left: Issue, right: Issue): Int {
46 if (left == right) {
47 return 0
48 }
49 for (c in criteria) {
50 val result = when (c.field) {
51 Field.PHASE -> left.status.phase.compareTo(right.status.phase)
52 Field.ETA -> {
53 val l = left.eta
54 val r = right.eta
55 if (l == null && r == null) 0
56 else if (l == null) 1
57 else if (r == null) -1
58 else l.compareTo(r)
59 }
60 Field.UPDATED -> left.updated.compareTo(right.updated)
61 }
62 if (result != 0) {
63 return if (c.asc) result else -result
64 }
65 }
66 return 0
67 }
68 }
70 class IssueSummary {
71 var open = 0
72 var active = 0
73 var done = 0
75 val total get() = open + active + done
77 val openPercent get() = 100 - activePercent - donePercent
78 val activePercent get() = if (total > 0) (100f * active / total).roundToInt() else 0
79 val donePercent get() = if (total > 0) (100f * done / total).roundToInt() else 100
81 /**
82 * Adds the specified issue to the summary by incrementing the respective counter.
83 * @param issue the issue
84 */
85 fun add(issue: Issue) {
86 when (issue.status.phase) {
87 IssueStatusPhase.Open -> open++
88 IssueStatusPhase.WorkInProgress -> active++
89 IssueStatusPhase.Done -> done++
90 }
91 }
92 }
94 class IssueDetailView(
95 val issue: Issue,
96 val comments: List<IssueComment>,
97 val project: Project,
98 val version: Version?,
99 val component: Component?,
100 projectIssues: List<Issue>,
101 val currentRelations: List<IssueRelation>,
102 /**
103 * Optional resource key to an error message for the relation editor.
104 */
105 val relationError: String?
106 ) : View() {
107 val relationTypes = RelationType.values()
108 val linkableIssues = projectIssues.filterNot { it.id == issue.id }
110 private val parser: Parser
111 private val renderer: HtmlRenderer
113 init {
114 val options = MutableDataSet()
115 .set(SharedDataKeys.EXTENSIONS, listOf(TablesExtension.create(), StrikethroughExtension.create()))
116 parser = Parser.builder(options).build()
117 renderer = HtmlRenderer.builder(options
118 .set(HtmlRenderer.ESCAPE_HTML, true)
119 ).build()
121 issue.description = formatMarkdown(issue.description ?: "")
122 for (comment in comments) {
123 comment.commentFormatted = formatMarkdown(comment.comment)
124 }
125 }
127 private fun formatEmojis(text: String) = text
128 .replace("(/)", "✅")
129 .replace("(x)", "❌")
130 .replace("(!)", "⚡")
132 private fun formatMarkdown(text: String) =
133 renderer.render(parser.parse(formatEmojis(text)))
134 }
136 class IssueEditView(
137 val issue: Issue,
138 val versions: List<Version>,
139 val components: List<Component>,
140 val users: List<User>,
141 val project: Project, // TODO: allow null values to create issues from the IssuesServlet
142 val version: Version? = null,
143 val component: Component? = null
144 ) : EditView() {
146 val versionsUpcoming: List<Version>
147 val versionsRecent: List<Version>
149 val issueStatus = IssueStatus.values()
150 val issueCategory = IssueCategory.values()
152 init {
153 val recent = mutableListOf<Version>()
154 issue.affected?.let { recent.add(it) }
155 val upcoming = mutableListOf<Version>()
156 issue.resolved?.let { upcoming.add(it) }
158 for (v in versions) {
159 if (v.status.isReleased) {
160 if (v.status != VersionStatus.Deprecated) recent.add(v)
161 } else {
162 upcoming.add(v)
163 }
164 }
165 versionsRecent = recent.distinct()
166 versionsUpcoming = upcoming.distinct()
167 }
168 }