From 54a6a1215de753d12931181e94a368a5d275d189 Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Tue, 22 Oct 2024 16:20:06 +0800 Subject: [PATCH 1/2] refactor(clog): update changelog logic. --- tools/change.md | 9 +++ tools/change2.md | 9 +++ tools/change3.md | 9 +++ tools/change4.md | 20 +++++ tools/change5.md | 9 +++ tools/changelog.go | 177 +++++++++++++++++++++++++++++++++++++++++++++ tools/main.go | 109 ++++++---------------------- 7 files changed, 257 insertions(+), 85 deletions(-) create mode 100644 tools/change.md create mode 100644 tools/change2.md create mode 100644 tools/change3.md create mode 100644 tools/change4.md create mode 100644 tools/change5.md create mode 100644 tools/changelog.go diff --git a/tools/change.md b/tools/change.md new file mode 100644 index 0000000..563120a --- /dev/null +++ b/tools/change.md @@ -0,0 +1,9 @@ +## [v0.0.33](https://github.com/openimsdk/actions-test/releases/tag/v0.0.33) 2024-10-21T07:47:31Z + +### Others +* rm by @mo3et in https://github.com/openimsdk/actions-test/pull/51 +* 21321 by @mo3et in https://github.com/openimsdk/actions-test/pull/53 +* 1x by @mo3et in https://github.com/openimsdk/actions-test/pull/54 +**Full Changelog**: https://github.com/openimsdk/actions-test/compare/v3.3.3...v0.0.33 + + diff --git a/tools/change2.md b/tools/change2.md new file mode 100644 index 0000000..0217589 --- /dev/null +++ b/tools/change2.md @@ -0,0 +1,9 @@ +## [v0.0.33](https://github.com/openimsdk/actions-test/releases/tag/v0.0.33) 2024-10-21 + +### Others +* rm [#51](https://github.com/openimsdk/actions-test/pull/51) +* 21321 [#53](https://github.com/openimsdk/actions-test/pull/53) +* 1x [#54](https://github.com/openimsdk/actions-test/pull/54) + +**Full Changelog**: https://github.com/openimsdk/actions-test/compare/v3.3.3...v0.0.33 + diff --git a/tools/change3.md b/tools/change3.md new file mode 100644 index 0000000..0217589 --- /dev/null +++ b/tools/change3.md @@ -0,0 +1,9 @@ +## [v0.0.33](https://github.com/openimsdk/actions-test/releases/tag/v0.0.33) 2024-10-21 + +### Others +* rm [#51](https://github.com/openimsdk/actions-test/pull/51) +* 21321 [#53](https://github.com/openimsdk/actions-test/pull/53) +* 1x [#54](https://github.com/openimsdk/actions-test/pull/54) + +**Full Changelog**: https://github.com/openimsdk/actions-test/compare/v3.3.3...v0.0.33 + diff --git a/tools/change4.md b/tools/change4.md new file mode 100644 index 0000000..e76e579 --- /dev/null +++ b/tools/change4.md @@ -0,0 +1,20 @@ +## [v0.0.41](https://github.com/openimsdk/actions-test/releases/tag/v0.0.41) 2024-10-21 + +### New Features +* feat: Update version to v0.0.19 [#30](https://github.com/openimsdk/actions-test/pull/30) + +### Bug Fixes +* fix: swm [#34](https://github.com/openimsdk/actions-test/pull/34) + +### 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) +* rm [#51](https://github.com/openimsdk/actions-test/pull/51) +* 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) + +**Full Changelog**: [vv0.0.30...v0.0.41](https://github.com/openimsdk/actions-test/compare/v0.0.30...v0.0.41) + diff --git a/tools/change5.md b/tools/change5.md new file mode 100644 index 0000000..d963e84 --- /dev/null +++ b/tools/change5.md @@ -0,0 +1,9 @@ +## [v0.0.33](https://github.com/openimsdk/actions-test/releases/tag/v0.0.33) 2024-10-21 + +### Others +* rm [#51](https://github.com/openimsdk/actions-test/pull/51) +* 21321 [#53](https://github.com/openimsdk/actions-test/pull/53) +* 1x [#54](https://github.com/openimsdk/actions-test/pull/54) + +**Full Changelog**: [vv3.3.3...v0.0.33](https://github.com/openimsdk/actions-test/compare/v3.3.3...v0.0.33) + diff --git a/tools/changelog.go b/tools/changelog.go new file mode 100644 index 0000000..19cc5e0 --- /dev/null +++ b/tools/changelog.go @@ -0,0 +1,177 @@ +package main + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "regexp" + "strings" +) + +// GitHubRepo struct represents the repo details. +type GitHubRepo struct { + Owner string + Repo string + FullChangelog string +} + +// ReleaseData represents the JSON structure for release data. +type ReleaseData struct { + TagName string `json:"tag_name"` + Body string `json:"body"` + HtmlUrl string `json:"html_url"` + Published string `json:"published_at"` +} + +// Method to classify and format release notes. +func (g *GitHubRepo) classifyReleaseNotes(body string) map[string][]string { + result := map[string][]string{ + "feat": {}, + "fix": {}, + "chore": {}, + "refactor": {}, + "other": {}, + } + + // Regular expression to extract PR number and URL + rePR := regexp.MustCompile(`in (https://github\.com/[^\s]+/pull/(\d+))`) + + // Split the body into individual lines. + lines := strings.Split(body, "\n") + + for _, line := range lines { + // Use a regular expression to extract Full Changelog link and its title. + if strings.Contains(line, "**Full Changelog**") { + matches := regexp.MustCompile(`\*\*Full Changelog\*\*: (https://github\.com/[^\s]+/compare/([^\s]+))`).FindStringSubmatch(line) + if len(matches) > 2 { + // Format the Full Changelog link with title + g.FullChangelog = fmt.Sprintf("[v%s](%s)", matches[2], matches[1]) + } + continue // Skip further processing for this line. + } + + 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 if strings.HasPrefix(line, "* refactor") { + category = "refactor" + } else { + category = "other" + } + + // Extract PR number and URL + matches := rePR.FindStringSubmatch(line) + if len(matches) == 3 { + prURL := matches[1] + prNumber := matches[2] + // 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 +} + +// Method to generate the final changelog. +func (g *GitHubRepo) generateChangelog(tag, date, htmlURL, body string) string { + sections := g.classifyReleaseNotes(body) + + // Convert ISO 8601 date to simpler format (YYYY-MM-DD) + formattedDate := date[:10] + + // Changelog header with tag, date, and links. + changelog := fmt.Sprintf("## [%s](%s) %s\n\n", tag, htmlURL, formattedDate) + + 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["refactor"]) > 0 { + changelog += "### Refactors\n" + strings.Join(sections["refactor"], "\n") + "\n\n" + } + if len(sections["other"]) > 0 { + changelog += "### Others\n" + strings.Join(sections["other"], "\n") + "\n\n" + } + + // Add the Full Changelog link at the end, if available. + if g.FullChangelog != "" { + changelog += fmt.Sprintf("**Full Changelog**: %s\n", g.FullChangelog) + } + + return changelog +} + +// Method to fetch release data from GitHub API. +func (g *GitHubRepo) fetchReleaseData(version string) (*ReleaseData, error) { + var apiURL string + + if version == "" { + // Fetch the latest release. + apiURL = fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", g.Owner, g.Repo) + } else { + // Fetch a specific version. + apiURL = fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/tags/%s", g.Owner, g.Repo, version) + } + + resp, err := http.Get(apiURL) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + var releaseData ReleaseData + err = json.Unmarshal(body, &releaseData) + if err != nil { + return nil, err + } + + return &releaseData, nil +} + +func main() { + // Create a new GitHubRepo instance + repoOwner := "openimsdk" + repoName := "actions-test" + repo := &GitHubRepo{Owner: repoOwner, Repo: repoName} + + // Get the version from command line arguments, if provided + var version string + if len(os.Args) > 1 { + version = os.Args[1] // Use the provided version + } + + // Fetch release data (either for latest or specific version) + releaseData, err := repo.fetchReleaseData(version) + if err != nil { + fmt.Println("Error fetching release data:", err) + return + } + + // Generate and print the formatted changelog + changelog := repo.generateChangelog(releaseData.TagName, releaseData.Published, releaseData.HtmlUrl, releaseData.Body) + fmt.Println(changelog) +} diff --git a/tools/main.go b/tools/main.go index 297402e..1937842 100644 --- a/tools/main.go +++ b/tools/main.go @@ -1,85 +1,24 @@ -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) -} +// package main + +// import ( +// "fmt" +// ) + +// func main() { +// // Create a new GitHubRepo instance +// repoOwner := "openimsdk" +// repoName := "actions-test" +// repo := &GitHubRepo{Owner: repoOwner, Repo: repoName} + +// // Fetch release data (you can pass a version or leave it empty for latest) +// version := "v0.0.33" // Example version +// releaseData, err := repo.fetchReleaseData(version) +// if err != nil { +// fmt.Println("Error fetching release data:", err) +// return +// } + +// // Generate and print the formatted changelog +// changelog := repo.generateChangelog(releaseData.TagName, releaseData.Published, releaseData.HtmlUrl, releaseData.Body) +// fmt.Println(changelog) +// } From 7277ed1e878c6b8319791f4b8bf5b08aeeb8e50b Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Tue, 22 Oct 2024 16:30:35 +0800 Subject: [PATCH 2/2] add logic. --- tools/changelog.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/tools/changelog.go b/tools/changelog.go index 19cc5e0..6a4c53b 100644 --- a/tools/changelog.go +++ b/tools/changelog.go @@ -10,6 +10,16 @@ import ( "strings" ) +// You can specify a tag as a command line argument to generate the changelog for a specific version. +// Example: go run tools/changelog.go v0.0.33 +// If no tag is provided, the latest release will be used. + +// Setting repo owner and repo name by generate changelog +const ( + repoOwner = "openimsdk" + repoName = "actions-test" +) + // GitHubRepo struct represents the repo details. type GitHubRepo struct { Owner string @@ -32,6 +42,7 @@ func (g *GitHubRepo) classifyReleaseNotes(body string) map[string][]string { "fix": {}, "chore": {}, "refactor": {}, + "build": {}, "other": {}, } @@ -64,6 +75,8 @@ func (g *GitHubRepo) classifyReleaseNotes(body string) map[string][]string { category = "chore" } else if strings.HasPrefix(line, "* refactor") { category = "refactor" + } else if strings.HasPrefix(line, "* build") { + category = "build" } else { category = "other" } @@ -108,11 +121,13 @@ func (g *GitHubRepo) generateChangelog(tag, date, htmlURL, body string) string { if len(sections["refactor"]) > 0 { changelog += "### Refactors\n" + strings.Join(sections["refactor"], "\n") + "\n\n" } + if len(sections["build"]) > 0 { + changelog += "### Builds\n" + strings.Join(sections["build"], "\n") + "\n\n" + } if len(sections["other"]) > 0 { changelog += "### Others\n" + strings.Join(sections["other"], "\n") + "\n\n" } - // Add the Full Changelog link at the end, if available. if g.FullChangelog != "" { changelog += fmt.Sprintf("**Full Changelog**: %s\n", g.FullChangelog) } @@ -153,13 +168,11 @@ func (g *GitHubRepo) fetchReleaseData(version string) (*ReleaseData, error) { } func main() { - // Create a new GitHubRepo instance - repoOwner := "openimsdk" - repoName := "actions-test" repo := &GitHubRepo{Owner: repoOwner, Repo: repoName} // Get the version from command line arguments, if provided - var version string + var version string // Default is use latest + if len(os.Args) > 1 { version = os.Args[1] // Use the provided version }