|
| 1 | +package com.cloudogu.ces.cesbuildlib |
| 2 | + |
| 3 | +/** |
| 4 | + * Enables interaction with release notes files |
| 5 | + */ |
| 6 | +class ReleaseNotes implements Serializable { |
| 7 | + private script |
| 8 | + private String releaseNotesFileNameDE |
| 9 | + private String releaseNotesFileNameEN |
| 10 | + |
| 11 | + ReleaseNotes(script) { |
| 12 | + this(script, 'docs/gui/release_notes_de.md', 'docs/gui/release_notes_en.md') |
| 13 | + } |
| 14 | + |
| 15 | + ReleaseNotes(script, releaseNotesFileNameDE, releaseNotesFileNameEN) { |
| 16 | + this.script = script |
| 17 | + this.releaseNotesFileNameDE = releaseNotesFileNameDE |
| 18 | + this.releaseNotesFileNameEN = releaseNotesFileNameEN |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * @return Returns the content of german release notes file. |
| 23 | + */ |
| 24 | + private String readReleaseNotesDE(){ |
| 25 | + script.readFile releaseNotesFileNameDE |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @return Returns the content of english release notes file. |
| 30 | + */ |
| 31 | + private String readReleaseNotesEN(){ |
| 32 | + script.readFile releaseNotesFileNameEN |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Extracts the changes for a given version out of the german release notes. |
| 37 | + * |
| 38 | + * @param releaseVersion The version to get the changes for. |
| 39 | + * @return Returns the changes as String. |
| 40 | + */ |
| 41 | + String changesForDEVersion(String releaseVersion) { |
| 42 | + def releaseNotesDE = readReleaseNotesDE() |
| 43 | + def start = changesStartIndex(releaseNotesDE, releaseVersion) |
| 44 | + def end = changesEndIndex(releaseNotesDE, start) |
| 45 | + return escapeForJson(releaseNotesDE.substring(start, end).trim()) |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Extracts the changes for a given version out of the english release notes. |
| 50 | + * |
| 51 | + * @param releaseVersion The version to get the changes for. |
| 52 | + * @return Returns the changes as String. |
| 53 | + */ |
| 54 | + String changesForENVersion(String releaseVersion) { |
| 55 | + def releaseNotesEN = readReleaseNotesEN() |
| 56 | + def start = changesStartIndex(releaseNotesEN, releaseVersion) |
| 57 | + def end = changesEndIndex(releaseNotesEN, start) |
| 58 | + return escapeForJson(releaseNotesEN.substring(start, end).trim()) |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Removes characters from a string that could break the json struct when passing the string as json value. |
| 63 | + * |
| 64 | + * @param string The string to format. |
| 65 | + * @return Returns the formatted string. |
| 66 | + */ |
| 67 | + private static String escapeForJson(String string) { |
| 68 | + return string |
| 69 | + .replace("\"", "") |
| 70 | + .replace("'", "") |
| 71 | + .replace("\\", "") |
| 72 | + .replace("\n", "\\n") |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns the start index of changes of a specific release version in the release notes. |
| 77 | + * |
| 78 | + * @param releaseVersion The version to get the changes for. |
| 79 | + * @return Returns the index in the release notes string where the changes start. |
| 80 | + */ |
| 81 | + private static int changesStartIndex(String releaseNotes, String releaseVersion) { |
| 82 | + def index = releaseNotes.indexOf("## [${releaseVersion}]") |
| 83 | + if (index == -1){ |
| 84 | + throw new IllegalArgumentException("The desired version '${releaseVersion}' could not be found in the release notes.") |
| 85 | + } |
| 86 | + def offset = releaseNotes.substring(index).indexOf("\n") |
| 87 | + return index + offset |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns the end index of changes of a specific release version in the release notes. |
| 92 | + * |
| 93 | + * @param start The start index of the changes for this version. |
| 94 | + * @return Returns the index in the release notes string where the changes end. |
| 95 | + */ |
| 96 | + private static int changesEndIndex(String releaseNotes, int start) { |
| 97 | + def releaseNotesAfterStartIndex = releaseNotes.substring(start) |
| 98 | + def index = releaseNotesAfterStartIndex.indexOf("\n## [") |
| 99 | + if (index == -1) { |
| 100 | + return releaseNotes.length() |
| 101 | + } |
| 102 | + return index + start |
| 103 | + } |
| 104 | +} |
0 commit comments