Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
Use github token if it is available for api requests (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zack Chase authored Jan 13, 2023
1 parent a14c8ac commit 47ef794
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
7 changes: 3 additions & 4 deletions cmd/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,8 @@ func getTagFromKeywords(keywords []string, tag string) *string {
}

func getGitHubTags(repoSlug string) ([]pkg.GitHubTag, error) {
tagsUrl := fmt.Sprintf("https://api.github.com/repos/%s/tags", repoSlug)

var tags []pkg.GitHubTag
tagsResp, err := http.Get(tagsUrl)
path := fmt.Sprintf("/repos/%s/tags", repoSlug)
tagsResp, err := pkg.GetGitHubAPI(path)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("getting tags info for %s", repoSlug))
}
Expand All @@ -418,6 +416,7 @@ func getGitHubTags(repoSlug string) ([]pkg.GitHubTag, error) {
return nil, fmt.Errorf("getting tags info for %s: %s", repoSlug, string(respBody))
}

var tags []pkg.GitHubTag
err = json.NewDecoder(tagsResp.Body).Decode(&tags)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("constructing tags information for %s", repoSlug))
Expand Down
14 changes: 8 additions & 6 deletions cmd/pkgversion/pkgversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pkgversion
import (
"encoding/json"
"fmt"

"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/pulumi/registrygen/pkg"
Expand Down Expand Up @@ -33,8 +34,7 @@ func CheckVersion() *cobra.Command {
repoName = githubSlugParts[1]
}

latest := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", repoSlug)
version, err := getLatestVersion(latest)
version, err := getLatestVersion(repoSlug)
if err != nil {
return err
}
Expand All @@ -61,15 +61,17 @@ func CheckVersion() *cobra.Command {
return cmd
}

func getLatestVersion(url string) (string, error) {
resp, err := http.Get(url)
func getLatestVersion(repoSlug string) (string, error) {
path := fmt.Sprintf("/repos/%s/releases/latest", repoSlug)
resp, err := pkg.GetGitHubAPI(path)

if err != nil {
return "", errors.Wrap(err, fmt.Sprintf("getting latest version from %s", url))
return "", errors.Wrap(err, fmt.Sprintf("getting latest version from https://api.github.com%s", path))
}

defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", errors.Wrap(err, fmt.Sprintf("Could not find a release at %s", url))
return "", errors.Wrap(err, fmt.Sprintf("Could not find a release at https://api.github.com%s", path))
}

var tag pkg.GitHubTag
Expand Down
26 changes: 26 additions & 0 deletions pkg/githubInfo.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
package pkg

import (
"fmt"
"net/http"
"os"
"time"

"github.com/pkg/errors"
)

func GetGitHubAPI(path string) (*http.Response, error) {
token := os.Getenv("GITHUB_TOKEN")
url := fmt.Sprintf("https://api.github.com%s", path)

client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, errors.Wrap(err, "creating request")
}

req.Header = http.Header{
"Content-Type": {"application/json"},
}

if token != "" {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
}

return client.Do(req)
}

type GitHubTag struct {
Name string `json:"name"`
ZipballURL string `json:"zipball_url"`
Expand Down

0 comments on commit 47ef794

Please sign in to comment.