Skip to content

Commit

Permalink
chore: 🔧 Apply auto curseforge upload fixes from core
Browse files Browse the repository at this point in the history
  • Loading branch information
P3pp3rF1y committed Sep 16, 2024
1 parent 64a806f commit ccf7338
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 200 # Fetch 200 latest commits to make sure changelog can be created from merge commits
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
Expand Down
149 changes: 79 additions & 70 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -240,95 +240,104 @@ sonarqube {
}

task generateChangelog {
def changelog = new StringBuilder()

// Function to remove characters with Unicode code points 129 or higher
def removeHighUnicodeChars = { text ->
text.replaceAll(/[\u0081-\uFFFF]/, '')
}

// Function to format commit messages with nesting
def formatMultilineMessage = { message ->
// Split message by lines, trim whitespace, and create formatted list
def lines = message.split('\n')
def formattedMessage = lines[0].trim() // First line as top-level list item
if (lines.size() > 1) {
formattedMessage += "\n" + lines[1..-1].collect { line ->
// Trim the line and remove leading dash if present
def trimmedLine = line.trim()
if (trimmedLine.startsWith('-')) {
trimmedLine = trimmedLine.substring(1).trim()
}
" - ${trimmedLine}" // Nested list for additional lines
}.join('\n')
doLast {
def changelog = new StringBuilder()

// Function to remove characters with Unicode code points 129 or higher
def removeHighUnicodeChars = { text ->
text.codePoints()
.filter { codePoint -> codePoint <= 0x007F } // Keep only ASCII characters (U+0000 to U+007F)
.collect { codePoint -> new String(Character.toChars(codePoint)) }
.join('')
}
return formattedMessage
}

// Function to remove [DEV] section from commit message
def removeDevSection = { message ->
def devIndex = message.indexOf('[DEV]')
if (devIndex != -1) {
return message.substring(0, devIndex).trim()
// Function to format commit messages with nesting
def formatMultilineMessage = { message ->
// Split message by lines, trim whitespace, and create formatted list
def lines = message.split('\n')
def formattedMessage = lines[0].trim() // First line as top-level list item
if (lines.size() > 1) {
formattedMessage += "\n" + lines[1..-1].collect { line ->
// Trim the line and remove leading dash if present
def trimmedLine = line.trim()
if (trimmedLine.startsWith('-')) {
trimmedLine = trimmedLine.substring(1).trim()
}
" - ${trimmedLine}" // Nested list for additional lines
}.join('\n')
}
return formattedMessage
}
return message
}

// Get the latest commit hash
def latestCommitHash = "git rev-parse HEAD".execute().text.trim()

// Check if the latest commit is a merge commit
def parentCommits = "git rev-list --parents -n 1 ${latestCommitHash}".execute().text.split()
// Function to remove [DEV] section from commit message
def removeDevSection = { message ->
def devIndex = message.indexOf('[DEV]')
if (devIndex != -1) {
return message.substring(0, devIndex).trim()
}
return message
}

def commitMessages = []
if (parentCommits.size() > 2) { // Merge commit has more than 2 parents
def firstParent = parentCommits[1]
def secondParent = parentCommits[2]
def gitLogCommand = [
"bash", "-c", "git log --pretty=format:%B ${firstParent}..${secondParent}"
]
commitMessages = gitLogCommand.execute().text.split('\n\n') // Split by two newlines for each commit
} else {
// Single commit log
commitMessages = "git log -1 --pretty=%B".execute().text.split('\n\n') // Split by two newlines for each commit
}
// Get the latest commit hash
def latestCommitHash = "git rev-parse HEAD".execute().text.trim()

// Check if the latest commit is a merge commit
def parentCommits = "git rev-list --parents -n 1 ${latestCommitHash}".execute().text.split()
logger.info("Parent commits: ${parentCommits}")

def commitMessages = []
if (parentCommits.size() > 2) { // Merge commit has more than 2 parents
def firstParent = parentCommits[1]
def secondParent = parentCommits[2]
def gitLogCommand = [
"bash", "-c", "git log --pretty=format:%B ${firstParent}..${secondParent}"
]
commitMessages = gitLogCommand.execute().text.split('\n\n') // Split by two newlines for each commit
logger.info("Merge commit, ran git log command: ${gitLogCommand.join(' ')} and got ${commitMessages}")
} else {
// Single commit log
commitMessages = "git log -1 --pretty=%B".execute().text.split('\n\n')
// Split by two newlines for each commit
logger.info("Single commit, ran git log command: git log -1 --pretty=%B and got ${commitMessages}");
}

def features = []
def fixes = []
def features = []
def fixes = []
commitMessages.each { commitMessage ->
commitMessage = removeHighUnicodeChars(commitMessage) // Remove high Unicode characters
commitMessage = removeDevSection(commitMessage) // Remove [DEV] section

commitMessages.each { commitMessage ->
commitMessage = removeHighUnicodeChars(commitMessage) // Remove high Unicode characters
commitMessage = removeDevSection(commitMessage) // Remove [DEV] section
if (commitMessage.startsWith('feat: ')) {
features.add(commitMessage.replaceFirst('feat: ', '').trim())
} else if (commitMessage.startsWith('fix: ')) {
fixes.add(commitMessage.replaceFirst('fix: ', '').trim())
}
}

if (commitMessage.startsWith('feat: ')) {
features.add(commitMessage.replaceFirst('feat: ', '').trim())
} else if (commitMessage.startsWith('fix: ')) {
fixes.add(commitMessage.replaceFirst('fix: ', '').trim())
if (features) {
changelog.append("### Features\n")
features.each { feature -> changelog.append("- ${formatMultilineMessage(feature)}\n") }
}
}

if (features) {
changelog.append("### Features\n")
features.each { feature -> changelog.append("- ${formatMultilineMessage(feature)}\n") }
}
if (fixes) {
changelog.append("### Fixes\n")
fixes.each { fix -> changelog.append("- ${formatMultilineMessage(fix)}\n") }
}

if (fixes) {
changelog.append("### Fixes\n")
fixes.each { fix -> changelog.append("- ${formatMultilineMessage(fix)}\n") }
// Store the changelog in a project property or an environment variable
logger.info("Compiled changelog: " + changelog.toString())
project.ext.changelog = changelog.toString()
}

// Store the changelog in a project property or an environment variable
project.ext.changelog = changelog.toString()
println(changelog.toString())
project.ext.changelog = ""
}

task curseforge(type: net.darkhax.curseforgegradle.TaskPublishCurseForge) {
dependsOn 'generateChangelog'

apiToken = System.getenv("CURSEFORGE_TOkEN")
apiToken = System.getenv("CURSEFORGE_TOKEN")
def mainFile = upload(project.curseforge_id, file("${project.buildDir}/libs/${archivesBaseName}-${version}.jar"))
mainFile.changelogType = 'markdown'
mainFile.changelog = project.ext.changelog
mainFile.changelog = {project.ext.changelog}
mainFile.addModLoader('NeoForge')
mainFile.addModLoader('Forge')
mainFile.releaseType = "${release_type}"
Expand Down

0 comments on commit ccf7338

Please sign in to comment.