Skip to content

Commit d8279c3

Browse files
committed
feat: add container / tests and release ci
1 parent 8cd74e7 commit d8279c3

File tree

10 files changed

+136
-1
lines changed

10 files changed

+136
-1
lines changed

.github/workflows/release.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: goreleaser
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
tags:
7+
- '*'
8+
9+
jobs:
10+
goreleaser:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
with:
16+
fetch-depth: 0
17+
- name: Set up Go
18+
uses: actions/setup-go@v2
19+
with:
20+
go-version: '^1.17'
21+
- name: Set up QEMU
22+
uses: docker/setup-qemu-action@v1
23+
- name: Set up Docker Buildx
24+
uses: docker/setup-buildx-action@v1
25+
- name: Login to DockerHub
26+
uses: docker/login-action@v1
27+
with:
28+
username: gsdevmebot
29+
password: ${{ secrets.DOCKER_HUB }}
30+
- name: Run GoReleaser
31+
uses: goreleaser/goreleaser-action@v2
32+
with:
33+
version: latest
34+
args: release --rm-dist
35+
env:
36+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
dist/
1+
dist/
2+
coverage.out

.goreleaser.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ builds:
1313
goos:
1414
- linux
1515
- darwin
16+
dockers:
17+
- image_templates:
18+
- "gsdevme/gitops-commit:latest"
19+
- "gsdevme/gitops-commit:{{ .Tag }}"
20+
- "gsdevme/gitops-commit:v{{ .Major }}"
1621
archives:
1722
- replacements:
1823
darwin: Darwin

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM scratch
2+
3+
ENV SLACK_SIGNING_SECRET=""
4+
ENV PORT=8080
5+
6+
ENTRYPOINT ["/gitops-commit"]
7+
COPY gitops-commit /

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ default: build;
44
fmt:
55
go fmt ./...
66

7+
lint:
8+
golangci-lint run
9+
710
tests:
811
go test ./...
912

13+
coverage:
14+
go test -coverprofile=coverage.out ./... && go tool cover -html=coverage.out
15+
1016
run:
1117
./dist/gitops-commit_darwin_amd64/gitops-commit
1218

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.17
44

55
require (
66
github.com/go-git/go-git/v5 v5.4.2
7+
github.com/google/martian v2.1.0+incompatible
78
github.com/gorilla/mux v1.8.0
89
github.com/slack-go/slack v0.9.5
910
github.com/spf13/cobra v1.2.1

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
146146
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
147147
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
148148
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
149+
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
149150
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
150151
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
151152
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package slackhttp
2+
3+
import (
4+
"encoding/json"
5+
"github.com/slack-go/slack"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
)
10+
11+
func Test_handleSlackCommand(t *testing.T) {
12+
t.Run("invalid command", func(t *testing.T) {
13+
s := &server{}
14+
r := &NamedRepositoryRegistry{}
15+
16+
tests := []struct{
17+
name string
18+
command string
19+
expect string
20+
}{
21+
{
22+
name: "when using missing bits",
23+
command: "totally wrong",
24+
expect: "Incorrect usage, expected /gitops-commit [command] [name] [tag]",
25+
},
26+
{
27+
name: "when using an invalid command",
28+
command: "wrong thing v1.2.3",
29+
expect: "Unknown command 'wrong', expected /gitops-commit [command] [name] [tag]",
30+
},
31+
{
32+
name: "when using a valid command ",
33+
command: "deploy thing v1.2.3",
34+
expect: "Unknown named repository, cannot handle \"thing\", availabe options ()",
35+
},
36+
}
37+
38+
for _, tt := range tests {
39+
slackCommand := slack.SlashCommand{
40+
Text: tt.command,
41+
}
42+
43+
t.Run(tt.name, func(t *testing.T) {
44+
rr := httptest.NewRecorder()
45+
46+
s.handleSlackCommand(r)(rr, slackCommand)
47+
48+
if c := rr.Code; c != http.StatusOK {
49+
t.Errorf("handleSlackCommand() = %v, want %v", c, http.StatusOK)
50+
}
51+
52+
response := make(map[string]interface{})
53+
err := json.Unmarshal(rr.Body.Bytes(), &response)
54+
55+
if err != nil {
56+
t.Errorf("handleSlackCommand() invalid json: %s", err)
57+
}
58+
59+
if got, ok := response["text"].(string); ok {
60+
if got != tt.expect {
61+
t.Errorf("expected '%s', got '%s'", tt.expect, got)
62+
}
63+
}
64+
})
65+
}
66+
})
67+
}

internal/app/gitops-commit/slackhttp/middleware.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package slackhttp
22

33
import (
4+
"github.com/google/martian/log"
45
"github.com/slack-go/slack"
56
"io"
67
"io/ioutil"
@@ -27,6 +28,8 @@ func (s *server) SlackCommandMiddleware(next func(w http.ResponseWriter, s slack
2728
if err = verifier.Ensure(); err != nil {
2829
w.WriteHeader(http.StatusUnauthorized)
2930

31+
log.Debugf("unauthorized request")
32+
3033
return
3134
}
3235

internal/app/gitops-commit/slackhttp/model.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ func (c *NamedRepositoryRegistry) Add(name string, r string, f string, n string,
3131
}
3232

3333
func (c *NamedRepositoryRegistry) findNamedRepository(n string) (*NamedRepository, error) {
34+
if c.r == nil {
35+
return nil, fmt.Errorf("no named repository found for %s", n)
36+
}
37+
3438
for _, r := range *c.r {
3539
if r.Name == n {
3640
return &r, nil
@@ -41,6 +45,10 @@ func (c *NamedRepositoryRegistry) findNamedRepository(n string) (*NamedRepositor
4145
}
4246

4347
func (c *NamedRepositoryRegistry) getNamesFlattened() string {
48+
if c.r == nil {
49+
return ""
50+
}
51+
4452
var names string
4553

4654
for _, r := range *c.r {

0 commit comments

Comments
 (0)