From 041db0385444c35ffdd93a7150095a64e3e49f25 Mon Sep 17 00:00:00 2001 From: Kattouf Date: Thu, 24 Oct 2024 10:35:52 +0700 Subject: [PATCH] project(release): generate release notes in release flow --- SakeApp/BrewCommands.swift | 12 +++++++++ SakeApp/ReleaseCommands.swift | 50 ++++++++++++++++++++++++++++++++--- cliff.toml | 2 +- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/SakeApp/BrewCommands.swift b/SakeApp/BrewCommands.swift index ed7f377..efccb9c 100644 --- a/SakeApp/BrewCommands.swift +++ b/SakeApp/BrewCommands.swift @@ -38,4 +38,16 @@ struct BrewCommands { } ) } + + static var ensureGitCliffInstalled: Command { + Command( + description: "Ensure git-cliff is installed", + skipIf: { _ in + run("which", "git-cliff").succeeded + }, + run: { _ in + try runAndPrint("brew", "install", "git-cliff") + } + ) + } } diff --git a/SakeApp/ReleaseCommands.swift b/SakeApp/ReleaseCommands.swift index 2e17216..1ae2b14 100644 --- a/SakeApp/ReleaseCommands.swift +++ b/SakeApp/ReleaseCommands.swift @@ -27,7 +27,14 @@ struct ReleaseCommands { public static var release: Command { Command( description: "Release", - dependencies: [bumpVersion, buildReleaseArtifacts, calculateBuildArtifactsSha256, createAndPushTag, draftReleaseWithArtifacts] + dependencies: [ + bumpVersion, + buildReleaseArtifacts, + calculateBuildArtifactsSha256, + createAndPushTag, + generateReleaseNotes, + draftReleaseWithArtifacts, + ] ) } @@ -63,7 +70,7 @@ struct ReleaseCommands { try versionFileContent.write(toFile: versionFilePath, atomically: true, encoding: .utf8) try runAndPrint("git", "add", versionFilePath) - try runAndPrint("git", "commit", "-m", "Bump version to \(version)") + try runAndPrint("git", "commit", "-m", "chore(release): Bump version to \(version)") print("Version bumped to \(version)") } ) @@ -202,6 +209,35 @@ struct ReleaseCommands { ) } + static var generateReleaseNotes: Command { + Command( + description: "Generate release notes", + dependencies: [BrewCommands.ensureGitCliffInstalled], + skipIf: { context in + let arguments = try ReleaseArguments.parse(context.arguments) + try arguments.validate() + + let version = arguments.version + let releaseNotesPath = releaseNotesPath(version: version) + if FileManager.default.fileExists(atPath: releaseNotesPath) { + print("Release notes for \(version) already exist at \(releaseNotesPath). Skipping...") + return true + } else { + return false + } + }, + run: { context in + let arguments = try ReleaseArguments.parse(context.arguments) + try arguments.validate() + + let version = arguments.version + let releaseNotesPath = releaseNotesPath(version: version) + try runAndPrint("git", "cliff", "--unreleased", "--strip=all", "--tag", version, "--output", releaseNotesPath) + print("Release notes generated at \(releaseNotesPath)") + } + ) + } + static var draftReleaseWithArtifacts: Command { Command( description: "Draft a release on GitHub", @@ -227,9 +263,17 @@ struct ReleaseCommands { let tagName = arguments.version let releaseTitle = arguments.version let draftReleaseCommand = - "gh release create \(tagName) \(Constants.buildArtifactsDirectory)/*.zip --title '\(releaseTitle)' --draft --verify-tag --generate-notes" + "gh release create \(tagName) \(Constants.buildArtifactsDirectory)/*.zip --title '\(releaseTitle)' --draft --verify-tag --notes-file \(releaseNotesPath(version: tagName))" try runAndPrint(bash: draftReleaseCommand) } ) } } + +// MARK: - Helpers + +extension ReleaseCommands { + private static func releaseNotesPath(version: String) -> String { + ".build/artifacts/release-notes-\(version).md" + } +} diff --git a/cliff.toml b/cliff.toml index 6fdaf7f..34a1a73 100644 --- a/cliff.toml +++ b/cliff.toml @@ -105,7 +105,7 @@ commit_preprocessors = [ ] # regex for parsing and grouping commits commit_parsers = [ - { message = '^chore\(release\): bump version', skip = true }, + { message = '^chore\(release\): Bump version to', skip = true }, { message = '^(chore|fix)\(deps\):', group = ":package: Dependency Updates", scope = "" }, { message = '^feat', group = ":rocket: Features" }, { message = '^fix', group = ":bug: Bug Fixes" },