Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(changelog): Implement changelog generate tool #76

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/openimsdk/actions-test

go 1.23.2
22 changes: 22 additions & 0 deletions tools/changelog-test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Changelogs

### New Features
* feat: Update version to v0.0.19 [#30](https://github.com/openimsdk/actions-test/pull/30)
* feat: Update 66666 [#66](https://github.com/openimsdk/actions-test/pull/66)

### Bug Fixes
* fix: swm [#34](https://github.com/openimsdk/actions-test/pull/34)

### Chores
* chore: rm [#51](https://github.com/openimsdk/actions-test/pull/51)

### Others
* Update version to v0.0.21 [#32](https://github.com/openimsdk/actions-test/pull/32)
* T1 [#35](https://github.com/openimsdk/actions-test/pull/35)
* update teset [#36](https://github.com/openimsdk/actions-test/pull/36)
* hello [#39](https://github.com/openimsdk/actions-test/pull/39)
* 21321 [#53](https://github.com/openimsdk/actions-test/pull/53)
* 1x [#54](https://github.com/openimsdk/actions-test/pull/54)
* Update CHANGELOG for release v0.0.34 [#74](https://github.com/openimsdk/actions-test/pull/74)


85 changes: 85 additions & 0 deletions tools/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package main

import (
"fmt"
"regexp"
"strings"
)

// Function to classify and format release notes
func classifyReleaseNotes(body string) map[string][]string {
result := map[string][]string{
"feat": {},
"fix": {},
"chore": {},
"other": {},
}

// Regular expression to extract PR number and URL
rePR := regexp.MustCompile(`https://github\.com/[^\s]+/pull/(\d+)`)

// Split the body into individual lines
lines := strings.Split(body, "\n")

for _, line := range lines {
if strings.HasPrefix(line, "*") {
var category string

// Determine the category based on the prefix
if strings.HasPrefix(line, "* feat") {
category = "feat"
} else if strings.HasPrefix(line, "* fix") {
category = "fix"
} else if strings.HasPrefix(line, "* chore") {
category = "chore"
} else {
category = "other"
}

// Extract PR number and URL
matches := rePR.FindStringSubmatch(line)
if len(matches) == 2 {
prURL := matches[0]
prNumber := matches[1]
// Format the line with the PR link
formattedLine := fmt.Sprintf("* %s [#%s](%s)", strings.Split(line, " by ")[0][2:], prNumber, prURL)
result[category] = append(result[category], formattedLine)
} else {
// If no PR link is found, just add the line as is
result[category] = append(result[category], line)
}
}
}

return result
}

// Function to generate the final changelog
func generateChangelog(body string) string {
sections := classifyReleaseNotes(body)

changelog := "# Changelogs\n\n"
if len(sections["feat"]) > 0 {
changelog += "### New Features\n" + strings.Join(sections["feat"], "\n") + "\n\n"
}
if len(sections["fix"]) > 0 {
changelog += "### Bug Fixes\n" + strings.Join(sections["fix"], "\n") + "\n\n"
}
if len(sections["chore"]) > 0 {
changelog += "### Chores\n" + strings.Join(sections["chore"], "\n") + "\n\n"
}
if len(sections["other"]) > 0 {
changelog += "### Others\n" + strings.Join(sections["other"], "\n") + "\n\n"
}

return changelog
}

func main() {
// Example release notes content
body := "## What's Changed\r\n* feat(test): Update version to v0.0.19 by @github-actions in https://github.com/openimsdk/actions-test/pull/30\r\n* Update version to v0.0.21 by @github-actions in https://github.com/openimsdk/actions-test/pull/32\r\n* fix: swm by @mo3et in https://github.com/openimsdk/actions-test/pull/34\r\n* feat: Update 66666 by @github-actions in https://github.com/openimsdk/actions-test/pull/66\r\n* T1 by @mo3et in https://github.com/openimsdk/actions-test/pull/35\r\n* update teset by @mo3et in https://github.com/openimsdk/actions-test/pull/36\r\n* hello by @mo3et in https://github.com/openimsdk/actions-test/pull/39\r\n* chore: rm by @mo3et in https://github.com/openimsdk/actions-test/pull/51\r\n* 21321 by @mo3et in https://github.com/openimsdk/actions-test/pull/53\r\n* 1x by @mo3et in https://github.com/openimsdk/actions-test/pull/54\r\n* Update CHANGELOG for release v0.0.34 by @github-actions in https://github.com/openimsdk/actions-test/pull/74"

// Generate and print the formatted changelog
changelog := generateChangelog(body)
fmt.Println(changelog)
}
Loading