Skip to content

Commit

Permalink
Add DeleteTag & Correct DeleteReleaseByTag (#488)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
6543 committed Feb 13, 2021
1 parent fd7e38f commit c5a9813
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
4 changes: 2 additions & 2 deletions gitea/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ func (c *Client) DeleteRelease(user, repo string, id int64) (*Response, error) {
return resp, err
}

// DeleteReleaseTag deletes a tag from a repository, if no release refers to it.
func (c *Client) DeleteReleaseTag(user, repo string, tag string) (*Response, error) {
// DeleteReleaseByTag deletes a release frm a repository by tag
func (c *Client) DeleteReleaseByTag(user, repo string, tag string) (*Response, error) {
if err := c.checkServerVersionGreaterThanOrEqual(version1_14_0); err != nil {
return nil, err
}
Expand Down
20 changes: 13 additions & 7 deletions gitea/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,22 @@ func TestRelease(t *testing.T) {
assert.NoError(t, err)
rl, _, _ = c.ListReleases(repo.Owner.UserName, repo.Name, ListReleasesOptions{})
assert.Len(t, rl, 0)
tags, _, err := c.ListRepoTags(repo.Owner.UserName, repo.Name, ListRepoTagsOptions{})
assert.NoError(t, err)
assert.Len(t, tags, 1)

// DeleteReleaseTag
_, err = c.DeleteReleaseTag(repo.Owner.UserName, repo.Name, r.TagName)
// CreateRelease
_, _, err = c.CreateRelease(repo.Owner.UserName, repo.Name, CreateReleaseOption{
TagName: "aNewReleaseTag",
Target: "master",
Title: "Title of aNewReleaseTag",
})
assert.NoError(t, err)
tags, _, err = c.ListRepoTags(repo.Owner.UserName, repo.Name, ListRepoTagsOptions{})

// DeleteReleaseByTag
_, err = c.DeleteReleaseByTag(repo.Owner.UserName, repo.Name, "aNewReleaseTag")
assert.NoError(t, err)
assert.Len(t, tags, 0)
rl, _, _ = c.ListReleases(repo.Owner.UserName, repo.Name, ListReleasesOptions{})
assert.Len(t, rl, 0)
_, err = c.DeleteReleaseByTag(repo.Owner.UserName, repo.Name, "aNewReleaseTag")
assert.Error(t, err)

// Test Response if try to get not existing release
_, resp, err := c.GetRelease(repo.Owner.UserName, repo.Name, 1234)
Expand Down
11 changes: 11 additions & 0 deletions gitea/repo_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@ func (c *Client) ListRepoTags(user, repo string, opt ListRepoTagsOptions) ([]*Ta
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/tags?%s", user, repo, opt.getURLQuery().Encode()), nil, nil, &tags)
return tags, resp, err
}

// DeleteTag deletes a tag from a repository, if no release refers to it
func (c *Client) DeleteTag(user, repo string, tag string) (*Response, error) {
if err := c.checkServerVersionGreaterThanOrEqual(version1_14_0); err != nil {
return nil, err
}
_, resp, err := c.getResponse("DELETE",
fmt.Sprintf("/repos/%s/%s/tags/%s", user, repo, tag),
nil, nil)
return resp, err
}
47 changes: 47 additions & 0 deletions gitea/repo_tag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 (
"log"
"testing"

"github.com/stretchr/testify/assert"
)

func TestTags(t *testing.T) {
log.Println("== TestTags ==")
c := newTestClient()

repo, _ := createTestRepo(t, "TestTags", c)

// Create Tags
createTestTag(t, c, repo, "tag1")

tags, _, err := c.ListRepoTags(repo.Owner.UserName, repo.Name, ListRepoTagsOptions{})
assert.NoError(t, err)
assert.Len(t, tags, 1)

// DeleteReleaseTag
resp, err := c.DeleteTag(repo.Owner.UserName, repo.Name, "tag1")
assert.NoError(t, err)
assert.EqualValues(t, 204, resp.StatusCode)
tags, _, err = c.ListRepoTags(repo.Owner.UserName, repo.Name, ListRepoTagsOptions{})
assert.NoError(t, err)
assert.Len(t, tags, 0)
}

// createTestTag use create release api since there exist no api to create tag only
// https://github.com/go-gitea/gitea/issues/14669
func createTestTag(t *testing.T, c *Client, repo *Repository, name string) {
rel, _, err := c.CreateRelease(repo.Owner.UserName, repo.Name, CreateReleaseOption{
TagName: name,
Target: "master",
Title: "TMP Release",
})
assert.NoError(t, err)
_, err = c.DeleteRelease(repo.Owner.UserName, repo.Name, rel.ID)
assert.NoError(t, err)
}

0 comments on commit c5a9813

Please sign in to comment.