Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix checking token #52

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions gh/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type AssetOption struct {

func GetReleaseAsset(ctx context.Context, owner, repo string, opt *AssetOption) (*github.ReleaseAsset, fs.FS, error) {
const versionLatest = "latest"
c, err := client(ctx)
c, err := client(ctx, owner, repo)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func makeFS(owner, repo string, a *github.ReleaseAsset) (fs.FS, error) {
}

func downloadAsset(owner, repo string, a *github.ReleaseAsset) ([]byte, error) {
client, err := httpClient()
client, err := httpClient(owner, repo)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -266,7 +266,7 @@ func contains(s []string, e string) bool {
return false
}

func client(ctx context.Context) (*github.Client, error) {
func client(ctx context.Context, owner, repo string) (*github.Client, error) {
token, _, _, _ := factory.GetTokenAndEndpoints()
if token == "" {
log.Println("No credentials found, access without credentials")
Expand All @@ -277,14 +277,14 @@ func client(ctx context.Context) (*github.Client, error) {
if err != nil {
return nil, err
}
if _, _, err := c.Users.Get(ctx, ""); err != nil {
if _, _, err := c.Repositories.Get(ctx, owner, repo); err != nil {
log.Println("Authentication failed, access without credentials")
return factory.NewGithubClient(factory.SkipAuth(true))
}
return c, nil
}

func httpClient() (*http.Client, error) {
func httpClient(owner, repo string) (*http.Client, error) {
token, v3ep, _, _ := factory.GetTokenAndEndpoints()
if token == "" {
log.Println("No credentials found, access without credentials")
Expand All @@ -298,7 +298,7 @@ func httpClient() (*http.Client, error) {
if err != nil {
return nil, err
}
u := fmt.Sprintf("%s/user", v3ep)
u := fmt.Sprintf("%s/repos/%s/%s", v3ep, owner, repo)
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, err
Expand Down