You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

github-releases.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package httplistener
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/spf13/viper"
  7. "net/http"
  8. )
  9. type githubRepository struct {
  10. Name string
  11. Description string
  12. }
  13. type githubRelease struct {
  14. Html_url string
  15. Tag_name string
  16. Name string
  17. }
  18. type githubPayload struct {
  19. Action string
  20. Release githubRelease
  21. Repository githubRepository
  22. }
  23. func (hl *HTTPListener) githubReleasesHandler(w http.ResponseWriter, request *http.Request) {
  24. if request.Method != "POST" {
  25. http.NotFound(w, request)
  26. return
  27. }
  28. var payload githubPayload
  29. buf := new(bytes.Buffer)
  30. buf.ReadFrom(request.Body)
  31. json.Unmarshal(buf.Bytes(), &payload)
  32. if payload.Action == "published" {
  33. name := payload.Repository.Description
  34. if name == "" {
  35. name = payload.Repository.Name
  36. }
  37. release := payload.Release.Name
  38. if release == "" {
  39. release = payload.Release.Tag_name
  40. }
  41. msg := fmt.Sprintf("[\x02Release\x0f] \x02%s\x0f version \x02%s\x0f has been published: %s", name, release, payload.Release.Html_url)
  42. channelKey := fmt.Sprintf("http.listeners.github-releases.%s", payload.Repository.Name)
  43. channel := viper.GetString(channelKey)
  44. log.Infof("%s [%s] GitHub Release", request.RemoteAddr, channel)
  45. hl.irc.Privmsgf(channel, msg)
  46. }
  47. }