Skip to content

Commit

Permalink
feat: replace custom Slack implementation with go-slack
Browse files Browse the repository at this point in the history
Built together with @aexvir.

Closes #167
  • Loading branch information
fallion authored and kodiakhq[bot] committed Jan 23, 2021
1 parent e435644 commit 49becf2
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 221 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/json-iterator/go v1.1.10
github.com/magefile/mage v1.10.0
github.com/pelletier/go-toml v1.6.0 // indirect
github.com/slack-go/slack v0.7.2
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.0.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand All @@ -105,6 +107,7 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand Down Expand Up @@ -218,6 +221,8 @@ github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/slack-go/slack v0.7.2 h1:oLy2a2YqrtoHSSxbjRhrtLDGbCKcZJwgbuQ826BWxaI=
github.com/slack-go/slack v0.7.2/go.mod h1:FGqNzJBmxIsZURAxh2a8D21AnOVvvXZvGligs4npPUM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
Expand Down Expand Up @@ -246,6 +251,7 @@ github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/y
github.com/spf13/viper v1.7.1 h1:pM5oEahlgWv/WnHXpgbKz7iLIxRf65tye2Ci+XFK5sk=
github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
23 changes: 6 additions & 17 deletions internal/slack/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package slack

import (
"bytes"
"net/http"
"time"

"github.com/aevea/quoad"
jsoniter "github.com/json-iterator/go"
"github.com/slack-go/slack"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary
Expand All @@ -25,21 +24,11 @@ func jsonMarshal(t interface{}) ([]byte, error) {
func (s *Slack) Publish(commits map[string][]quoad.Commit, remote GitRemoter) error {
releaseNotes := GenerateReleaseNotes(commits, remote)

client := http.Client{
Timeout: time.Second * 5,
msg := slack.WebhookMessage{
Blocks: &slack.Blocks{
BlockSet: releaseNotes,
},
}

jsonBody, err := jsonMarshal(releaseNotes)

if err != nil {
return err
}

_, err = client.Post(s.WebHookURL, "application/json", bytes.NewBuffer(jsonBody))

if err != nil {
return err
}

return nil
return slack.PostWebhook(s.WebHookURL, &msg)
}
36 changes: 13 additions & 23 deletions internal/slack/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,9 @@ func TestPublish(t *testing.T) {

assert.NoError(t, err)

expectedBody, err := ioutil.ReadFile("./testdata/expected_output.txt")
expectedBody, err := ioutil.ReadFile("./testdata/expected_output.json")

type BlockList struct {
Blocks []Block
}

var receivedBlocks, expectedBlocks BlockList

rbErr, ebErr := json.Unmarshal(body, &receivedBlocks), json.Unmarshal(expectedBody, &expectedBlocks)

assert.NoError(t, rbErr)
assert.NoError(t, ebErr)
assert.ElementsMatch(t, expectedBlocks.Blocks, receivedBlocks.Blocks)
assert.Equal(t, string(body), string(expectedBody))

_, err = rw.Write([]byte(`ok`))

Expand All @@ -45,20 +35,20 @@ func TestPublish(t *testing.T) {
}

testData := map[string][]quoad.Commit{
"features": []quoad.Commit{
quoad.Commit{Category: "feat", Scope: "ci", Heading: "ci test"},
"features": {
{Category: "feat", Scope: "ci", Heading: "ci test"},
},
"bugs": []quoad.Commit{
quoad.Commit{Category: "bug", Scope: "", Heading: "huge bug"},
quoad.Commit{Category: "fix", Scope: "", Heading: "bug fix"},
"bugs": {
{Category: "bug", Scope: "", Heading: "huge bug"},
{Category: "fix", Scope: "", Heading: "bug fix"},
},
"chores": []quoad.Commit{
quoad.Commit{Category: "chore", Scope: "", Heading: "testing", Issues: []int{1, 2}},
quoad.Commit{Category: "improvement", Scope: "", Heading: "this should end up in chores", Issues: []int{3}},
"chores": {
{Category: "chore", Scope: "", Heading: "testing", Issues: []int{1, 2}},
{Category: "improvement", Scope: "", Heading: "this should end up in chores", Issues: []int{3}},
},
"others": []quoad.Commit{
quoad.Commit{Category: "other", Scope: "", Heading: "merge master in something"},
quoad.Commit{Category: "bs", Scope: "", Heading: "random"},
"others": {
{Category: "other", Scope: "", Heading: "merge master in something"},
{Category: "bs", Scope: "", Heading: "random"},
},
}

Expand Down
122 changes: 65 additions & 57 deletions internal/slack/release_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package slack

import (
"fmt"
"os"
"strings"

"github.com/aevea/quoad"
"github.com/aevea/release-notary/internal"
"github.com/slack-go/slack"
)

func pluralize(base string, count int) string {
Expand All @@ -31,88 +31,85 @@ 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) WebhookMessage {
blocks := []Block{buildReleaseTitle(remote)}
func GenerateReleaseNotes(sections map[string][]quoad.Commit, remote GitRemoter) []slack.Block {
blocks := []slack.Block{buildReleaseTitle(remote)}

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

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

sectionContext := Block{
Type: "context",
Elements: []content{
content{
Type: "mrkdwn",
Text: fmt.Sprintf(
"%s referencing %s",
pluralize("commit", len(commits)),
pluralize("issue", countReferences(commits)),
),
},
sectionContext := slack.NewContextBlock(
"context",
&slack.TextBlockObject{
Type: "mrkdwn",
Text: fmt.Sprintf(
"%s referencing %s",
pluralize("commit", len(commits)),
pluralize("issue", countReferences(commits)),
),
},
}

sectionCommits := Block{
Type: "section",
Section: buildCommitList(commits, remote.GetRemoteURL()),
}
)

blocks = append(
blocks,
sectionTitle,
sectionContext,
sectionCommits,
Block{Type: "divider"},
)
}
}

return WebhookMessage{Blocks: blocks}
}
blocks = append(blocks, buildCommitList(commits, remote.GetRemoteURL())...)

func buildReleaseTitle(remote GitRemoter) Block {
// This is also quite hacky, it shouldn't be done here, but somewhere before
release, isGithub := os.LookupEnv("GITHUB_REPOSITORY")
remoteURL := remote.GetRemoteURL()

// TODO: Improve this logic
if !isGithub {
return Block{
Type: "section",
Section: content{
Type: "mrkdwn",
Text: fmt.Sprintf(
":tada: Release <%s/releases/tag/%s|*%s*> for <%s|*%s*>",
remoteURL,
release,
release,
remoteURL,
remote.Project(),
),
},
// Check if there is another section following this one in order to display a divider
if currentSection+1 < len(sections) {
blocks = append(blocks, slack.NewDividerBlock())
}

currentSection++
}
}

// For GitHub it will be skipped for now :/ We'll need to fetch it via the API
return Block{
Type: "section",
Section: content{
Type: "mrkdwn",
Text: fmt.Sprintf(":tada: New release for <%s|*%s*>", remoteURL, remote.Project()),
return blocks
}

func buildReleaseTitle(remote GitRemoter) slack.Block {
return slack.HeaderBlock{
Type: "header",
Text: &slack.TextBlockObject{
Type: "plain_text",
Text: fmt.Sprintf(":tada: New release for %s", remote.Project()),
Emoji: true,
},
}
}

func buildCommitList(commits []quoad.Commit, remote string) content {
func buildCommitList(commits []quoad.Commit, remote string) []slack.Block {
builder := strings.Builder{}
blocks := []slack.Block{}

if len(commits) > 15 {
commits = commits[:15]
blocks = append(
blocks,
slack.NewContextBlock(
"",
&slack.TextBlockObject{
Type: "mrkdwn",
Text: fmt.Sprintf(
"Only last 15 commits shown. *Full changelog <%s|here>*",
remote,
),
},
),
)
}

for _, commit := range commits {
smallHash := commit.Hash.String()[:8]
Expand Down Expand Up @@ -148,7 +145,18 @@ func buildCommitList(commits []quoad.Commit, remote string) content {
builder.WriteString("\r\n")
}

section := content{Type: "mrkdwn", Text: builder.String()}
blocks = append(
[]slack.Block{
slack.SectionBlock{
Type: "section",
Text: &slack.TextBlockObject{
Type: "mrkdwn",
Text: builder.String(),
},
},
},
blocks...,
)

return section
return blocks
}
Loading

0 comments on commit 49becf2

Please sign in to comment.