Sat, 27 Nov 2021 12:12:20 +0100
#109 changes assignee filter
universe@195 | 1 | /* |
universe@195 | 2 | * Copyright 2021 Mike Becker. All rights reserved. |
universe@195 | 3 | * |
universe@195 | 4 | * Redistribution and use in source and binary forms, with or without |
universe@195 | 5 | * modification, are permitted provided that the following conditions are met: |
universe@195 | 6 | * |
universe@195 | 7 | * 1. Redistributions of source code must retain the above copyright |
universe@195 | 8 | * notice, this list of conditions and the following disclaimer. |
universe@195 | 9 | * |
universe@195 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@195 | 11 | * notice, this list of conditions and the following disclaimer in the |
universe@195 | 12 | * documentation and/or other materials provided with the distribution. |
universe@195 | 13 | * |
universe@195 | 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@195 | 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@195 | 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
universe@195 | 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
universe@195 | 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
universe@195 | 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
universe@195 | 20 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
universe@195 | 21 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
universe@195 | 22 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
universe@195 | 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
universe@195 | 24 | */ |
universe@195 | 25 | |
universe@195 | 26 | package de.uapcore.lightpit.servlet |
universe@195 | 27 | |
universe@236 | 28 | import com.github.difflib.text.DiffRow |
universe@236 | 29 | import com.github.difflib.text.DiffRowGenerator |
universe@195 | 30 | import de.uapcore.lightpit.AbstractServlet |
universe@195 | 31 | import de.uapcore.lightpit.HttpRequest |
universe@195 | 32 | import de.uapcore.lightpit.dao.DataAccessObject |
universe@235 | 33 | import de.uapcore.lightpit.entities.IssueHistoryData |
universe@235 | 34 | import de.uapcore.lightpit.entities.IssueHistoryEntry |
universe@235 | 35 | import de.uapcore.lightpit.viewmodel.IssueDiff |
universe@195 | 36 | import de.uapcore.lightpit.viewmodel.IssueFeed |
universe@235 | 37 | import de.uapcore.lightpit.viewmodel.IssueFeedEntry |
universe@235 | 38 | import java.text.SimpleDateFormat |
universe@195 | 39 | import javax.servlet.annotation.WebServlet |
universe@195 | 40 | |
universe@236 | 41 | |
universe@195 | 42 | @WebServlet(urlPatterns = ["/feed/*"]) |
universe@195 | 43 | class FeedServlet : AbstractServlet() { |
universe@195 | 44 | |
universe@195 | 45 | init { |
universe@198 | 46 | get("/%project/issues.rss", this::issues) |
universe@198 | 47 | } |
universe@198 | 48 | |
universe@235 | 49 | private fun fullContent(issue: IssueHistoryData) = IssueDiff( |
universe@235 | 50 | issue.id, |
universe@235 | 51 | issue.subject, |
universe@235 | 52 | issue.component, |
universe@235 | 53 | issue.status.name, |
universe@235 | 54 | issue.category.name, |
universe@235 | 55 | issue.subject, |
universe@236 | 56 | issue.description.replace("\r", ""), |
universe@235 | 57 | issue.assignee, |
universe@235 | 58 | issue.eta?.let { SimpleDateFormat("dd.MM.yyyy").format(it) } ?: "", |
universe@235 | 59 | issue.affected, |
universe@235 | 60 | issue.resolved |
universe@235 | 61 | ) |
universe@235 | 62 | |
universe@235 | 63 | private fun diffContent(cur: IssueHistoryData, next: IssueHistoryData): IssueDiff { |
universe@236 | 64 | val generator = DiffRowGenerator.create() |
universe@236 | 65 | .showInlineDiffs(true) |
universe@236 | 66 | .mergeOriginalRevised(true) |
universe@236 | 67 | .inlineDiffByWord(true) |
universe@236 | 68 | .oldTag { start -> if (start) "<strike style=\"color:red\">" else "</strike>" } |
universe@236 | 69 | .newTag { start -> if (start) "<i style=\"color: green\">" else "</i>" } |
universe@236 | 70 | .build() |
universe@236 | 71 | |
universe@235 | 72 | val prev = fullContent(next) |
universe@235 | 73 | val diff = fullContent(cur) |
universe@236 | 74 | |
universe@236 | 75 | val result = generator.generateDiffRows( |
universe@238 | 76 | listOf( |
universe@238 | 77 | prev.subject, prev.component, prev.status, |
universe@238 | 78 | prev.category, prev.assignee, prev.eta, prev.affected, prev.resolved |
universe@238 | 79 | ), |
universe@238 | 80 | listOf( |
universe@238 | 81 | diff.subject, diff.component, diff.status, |
universe@238 | 82 | diff.category, diff.assignee, diff.eta, diff.affected, diff.resolved |
universe@238 | 83 | ) |
universe@236 | 84 | ) |
universe@236 | 85 | |
universe@236 | 86 | diff.subject = result[0].oldLine |
universe@236 | 87 | diff.component = result[1].oldLine |
universe@236 | 88 | diff.status = result[2].oldLine |
universe@236 | 89 | diff.category = result[3].oldLine |
universe@236 | 90 | diff.assignee = result[4].oldLine |
universe@236 | 91 | diff.eta = result[5].oldLine |
universe@236 | 92 | diff.affected = result[6].oldLine |
universe@236 | 93 | diff.resolved = result[7].oldLine |
universe@236 | 94 | |
universe@236 | 95 | diff.description = generator.generateDiffRows( |
universe@236 | 96 | prev.description.split('\n'), |
universe@236 | 97 | diff.description.split('\n') |
universe@240 | 98 | ).joinToString("\n", transform = DiffRow::getOldLine) |
universe@236 | 99 | |
universe@235 | 100 | return diff |
universe@235 | 101 | } |
universe@235 | 102 | |
universe@235 | 103 | /** |
universe@235 | 104 | * Generates the feed entries. |
universe@235 | 105 | * Assumes that [historyEntry] is already sorted by timestamp (descending). |
universe@235 | 106 | */ |
universe@238 | 107 | private fun generateFeedEntries(historyEntry: List<IssueHistoryEntry>): List<IssueFeedEntry> = |
universe@238 | 108 | if (historyEntry.isEmpty()) { |
universe@238 | 109 | emptyList() |
universe@238 | 110 | } else { |
universe@238 | 111 | historyEntry.groupBy { it.data.id }.mapValues { (_, history) -> |
universe@238 | 112 | history.zipWithNext().map { (cur, next) -> |
universe@238 | 113 | IssueFeedEntry( |
universe@238 | 114 | cur.time, cur.type, diffContent(cur.data, next.data) |
universe@238 | 115 | ) |
universe@238 | 116 | }.plus( |
universe@238 | 117 | history.last().let { IssueFeedEntry(it.time, it.type, fullContent(it.data)) } |
universe@238 | 118 | ) |
universe@238 | 119 | }.flatMap { it.value }.sortedByDescending { it.time } |
universe@238 | 120 | } |
universe@235 | 121 | |
universe@195 | 122 | private fun issues(http: HttpRequest, dao: DataAccessObject) { |
universe@198 | 123 | val project = http.pathParams["project"]?.let { dao.findProjectByNode(it) } |
universe@198 | 124 | if (project == null) { |
universe@198 | 125 | http.response.sendError(404) |
universe@198 | 126 | return |
universe@198 | 127 | } |
universe@239 | 128 | val assignees = http.param("assignee")?.split(',') |
universe@195 | 129 | |
universe@235 | 130 | val days = http.param("days")?.toIntOrNull() ?: 30 |
universe@195 | 131 | |
universe@239 | 132 | val issuesFromDb = dao.listIssueHistory(project.id, days) |
universe@239 | 133 | val issueHistory = if (assignees == null) issuesFromDb else |
universe@241 | 134 | issuesFromDb.filter { assignees.contains(it.currentAssignee) } |
universe@239 | 135 | |
universe@235 | 136 | // TODO: add comment history depending on parameter |
universe@235 | 137 | |
universe@235 | 138 | http.view = IssueFeed(project, generateFeedEntries(issueHistory)) |
universe@198 | 139 | http.renderFeed("issues-feed") |
universe@195 | 140 | } |
universe@195 | 141 | } |