Przeglądaj źródła

Merge pull request #2701 from porter-dev/nafees/fix-install-script-gh-ratelimit

[hotfix] Use github PAT for install script
Mohammed Nafees 3 lat temu
rodzic
commit
fbe49f0403
1 zmienionych plików z 26 dodań i 4 usunięć
  1. 26 4
      services/cli_install_script_container/main.go

+ 26 - 4
services/cli_install_script_container/main.go

@@ -8,6 +8,11 @@ import (
 	"text/template"
 
 	"github.com/google/go-github/v50/github"
+	"golang.org/x/oauth2"
+)
+
+var (
+	ghClient *github.Client
 )
 
 type Tag struct {
@@ -15,9 +20,7 @@ type Tag struct {
 }
 
 func getLatestCLIRelease() (string, error) {
-	client := github.NewClient(nil)
-
-	rel, _, err := client.Repositories.GetLatestRelease(context.Background(), "porter-dev", "porter")
+	rel, _, err := ghClient.Repositories.GetLatestRelease(context.Background(), "porter-dev", "porter")
 
 	if err != nil {
 		return "", err
@@ -30,6 +33,7 @@ func serve(w http.ResponseWriter, req *http.Request) {
 	latestTag, err := getLatestCLIRelease()
 
 	if err != nil {
+		fmt.Fprintf(os.Stderr, "error getting latest release: %v\n", err)
 		w.WriteHeader(http.StatusInternalServerError)
 		return
 	}
@@ -37,6 +41,7 @@ func serve(w http.ResponseWriter, req *http.Request) {
 	contents, err := os.ReadFile("install.sh")
 
 	if err != nil {
+		fmt.Fprintf(os.Stderr, "error reading install.sh file: %v\n", err)
 		w.WriteHeader(http.StatusInternalServerError)
 		return
 	}
@@ -44,6 +49,7 @@ func serve(w http.ResponseWriter, req *http.Request) {
 	tmpl, err := template.New("install").Parse(string(contents))
 
 	if err != nil {
+		fmt.Fprintf(os.Stderr, "error parsing install.sh template: %v\n", err)
 		w.WriteHeader(http.StatusInternalServerError)
 		return
 	}
@@ -51,6 +57,7 @@ func serve(w http.ResponseWriter, req *http.Request) {
 	err = tmpl.Execute(w, Tag{TagName: latestTag})
 
 	if err != nil {
+		fmt.Fprintf(os.Stderr, "error executing install.sh template: %v\n", err)
 		w.WriteHeader(http.StatusInternalServerError)
 	} else {
 		w.Header().Add("Content-Type", "text/plain")
@@ -58,11 +65,26 @@ func serve(w http.ResponseWriter, req *http.Request) {
 }
 
 func main() {
+	ghAccessToken := os.Getenv("GH_ACCESS_TOKEN")
+
+	if ghAccessToken == "" {
+		fmt.Fprintf(os.Stderr, "GH_ACCESS_TOKEN not set\n")
+		return
+	}
+
+	ghClient = github.NewClient(oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
+		&oauth2.Token{AccessToken: ghAccessToken},
+	)))
+
 	var port string
+
 	if port = os.Getenv("PORT"); port == "" {
 		port = "80"
 	}
 
 	http.HandleFunc("/", serve)
-	http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
+
+	if err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil); err != nil {
+		fmt.Fprintf(os.Stderr, "error starting server: %v\n", err)
+	}
 }