Add GetReleaseByTag (#427)
Add GetReleaseByTag & Test for all Release functiosn Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/427 Reviewed-by: techknowlogick <techknowlogick@gitea.io> Reviewed-by: lafriks <lafriks@noreply.gitea.io>
This commit is contained in:
parent
3226ac2e53
commit
61f89c458f
2 changed files with 129 additions and 1 deletions
|
@ -8,6 +8,7 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
@ -45,11 +46,23 @@ func (c *Client) ListReleases(user, repo string, opt ListReleasesOptions) ([]*Re
|
|||
return releases, resp, err
|
||||
}
|
||||
|
||||
// GetRelease get a release of a repository
|
||||
// GetRelease get a release of a repository by id
|
||||
func (c *Client) GetRelease(user, repo string, id int64) (*Release, *Response, error) {
|
||||
r := new(Release)
|
||||
resp, err := c.getParsedResponse("GET",
|
||||
fmt.Sprintf("/repos/%s/%s/releases/%d", user, repo, id),
|
||||
jsonHeader, nil, &r)
|
||||
return r, resp, err
|
||||
}
|
||||
|
||||
// GetReleaseByTag get a release of a repository by tag
|
||||
func (c *Client) GetReleaseByTag(user, repo string, tag string) (*Release, *Response, error) {
|
||||
if c.CheckServerVersionConstraint(">=1.13.0") != nil {
|
||||
return c.fallbackGetReleaseByTag(user, repo, tag)
|
||||
}
|
||||
r := new(Release)
|
||||
resp, err := c.getParsedResponse("GET",
|
||||
fmt.Sprintf("/repos/%s/%s/releases/tags/%s", user, repo, tag),
|
||||
nil, nil, &r)
|
||||
return r, resp, err
|
||||
}
|
||||
|
@ -118,3 +131,23 @@ func (c *Client) DeleteRelease(user, repo string, id int64) (*Response, error) {
|
|||
nil, nil)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// fallbackGetReleaseByTag is fallback for old gitea installations ( < 1.13.0 )
|
||||
func (c *Client) fallbackGetReleaseByTag(user, repo string, tag string) (*Release, *Response, error) {
|
||||
for i := 1; ; i++ {
|
||||
rl, resp, err := c.ListReleases(user, repo, ListReleasesOptions{ListOptions{Page: i}})
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
if len(rl) == 0 {
|
||||
return nil,
|
||||
&Response{&http.Response{StatusCode: 404}},
|
||||
fmt.Errorf("release with tag '%s' not found", tag)
|
||||
}
|
||||
for _, r := range rl {
|
||||
if r.TagName == tag {
|
||||
return r, resp, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
95
gitea/release_test.go
Normal file
95
gitea/release_test.go
Normal file
|
@ -0,0 +1,95 @@
|
|||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRelease(t *testing.T) {
|
||||
log.Println("== TestRelease ==")
|
||||
c := newTestClient()
|
||||
|
||||
repo, _ := createTestRepo(t, "ReleaseTests", c)
|
||||
|
||||
// ListReleases
|
||||
rl, _, err := c.ListReleases(repo.Owner.UserName, repo.Name, ListReleasesOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, rl, 0)
|
||||
|
||||
// CreateRelease
|
||||
r, _, err := c.CreateRelease(repo.Owner.UserName, repo.Name, CreateReleaseOption{
|
||||
TagName: "awesome",
|
||||
Target: "master",
|
||||
Title: "Release 1",
|
||||
Note: "yes it's awesome",
|
||||
IsDraft: true,
|
||||
IsPrerelease: true,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, "awesome", r.TagName)
|
||||
assert.EqualValues(t, true, r.IsPrerelease)
|
||||
assert.EqualValues(t, true, r.IsDraft)
|
||||
assert.EqualValues(t, "Release 1", r.Title)
|
||||
assert.EqualValues(t, fmt.Sprintf("%s/api/v1/repos/%s/releases/%d", c.url, repo.FullName, r.ID), r.URL)
|
||||
assert.EqualValues(t, "master", r.Target)
|
||||
assert.EqualValues(t, "yes it's awesome", r.Note)
|
||||
assert.EqualValues(t, c.username, r.Publisher.UserName)
|
||||
rl, _, _ = c.ListReleases(repo.Owner.UserName, repo.Name, ListReleasesOptions{})
|
||||
assert.Len(t, rl, 1)
|
||||
|
||||
// GetRelease
|
||||
r2, _, err := c.GetRelease(repo.Owner.UserName, repo.Name, r.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, r, r2)
|
||||
r2, _, err = c.GetReleaseByTag(repo.Owner.UserName, repo.Name, r.TagName)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, r, r2)
|
||||
// test fallback
|
||||
r2, _, err = c.fallbackGetReleaseByTag(repo.Owner.UserName, repo.Name, r.TagName)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, r, r2)
|
||||
|
||||
// EditRelease
|
||||
bFalse := false
|
||||
r2, _, err = c.EditRelease(repo.Owner.UserName, repo.Name, r.ID, EditReleaseOption{
|
||||
Title: "Release Awesome",
|
||||
Note: "",
|
||||
IsDraft: &bFalse,
|
||||
IsPrerelease: &bFalse,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, r.Target, r2.Target)
|
||||
assert.EqualValues(t, bFalse, r2.IsDraft)
|
||||
assert.EqualValues(t, bFalse, r2.IsPrerelease)
|
||||
assert.EqualValues(t, r.Note, r2.Note)
|
||||
|
||||
// DeleteRelease
|
||||
_, err = c.DeleteRelease(repo.Owner.UserName, repo.Name, r.ID)
|
||||
assert.NoError(t, err)
|
||||
rl, _, _ = c.ListReleases(repo.Owner.UserName, repo.Name, ListReleasesOptions{})
|
||||
assert.Len(t, rl, 0)
|
||||
|
||||
// Test Response if try to get not existing release
|
||||
_, resp, err := c.GetRelease(repo.Owner.UserName, repo.Name, 1234)
|
||||
assert.Error(t, err)
|
||||
if assert.NotNil(t, resp) {
|
||||
assert.EqualValues(t, 404, resp.StatusCode)
|
||||
}
|
||||
_, resp, err = c.GetReleaseByTag(repo.Owner.UserName, repo.Name, "not_here")
|
||||
assert.Error(t, err)
|
||||
if assert.NotNil(t, resp) {
|
||||
assert.EqualValues(t, 404, resp.StatusCode)
|
||||
}
|
||||
_, resp, err = c.fallbackGetReleaseByTag(repo.Owner.UserName, repo.Name, "not_here")
|
||||
assert.Error(t, err)
|
||||
if assert.NotNil(t, resp) {
|
||||
assert.EqualValues(t, 404, resp.StatusCode)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue