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 <techknowlogick@noreply.gitea.com>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/639
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Christian Groschupp <christian@groschupp.org>
Co-committed-by: Christian Groschupp <christian@groschupp.org>
This commit is contained in:
Christian Groschupp 2024-01-06 05:14:12 +00:00 committed by techknowlogick
parent 2abfdd7ba6
commit 98b424a8af
10 changed files with 45 additions and 28 deletions

View file

@ -28,7 +28,7 @@ jobs:
- run: make test - run: make test
services: services:
gitea: gitea:
image: gitea/gitea:1.18 image: gitea/gitea:1.21.1
cmd: cmd:
- bash - bash
- -c - -c

View file

@ -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 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_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)- GITEA_DL := https://dl.gitea.com/gitea/$(GITEA_VERSION)/gitea-$(GITEA_VERSION)-
UNAME_S := $(shell uname -s) UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux) ifeq ($(UNAME_S),Linux)

View file

@ -68,14 +68,14 @@ func TestLabels(t *testing.T) {
assert.EqualValues(t, labelTwo, label) assert.EqualValues(t, labelTwo, label)
label, _, err = c.EditLabel(repo.Owner.UserName, repo.Name, labelTwo.ID, EditLabelOption{ label, _, err = c.EditLabel(repo.Owner.UserName, repo.Name, labelTwo.ID, EditLabelOption{
Color: OptionalString("#0E0175"), Color: OptionalString("#0e0175"),
Description: OptionalString("blueish"), Description: OptionalString("blueish"),
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, &Label{ assert.EqualValues(t, &Label{
ID: labelTwo.ID, ID: labelTwo.ID,
Name: labelTwo.Name, Name: labelTwo.Name,
Color: "0E0175", Color: "0e0175",
Description: "blueish", Description: "blueish",
URL: labelTwo.URL, URL: labelTwo.URL,
}, label) }, label)

View file

@ -62,7 +62,7 @@ func deleteIssue(t *testing.T, c *Client) {
func editIssues(t *testing.T, c *Client) { func editIssues(t *testing.T, c *Client) {
log.Println("== TestEditIssues ==") log.Println("== TestEditIssues ==")
il, _, err := c.ListIssues(ListIssueOption{KeyWord: "soon"}) il, _, err := c.ListIssues(ListIssueOption{KeyWord: "soon!"})
assert.NoError(t, err) assert.NoError(t, err)
issue, _, err := c.GetIssue(il[0].Poster.UserName, il[0].Repository.Name, il[0].Index) issue, _, err := c.GetIssue(il[0].Poster.UserName, il[0].Repository.Name, il[0].Index)
assert.NoError(t, err) assert.NoError(t, err)

View file

@ -46,7 +46,7 @@ func TestNotifications(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
issue, _, err := c.CreateIssue(repoB.Owner.UserName, repoB.Name, CreateIssueOption{Title: "B Issue", Closed: false}) issue, _, err := c.CreateIssue(repoB.Owner.UserName, repoB.Name, CreateIssueOption{Title: "B Issue", Closed: false})
assert.NoError(t, err) assert.NoError(t, err)
time.Sleep(time.Second * 1) time.Sleep(time.Second * 5)
// CheckNotifications of user2 // CheckNotifications of user2
c.sudo = user2.UserName c.sudo = user2.UserName
@ -101,7 +101,7 @@ func TestNotifications(t *testing.T) {
c.sudo = "" c.sudo = ""
_, _, err = c.EditIssue(repoB.Owner.UserName, repoB.Name, issue.Index, EditIssueOption{State: &iState}) _, _, err = c.EditIssue(repoB.Owner.UserName, repoB.Name, issue.Index, EditIssueOption{State: &iState})
assert.NoError(t, err) assert.NoError(t, err)
time.Sleep(time.Second * 1) time.Sleep(time.Second * 5)
c.sudo = user2.UserName c.sudo = user2.UserName
nList, _, err = c.ListNotifications(ListNotificationOptions{}) nList, _, err = c.ListNotifications(ListNotificationOptions{})
@ -120,18 +120,17 @@ func TestNotifications(t *testing.T) {
notifications, _, err = c.ReadNotifications(MarkNotificationOptions{}) notifications, _, err = c.ReadNotifications(MarkNotificationOptions{})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, notifications, 2) assert.Len(t, notifications, 2)
_, _ = c.DeleteRepo("test01", "Reviews")
nList, _, err = c.ListNotifications(ListNotificationOptions{Status: []NotifyStatus{NotifyStatusRead}}) nList, _, err = c.ListNotifications(ListNotificationOptions{Status: []NotifyStatus{NotifyStatusRead}})
assert.NoError(t, err) 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) notification, _, err = c.ReadNotification(nList[1].ID, NotifyStatusUnread)
assert.EqualValues(t, notification.ID, nList[0].ID) assert.EqualValues(t, notification.ID, nList[1].ID)
assert.NoError(t, err) 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}}) nList, _, err = c.ListNotifications(ListNotificationOptions{Status: []NotifyStatus{NotifyStatusPinned, NotifyStatusUnread}})
assert.NoError(t, err) assert.NoError(t, err)
if assert.Len(t, nList, 2) { if assert.Len(t, nList, 2) {

View file

@ -78,6 +78,18 @@ func (c *Client) GetRelease(owner, repo string, id int64) (*Release, *Response,
return r, resp, err 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 // GetReleaseByTag get a release of a repository by tag
func (c *Client) GetReleaseByTag(owner, repo, tag string) (*Release, *Response, error) { func (c *Client) GetReleaseByTag(owner, repo, tag string) (*Release, *Response, error) {
if c.checkServerVersionGreaterThanOrEqual(version1_13_0) != nil { if c.checkServerVersionGreaterThanOrEqual(version1_13_0) != nil {

View file

@ -76,6 +76,11 @@ func TestRelease(t *testing.T) {
assert.EqualValues(t, false, r2.IsPrerelease) assert.EqualValues(t, false, r2.IsPrerelease)
assert.EqualValues(t, r.Note, r2.Note) 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 // DeleteRelease
_, err = c.DeleteRelease(repo.Owner.UserName, repo.Name, r.ID) _, err = c.DeleteRelease(repo.Owner.UserName, repo.Name, r.ID)
assert.NoError(t, err) assert.NoError(t, err)

View file

@ -6,8 +6,8 @@ package gitea
import ( import (
"log" "log"
"sort"
"testing" "testing"
"time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -21,22 +21,23 @@ func TestRepoBranches(t *testing.T) {
if repo == nil { if repo == nil {
return return
} }
time.Sleep(1 * time.Second)
bl, _, err := c.ListRepoBranches(repo.Owner.UserName, repo.Name, ListRepoBranchesOptions{}) bl, _, err := c.ListRepoBranches(repo.Owner.UserName, repo.Name, ListRepoBranchesOptions{})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, bl, 3) assert.Len(t, bl, 3)
sort.Slice(bl, func(i, j int) bool { branchNames := make([]string, len(bl))
return bl[i].Name < bl[j].Name branches := make(map[string]Branch, len(bl))
}) for index, branch := range bl {
assert.EqualValues(t, "feature", bl[0].Name) branchNames[index] = branch.Name
assert.EqualValues(t, "main", bl[1].Name) branches[branch.Name] = *branch
assert.EqualValues(t, "update", bl[2].Name) }
assert.ElementsMatch(t, []string{"feature", "main", "update"}, branchNames)
b, _, err := c.GetRepoBranch(repo.Owner.UserName, repo.Name, "update") b, _, err := c.GetRepoBranch(repo.Owner.UserName, repo.Name, "update")
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, bl[2].Commit.ID, b.Commit.ID) assert.EqualValues(t, branches["update"].Commit.ID, b.Commit.ID)
assert.EqualValues(t, bl[2].Commit.Added, b.Commit.Added) assert.EqualValues(t, branches["update"].Commit.Added, b.Commit.Added)
s, _, err := c.DeleteRepoBranch(repo.Owner.UserName, repo.Name, "main") s, _, err := c.DeleteRepoBranch(repo.Owner.UserName, repo.Name, "main")
assert.NoError(t, err) assert.NoError(t, err)

View file

@ -65,7 +65,7 @@ func TestRepoMigrateAndLanguages(t *testing.T) {
assert.NotEqual(t, zeroTime, repoG.MirrorUpdated) assert.NotEqual(t, zeroTime, repoG.MirrorUpdated)
log.Println("== TestRepoLanguages ==") log.Println("== TestRepoLanguages ==")
time.Sleep(time.Second) time.Sleep(time.Second * 2)
lang, _, err := c.GetRepoLanguages(repoM.Owner.UserName, repoM.Name) lang, _, err := c.GetRepoLanguages(repoM.Owner.UserName, repoM.Name)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, lang, 2) assert.Len(t, lang, 2)

View file

@ -44,7 +44,7 @@ func TestGetGlobalSettings(t *testing.T) {
} }
assert.EqualValues(t, &GlobalAttachmentSettings{ assert.EqualValues(t, &GlobalAttachmentSettings{
Enabled: true, Enabled: true,
MaxSize: 4, MaxSize: 2048,
MaxFiles: 5, MaxFiles: 5,
}, attachSettings) }, attachSettings)
} }