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

chore: add get release tag commit sha #612

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,39 @@ func GetLatestReleaseTagsSorted(org string, repo string) ([]string, error) {
return versions, nil
}

// Gets release tag from github for a given org name, repo name(in that org) and tag
func GetLatestReleaseTagCommitSHA(org string, repo string) (string, error) {
url := fmt.Sprintf("https://github.com/%s/%s/tags", org, repo)

resp, err := http.Get(url)
if err != nil {
return "", errors.New("cannot get list of tags from Github")
}
defer safeClose(resp.Body)

if (resp.StatusCode == 404) {
return "", errors.New("repository is not found")
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
re := regexp.MustCompile("/commit/(.*?)\"")
releases := re.FindAllString(string(body), -1)
if len(releases) == 0 {
return "", errors.New("no commit found in this repository")
}
var commits []string
for _, rel := range releases {
latest := strings.ReplaceAll(rel, "/commit/", "")
latest = strings.ReplaceAll(latest, "\"", "")
commits = append(commits, latest)
}

return commits[0], nil
}

// SafeClose is a helper function help to close the io
func safeClose(co io.Closer) {
if cerr := co.Close(); cerr != nil {
Expand Down
46 changes: 46 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,49 @@ func TestTransformMapKeys(t *testing.T) {
})
}
}

func TestGetLatestReleaseTagCommitSHAInvalid(t *testing.T) {
cases := []struct{
description string
org string
repo string
expectedErr string
}{
{
description: "Test cases negative not existed repository",
org: "layer5labs",
repo: "meshery-extensions-package",
expectedErr: "repository is not found",
},
{
description: "Test cases negative not existed repository",
org: "layer5io",
repo: "docs",
expectedErr: "no commit found in this repository",
},
}
for _, tt := range cases {
t.Run(tt.description, func(t *testing.T){
commitSHA, err := GetLatestReleaseTagCommitSHA(tt.org, tt.repo)
if err.Error() != tt.expectedErr {
t.Errorf("expected error %s, but got error %s", err, err.Error())
}

if commitSHA != "" {
t.Errorf("expected commitSHA string empty, but got %s", commitSHA)
}
})
}
}

func TestGetLatestReleaseTagCommitSHA(t *testing.T) {
commitSHA, err := GetLatestReleaseTagCommitSHA("kelseyhightower", "nocode")
if err != nil {
t.Errorf("expected no error, but got error %s", err)
}

expectedSHA := "ed6c73fc16578ec53ea374585df2b965ce9f4a31"
if commitSHA != "ed6c73fc16578ec53ea374585df2b965ce9f4a31" {
t.Errorf("expected commitSHA %s, but got %s", commitSHA, expectedSHA)
}
}
Loading