Skip to content

Commit 21b9376

Browse files
authored
Merge pull request #2 from lucasgomide/documantation
Adding documentation
2 parents 2527320 + b726cc4 commit 21b9376

File tree

16 files changed

+115
-65
lines changed

16 files changed

+115
-65
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Snitch
22
[![Build Status](https://travis-ci.org/lucasgomide/snitch.svg?branch=master)](https://travis-ci.org/lucasgomide/snitch)
33
[![Coverage Status](https://coveralls.io/repos/github/lucasgomide/snitch/badge.svg?branch=master)](https://coveralls.io/github/lucasgomide/snitch?branch=master)
4+
[![Documentation](https://godoc.org/github.com/lucasgomide/snitch?status.svg)](http://godoc.org/github.com/lucasgomide/snitch)
45

56
Keep updated about each deploy via [Tsuru](https://docs.tsuru.io/stable/).
67

config/config.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package config
22

33
import (
4-
yaml "gopkg.in/yaml.v2"
54
"io/ioutil"
5+
6+
yaml "gopkg.in/yaml.v2"
67
)
78

89
type configuration struct {
@@ -11,6 +12,10 @@ type configuration struct {
1112

1213
var configs configuration
1314

15+
// ReadConfigFile reads the yml config file of given string
16+
// and save configs into struct configuration
17+
// To access the config use Data() function
18+
// It returns any errors encountered
1419
func ReadConfigFile(filePath string) error {
1520
data, err := ioutil.ReadFile(filePath)
1621
if err != nil {
@@ -28,6 +33,8 @@ func readBytes(data []byte) error {
2833
return err
2934
}
3035

36+
// Data reads data's struct field
37+
// It returns the configuration file parsead
3138
func Data() map[interface{}]interface{} {
3239
return configs.data
3340
}

hook/hook.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package hook
22

33
import (
44
"bytes"
5+
"reflect"
6+
"strings"
7+
58
"github.com/lucasgomide/snitch/config"
69
"github.com/lucasgomide/snitch/types"
710
"github.com/lucasgomide/snitch/utils"
8-
"reflect"
9-
"strings"
1011
)
1112

13+
// Execute runs all hooks that have been configured on
14+
// config file with your options
1215
func Execute(h types.Hook, t types.Tsuru) {
1316
var err error
1417

@@ -31,7 +34,7 @@ func Execute(h types.Hook, t types.Tsuru) {
3134
default:
3235
continue
3336
}
34-
if err := ExecuteHook(h, deploy, conf); err != nil {
37+
if err := executeHook(h, deploy, conf); err != nil {
3538
utils.LogError(err.Error())
3639
}
3740
}
@@ -43,14 +46,14 @@ func findLastDeploy(t types.Tsuru, deploy *[]types.Deploy) error {
4346
}
4447

4548
func callHook(h types.Hook, deploy []types.Deploy) error {
46-
if err := h.ValidatesFields(); err == nil {
49+
err := h.ValidatesFields()
50+
if err == nil {
4751
return h.CallHook(deploy)
48-
} else {
49-
return err
5052
}
53+
return err
5154
}
5255

53-
func ExecuteHook(h types.Hook, deploy []types.Deploy, conf interface{}) error {
56+
func executeHook(h types.Hook, deploy []types.Deploy, conf interface{}) error {
5457
s := reflect.ValueOf(h).Elem()
5558

5659
for k, v := range conf.(map[interface{}]interface{}) {

hook/hook_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ package hook
33
import (
44
"bytes"
55
"errors"
6-
"github.com/lucasgomide/snitch/config"
7-
"github.com/lucasgomide/snitch/types"
8-
"gopkg.in/jarcoal/httpmock.v1"
96
"log"
107
"os"
118
"strings"
129
"testing"
10+
11+
"github.com/lucasgomide/snitch/config"
12+
"github.com/lucasgomide/snitch/types"
13+
"gopkg.in/jarcoal/httpmock.v1"
1314
)
1415

1516
var (
1617
h HookFake
1718
tsuruFake TsuruFake
1819
err error
1920
configFilePath = "../testdata/config.yaml"
20-
d = []types.Deploy{types.Deploy{"app", "1234125125", "sha1", "user@42.com", "v15"}}
21+
d = []types.Deploy{{"app", "1234125125", "sha1", "user@42.com", "v15"}}
2122
conf = map[interface{}]interface{}{"field_sample": "key_value"}
2223
)
2324

@@ -50,7 +51,7 @@ func (t TsuruFake) FindLastDeploy(deploy *[]types.Deploy) error {
5051
}
5152

5253
func TestHookExecutedSuccessfully(t *testing.T) {
53-
if err = ExecuteHook(&h, d, conf); err != nil {
54+
if err = executeHook(&h, d, conf); err != nil {
5455
t.Error(err)
5556
}
5657

@@ -62,7 +63,7 @@ func TestHookExecutedSuccessfully(t *testing.T) {
6263
func TestReturnsErrorWhenCallHookFail(t *testing.T) {
6364
expected := "CallHook has failed"
6465
h.Err = errors.New(expected)
65-
err = ExecuteHook(&h, d, conf)
66+
err = executeHook(&h, d, conf)
6667

6768
if err == nil {
6869
t.Error("Expected error, got nil")
@@ -74,7 +75,7 @@ func TestReturnsErrorWhenCallHookFail(t *testing.T) {
7475

7576
func TestSetFieldsWithEnvValues(t *testing.T) {
7677
os.Setenv("NEW_ENV", "gotham")
77-
if err = ExecuteHook(&h, d, map[interface{}]interface{}{"field_sample": "$NEW_ENV"}); err != nil {
78+
if err = executeHook(&h, d, map[interface{}]interface{}{"field_sample": "$NEW_ENV"}); err != nil {
7879
t.Error(err)
7980
}
8081

hook/newrelic.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,28 @@ package hook
33
import (
44
"bytes"
55
"errors"
6-
"github.com/lucasgomide/snitch/types"
76
"net/http"
87
"time"
8+
9+
"github.com/lucasgomide/snitch/types"
910
)
1011

12+
// NewRelic represents a single struct to notify New Relic
13+
// about a new deploy
1114
type NewRelic struct {
12-
Host string
13-
ApplicationId string
14-
ApiKey string
15-
Revision string
15+
Host string
16+
ApplicationId string
17+
ApiKey string
18+
Revision string
1619
}
1720

21+
// CallHook will notify New Relic about a new deploy
22+
// It returns any errors encountered
1823
func (s NewRelic) CallHook(deploys []types.Deploy) error {
1924
httpClient := &http.Client{
2025
Timeout: time.Second * 10,
2126
}
22-
if err := s.createDeploy(httpClient, deploys[0]); err != nil {
23-
return err
24-
}
25-
return nil
27+
return s.createDeploy(httpClient, deploys[0])
2628
}
2729

2830
func (s *NewRelic) createDeploy(httpClient *http.Client, deploy types.Deploy) error {
@@ -36,7 +38,7 @@ func (s *NewRelic) createDeploy(httpClient *http.Client, deploy types.Deploy) er
3638
}
3739
}`)
3840

39-
url := s.Host+"/v2/applications/"+s.ApplicationId+"/deployments.json"
41+
url := s.Host + "/v2/applications/" + s.ApplicationId + "/deployments.json"
4042
req, err := http.NewRequest("POST", url, bytes.NewReader(data))
4143
if err != nil {
4244
return err
@@ -56,7 +58,9 @@ func (s *NewRelic) createDeploy(httpClient *http.Client, deploy types.Deploy) er
5658
return nil
5759
}
5860

59-
61+
// ValidatesFields checks if there are some field on NewRelic struct invalid
62+
// It returns an error if there are some invalid field
63+
// and if there are no, returns nil
6064
func (s NewRelic) ValidatesFields() error {
6165
if s.Host == "" {
6266
return errors.New("Field host into NewRelic hook is required")

hook/newrelic_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package hook
22

33
import (
4+
"testing"
5+
46
"github.com/lucasgomide/snitch/types"
57
"gopkg.in/jarcoal/httpmock.v1"
6-
"testing"
78
)
89

9-
var newrelic_host = "https://api.newrelic.com"
10-
10+
var newrelicHost = "https://api.newrelic.com"
1111

1212
func TestNewRelicDeploySuccessful(t *testing.T) {
1313
httpmock.Activate()
@@ -16,26 +16,24 @@ func TestNewRelicDeploySuccessful(t *testing.T) {
1616
var deploys []types.Deploy
1717
deploys = append(deploys, types.Deploy{"app-sample", "12345678909", "sha1", "user@g.com", "v15"})
1818

19-
s := NewRelic{newrelic_host, "app-id-here", "api-key-here", "revision-here"}
19+
s := NewRelic{newrelicHost, "app-id-here", "api-key-here", "revision-here"}
2020

2121
httpmock.RegisterResponder("POST", s.Host+"/v2/applications/"+s.ApplicationId+"/deployments.json",
2222
httpmock.NewStringResponder(201, `ok`))
2323

24-
2524
if err := s.CallHook(deploys); err != nil {
2625
t.Error(err)
2726
}
2827
}
2928

30-
3129
func TestNewRelicReturnsErrorWhenCreateDeployFails(t *testing.T) {
3230
httpmock.Activate()
3331
defer httpmock.DeactivateAndReset()
3432

3533
var deploys []types.Deploy
3634
deploys = append(deploys, types.Deploy{"app-sample", "12345678909", "sha1", "user@g.com", "v15"})
3735

38-
s := NewRelic{newrelic_host, "app-id-here", "api-key-here", "revision-here"}
36+
s := NewRelic{newrelicHost, "app-id-here", "api-key-here", "revision-here"}
3937

4038
httpmock.RegisterResponder("POST", s.Host+"/v2/applications/"+s.ApplicationId+"/deployments.json",
4139
httpmock.NewStringResponder(502, `error`))
@@ -47,7 +45,6 @@ func TestNewRelicReturnsErrorWhenCreateDeployFails(t *testing.T) {
4745
}
4846
}
4947

50-
5148
func TestNewRelicValidateFields(t *testing.T) {
5249
s := NewRelic{}
5350

hook/rollbar.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ package hook
33
import (
44
"bytes"
55
"errors"
6-
"github.com/lucasgomide/snitch/types"
76
"net/http"
7+
8+
"github.com/lucasgomide/snitch/types"
89
)
910

11+
// Rollbar represents a single struct to notify Rollbar
12+
// about a new deploy
1013
type Rollbar struct {
1114
AccessToken string
1215
Env string
1316
}
1417

18+
// CallHook creates a new deploy on Rollbar
19+
// It returns any errors encountered
1520
func (r Rollbar) CallHook(deploys []types.Deploy) error {
1621
data := []byte(
1722
`{
@@ -34,6 +39,9 @@ func (r Rollbar) CallHook(deploys []types.Deploy) error {
3439
return nil
3540
}
3641

42+
// ValidatesFields checks if there are some field on Rollbar struct invalid
43+
// It returns an error if there are some invalid field
44+
// and if there are no, returns nil
3745
func (r Rollbar) ValidatesFields() error {
3846
if r.AccessToken == "" {
3947
return errors.New("Field access_token into Rollbar hook is required")

hook/rollbar_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
package hook
22

33
import (
4-
"github.com/lucasgomide/snitch/types"
5-
"gopkg.in/jarcoal/httpmock.v1"
64
"strings"
75
"testing"
6+
7+
"github.com/lucasgomide/snitch/types"
8+
"gopkg.in/jarcoal/httpmock.v1"
89
)
910

1011
func TestCreateDeploySuccessfulOnRollbar(t *testing.T) {
1112
r := Rollbar{"abc", "development"}
1213

1314
var (
1415
deploys []types.Deploy
15-
urlRollbarApi = "https://api.rollbar.com/api/1/deploy/"
16+
urlRollbarAPI = "https://api.rollbar.com/api/1/deploy/"
1617
)
1718

1819
deploys = append(deploys, types.Deploy{"", "", "sha1", "user@g.com", ""})
1920

2021
httpmock.Activate()
2122
defer httpmock.DeactivateAndReset()
2223

23-
httpmock.RegisterResponder("POST", urlRollbarApi,
24+
httpmock.RegisterResponder("POST", urlRollbarAPI,
2425
httpmock.NewStringResponder(200, `ok`))
2526

2627
if err := r.CallHook(deploys); err != nil {
@@ -49,14 +50,14 @@ func TestReturnsErrorWhenRequestToCreateDeployIsnt200OnRollbar(t *testing.T) {
4950
r := Rollbar{"abc123", "development"}
5051
var (
5152
deploys []types.Deploy
52-
urlRollbarApi = "https://api.rollbar.com/api/1/deploy/"
53+
urlRollbarAPI = "https://api.rollbar.com/api/1/deploy/"
5354
)
5455
deploys = append(deploys, types.Deploy{"", "", "sha1", "user@g.com", ""})
5556

5657
httpmock.Activate()
5758
defer httpmock.DeactivateAndReset()
5859

59-
httpmock.RegisterResponder("POST", urlRollbarApi,
60+
httpmock.RegisterResponder("POST", urlRollbarAPI,
6061
httpmock.NewStringResponder(501, `error`))
6162
expected := "Rollbar - response status code isn't 200"
6263
if err := r.CallHook(deploys); err == nil {

hook/sentry.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package hook
33
import (
44
"bytes"
55
"errors"
6-
"github.com/lucasgomide/snitch/types"
76
"net/http"
87
"time"
8+
9+
"github.com/lucasgomide/snitch/types"
910
)
1011

12+
// Sentry represents a single struct to notify Sentry
13+
// about a new deploy
1114
type Sentry struct {
1215
Host string
1316
OrganizationSlug string
@@ -17,17 +20,16 @@ type Sentry struct {
1720
ReleaseVersion string
1821
}
1922

23+
// CallHook creates a new release and deploy on Sentry
24+
// It returns any errors encountered
2025
func (s Sentry) CallHook(deploys []types.Deploy) error {
2126
httpClient := &http.Client{
2227
Timeout: time.Second * 10,
2328
}
2429
if err := s.createRelease(httpClient, deploys[0]); err != nil {
2530
return err
2631
}
27-
if err := s.createDeploy(httpClient); err != nil {
28-
return err
29-
}
30-
return nil
32+
return s.createDeploy(httpClient)
3133
}
3234

3335
func (s *Sentry) createRelease(httpClient *http.Client, deploy types.Deploy) error {
@@ -76,6 +78,9 @@ func (s Sentry) createDeploy(httpClient *http.Client) error {
7678
return nil
7779
}
7880

81+
// ValidatesFields checks if there are some field on Sentry struct invalid
82+
// It returns an error if there are some invalid field
83+
// and if there are no, returns nil
7984
func (s Sentry) ValidatesFields() error {
8085
if s.Host == "" {
8186
return errors.New("Field host into Sentry hook is required")

0 commit comments

Comments
 (0)