Skip to content

Commit

Permalink
feat: add code for send to slack
Browse files Browse the repository at this point in the history
  • Loading branch information
gotoeveryone committed Mar 14, 2023
1 parent be96feb commit 855b00a
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 129 deletions.
53 changes: 0 additions & 53 deletions .air.toml

This file was deleted.

2 changes: 0 additions & 2 deletions .dockerignore

This file was deleted.

1 change: 0 additions & 1 deletion .env.example

This file was deleted.

39 changes: 39 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: CI

on: [push]

jobs:
ci:
name: CI
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Cache dependencies
uses: actions/cache@v3
id: cache
with:
path: ~/go/pkg/mod
key: ${{ runner.OS }}-1.19-${{ hashFiles('**/go.sum') }}

- name: Get dependencies
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
run: go mod download

- name: Run vet
run: go vet ./...

- name: Run staticcheck
uses: dominikh/staticcheck-action@v1
with:
version: 2022.1.2
install-go: false
cache-key: app

- name: Run test
run: go test -v ./...
6 changes: 0 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,3 @@ Thumbs.db

# asdf
.tool-versions

# dotenv
.env

# Output directory of binary
/tmp
43 changes: 0 additions & 43 deletions Dockerfile

This file was deleted.

19 changes: 6 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
# Sentry to Slack

Sentry のデータを Slack に連携するためのツール
Sentry のデータを Slack に連携するための関数群

## Requirements

- Docker
- Go 1.19+

## Run
## Environment Variables

```console
$ cp .env.example .env
$ docker compose up
```

## Post data

```console
$ curl -X POST -d '{"url":"https://test.example.com","event":{"title":"TestError","level":"error","environment": "test"}}' http://localhost:8080
```
- SLACK_API_TOKEN: Slack の API トークン
- SLACK_CHANNEL: 通知先のチャンネル
- TAGS: 通知に利用したいタグをカンマ区切りで定義 (例: `server_name,environment`)
11 changes: 0 additions & 11 deletions compose.yml

This file was deleted.

46 changes: 46 additions & 0 deletions entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package sentry_to_slack

type Body struct {
Url string `json:"url"`
Project string `json:"project"`
Level string `json:"level"`
Event Event `json:"event"`
}

func (b *Body) Color() string {
switch b.Level {
case "error":
return "#ff7738"
case "warning":
return "#b28000"
case "info":
return "#3070e8"
default:
return ""
}
}

type Event struct {
Title string `json:"title"`
Culprit string `json:"culprit"`
Environment string `json:"environment"`
Tags [][]string `json:"tags"`
}

// Slack へリクエストするための構造体
type Request struct {
Channel string `json:"channel"`
Attachments []RequestAttachment `json:"attachments"`
}

type RequestAttachment struct {
Title string `json:"title"`
Color string `json:"color"`
Fields []RequestField `json:"fields"`
}

type RequestField struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short"`
}
21 changes: 21 additions & 0 deletions entity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package sentry_to_slack

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestColor(t *testing.T) {
for _, p := range []struct {
level string
color string
}{
{"error", "#ff7738"},
{"warning", "#b28000"},
{"info", "#3070e8"},
} {
b := Body{Level: p.level}
assert.Equal(t, b.Color(), p.color)
}
}
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/gotoeveryone/sentry_to_slack

go 1.19

require github.com/stretchr/testify v1.7.2

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
64 changes: 64 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package sentry_to_slack

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
)

func SendToSlack(r Request) (*http.Response, error) {
body, err := json.Marshal(r)
if err != nil {
return nil, err
}
c := http.Client{}
req, err := http.NewRequest("POST", "https://slack.com/api/chat.postMessage", bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("SLACK_API_TOKEN")))
res, err := c.Do(req)
if err != nil {
return nil, err
}
return res, nil
}

func CreateRequest(b Body) Request {
req := Request{
Channel: os.Getenv("SLACK_CHANNEL"),
Attachments: []RequestAttachment{
{
Title: fmt.Sprintf("<%s|%s>",
b.Url,
b.Event.Title,
),
Color: b.Color(),
Fields: []RequestField{
{Title: "", Value: b.Event.Culprit},
},
},
},
}

tags := strings.Split(os.Getenv("TAGS"), ",")
for _, tag := range tags {
for _, t := range b.Event.Tags {
if len(t) < 2 {
continue
}
key := t[0]
if key != strings.TrimSpace(tag) {
continue
}
value := t[1]
req.Attachments[0].Fields = append(req.Attachments[0].Fields, RequestField{Title: key, Value: value, Short: true})
}
}

return req
}
46 changes: 46 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package sentry_to_slack

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCreateRequest(t *testing.T) {
os.Setenv("SLACK_CHANNEL", "test_channel")
os.Setenv("TAGS", "server_name ,environment")
b := Body{
Url: "https://test.example.com",
Level: "error",
Project: "test_project",
Event: Event{
Title: "TestError",
Culprit: "This is test error",
Tags: [][]string{
{"environment", "test"},
{"server_name", "test.example.com"},
{"ignore", "test"},
},
},
}
r := CreateRequest(b)
assert.Equal(t, r, Request{
Channel: "test_channel",
Attachments: []RequestAttachment{
{
Title: fmt.Sprintf("<%s|%s>",
b.Url,
b.Event.Title,
),
Color: b.Color(),
Fields: []RequestField{
{Title: "", Value: b.Event.Culprit},
{Title: "server_name", Value: "test.example.com", Short: true},
{Title: "environment", Value: "test", Short: true},
},
},
},
})
}

0 comments on commit 855b00a

Please sign in to comment.