Skip to content

Commit c5a9813

Browse files
committed
Add DeleteTag & Correct DeleteReleaseByTag (#488)
* Add DeleteTag + Tests * Correct DeleteReleaseByTag func + Tests this is because of go-gitea/gitea#14563 witch fixed unreleased inconsistency Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/488 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: khmarbaise <khmarbaise@noreply.gitea.io> Co-authored-by: 6543 <6543@obermui.de> Co-committed-by: 6543 <6543@obermui.de>
1 parent fd7e38f commit c5a9813

File tree

4 files changed

+73
-9
lines changed

4 files changed

+73
-9
lines changed

gitea/release.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ func (c *Client) DeleteRelease(user, repo string, id int64) (*Response, error) {
133133
return resp, err
134134
}
135135

136-
// DeleteReleaseTag deletes a tag from a repository, if no release refers to it.
137-
func (c *Client) DeleteReleaseTag(user, repo string, tag string) (*Response, error) {
136+
// DeleteReleaseByTag deletes a release frm a repository by tag
137+
func (c *Client) DeleteReleaseByTag(user, repo string, tag string) (*Response, error) {
138138
if err := c.checkServerVersionGreaterThanOrEqual(version1_14_0); err != nil {
139139
return nil, err
140140
}

gitea/release_test.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,22 @@ func TestRelease(t *testing.T) {
7474
assert.NoError(t, err)
7575
rl, _, _ = c.ListReleases(repo.Owner.UserName, repo.Name, ListReleasesOptions{})
7676
assert.Len(t, rl, 0)
77-
tags, _, err := c.ListRepoTags(repo.Owner.UserName, repo.Name, ListRepoTagsOptions{})
78-
assert.NoError(t, err)
79-
assert.Len(t, tags, 1)
8077

81-
// DeleteReleaseTag
82-
_, err = c.DeleteReleaseTag(repo.Owner.UserName, repo.Name, r.TagName)
78+
// CreateRelease
79+
_, _, err = c.CreateRelease(repo.Owner.UserName, repo.Name, CreateReleaseOption{
80+
TagName: "aNewReleaseTag",
81+
Target: "master",
82+
Title: "Title of aNewReleaseTag",
83+
})
8384
assert.NoError(t, err)
84-
tags, _, err = c.ListRepoTags(repo.Owner.UserName, repo.Name, ListRepoTagsOptions{})
85+
86+
// DeleteReleaseByTag
87+
_, err = c.DeleteReleaseByTag(repo.Owner.UserName, repo.Name, "aNewReleaseTag")
8588
assert.NoError(t, err)
86-
assert.Len(t, tags, 0)
89+
rl, _, _ = c.ListReleases(repo.Owner.UserName, repo.Name, ListReleasesOptions{})
90+
assert.Len(t, rl, 0)
91+
_, err = c.DeleteReleaseByTag(repo.Owner.UserName, repo.Name, "aNewReleaseTag")
92+
assert.Error(t, err)
8793

8894
// Test Response if try to get not existing release
8995
_, resp, err := c.GetRelease(repo.Owner.UserName, repo.Name, 1234)

gitea/repo_tag.go

+11
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,14 @@ func (c *Client) ListRepoTags(user, repo string, opt ListRepoTagsOptions) ([]*Ta
2929
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/tags?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &tags)
3030
return tags, resp, err
3131
}
32+
33+
// DeleteTag deletes a tag from a repository, if no release refers to it
34+
func (c *Client) DeleteTag(user, repo string, tag string) (*Response, error) {
35+
if err := c.checkServerVersionGreaterThanOrEqual(version1_14_0); err != nil {
36+
return nil, err
37+
}
38+
_, resp, err := c.getResponse("DELETE",
39+
fmt.Sprintf("/repos/%s/%s/tags/%s", user, repo, tag),
40+
nil, nil)
41+
return resp, err
42+
}

gitea/repo_tag_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gitea
6+
7+
import (
8+
"log"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestTags(t *testing.T) {
15+
log.Println("== TestTags ==")
16+
c := newTestClient()
17+
18+
repo, _ := createTestRepo(t, "TestTags", c)
19+
20+
// Create Tags
21+
createTestTag(t, c, repo, "tag1")
22+
23+
tags, _, err := c.ListRepoTags(repo.Owner.UserName, repo.Name, ListRepoTagsOptions{})
24+
assert.NoError(t, err)
25+
assert.Len(t, tags, 1)
26+
27+
// DeleteReleaseTag
28+
resp, err := c.DeleteTag(repo.Owner.UserName, repo.Name, "tag1")
29+
assert.NoError(t, err)
30+
assert.EqualValues(t, 204, resp.StatusCode)
31+
tags, _, err = c.ListRepoTags(repo.Owner.UserName, repo.Name, ListRepoTagsOptions{})
32+
assert.NoError(t, err)
33+
assert.Len(t, tags, 0)
34+
}
35+
36+
// createTestTag use create release api since there exist no api to create tag only
37+
// https://github.com/go-gitea/gitea/issues/14669
38+
func createTestTag(t *testing.T, c *Client, repo *Repository, name string) {
39+
rel, _, err := c.CreateRelease(repo.Owner.UserName, repo.Name, CreateReleaseOption{
40+
TagName: name,
41+
Target: "master",
42+
Title: "TMP Release",
43+
})
44+
assert.NoError(t, err)
45+
_, err = c.DeleteRelease(repo.Owner.UserName, repo.Name, rel.ID)
46+
assert.NoError(t, err)
47+
}

0 commit comments

Comments
 (0)