Przeglądaj źródła

Merge pull request #2220 from porter-dev/nafees/github-status

[POR-573] Use unresolved incidents json endpoint for Github
abelanger5 3 lat temu
rodzic
commit
09d4758455
2 zmienionych plików z 31 dodań i 13 usunięć
  1. 25 13
      api/server/handlers/status/github.go
  2. 6 0
      api/types/status.go

+ 25 - 13
api/server/handlers/status/github.go

@@ -1,15 +1,16 @@
 package status
 
 import (
+	"encoding/json"
 	"fmt"
+	"io"
 	"net/http"
-	"strings"
 
-	"github.com/mmcdole/gofeed"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"
 	"github.com/porter-dev/porter/api/server/shared/apierrors"
 	"github.com/porter-dev/porter/api/server/shared/config"
+	"github.com/porter-dev/porter/api/types"
 )
 
 type GetGithubStatusHandler struct {
@@ -26,23 +27,34 @@ func NewGetGithubStatusHandler(
 }
 
 func (c *GetGithubStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-	fp := gofeed.NewParser()
-	feed, err := fp.ParseURL("https://www.githubstatus.com/history.rss")
+	resp, err := http.Get("https://www.githubstatus.com/api/v2/incidents/unresolved.json")
 
 	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error fetching github status RSS: %w", err)))
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error fetching github incidents: %w", err)))
 		return
 	}
 
-	if len(feed.Items) > 0 {
-		description := feed.Items[0].Description
-		link := feed.Items[0].Link
+	defer resp.Body.Close()
 
-		if !strings.Contains(description, "This incident has been resolved") {
-			// ongoing incident
-			c.WriteResult(w, r, link)
-			return
-		}
+	data, err := io.ReadAll(resp.Body)
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error reading github incidents: %w", err)))
+		return
+	}
+
+	var incidents types.GithubUnresolvedIncidents
+
+	err = json.Unmarshal(data, &incidents)
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error unmarshalling json: %w", err)))
+		return
+	}
+
+	if len(incidents.Incidents) > 0 {
+		c.WriteResult(w, r, fmt.Sprintf("https://www.githubstatus.com/incidents/%s", incidents.Incidents[0].ID))
+		return
 	}
 
 	c.WriteResult(w, r, "no active incidents")

+ 6 - 0
api/types/status.go

@@ -7,3 +7,9 @@ const (
 type StreamStatusRequest struct {
 	Selectors string `schema:"selectors"`
 }
+
+type GithubUnresolvedIncidents struct {
+	Incidents []*struct {
+		ID string `json:"id"`
+	} `json:"incidents"`
+}