mirror of
https://github.com/thegeeklab/github-releases-notifier.git
synced 2024-11-13 01:30:40 +00:00
63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type SlackSender struct {
|
|
Hook string
|
|
}
|
|
|
|
type slackPayload struct {
|
|
Username string `json:"username"`
|
|
IconEmoji string `json:"icon_emoji"`
|
|
Text string `json:"text"`
|
|
}
|
|
|
|
func (s *SlackSender) Send(repository Repository) error {
|
|
payload := slackPayload{
|
|
Username: "GitHub Releases",
|
|
IconEmoji: ":github:",
|
|
Text: fmt.Sprintf(
|
|
"<%s|%s/%s>: <%s|%s> released",
|
|
repository.URL.String(),
|
|
repository.Owner,
|
|
repository.Name,
|
|
repository.Release.URL.String(),
|
|
repository.Release.Name,
|
|
),
|
|
}
|
|
|
|
payloadData, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req, err := http.NewRequest(http.MethodPost, s.Hook, bytes.NewReader(payloadData))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
|
|
defer cancel()
|
|
req.WithContext(ctx)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
return fmt.Errorf("request didn't respond with 200 OK: %s, %s", resp.Status, body)
|
|
}
|
|
|
|
return nil
|
|
}
|