github.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package status
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  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. "github.com/porter-dev/porter/api/types"
  12. )
  13. type GetGithubStatusHandler struct {
  14. handlers.PorterHandlerWriter
  15. }
  16. func NewGetGithubStatusHandler(
  17. config *config.Config,
  18. writer shared.ResultWriter,
  19. ) *GetGithubStatusHandler {
  20. return &GetGithubStatusHandler{
  21. PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  22. }
  23. }
  24. func (c *GetGithubStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  25. resp, err := http.Get("https://www.githubstatus.com/api/v2/incidents/unresolved.json")
  26. if err != nil {
  27. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error fetching github incidents: %w", err)))
  28. return
  29. }
  30. defer resp.Body.Close()
  31. data, err := io.ReadAll(resp.Body)
  32. if err != nil {
  33. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error reading github incidents: %w", err)))
  34. return
  35. }
  36. var incidents types.GithubUnresolvedIncidents
  37. err = json.Unmarshal(data, &incidents)
  38. if err != nil {
  39. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error unmarshalling json: %w", err)))
  40. return
  41. }
  42. if len(incidents.Incidents) > 0 {
  43. c.WriteResult(w, r, fmt.Sprintf("https://www.githubstatus.com/incidents/%s", incidents.Incidents[0].ID))
  44. return
  45. }
  46. c.WriteResult(w, r, "no active incidents")
  47. }