Skip to content

Commit 33272b3

Browse files
committed
added integration test for good measure
1 parent 5adb327 commit 33272b3

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ func run(c *cli.Context) error {
237237
LinkNames: c.Bool("link-names"),
238238
},
239239
}
240-
240+
if plugin.Build.Commit == "" {
241+
plugin.Build.Commit = "0000000000000000000000000000000000000000"
242+
}
241243
if plugin.Config.Webhook == "" {
242244
return errors.New("Missing webhook")
243245
}

plugin.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,6 @@ func (p Plugin) Exec() error {
137137
return client.PostMessage(&payload)
138138
}
139139

140-
func detectRef(build Build) string {
141-
if build.Commit != "" {
142-
return build.Commit[:8]
143-
}
144-
145-
return build.Tag
146-
}
147-
148140
func templateMessage(t string, plugin Plugin) (string, error) {
149141
return template.RenderTrim(t, plugin)
150142
}
@@ -155,7 +147,7 @@ func message(repo Repo, build Build) string {
155147
build.Link,
156148
repo.Owner,
157149
repo.Name,
158-
detectRef(build),
150+
build.Commit[:8],
159151
build.Branch,
160152
build.Author,
161153
)
@@ -166,7 +158,7 @@ func fallback(repo Repo, build Build) string {
166158
build.Status,
167159
repo.Owner,
168160
repo.Name,
169-
detectRef(build),
161+
build.Commit[:8],
170162
build.Branch,
171163
build.Author,
172164
)

plugin_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
11
package main
22

33
import (
4+
"io/ioutil"
5+
"net/http"
6+
"net/http/httptest"
47
"testing"
58

69
"gotest.tools/assert"
710
)
811

12+
func TestExec(t *testing.T) {
13+
plugin := Plugin{
14+
Repo: getTestRepo(),
15+
Build: getTestBuild(),
16+
Job: getTestJob(),
17+
Config: getTestConfig(),
18+
}
19+
20+
handler := func(w http.ResponseWriter, r *http.Request) {
21+
out, _ := ioutil.ReadAll(r.Body)
22+
got := string(out)
23+
want := `{"attachments":[{"color":"good","fallback":"Message Template Fallback:\nInitial commit\nmaster\nsuccess","text":"Message Template:\nInitial commit\n\nMessage body\nInitial commit\nMessage body","mrkdwn_in":["text","fallback"]}]}`
24+
assert.Equal(t, got, want)
25+
}
26+
27+
server := httptest.NewServer(http.HandlerFunc(handler))
28+
defer server.Close()
29+
30+
plugin.Config.Webhook = server.URL
31+
plugin.Exec()
32+
}
33+
934
func TestNewCommitMessage(t *testing.T) {
1035
testCases := map[string]struct {
1136
Msg string
@@ -135,6 +160,12 @@ func getTestBuild() Build {
135160
}
136161
}
137162

163+
func getTestJob() Job {
164+
return Job{
165+
Started: 1546340400,
166+
}
167+
}
168+
138169
func getTestConfig() Config {
139170
t := `Message Template:
140171
{{build.message}}

0 commit comments

Comments
 (0)