Sun, 10 Oct 2021 14:46:10 +0200
add diffs to RSS feed
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@236 | 76 | listOf(prev.subject, prev.component, prev.status, |
universe@236 | 77 | prev.category, prev.assignee, prev.eta, prev.affected, prev.resolved), |
universe@236 | 78 | listOf(diff.subject, diff.component, diff.status, |
universe@236 | 79 | diff.category, diff.assignee, diff.eta, diff.affected, diff.resolved) |
universe@236 | 80 | ) |
universe@236 | 81 | |
universe@236 | 82 | diff.subject = result[0].oldLine |
universe@236 | 83 | diff.component = result[1].oldLine |
universe@236 | 84 | diff.status = result[2].oldLine |
universe@236 | 85 | diff.category = result[3].oldLine |
universe@236 | 86 | diff.assignee = result[4].oldLine |
universe@236 | 87 | diff.eta = result[5].oldLine |
universe@236 | 88 | diff.affected = result[6].oldLine |
universe@236 | 89 | diff.resolved = result[7].oldLine |
universe@236 | 90 | |
universe@236 | 91 | diff.description = generator.generateDiffRows( |
universe@236 | 92 | prev.description.split('\n'), |
universe@236 | 93 | diff.description.split('\n') |
universe@236 | 94 | ).joinToString("<br>", transform = DiffRow::getOldLine) |
universe@236 | 95 | |
universe@235 | 96 | return diff |
universe@235 | 97 | } |
universe@235 | 98 | |
universe@235 | 99 | /** |
universe@235 | 100 | * Generates the feed entries. |
universe@235 | 101 | * Assumes that [historyEntry] is already sorted by timestamp (descending). |
universe@235 | 102 | */ |
universe@235 | 103 | private fun generateFeedEntries(historyEntry: List<IssueHistoryEntry>) = |
universe@235 | 104 | if (historyEntry.isEmpty()) emptyList() |
universe@235 | 105 | else historyEntry.zipWithNext().map { (cur, next) -> |
universe@235 | 106 | IssueFeedEntry( |
universe@235 | 107 | cur.time, cur.type, diffContent(cur.data, next.data) |
universe@235 | 108 | ) |
universe@235 | 109 | }.plus( |
universe@235 | 110 | historyEntry.last().let { IssueFeedEntry(it.time, it.type, fullContent(it.data)) } |
universe@235 | 111 | ) |
universe@235 | 112 | |
universe@195 | 113 | private fun issues(http: HttpRequest, dao: DataAccessObject) { |
universe@198 | 114 | val project = http.pathParams["project"]?.let { dao.findProjectByNode(it) } |
universe@198 | 115 | if (project == null) { |
universe@198 | 116 | http.response.sendError(404) |
universe@198 | 117 | return |
universe@198 | 118 | } |
universe@195 | 119 | |
universe@235 | 120 | val days = http.param("days")?.toIntOrNull() ?: 30 |
universe@195 | 121 | |
universe@235 | 122 | val issueHistory = dao.listIssueHistory(project.id, days) |
universe@235 | 123 | // TODO: add comment history depending on parameter |
universe@235 | 124 | |
universe@235 | 125 | http.view = IssueFeed(project, generateFeedEntries(issueHistory)) |
universe@198 | 126 | http.renderFeed("issues-feed") |
universe@195 | 127 | } |
universe@195 | 128 | } |