Skip to content

Commit

Permalink
token version
Browse files Browse the repository at this point in the history
  • Loading branch information
Listener430 committed Jan 5, 2025
1 parent 52a3442 commit 0c63e6d
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion pkg/utils/github_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@ package utils

import (
"context"
"os"
"time"

"github.com/google/go-github/v59/github"
"golang.org/x/oauth2"
)

// newGitHubClient creates a new GitHub client. If a token is provided, it returns an authenticated client;
// otherwise, it returns an unauthenticated client.
func newGitHubClient(ctx context.Context) *github.Client {
githubToken := os.Getenv("GITHUB_TOKEN")
if githubToken == "" {
return github.NewClient(nil)
}

// Token found, create an authenticated client
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)
tc := oauth2.NewClient(ctx, ts)

return github.NewClient(tc)
}

// GetLatestGitHubRepoRelease returns the latest release tag for a GitHub repository
func GetLatestGitHubRepoRelease(owner string, repo string) (string, error) {
opt := &github.ListOptions{Page: 1, PerPage: 1}
client := github.NewClient(nil)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()

client := newGitHubClient(ctx)

releases, _, err := client.Repositories.ListReleases(ctx, owner, repo, opt)
if err != nil {
return "", err
Expand Down

0 comments on commit 0c63e6d

Please sign in to comment.