Skip to content

Commit

Permalink
fix(slack): use fixed section order when generating changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
aexvir authored and kodiakhq[bot] committed Jan 26, 2021
1 parent 50a9405 commit 92b8307
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 59 deletions.
22 changes: 17 additions & 5 deletions internal/sections.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@ package internal

// SectionInfo contains the details that we store for each section
type SectionInfo struct {
ID string
Title string
Icon string
// whether the section should not be collapsed by default
OpenByDefault bool
Position int
}

// PredefinedSections contains a list of the predefined sections we consider when generating the release notes
var PredefinedSections = map[string]SectionInfo{
"breaking": SectionInfo{Title: "Breaking Changes", Icon: "warning"},
"features": SectionInfo{Title: "Features", Icon: "rocket", OpenByDefault: true},
"chores": SectionInfo{Title: "Chores and Improvements", Icon: "wrench"},
"bugs": SectionInfo{Title: "Bug fixes", Icon: "bug"},
"others": SectionInfo{Title: "Other", Icon: "package"},
"features": {Title: "Features", Icon: "rocket", OpenByDefault: true, Position: 0},
"bugs": {Title: "Bug fixes", Icon: "bug", Position: 1},
"chores": {Title: "Chores and Improvements", Icon: "wrench", Position: 2},
"breaking": {Title: "Breaking Changes", Icon: "warning", Position: 3},
"others": {Title: "Other", Icon: "package", Position: 4},
}

// SortedSections returns a slice of Sections sorted by position
var SortedSections = make([]SectionInfo, len(PredefinedSections))

func init() {
for identifier, section := range PredefinedSections {
section.ID = identifier
SortedSections[section.Position] = section
}
}
2 changes: 0 additions & 2 deletions internal/slack/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
)

func TestPublish(t *testing.T) {
t.Skip()

server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
assert.Equal(t, "/webhook", req.URL.String())
assert.Equal(t, "application/json", req.Header["Content-Type"][0])
Expand Down
10 changes: 6 additions & 4 deletions internal/slack/release_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ func countReferences(commits []quoad.Commit) int {
}

// GenerateReleaseNotes creates a string from release notes that conforms with the Slack formatting. Expected format can be found in testdata.
func GenerateReleaseNotes(sections map[string][]quoad.Commit, remote GitRemoter) []slack.Block {
func GenerateReleaseNotes(changelog map[string][]quoad.Commit, remote GitRemoter) []slack.Block {
blocks := []slack.Block{buildReleaseTitle(remote)}

sections := internal.SortedSections

currentSection := 0
for name, commits := range sections {
for _, section := range sections {
commits := changelog[section.ID]
if len(commits) > 0 {
sectionInfo := internal.PredefinedSections[name]

sectionTitle := slack.SectionBlock{
Type: "section",
Text: &slack.TextBlockObject{
Type: "mrkdwn",
Text: fmt.Sprintf(":%s: *%s*", sectionInfo.Icon, sectionInfo.Title),
Text: fmt.Sprintf(":%s: *%s*", section.Icon, section.Title),
},
}

Expand Down
47 changes: 0 additions & 47 deletions internal/slack/release_notes_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/slack/testdata/expected_output.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"blocks":[{"type":"header","text":{"type":"plain_text","text":":tada: New release for some/thing","emoji":true}},{"type":"section","text":{"type":"mrkdwn","text":":rocket: *Features*"}},{"type":"context","block_id":"","elements":[{"type":"mrkdwn","text":"1 commit referencing 0 issues"}]},{"type":"section","text":{"type":"mrkdwn","text":"`00000000` \u003chttps://example.com/some/thing/commit/00000000|*ci test*\u003e\r\n"}},{"type":"divider"},{"type":"section","text":{"type":"mrkdwn","text":":bug: *Bug fixes*"}},{"type":"context","block_id":"","elements":[{"type":"mrkdwn","text":"2 commits referencing 0 issues"}]},{"type":"section","text":{"type":"mrkdwn","text":"`00000000` \u003chttps://example.com/some/thing/commit/00000000|*huge bug*\u003e\r\n`00000000` \u003chttps://example.com/some/thing/commit/00000000|*bug fix*\u003e\r\n"}},{"type":"divider"},{"type":"section","text":{"type":"mrkdwn","text":":wrench: *Chores and Improvements*"}},{"type":"context","block_id":"","elements":[{"type":"mrkdwn","text":"2 commits referencing 3 issues"}]},{"type":"section","text":{"type":"mrkdwn","text":"`00000000` \u003chttps://example.com/some/thing/commit/00000000|*testing*\u003e _ref \u003chttps://example.com/some/thing/issues/1|#1\u003e,\u003chttps://example.com/some/thing/issues/2|#2\u003e_\r\n`00000000` \u003chttps://example.com/some/thing/commit/00000000|*this should end up in chores*\u003e _ref \u003chttps://example.com/some/thing/issues/3|#3\u003e_\r\n"}},{"type":"divider"},{"type":"section","text":{"type":"mrkdwn","text":":package: *Other*"}},{"type":"context","block_id":"","elements":[{"type":"mrkdwn","text":"2 commits referencing 0 issues"}]},{"type":"section","text":{"type":"mrkdwn","text":"`00000000` \u003chttps://example.com/some/thing/commit/00000000|*merge master in something*\u003e\r\n`00000000` \u003chttps://example.com/some/thing/commit/00000000|*random*\u003e\r\n"}}]}
{"blocks":[{"type":"header","text":{"type":"plain_text","text":":tada: New release for some/thing","emoji":true}},{"type":"section","text":{"type":"mrkdwn","text":":rocket: *Features*"}},{"type":"context","elements":[{"type":"mrkdwn","text":"1 commit referencing 0 issues"}]},{"type":"section","text":{"type":"mrkdwn","text":"`00000000` \u003chttps://example.com/some/thing/commit/00000000|*ci test*\u003e\r\n"}},{"type":"divider"},{"type":"section","text":{"type":"mrkdwn","text":":bug: *Bug fixes*"}},{"type":"context","elements":[{"type":"mrkdwn","text":"2 commits referencing 0 issues"}]},{"type":"section","text":{"type":"mrkdwn","text":"`00000000` \u003chttps://example.com/some/thing/commit/00000000|*huge bug*\u003e\r\n`00000000` \u003chttps://example.com/some/thing/commit/00000000|*bug fix*\u003e\r\n"}},{"type":"divider"},{"type":"section","text":{"type":"mrkdwn","text":":wrench: *Chores and Improvements*"}},{"type":"context","elements":[{"type":"mrkdwn","text":"2 commits referencing 3 issues"}]},{"type":"section","text":{"type":"mrkdwn","text":"`00000000` \u003chttps://example.com/some/thing/commit/00000000|*testing*\u003e _ref \u003chttps://example.com/some/thing/issues/1|#1\u003e,\u003chttps://example.com/some/thing/issues/2|#2\u003e_\r\n`00000000` \u003chttps://example.com/some/thing/commit/00000000|*this should end up in chores*\u003e _ref \u003chttps://example.com/some/thing/issues/3|#3\u003e_\r\n"}},{"type":"divider"},{"type":"section","text":{"type":"mrkdwn","text":":package: *Other*"}},{"type":"context","elements":[{"type":"mrkdwn","text":"2 commits referencing 0 issues"}]},{"type":"section","text":{"type":"mrkdwn","text":"`00000000` \u003chttps://example.com/some/thing/commit/00000000|*merge master in something*\u003e\r\n`00000000` \u003chttps://example.com/some/thing/commit/00000000|*random*\u003e\r\n"}},{"type":"divider"}]}

0 comments on commit 92b8307

Please sign in to comment.