-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add changelog command #14
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
62da804
7298ab6
c6ce7f8
e26259d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/bwmarrin/discordgo" | ||
| gogithub "github.com/google/go-github/v57/github" | ||
| ) | ||
|
|
||
| var ( | ||
| releaseCache []*gogithub.RepositoryRelease | ||
| releaseCacheMutex sync.RWMutex | ||
| lastCacheUpdate time.Time | ||
| cacheDuration = 5 * time.Minute | ||
| ) | ||
|
|
||
| func handleChangelog(s *discordgo.Session, i *discordgo.InteractionCreate) { | ||
| options := i.ApplicationCommandData().Options | ||
| optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options)) | ||
| for _, opt := range options { | ||
| optionMap[opt.Name] = opt | ||
| } | ||
|
|
||
| var base, head string | ||
| if opt, ok := optionMap["base"]; ok { | ||
| base = opt.StringValue() | ||
| } | ||
| if opt, ok := optionMap["head"]; ok { | ||
| head = opt.StringValue() | ||
| } | ||
|
|
||
| if base == "" || head == "" { | ||
| s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ | ||
| Type: discordgo.InteractionResponseChannelMessageWithSource, | ||
| Data: &discordgo.InteractionResponseData{ | ||
| Content: "Please provide both base and head versions.", | ||
| Flags: discordgo.MessageFlagsEphemeral, | ||
| }, | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| // Defer response as API call might take time | ||
| s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ | ||
| Type: discordgo.InteractionResponseDeferredChannelMessageWithSource, | ||
| }) | ||
|
|
||
| comparison, err := GithubClient.CompareCommits(GithubOwner, GithubRepo, base, head) | ||
| if err != nil { | ||
| log.Printf("Error comparing commits: %v", err) | ||
| s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ | ||
| Content: &[]string{fmt.Sprintf("Failed to compare versions: %s...%s", base, head)}[0], | ||
danditomaso marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }) | ||
| return | ||
| } | ||
|
|
||
| // Format the output | ||
| message := formatChangelogMessage(base, head, comparison) | ||
|
|
||
| s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ | ||
| Content: &message, | ||
| }) | ||
| } | ||
|
Comment on lines
21
to
68
|
||
|
|
||
| func formatChangelogMessage(base, head string, comparison *gogithub.CommitsComparison) string { | ||
| var sb strings.Builder | ||
| sb.WriteString(fmt.Sprintf("## Changes from %s to %s\n", base, head)) | ||
| sb.WriteString(fmt.Sprintf("Total commits: %d\n\n", comparison.GetTotalCommits())) | ||
|
|
||
| // List commits (limit to last 10 to avoid hitting message length limits) | ||
| commits := comparison.Commits | ||
| if len(commits) > 10 { | ||
| sb.WriteString(fmt.Sprintf("*Showing last 10 of %d commits*\n\n", len(commits))) | ||
| commits = commits[len(commits)-10:] | ||
| } | ||
|
|
||
| for _, commit := range commits { | ||
| message := commit.GetCommit().GetMessage() | ||
| // Take only the first line of the commit message | ||
| if idx := strings.Index(message, "\n"); idx != -1 { | ||
| message = message[:idx] | ||
| } | ||
|
|
||
| author := commit.GetAuthor().GetLogin() | ||
| if author == "" { | ||
| author = commit.GetCommit().GetAuthor().GetName() | ||
danditomaso marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| sb.WriteString(fmt.Sprintf("- [`%s`](<%s>) %s - *%s*\n", | ||
| commit.GetSHA()[:7], | ||
danditomaso marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| commit.GetHTMLURL(), | ||
| message, | ||
| author, | ||
| )) | ||
| } | ||
|
|
||
| sb.WriteString(fmt.Sprintf("\n[View Full Comparison](<%s>)", comparison.GetHTMLURL())) | ||
| return sb.String() | ||
| } | ||
|
|
||
| func handleChangelogAutocomplete(s *discordgo.Session, i *discordgo.InteractionCreate) { | ||
| // Update cache if needed | ||
| if err := updateReleaseCache(); err != nil { | ||
| log.Printf("Error updating release cache: %v", err) | ||
| } | ||
|
Comment on lines
+115
to
+119
|
||
|
|
||
| releaseCacheMutex.RLock() | ||
| defer releaseCacheMutex.RUnlock() | ||
|
|
||
| data := i.ApplicationCommandData() | ||
| var currentInput string | ||
| for _, opt := range data.Options { | ||
| if opt.Focused { | ||
| currentInput = strings.ToLower(opt.StringValue()) | ||
| break | ||
| } | ||
| } | ||
|
|
||
| choices := make([]*discordgo.ApplicationCommandOptionChoice, 0, 25) | ||
| for _, release := range releaseCache { | ||
| tagName := release.GetTagName() | ||
| if currentInput == "" || strings.Contains(strings.ToLower(tagName), currentInput) { | ||
| choices = append(choices, &discordgo.ApplicationCommandOptionChoice{ | ||
| Name: tagName, | ||
| Value: tagName, | ||
| }) | ||
| } | ||
| if len(choices) >= 25 { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ | ||
| Type: discordgo.InteractionApplicationCommandAutocompleteResult, | ||
| Data: &discordgo.InteractionResponseData{ | ||
| Choices: choices, | ||
| }, | ||
| }) | ||
| } | ||
|
Comment on lines
+115
to
+153
|
||
|
|
||
| func updateReleaseCache() error { | ||
| releaseCacheMutex.RLock() | ||
| if time.Since(lastCacheUpdate) < cacheDuration && len(releaseCache) > 0 { | ||
| releaseCacheMutex.RUnlock() | ||
| return nil | ||
| } | ||
| releaseCacheMutex.RUnlock() | ||
|
|
||
| releaseCacheMutex.Lock() | ||
| defer releaseCacheMutex.Unlock() | ||
|
|
||
| // Double check after acquiring write lock | ||
| if time.Since(lastCacheUpdate) < cacheDuration && len(releaseCache) > 0 { | ||
| return nil | ||
| } | ||
|
Comment on lines
+155
to
+169
|
||
|
|
||
| // Fetch releases | ||
| releases, err := GithubClient.GetReleases(GithubOwner, GithubRepo, 100) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| releaseCache = releases | ||
| lastCacheUpdate = time.Now() | ||
| return nil | ||
| } | ||
|
Comment on lines
+155
to
+180
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| gogithub "github.com/google/go-github/v57/github" | ||
| ) | ||
|
|
||
| func TestFormatChangelogMessage(t *testing.T) { | ||
| strPtr := func(s string) *string { return &s } | ||
| intPtr := func(i int) *int { return &i } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| base string | ||
| head string | ||
| comparison *gogithub.CommitsComparison | ||
| want []string // Substrings that should be present | ||
| dontWant []string // Substrings that should NOT be present | ||
| }{ | ||
| { | ||
| name: "basic comparison", | ||
| base: "v1.0.0", | ||
| head: "v1.1.0", | ||
| comparison: &gogithub.CommitsComparison{ | ||
| TotalCommits: intPtr(2), | ||
| HTMLURL: strPtr("https://github.com/org/repo/compare/v1.0.0...v1.1.0"), | ||
| Commits: []*gogithub.RepositoryCommit{ | ||
| { | ||
| SHA: strPtr("abcdef123456"), | ||
| HTMLURL: strPtr("https://github.com/org/repo/commit/abcdef1"), | ||
| Commit: &gogithub.Commit{ | ||
| Message: strPtr("feat: cool feature"), | ||
| Author: &gogithub.CommitAuthor{ | ||
| Name: strPtr("John Doe"), | ||
| }, | ||
| }, | ||
| Author: &gogithub.User{ | ||
| Login: strPtr("johndoe"), | ||
| }, | ||
| }, | ||
| { | ||
| SHA: strPtr("123456abcdef"), | ||
| HTMLURL: strPtr("https://github.com/org/repo/commit/123456a"), | ||
| Commit: &gogithub.Commit{ | ||
| Message: strPtr("fix: nasty bug\n\nSome details"), | ||
| Author: &gogithub.CommitAuthor{ | ||
| Name: strPtr("Jane Smith"), | ||
| }, | ||
| }, | ||
| Author: &gogithub.User{ | ||
| Login: strPtr("janesmith"), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| want: []string{ | ||
| "## Changes from v1.0.0 to v1.1.0", | ||
| "Total commits: 2", | ||
| "[`abcdef1`](<https://github.com/org/repo/commit/abcdef1>)", | ||
| "feat: cool feature", | ||
| "johndoe", | ||
| "[`123456a`](<https://github.com/org/repo/commit/123456a>)", | ||
| "fix: nasty bug", | ||
| "janesmith", | ||
| "[View Full Comparison](<https://github.com/org/repo/compare/v1.0.0...v1.1.0>)", | ||
| }, | ||
| dontWant: []string{ | ||
| "Some details", | ||
| "Showing last 10", | ||
| }, | ||
| }, | ||
| { | ||
| name: "many commits truncated", | ||
| base: "v1.0.0", | ||
| head: "v1.1.0", | ||
| comparison: &gogithub.CommitsComparison{ | ||
| TotalCommits: intPtr(15), | ||
| HTMLURL: strPtr("https://github.com/compare"), | ||
| Commits: func() []*gogithub.RepositoryCommit { | ||
| commits := make([]*gogithub.RepositoryCommit, 15) | ||
| for i := 0; i < 15; i++ { | ||
| commits[i] = &gogithub.RepositoryCommit{ | ||
| SHA: strPtr("longhashvalue"), | ||
| HTMLURL: strPtr("url"), | ||
| Commit: &gogithub.Commit{ | ||
| Message: strPtr("msg"), | ||
| Author: &gogithub.CommitAuthor{Name: strPtr("author")}, | ||
| }, | ||
| Author: &gogithub.User{Login: strPtr("user")}, | ||
| } | ||
| } | ||
| return commits | ||
| }(), | ||
| }, | ||
| want: []string{ | ||
| "Total commits: 15", | ||
| "*Showing last 10 of 15 commits*", | ||
| }, | ||
| }, | ||
|
Comment on lines
+74
to
+101
|
||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := formatChangelogMessage(tt.base, tt.head, tt.comparison) | ||
|
|
||
| for _, w := range tt.want { | ||
| if !strings.Contains(got, w) { | ||
| t.Errorf("formatChangelogMessage() missing %q\nGot:\n%s", w, got) | ||
| } | ||
| } | ||
|
|
||
| for _, dw := range tt.dontWant { | ||
| if strings.Contains(got, dw) { | ||
| t.Errorf("formatChangelogMessage() unexpectedly contains %q", dw) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
Comment on lines
+10
to
+121
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,25 @@ func NewClient(token string) *Client { | |
| } | ||
| } | ||
|
|
||
| func (c *Client) GetReleases(owner, repo string, limit int) ([]*github.RepositoryRelease, error) { | ||
| opts := &github.ListOptions{ | ||
| PerPage: limit, | ||
| } | ||
| releases, _, err := c.client.Repositories.ListReleases(c.ctx, owner, repo, opts) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to list releases: %w", err) | ||
| } | ||
| return releases, nil | ||
| } | ||
|
Comment on lines
+43
to
+52
|
||
|
|
||
| func (c *Client) CompareCommits(owner, repo, base, head string) (*github.CommitsComparison, error) { | ||
| comparison, _, err := c.client.Repositories.CompareCommits(c.ctx, owner, repo, base, head, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to compare commits: %w", err) | ||
| } | ||
| return comparison, nil | ||
| } | ||
|
Comment on lines
+54
to
+60
|
||
|
|
||
| func (c *Client) CreateIssue(owner, repo, title, body string, labels []string) (*IssueResponse, error) { | ||
| log.Printf("[GitHub API] Creating issue in %s/%s", owner, repo) | ||
| log.Printf("[GitHub API] Title: %s", title) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider adding a comment explaining the cache invalidation strategy. The double-checked locking pattern is correctly implemented, but it would be helpful to document why a 5-minute cache duration was chosen and what tradeoffs it represents (freshness vs. API rate limits).