summaryrefslogtreecommitdiff
path: root/src/main/kotlin/Main.kt
blob: fb273576fbb87af256801df38ba0d14ef7926233 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package org.example

import com.google.gson.GsonBuilder
import com.google.gson.JsonArray

import java.net.URL
import java.time.Instant
import java.util.regex.Matcher
import java.util.regex.Pattern
import kotlin.system.exitProcess

val allowedCategories = listOf("New Features", "Improvements", "Fixes", "Technical Details", "Removed Features")

val categoryPattern = "## Changelog (?<category>.*)".toPattern()
val changePattern = "\\+ (?<text>.*) - (?<author>.*)".toPattern()
val extraInfoPattern = " {4}\\* (?<text>.*)".toPattern()
val illegalStartPattern = "^[-=*+ ].*".toPattern()

fun getTextFromUrl(urlString: String): List<String> {
    val url = URL(urlString)
    val connection = url.openConnection()
    val inputStream = connection.getInputStream()
    val text = mutableListOf<String>()

    inputStream.bufferedReader().useLines { lines ->
        lines.forEach {
            text.add(it)
        }
    }

    return text
}

enum class WhatToDo {
    NEXT_BETA, OPEN_PRS,
    ;
}

fun main() {
    val firstPr = 1072
    val hideWhenError = true
    val title = "Version 0.24 Beta 7"

    val whatToDo = WhatToDo.NEXT_BETA

    println("")

    @Suppress("KotlinConstantConditions")
    val url = when (whatToDo) {
        WhatToDo.NEXT_BETA -> "https://api.github.com/repos/hannibal002/SkyHanni/pulls?state=closed&sort=updated&direction=desc&per_page=50"
        WhatToDo.OPEN_PRS -> "https://api.github.com/repos/hannibal002/SkyHanni/pulls?state=open&sort=updated&direction=desc&per_page=20"
    }

    val data = getTextFromUrl(url).joinToString("")
    val gson = GsonBuilder().create()
    val fromJson = gson.fromJson(data, JsonArray::class.java)
    val prs = fromJson.map { gson.fromJson(it, PullRequest::class.java) }
    readPrs(prs, firstPr, hideWhenError, title, whatToDo)
}

fun readPrs(prs: List<PullRequest>, firstPr: Int, hideWhenError: Boolean, title: String, whatToDo: WhatToDo) {
    val categories = mutableListOf<Category>()
    val allChanges = mutableListOf<Change>()
    var errors = 0
    var done = 0
    // TODO find better solution for this sorting logic
    val filtered = when (whatToDo) {
        WhatToDo.NEXT_BETA -> prs.filter { it.mergedAt != null }
            .map { it to it.mergedAt }

        WhatToDo.OPEN_PRS -> prs
            .map { it to it.updatedAt }

    }
        .map { it.first to Long.MAX_VALUE - Instant.parse(it.second).toEpochMilli() }
        .sortedBy { it.second }
        .map { it.first }
    for (pr in filtered) {
        val number = pr.number
        val prLink = pr.htmlUrl
        val body = pr.body

        val description = body?.split(System.lineSeparator()) ?: emptyList()
        try {
            allChanges.addAll(parseChanges(description, prLink, categories))
            done++
        } catch (t: Throwable) {
            println("")
            println("Error in #$number ($prLink)")
            println(t.message)
            errors++
        }
        if (whatToDo == WhatToDo.NEXT_BETA) {
            if (number == firstPr) break
        }
    }
    println("")

    for (type in OutputType.entries) {
        print(categories, allChanges, type, title)
    }
    println("")
    println("Found $errors PRs with errors")
    println("Loaded $done PRs correctly")
    if (errors > 0) {
        if (hideWhenError) {
            exitProcess(-1)
        }
    }
}

enum class OutputType {
    DISCORD_INTERNAL, GITHUB, DISCORD_PUBLIC,
}

private fun print(
    categories: MutableList<Category>,
    allChanges: MutableList<Change>,
    outputType: OutputType,
    title: String,
) {
    val extraInfoPrefix = when (outputType) {
        OutputType.DISCORD_PUBLIC -> " = "
        OutputType.GITHUB -> "   * "
        OutputType.DISCORD_INTERNAL -> " - "
    }
    val border = "================================================================================="
    println("")
    println("outputType ${outputType.name.lowercase()}:")
    println(border)
    println("## $title")
    for (category in allowedCategories.map { getCategory(categories, it) }) {
        if (outputType == OutputType.DISCORD_PUBLIC && category.name == "Technical Details") continue
        val changes = allChanges.filter { it.category == category }
        if (changes.isEmpty()) continue
        println("### " + category.name)
        if (outputType == OutputType.DISCORD_PUBLIC) {
            println("```diff")
        }
        for (change in changes) {
            val pr = when (outputType) {
                OutputType.DISCORD_PUBLIC -> ""
                OutputType.GITHUB -> " (${change.prLink})"
                OutputType.DISCORD_INTERNAL -> " [PR](<${change.prLink}>)"
            }
            val changePrefix = getChangePrefix(category.name, outputType)
            println("$changePrefix${change.text} - ${change.author}$pr")
            for (s in change.extraInfo) {
                println("$extraInfoPrefix$s")
            }
        }
        if (outputType == OutputType.DISCORD_PUBLIC) {
            println("```")
        }
    }
    println(border)
}

fun getChangePrefix(name: String, outputType: OutputType): String = when (outputType) {
    OutputType.DISCORD_INTERNAL -> "- "
    OutputType.GITHUB -> "+ "
    OutputType.DISCORD_PUBLIC -> when (name) {
        "New Features" -> "+ "
        "Improvements" -> "+ "
        "Fixes" -> "~ "
        "Removed Features" -> "- "
        else -> error("impossible change prefix")
    }
}

inline fun <T> Pattern.matchMatcher(text: String, consumer: Matcher.() -> T) =
    matcher(text).let { if (it.matches()) consumer(it) else null }

@Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION")
fun parseChanges(
    description: List<String>,
    prLink: String,
    categories: MutableList<Category>,
): List<Change> {
    var currentCategory: Category? = null
    var currentChange: Change? = null
    val changes = mutableListOf<Change>()
    for (line in description) {
        if (line == "") {
            currentChange = null
            currentCategory = null
            continue
        }

        categoryPattern.matchMatcher(line) {
            val categoryName = group("category")
            if (categoryName !in allowedCategories) {
                error("unknown category: '$categoryName'")
            }
            currentCategory = getCategory(categories, categoryName)
            currentChange = null
            continue
        }

        val category = currentCategory ?: continue

        changePattern.matchMatcher(line) {
            val author = group("author")
            if (author == "your_name_here") {
                error("no author name")
            }
            val text = group("text")
            if (illegalStartPattern.matcher(text).matches()) {
                error("illegal start at change: '$text'")
            }
            currentChange = Change(text, category, prLink, author).also {
                changes.add(it)
            }
            continue
        }

        extraInfoPattern.matchMatcher(line) {
            val change = currentChange ?: error("Found extra info without change: '$line'")
            val text = group("text")
            if (illegalStartPattern.matcher(text).matches()) {
                error("illegal start at extra info: '$text'")
            }
            change.extraInfo.add(text)
            continue
        }
        error("found unexpected line: '$line'")
    }

    if (changes.isEmpty()) {
        error("no changes found")
    }

    return changes
}

fun getCategory(categories: MutableList<Category>, newName: String): Category {
    for (category in categories) {
        if (category.name == newName) {
            return category
        }
    }
    val category = Category(newName)
    categories.add(category)
    return category
}

class Category(val name: String)

class Change(val text: String, val category: Category, val prLink: String, val author: String) {
    val extraInfo = mutableListOf<String>()
}