Skip to content

Commit

Permalink
Adding documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasgomide committed Nov 22, 2017
1 parent 2527320 commit b45058c
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 65 deletions.
9 changes: 8 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package config

import (
yaml "gopkg.in/yaml.v2"
"io/ioutil"

yaml "gopkg.in/yaml.v2"
)

type configuration struct {
Expand All @@ -11,6 +12,10 @@ type configuration struct {

var configs configuration

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

// Data reads data's struct field
// It returns the configuration file parsead
func Data() map[interface{}]interface{} {
return configs.data
}
17 changes: 10 additions & 7 deletions hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package hook

import (
"bytes"
"reflect"
"strings"

"github.com/lucasgomide/snitch/config"
"github.com/lucasgomide/snitch/types"
"github.com/lucasgomide/snitch/utils"
"reflect"
"strings"
)

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

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

func callHook(h types.Hook, deploy []types.Deploy) error {
if err := h.ValidatesFields(); err == nil {
err := h.ValidatesFields()
if err == nil {
return h.CallHook(deploy)
} else {
return err
}
return err
}

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

for k, v := range conf.(map[interface{}]interface{}) {
Expand Down
15 changes: 8 additions & 7 deletions hook/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ package hook
import (
"bytes"
"errors"
"github.com/lucasgomide/snitch/config"
"github.com/lucasgomide/snitch/types"
"gopkg.in/jarcoal/httpmock.v1"
"log"
"os"
"strings"
"testing"

"github.com/lucasgomide/snitch/config"
"github.com/lucasgomide/snitch/types"
"gopkg.in/jarcoal/httpmock.v1"
)

var (
h HookFake
tsuruFake TsuruFake
err error
configFilePath = "../testdata/config.yaml"
d = []types.Deploy{types.Deploy{"app", "1234125125", "sha1", "user@42.com", "v15"}}
d = []types.Deploy{{"app", "1234125125", "sha1", "user@42.com", "v15"}}
conf = map[interface{}]interface{}{"field_sample": "key_value"}
)

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

func TestHookExecutedSuccessfully(t *testing.T) {
if err = ExecuteHook(&h, d, conf); err != nil {
if err = executeHook(&h, d, conf); err != nil {
t.Error(err)
}

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

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

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

Expand Down
26 changes: 15 additions & 11 deletions hook/newrelic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@ package hook
import (
"bytes"
"errors"
"github.com/lucasgomide/snitch/types"
"net/http"
"time"

"github.com/lucasgomide/snitch/types"
)

// NewRelic represents a single struct to notify New Relic
// about a new deploy
type NewRelic struct {
Host string
ApplicationId string
ApiKey string
Revision string
Host string
ApplicationId string
ApiKey string
Revision string
}

// CallHook will notify New Relic about a new deploy
// It returns any errors encountered
func (s NewRelic) CallHook(deploys []types.Deploy) error {
httpClient := &http.Client{
Timeout: time.Second * 10,
}
if err := s.createDeploy(httpClient, deploys[0]); err != nil {
return err
}
return nil
return s.createDeploy(httpClient, deploys[0])
}

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

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


// ValidatesFields checks if there are some field on NewRelic struct invalid
// It returns an error if there are some invalid field
// and if there are no, returns nil
func (s NewRelic) ValidatesFields() error {
if s.Host == "" {
return errors.New("Field host into NewRelic hook is required")
Expand Down
13 changes: 5 additions & 8 deletions hook/newrelic_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package hook

import (
"testing"

"github.com/lucasgomide/snitch/types"
"gopkg.in/jarcoal/httpmock.v1"
"testing"
)

var newrelic_host = "https://api.newrelic.com"

var newrelicHost = "https://api.newrelic.com"

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

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

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


if err := s.CallHook(deploys); err != nil {
t.Error(err)
}
}


func TestNewRelicReturnsErrorWhenCreateDeployFails(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

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

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

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


func TestNewRelicValidateFields(t *testing.T) {
s := NewRelic{}

Expand Down
10 changes: 9 additions & 1 deletion hook/rollbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ package hook
import (
"bytes"
"errors"
"github.com/lucasgomide/snitch/types"
"net/http"

"github.com/lucasgomide/snitch/types"
)

// Rollbar represents a single struct to notify Rollbar
// about a new deploy
type Rollbar struct {
AccessToken string
Env string
}

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

// ValidatesFields checks if there are some field on Rollbar struct invalid
// It returns an error if there are some invalid field
// and if there are no, returns nil
func (r Rollbar) ValidatesFields() error {
if r.AccessToken == "" {
return errors.New("Field access_token into Rollbar hook is required")
Expand Down
13 changes: 7 additions & 6 deletions hook/rollbar_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package hook

import (
"github.com/lucasgomide/snitch/types"
"gopkg.in/jarcoal/httpmock.v1"
"strings"
"testing"

"github.com/lucasgomide/snitch/types"
"gopkg.in/jarcoal/httpmock.v1"
)

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

var (
deploys []types.Deploy
urlRollbarApi = "https://api.rollbar.com/api/1/deploy/"
urlRollbarAPI = "https://api.rollbar.com/api/1/deploy/"
)

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

httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("POST", urlRollbarApi,
httpmock.RegisterResponder("POST", urlRollbarAPI,
httpmock.NewStringResponder(200, `ok`))

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

httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("POST", urlRollbarApi,
httpmock.RegisterResponder("POST", urlRollbarAPI,
httpmock.NewStringResponder(501, `error`))
expected := "Rollbar - response status code isn't 200"
if err := r.CallHook(deploys); err == nil {
Expand Down
15 changes: 10 additions & 5 deletions hook/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package hook
import (
"bytes"
"errors"
"github.com/lucasgomide/snitch/types"
"net/http"
"time"

"github.com/lucasgomide/snitch/types"
)

// Sentry represents a single struct to notify Sentry
// about a new deploy
type Sentry struct {
Host string
OrganizationSlug string
Expand All @@ -17,17 +20,16 @@ type Sentry struct {
ReleaseVersion string
}

// CallHook creates a new release and deploy on Sentry
// It returns any errors encountered
func (s Sentry) CallHook(deploys []types.Deploy) error {
httpClient := &http.Client{
Timeout: time.Second * 10,
}
if err := s.createRelease(httpClient, deploys[0]); err != nil {
return err
}
if err := s.createDeploy(httpClient); err != nil {
return err
}
return nil
return s.createDeploy(httpClient)
}

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

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

0 comments on commit b45058c

Please sign in to comment.