-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be96feb
commit 855b00a
Showing
14 changed files
with
245 additions
and
129 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,9 +27,3 @@ Thumbs.db | |
|
||
# asdf | ||
.tool-versions | ||
|
||
# dotenv | ||
.env | ||
|
||
# Output directory of binary | ||
/tmp |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} |