forked from mcuadros/ofelia
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
3 changed files
with
161 additions
and
3 deletions.
There are no files selected for viewing
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
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,79 @@ | ||
package middlewares | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/mcuadros/ofelia/core" | ||
) | ||
|
||
type GotifyConfig struct { | ||
GotifyWebhook string `gcfg:"gotify-webhook" mapstructure:"gotify-webhook"` | ||
GotifyOnlyOnError bool `gcfg:"gotify-only-on-error" mapstructure:"gotify-only-on-error"` | ||
GotifyPriority int64 `gcfg:"gotify-priority" mapstructure:"gotify-priority"` | ||
} | ||
|
||
// NewGotify returns a Gotify middleware if the given configuration is not empty | ||
func NewGotify(c *GotifyConfig) core.Middleware { | ||
var m core.Middleware | ||
if !IsEmpty(c) { | ||
m = &Gotify{*c} | ||
} | ||
|
||
return m | ||
} | ||
|
||
type Gotify struct { | ||
GotifyConfig | ||
} | ||
|
||
// ContinueOnStop return allways true, we want always report the final status | ||
func (m *Gotify) ContinueOnStop() bool { | ||
return true | ||
} | ||
|
||
func (m *Gotify) Run(ctx *core.Context) error { | ||
err := ctx.Next() | ||
ctx.Stop(err) | ||
|
||
if ctx.Execution.Failed || !m.GotifyOnlyOnError { | ||
m.pushMessage(ctx) | ||
} | ||
|
||
return err | ||
} | ||
|
||
func (m *Gotify) pushMessage(ctx *core.Context) { | ||
content, _ := json.Marshal(m.buildMessage(ctx)) | ||
|
||
r, err := http.Post(m.GotifyWebhook, "application/json", bytes.NewReader(content)) | ||
if err != nil { | ||
ctx.Logger.Errorf("Gotify error calling %q error: %q", m.GotifyWebhook, err) | ||
} else if r.StatusCode != 200 { | ||
ctx.Logger.Errorf("Gotify error non-200 status code calling %q", m.GotifyWebhook) | ||
} | ||
} | ||
|
||
func (m *Gotify) buildMessage(ctx *core.Context) *gotifyMessage { | ||
msg := &gotifyMessage{Title: ctx.Job.GetName(), Priority: m.GotifyPriority} | ||
|
||
msg.Message = fmt.Sprintf( | ||
"Job *%q* finished in *%s*, command `%s`", | ||
ctx.Job.GetName(), ctx.Execution.Duration, ctx.Job.GetCommand(), | ||
) | ||
|
||
if ctx.Execution.Failed { | ||
msg.Message = "FAILED: " + msg.Message | ||
} else if ctx.Execution.Skipped { | ||
msg.Message = "Skipped: " + msg.Message | ||
} | ||
return msg | ||
} | ||
|
||
type gotifyMessage struct { | ||
Title string `json:"title"` | ||
Message string `json:"message"` | ||
Priority int64 `json:"priority"` | ||
} |
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,69 @@ | ||
package middlewares | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
|
||
. "gopkg.in/check.v1" | ||
) | ||
|
||
type SuiteGotify struct { | ||
BaseSuite | ||
} | ||
|
||
var _ = Suite(&SuiteGotify{}) | ||
|
||
func (s *SuiteGotify) TestNewGotifyEmpty(c *C) { | ||
c.Assert(NewGotify(&GotifyConfig{}), IsNil) | ||
} | ||
|
||
func (s *SuiteGotify) TestRunSuccess(c *C) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
var m gotifyMessage | ||
err := json.NewDecoder(r.Body).Decode(&m) | ||
c.Assert(err, Equals, nil) | ||
c.Assert(!strings.Contains(m.Message, "FAILED"), Equals, true) | ||
})) | ||
|
||
defer ts.Close() | ||
|
||
s.ctx.Start() | ||
s.ctx.Stop(nil) | ||
|
||
m := NewGotify(&GotifyConfig{GotifyWebhook: ts.URL}) | ||
c.Assert(m.Run(s.ctx), IsNil) | ||
} | ||
|
||
func (s *SuiteGotify) TestRunSuccessFailed(c *C) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
var m gotifyMessage | ||
err := json.NewDecoder(r.Body).Decode(&m) | ||
c.Assert(err, Equals, nil) | ||
c.Assert(strings.Contains(m.Message, "FAILED"), Equals, true) | ||
})) | ||
|
||
defer ts.Close() | ||
|
||
s.ctx.Start() | ||
s.ctx.Stop(errors.New("foo")) | ||
|
||
m := NewGotify(&GotifyConfig{GotifyWebhook: ts.URL}) | ||
c.Assert(m.Run(s.ctx), IsNil) | ||
} | ||
|
||
func (s *SuiteGotify) TestRunSuccessOnError(c *C) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
c.Assert(true, Equals, false) | ||
})) | ||
|
||
defer ts.Close() | ||
|
||
s.ctx.Start() | ||
s.ctx.Stop(nil) | ||
|
||
m := NewGotify(&GotifyConfig{GotifyWebhook: ts.URL, GotifyOnlyOnError: true}) | ||
c.Assert(m.Run(s.ctx), IsNil) | ||
} |