From 2f9a323113982e321ef1adcafd31bcab3d984b43 Mon Sep 17 00:00:00 2001 From: jerensl <54782057+jerensl@users.noreply.github.com> Date: Mon, 28 Oct 2024 18:20:16 +0800 Subject: [PATCH 1/3] chore: add get release tag commit sha Signed-off-by: jerensl <54782057+jerensl@users.noreply.github.com> --- utils/utils.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ utils/utils_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/utils/utils.go b/utils/utils.go index c1f41a5d..385836e8 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -235,6 +235,54 @@ func GetLatestReleaseTagsSorted(org string, repo string) ([]string, error) { return versions, nil } +type Commit struct { + SHA string `json:"sha"` + URL string `json:"url"` +} + +type ReleaseTags struct { + Name string `json:"name"` + Commit Commit `json:"commit"` + TarballURL string `json:"tarball_url"` + ZipballURL string `json:"zipball_url"` + NodeID string `json:"node_id"` +} + +// Gets release tag from github for a given org name, repo name(in that org) and tag +func GetReleaseTagCommitSHA(org string, repo string, tag string) (string, error) { + url := fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", org, repo) + + if len(tag) == 0 { + return "", errors.New("empty release tags") + } + + resp, err := http.Get(url) + if err != nil { + return "", errors.New("cannot get list of tags from Github API") + } + defer safeClose(resp.Body) + + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", errors.New("cannot parse response body from github api") + } + + var releaseTags []ReleaseTags + + err = json.Unmarshal(body, &releaseTags) + if err != nil { + return "", ErrUnmarshal(err) + } + + for _, value := range releaseTags { + if value.Name == tag { + return value.Commit.SHA, nil + } + } + + return "", errors.New("release tags not found") +} + // SafeClose is a helper function help to close the io func safeClose(co io.Closer) { if cerr := co.Close(); cerr != nil { diff --git a/utils/utils_test.go b/utils/utils_test.go index 523145be..9db165ad 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -78,3 +78,52 @@ func TestTransformMapKeys(t *testing.T) { }) } } + +func TestGetReleaseTagCommitSHAInvalid(t *testing.T) { + cases := []struct{ + description string + org string + repo string + tag string + expectedErr string + }{ + { + description: "Test cases negative empty tag", + org: "layer5labs", + repo: "meshery-extensions-packages", + tag: "", + expectedErr: "empty release tags", + }, + { + description: "Test cases negative old tag", + org: "layer5labs", + repo: "meshery-extensions-packages", + tag: "v1.0.0", + expectedErr: "release tags not found", + }, + } + for _, tt := range cases { + t.Run(tt.description, func(t *testing.T){ + commitSHA, err := GetReleaseTagCommitSHA(tt.org, tt.repo, tt.tag) + 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 TestGetReleaseTagCommitSHA(t *testing.T) { + commitSHA, err := GetReleaseTagCommitSHA("kelseyhightower", "nocode", "1.0.0") + 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) + } +} From 380bb13b1dcdf96aa38af98b20e853f5f1ce83e5 Mon Sep 17 00:00:00 2001 From: jerensl <54782057+jerensl@users.noreply.github.com> Date: Wed, 30 Oct 2024 15:29:59 +0800 Subject: [PATCH 2/3] ci: refactor using scrape data rather then rest api Signed-off-by: jerensl <54782057+jerensl@users.noreply.github.com> --- utils/utils.go | 46 ++++++++++++++++++++++----------------------- utils/utils_test.go | 27 ++++++++++++-------------- 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index 385836e8..39431db8 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -249,38 +249,36 @@ type ReleaseTags struct { } // Gets release tag from github for a given org name, repo name(in that org) and tag -func GetReleaseTagCommitSHA(org string, repo string, tag string) (string, error) { - url := fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", org, repo) - - if len(tag) == 0 { - return "", errors.New("empty release tags") - } +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 API") + return "", errors.New("cannot get list of tags from Github") } defer safeClose(resp.Body) - body, err := io.ReadAll(resp.Body) - if err != nil { - return "", errors.New("cannot parse response body from github api") - } - - var releaseTags []ReleaseTags - - err = json.Unmarshal(body, &releaseTags) - if err != nil { - return "", ErrUnmarshal(err) - } - - for _, value := range releaseTags { - if value.Name == tag { - return value.Commit.SHA, nil - } + 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 "", errors.New("release tags not found") + return commits[0], nil } // SafeClose is a helper function help to close the io diff --git a/utils/utils_test.go b/utils/utils_test.go index 9db165ad..b2df3700 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -79,32 +79,29 @@ func TestTransformMapKeys(t *testing.T) { } } -func TestGetReleaseTagCommitSHAInvalid(t *testing.T) { +func TestGetLatestReleaseTagCommitSHAInvalid(t *testing.T) { cases := []struct{ description string org string repo string - tag string expectedErr string }{ - { - description: "Test cases negative empty tag", + { + description: "Test cases negative not existed repository", org: "layer5labs", - repo: "meshery-extensions-packages", - tag: "", - expectedErr: "empty release tags", + repo: "meshery-extensions-package", + expectedErr: "repository is not found", }, { - description: "Test cases negative old tag", - org: "layer5labs", - repo: "meshery-extensions-packages", - tag: "v1.0.0", - expectedErr: "release tags 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 := GetReleaseTagCommitSHA(tt.org, tt.repo, tt.tag) + commitSHA, err := GetLatestReleaseTagCommitSHA(tt.org, tt.repo) if err.Error() != tt.expectedErr { t.Errorf("expected error %s, but got error %s", err, err.Error()) } @@ -116,8 +113,8 @@ func TestGetReleaseTagCommitSHAInvalid(t *testing.T) { } } -func TestGetReleaseTagCommitSHA(t *testing.T) { - commitSHA, err := GetReleaseTagCommitSHA("kelseyhightower", "nocode", "1.0.0") +func TestGetLatestReleaseTagCommitSHA(t *testing.T) { + commitSHA, err := GetLatestReleaseTagCommitSHA("kelseyhightower", "nocode") if err != nil { t.Errorf("expected no error, but got error %s", err) } From 75921f205e321a8e4a53a9462bc83511b436d586 Mon Sep 17 00:00:00 2001 From: jerensl <54782057+jerensl@users.noreply.github.com> Date: Wed, 30 Oct 2024 15:31:30 +0800 Subject: [PATCH 3/3] ci: remove unnecesary struct Signed-off-by: jerensl <54782057+jerensl@users.noreply.github.com> --- utils/utils.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index acd642ff..7c62df50 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -240,19 +240,6 @@ func GetLatestReleaseTagsSorted(org string, repo string) ([]string, error) { return versions, nil } -type Commit struct { - SHA string `json:"sha"` - URL string `json:"url"` -} - -type ReleaseTags struct { - Name string `json:"name"` - Commit Commit `json:"commit"` - TarballURL string `json:"tarball_url"` - ZipballURL string `json:"zipball_url"` - NodeID string `json:"node_id"` -} - // 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)