From 98b424a8af2af66ffa7c68d963bbd90251c248a8 Mon Sep 17 00:00:00 2001 From: Christian Groschupp Date: Sat, 6 Jan 2024 05:14:12 +0000 Subject: [PATCH] feat(release): add GetLatestRelease method (#639) This PR adds the GetLatestRelease method to support the /repos/{owner}/{repo}/releases/latest endpoint. ref: https://docs.gitea.com/api/1.20/#tag/repository/operation/repoGetLatestRelease Resolves gitea/go-sdk#630 Co-authored-by: techknowlogick Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/639 Reviewed-by: Lunny Xiao Co-authored-by: Christian Groschupp Co-committed-by: Christian Groschupp --- .gitea/workflows/testing.yml | 2 +- Makefile | 2 +- gitea/issue_label_test.go | 4 ++-- gitea/issue_test.go | 2 +- gitea/notifications_test.go | 21 ++++++++++----------- gitea/release.go | 12 ++++++++++++ gitea/release_test.go | 5 +++++ gitea/repo_branch_test.go | 21 +++++++++++---------- gitea/repo_test.go | 2 +- gitea/settings_test.go | 2 +- 10 files changed, 45 insertions(+), 28 deletions(-) diff --git a/.gitea/workflows/testing.yml b/.gitea/workflows/testing.yml index c3954b2..80ed285 100644 --- a/.gitea/workflows/testing.yml +++ b/.gitea/workflows/testing.yml @@ -28,7 +28,7 @@ jobs: - run: make test services: gitea: - image: gitea/gitea:1.18 + image: gitea/gitea:1.21.1 cmd: - bash - -c diff --git a/Makefile b/Makefile index 15cbf82..d19ef90 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ GOFUMPT_PACKAGE ?= mvdan.cc/gofumpt@v0.4.0 GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.0 GITEA_VET_PACKAGE ?= code.gitea.io/gitea-vet@v0.2.1 -GITEA_VERSION := 1.18 +GITEA_VERSION := 1.21.1 GITEA_DL := https://dl.gitea.com/gitea/$(GITEA_VERSION)/gitea-$(GITEA_VERSION)- UNAME_S := $(shell uname -s) ifeq ($(UNAME_S),Linux) diff --git a/gitea/issue_label_test.go b/gitea/issue_label_test.go index d57736b..a5daed6 100644 --- a/gitea/issue_label_test.go +++ b/gitea/issue_label_test.go @@ -68,14 +68,14 @@ func TestLabels(t *testing.T) { assert.EqualValues(t, labelTwo, label) label, _, err = c.EditLabel(repo.Owner.UserName, repo.Name, labelTwo.ID, EditLabelOption{ - Color: OptionalString("#0E0175"), + Color: OptionalString("#0e0175"), Description: OptionalString("blueish"), }) assert.NoError(t, err) assert.EqualValues(t, &Label{ ID: labelTwo.ID, Name: labelTwo.Name, - Color: "0E0175", + Color: "0e0175", Description: "blueish", URL: labelTwo.URL, }, label) diff --git a/gitea/issue_test.go b/gitea/issue_test.go index 6f7bdab..1a77558 100644 --- a/gitea/issue_test.go +++ b/gitea/issue_test.go @@ -62,7 +62,7 @@ func deleteIssue(t *testing.T, c *Client) { func editIssues(t *testing.T, c *Client) { log.Println("== TestEditIssues ==") - il, _, err := c.ListIssues(ListIssueOption{KeyWord: "soon"}) + il, _, err := c.ListIssues(ListIssueOption{KeyWord: "soon!"}) assert.NoError(t, err) issue, _, err := c.GetIssue(il[0].Poster.UserName, il[0].Repository.Name, il[0].Index) assert.NoError(t, err) diff --git a/gitea/notifications_test.go b/gitea/notifications_test.go index bdaffdc..ab5451d 100644 --- a/gitea/notifications_test.go +++ b/gitea/notifications_test.go @@ -46,7 +46,7 @@ func TestNotifications(t *testing.T) { assert.NoError(t, err) issue, _, err := c.CreateIssue(repoB.Owner.UserName, repoB.Name, CreateIssueOption{Title: "B Issue", Closed: false}) assert.NoError(t, err) - time.Sleep(time.Second * 1) + time.Sleep(time.Second * 5) // CheckNotifications of user2 c.sudo = user2.UserName @@ -101,7 +101,7 @@ func TestNotifications(t *testing.T) { c.sudo = "" _, _, err = c.EditIssue(repoB.Owner.UserName, repoB.Name, issue.Index, EditIssueOption{State: &iState}) assert.NoError(t, err) - time.Sleep(time.Second * 1) + time.Sleep(time.Second * 5) c.sudo = user2.UserName nList, _, err = c.ListNotifications(ListNotificationOptions{}) @@ -120,18 +120,17 @@ func TestNotifications(t *testing.T) { notifications, _, err = c.ReadNotifications(MarkNotificationOptions{}) assert.NoError(t, err) assert.Len(t, notifications, 2) - _, _ = c.DeleteRepo("test01", "Reviews") nList, _, err = c.ListNotifications(ListNotificationOptions{Status: []NotifyStatus{NotifyStatusRead}}) assert.NoError(t, err) - assert.Len(t, nList, 2) + if assert.Len(t, nList, 2) { + notification, _, err := c.ReadNotification(nList[0].ID, NotifyStatusPinned) + assert.EqualValues(t, notification.ID, nList[0].ID) + assert.NoError(t, err) - notification, _, err := c.ReadNotification(nList[0].ID, NotifyStatusPinned) - assert.EqualValues(t, notification.ID, nList[0].ID) - assert.NoError(t, err) - - notification, _, err = c.ReadNotification(nList[1].ID, NotifyStatusUnread) - assert.EqualValues(t, notification.ID, nList[1].ID) - assert.NoError(t, err) + notification, _, err = c.ReadNotification(nList[1].ID, NotifyStatusUnread) + assert.EqualValues(t, notification.ID, nList[1].ID) + assert.NoError(t, err) + } nList, _, err = c.ListNotifications(ListNotificationOptions{Status: []NotifyStatus{NotifyStatusPinned, NotifyStatusUnread}}) assert.NoError(t, err) if assert.Len(t, nList, 2) { diff --git a/gitea/release.go b/gitea/release.go index 3a83417..f3707b7 100644 --- a/gitea/release.go +++ b/gitea/release.go @@ -78,6 +78,18 @@ func (c *Client) GetRelease(owner, repo string, id int64) (*Release, *Response, return r, resp, err } +// GetLatestRelease get the latest release of a repository +func (c *Client) GetLatestRelease(owner, repo string) (*Release, *Response, error) { + if err := escapeValidatePathSegments(&owner, &repo); err != nil { + return nil, nil, err + } + r := new(Release) + resp, err := c.getParsedResponse("GET", + fmt.Sprintf("/repos/%s/%s/releases/latest", owner, repo), + jsonHeader, nil, &r) + return r, resp, err +} + // GetReleaseByTag get a release of a repository by tag func (c *Client) GetReleaseByTag(owner, repo, tag string) (*Release, *Response, error) { if c.checkServerVersionGreaterThanOrEqual(version1_13_0) != nil { diff --git a/gitea/release_test.go b/gitea/release_test.go index 450fe8f..aa1d6f4 100644 --- a/gitea/release_test.go +++ b/gitea/release_test.go @@ -76,6 +76,11 @@ func TestRelease(t *testing.T) { assert.EqualValues(t, false, r2.IsPrerelease) assert.EqualValues(t, r.Note, r2.Note) + // GetLatestRelease + r3, _, err := c.GetLatestRelease(repo.Owner.UserName, repo.Name) + assert.NoError(t, err) + assert.EqualValues(t, r2, r3) + // DeleteRelease _, err = c.DeleteRelease(repo.Owner.UserName, repo.Name, r.ID) assert.NoError(t, err) diff --git a/gitea/repo_branch_test.go b/gitea/repo_branch_test.go index ab81b05..a0471e2 100644 --- a/gitea/repo_branch_test.go +++ b/gitea/repo_branch_test.go @@ -6,8 +6,8 @@ package gitea import ( "log" - "sort" "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -21,22 +21,23 @@ func TestRepoBranches(t *testing.T) { if repo == nil { return } - + time.Sleep(1 * time.Second) bl, _, err := c.ListRepoBranches(repo.Owner.UserName, repo.Name, ListRepoBranchesOptions{}) assert.NoError(t, err) assert.Len(t, bl, 3) - sort.Slice(bl, func(i, j int) bool { - return bl[i].Name < bl[j].Name - }) - assert.EqualValues(t, "feature", bl[0].Name) - assert.EqualValues(t, "main", bl[1].Name) - assert.EqualValues(t, "update", bl[2].Name) + branchNames := make([]string, len(bl)) + branches := make(map[string]Branch, len(bl)) + for index, branch := range bl { + branchNames[index] = branch.Name + branches[branch.Name] = *branch + } + assert.ElementsMatch(t, []string{"feature", "main", "update"}, branchNames) b, _, err := c.GetRepoBranch(repo.Owner.UserName, repo.Name, "update") assert.NoError(t, err) - assert.EqualValues(t, bl[2].Commit.ID, b.Commit.ID) - assert.EqualValues(t, bl[2].Commit.Added, b.Commit.Added) + assert.EqualValues(t, branches["update"].Commit.ID, b.Commit.ID) + assert.EqualValues(t, branches["update"].Commit.Added, b.Commit.Added) s, _, err := c.DeleteRepoBranch(repo.Owner.UserName, repo.Name, "main") assert.NoError(t, err) diff --git a/gitea/repo_test.go b/gitea/repo_test.go index f32d1fc..e6ddd14 100644 --- a/gitea/repo_test.go +++ b/gitea/repo_test.go @@ -65,7 +65,7 @@ func TestRepoMigrateAndLanguages(t *testing.T) { assert.NotEqual(t, zeroTime, repoG.MirrorUpdated) log.Println("== TestRepoLanguages ==") - time.Sleep(time.Second) + time.Sleep(time.Second * 2) lang, _, err := c.GetRepoLanguages(repoM.Owner.UserName, repoM.Name) assert.NoError(t, err) assert.Len(t, lang, 2) diff --git a/gitea/settings_test.go b/gitea/settings_test.go index e025464..efcac76 100644 --- a/gitea/settings_test.go +++ b/gitea/settings_test.go @@ -44,7 +44,7 @@ func TestGetGlobalSettings(t *testing.T) { } assert.EqualValues(t, &GlobalAttachmentSettings{ Enabled: true, - MaxSize: 4, + MaxSize: 2048, MaxFiles: 5, }, attachSettings) }