• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

odwrtw / polochon / 14283825881

05 Apr 2025 04:39PM UTC coverage: 30.292% (-36.9%) from 67.207%
14283825881

push

github

gregdel
Fix log warning

Signed-off-by: Lucas BEE <pouulet@gmail.com>

0 of 1 new or added line in 1 file covered. (0.0%)

2693 of 8890 relevant lines covered (30.29%)

0.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/modules/webhook/webhook.go
1
package webhook
2

3
import (
4
        "bytes"
5
        "context"
6
        "encoding/json"
7
        "errors"
8
        "fmt"
9
        "html/template"
10
        "net/http"
11
        "time"
12

13
        "gopkg.in/yaml.v2"
14

15
        polochon "github.com/odwrtw/polochon/lib"
16
        "github.com/sirupsen/logrus"
17
)
18

19
// Make sure that the module is a notifier
20
var _ polochon.Notifier = (*WebHook)(nil)
21

22
func init() {
×
23
        polochon.RegisterModule(&WebHook{})
×
24
}
×
25

26
// WebHook errors
27
var (
28
        ErrInvalidArgument = errors.New("webhook: invalid argument type")
29
)
30

31
// Module constants
32
const (
33
        moduleName = "webhook"
34
)
35

36
// Params are the params for webhooks
37
type Params struct {
38
        Hooks []*Hook `yaml:"hooks"`
39
}
40

41
// Hook represents a Hook
42
type Hook struct {
43
        URLTemplate *template.Template `yaml:"-"`
44
        URL         string             `yaml:"url"`
45
}
46

47
// WebHook stores the webhook configs
48
type WebHook struct {
49
        httpClient *http.Client
50
        hooks      []*Hook
51
        configured bool
52
}
53

54
// Init implements the module interface
55
func (w *WebHook) Init(p []byte) error {
×
56
        if w.configured {
×
57
                return nil
×
58
        }
×
59

60
        params := &Params{}
×
61
        if err := yaml.Unmarshal(p, &params); err != nil {
×
62
                return err
×
63
        }
×
64

65
        return w.InitWithParams(params)
×
66
}
67

68
// InitWithParams configures the module
69
func (w *WebHook) InitWithParams(params *Params) error {
×
70
        for _, h := range params.Hooks {
×
71
                url, err := template.New("url").Parse(h.URL)
×
72
                if err != nil {
×
73
                        return err
×
74
                }
×
75
                h.URLTemplate = url
×
76
        }
77

78
        w.hooks = params.Hooks
×
79
        w.httpClient = http.DefaultClient
×
80
        w.configured = true
×
81

×
82
        return nil
×
83
}
84

85
// Name implements the Module interface
86
func (w *WebHook) Name() string {
×
87
        return moduleName
×
88
}
×
89

90
// Status implements the Module interface
91
func (w *WebHook) Status() (polochon.ModuleStatus, error) {
×
92
        return polochon.StatusNotImplemented, nil
×
93
}
×
94

95
// Notify sends a notification to the recipient
96
func (w *WebHook) Notify(i interface{}, log *logrus.Entry) error {
×
97
        var videoType string
×
98
        var video polochon.Video
×
99

×
100
        switch v := i.(type) {
×
101
        case *polochon.ShowEpisode:
×
102
                videoType = "episode"
×
103
                video = v
×
104
        case *polochon.Movie:
×
105
                videoType = "movie"
×
106
                video = v
×
107
        default:
×
108
                return ErrInvalidArgument
×
109
        }
110

111
        for _, h := range w.hooks {
×
112
                err := w.notify(h, video, videoType)
×
113
                if err != nil {
×
NEW
114
                        log.Warn(err.Error())
×
115
                }
×
116
        }
117

118
        return nil
×
119
}
120

121
func (w *WebHook) notify(hook *Hook, video polochon.Video, videoType string) error {
×
122
        var URL bytes.Buffer
×
123
        err := hook.URLTemplate.Execute(&URL, video)
×
124
        if err != nil {
×
125
                return err
×
126
        }
×
127

128
        b := new(bytes.Buffer)
×
129
        json.NewEncoder(b).Encode(struct {
×
130
                Type string      `json:"type"`
×
131
                Data interface{} `json:"data"`
×
132
        }{
×
133
                Type: videoType,
×
134
                Data: video,
×
135
        })
×
136

×
137
        req, err := http.NewRequest("POST", URL.String(), b)
×
138
        if err != nil {
×
139
                return err
×
140
        }
×
141

142
        req.Header.Set("Content-Type", "application/json")
×
143

×
144
        // Add a context with a timeout to the request
×
145
        ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
×
146
        defer cancel()
×
147

×
148
        // Send request
×
149
        resp, err := w.httpClient.Do(req.WithContext(ctx))
×
150
        if err != nil {
×
151
                return err
×
152
        }
×
153
        defer resp.Body.Close()
×
154

×
155
        // If status > 400, something's wrong
×
156
        if resp.StatusCode >= http.StatusBadRequest {
×
157
                return fmt.Errorf("%s call failed with error %d", URL.String(), resp.StatusCode)
×
158
        }
×
159

160
        return nil
×
161
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc