github.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package status
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "github.com/mmcdole/gofeed"
  7. "github.com/porter-dev/porter/api/server/handlers"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/apierrors"
  10. "github.com/porter-dev/porter/api/server/shared/config"
  11. )
  12. type GetGithubStatusHandler struct {
  13. handlers.PorterHandlerWriter
  14. }
  15. func NewGetGithubStatusHandler(
  16. config *config.Config,
  17. writer shared.ResultWriter,
  18. ) *GetGithubStatusHandler {
  19. return &GetGithubStatusHandler{
  20. PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  21. }
  22. }
  23. func (c *GetGithubStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  24. fp := gofeed.NewParser()
  25. feed, err := fp.ParseURL("https://www.githubstatus.com/history.rss")
  26. if err != nil {
  27. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error fetching github status RSS: %w", err)))
  28. return
  29. }
  30. if len(feed.Items) > 0 {
  31. description := feed.Items[0].Description
  32. link := feed.Items[0].Link
  33. if !strings.Contains(description, "This incident has been resolved") {
  34. // ongoing incident
  35. c.WriteResult(w, r, link)
  36. return
  37. }
  38. }
  39. c.WriteResult(w, r, "no active incidents")
  40. }