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

Commit

Permalink
Check status code of github tags request (#14)
Browse files Browse the repository at this point in the history
* Check status code of github tags request

* Update error handling
  • Loading branch information
Zack Chase authored Jan 13, 2023
1 parent cf6aa1f commit a14c8ac
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
44 changes: 32 additions & 12 deletions cmd/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package metadata
import (
"encoding/json"
"fmt"
"github.com/golang/glog"
"io"
"io/ioutil"
"net/http"
"strings"
"time"

"github.com/golang/glog"

"github.com/ghodss/yaml"
"github.com/pkg/errors"
pschema "github.com/pulumi/pulumi/pkg/v3/codegen/schema"
Expand Down Expand Up @@ -94,18 +96,9 @@ func PackageMetadataCmd() *cobra.Command {
}

// try and get the version release data using the github releases API
tagsUrl := fmt.Sprintf("https://api.github.com/repos/%s/tags", repoSlug)

var tags []pkg.GitHubTag
tagsResp, err := http.Get(tagsUrl)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("getting tags info for %s", repoSlug))
}

defer tagsResp.Body.Close()
err = json.NewDecoder(tagsResp.Body).Decode(&tags)
tags, err := getGitHubTags(repoSlug)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("constructing tags information for %s", repoSlug))
return errors.Wrap(err, "github tags")
}

var commitDetails string
Expand Down Expand Up @@ -405,3 +398,30 @@ func getTagFromKeywords(keywords []string, tag string) *string {
glog.V(2).Infof("The tag %q was not found in the package's keywords", tag)
return nil
}

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)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("getting tags info for %s", repoSlug))
}
defer tagsResp.Body.Close()

if tagsResp.StatusCode != 200 {
respBody, err := io.ReadAll(tagsResp.Body)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("getting tags info for %s: %s", repoSlug, tagsResp.Status))
}

return nil, fmt.Errorf("getting tags info for %s: %s", repoSlug, string(respBody))
}

err = json.NewDecoder(tagsResp.Body).Decode(&tags)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("constructing tags information for %s", repoSlug))
}

return tags, nil
}
4 changes: 3 additions & 1 deletion pkg/githubInfo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pkg

import "time"
import (
"time"
)

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

0 comments on commit a14c8ac

Please sign in to comment.