Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Commit

Permalink
Delete webhook on failed request
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Kibish committed Apr 17, 2018
1 parent 0d81ec2 commit f2e4b85
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type mockFirebase struct {
set func(path string, v interface{}) (err error)
get func(path string) (result json.RawMessage, err error)
filterEqual func(path, field string, value interface{}) (result json.RawMessage, err error)
delete func(path string) (err error)
}

func (m mockFirebase) Set(path string, v interface{}) (err error) {
Expand All @@ -27,6 +28,9 @@ func (m mockFirebase) Get(path string) (result json.RawMessage, err error) {
func (m mockFirebase) FilterEqual(path, field string, value interface{}) (result json.RawMessage, err error) {
return m.filterEqual(path, field, value)
}
func (m mockFirebase) Delete(path string) (err error) {
return m.delete(path)
}

type mockSlack struct {
oAuthAccess func(code string) (r *slack.OAuthAccessResponse, err error)
Expand Down
13 changes: 13 additions & 0 deletions pkg/firebase/firebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type IFirebase interface {
Set(path string, v interface{}) (err error)
Get(path string) (result json.RawMessage, err error)
FilterEqual(path, field string, value interface{}) (result json.RawMessage, err error)
Delete(path string) (err error)
}

// New return new instance of the Firebase
Expand Down Expand Up @@ -65,6 +66,18 @@ func (f *Firebase) Get(path string) (result json.RawMessage, err error) {
return
}

// Delete deletes record from the database
func (f *Firebase) Delete(path string) (err error) {
ref, err := f.realFirebase.Ref(path)
if err != nil {
return
}

err = ref.Remove()

return
}

// FilterEqual filter records with field equal to specific value
func (f *Firebase) FilterEqual(path, field string, value interface{}) (result json.RawMessage, err error) {
ref, err := f.realFirebase.Ref(path)
Expand Down
10 changes: 6 additions & 4 deletions pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,17 @@ func (s *Scheduler) publish() (err error) {
break
}

// send trash to all published workspaces
// send trash to all subscribed workspaces
for _, v := range workspaces {
go func(wh string) {
go func(wd storage.WorkspaceData) {
json := []byte(fmt.Sprintf("{\"text\":%q}", trash))
_, err := http.Post(wh, "application/json", bytes.NewBuffer(json))
_, err := http.Post(wd.WebhookURL, "application/json", bytes.NewBuffer(json))
if err != nil {
log.Printf("Deleting workspace \"%s:%s\" because failed to POST", wd.ChannelID, wd.ID)
s.sg.Workspace.Delete(wd)
return
}
}(v.WebhookURL)
}(v)
}

return
Expand Down
5 changes: 5 additions & 0 deletions pkg/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type mockFirebase struct {
set func(path string, v interface{}) (err error)
get func(path string) (result json.RawMessage, err error)
filterEqual func(path, field string, value interface{}) (result json.RawMessage, err error)
delete func(path string) (err error)
}

func (m mockFirebase) Set(path string, v interface{}) (err error) {
Expand All @@ -22,6 +23,10 @@ func (m mockFirebase) FilterEqual(path, field string, value interface{}) (result
return m.filterEqual(path, field, value)
}

func (m mockFirebase) Delete(path string) (err error) {
return m.delete(path)
}

func TestNew(t *testing.T) {
fm := &mockFirebase{}

Expand Down
8 changes: 8 additions & 0 deletions pkg/storage/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ func (w *Workspace) GetAll() (finResult map[string]WorkspaceData, err error) {

return
}

// Delete deletes workspace data
func (w *Workspace) Delete(wd WorkspaceData) (err error) {
refKey := fmt.Sprintf("%s/%s:%s", workspacePath, wd.ID, wd.ChannelID)
err = w.firebase.Delete(refKey)

return
}
13 changes: 13 additions & 0 deletions pkg/storage/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,16 @@ func TestWorkspaceGetAllErr(t *testing.T) {
t.Error("Should be error, but everything is OK")
}
}

func TestWorkspaceDelete(t *testing.T) {
fm := &mockFirebase{}
fm.delete = func(path string) (err error) {
return nil
}

wp := Workspace{firebase: fm}
err := wp.Delete(WorkspaceData{ChannelID: "123", ID: "321", WebhookURL: "http://a/b/c"})
if err != nil {
t.Errorf("Error was not expected: %v", err)
}
}

0 comments on commit f2e4b85

Please sign in to comment.