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