github.com/supabase/cli@v1.168.1/internal/utils/release.go (about)

     1  package utils
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"os"
     7  	"sync"
     8  
     9  	"github.com/go-errors/errors"
    10  	"github.com/google/go-github/v62/github"
    11  	"golang.org/x/oauth2"
    12  )
    13  
    14  var (
    15  	githubClient *github.Client
    16  	githubOnce   sync.Once
    17  )
    18  
    19  func GetGtihubClient(ctx context.Context) *github.Client {
    20  	githubOnce.Do(func() {
    21  		var client *http.Client
    22  		token := os.Getenv("GITHUB_TOKEN")
    23  		if len(token) > 0 {
    24  			ts := oauth2.StaticTokenSource(
    25  				&oauth2.Token{AccessToken: token},
    26  			)
    27  			client = oauth2.NewClient(ctx, ts)
    28  		}
    29  		githubClient = github.NewClient(client)
    30  	})
    31  	return githubClient
    32  }
    33  
    34  const (
    35  	CLI_OWNER = "supabase"
    36  	CLI_REPO  = "cli"
    37  )
    38  
    39  func GetLatestRelease(ctx context.Context) (string, error) {
    40  	client := GetGtihubClient(ctx)
    41  	release, _, err := client.Repositories.GetLatestRelease(ctx, CLI_OWNER, CLI_REPO)
    42  	if err != nil {
    43  		return "", errors.Errorf("Failed to fetch latest release: %w", err)
    44  	}
    45  	if release.TagName == nil {
    46  		return "", nil
    47  	}
    48  	return *release.TagName, nil
    49  }