summaryrefslogtreecommitdiffstats
path: root/services/release
diff options
context:
space:
mode:
authora1012112796 <1012112796@qq.com>2021-03-01 03:57:45 +0800
committerGitHub <noreply@github.com>2021-02-28 19:57:45 +0000
commit83cf1a894e5a3a60ea72fa6b92744cb0705dd145 (patch)
tree516863d9c6ead4020004d1730d6bb2d91afbf56b /services/release
parent3e652860bb66fafe162079553f8640553c18fe79 (diff)
downloadgitea-83cf1a894e5a3a60ea72fa6b92744cb0705dd145.tar.gz
gitea-83cf1a894e5a3a60ea72fa6b92744cb0705dd145.zip
Create tag on ui (#13467)
Support create single tag directly support create tag with message from create release ui Signed-off-by: a1012112796 <1012112796@qq.com> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'services/release')
-rw-r--r--services/release/release.go57
-rw-r--r--services/release/release_test.go39
2 files changed, 76 insertions, 20 deletions
diff --git a/services/release/release.go b/services/release/release.go
index fd821cc813..d04e538c92 100644
--- a/services/release/release.go
+++ b/services/release/release.go
@@ -17,7 +17,7 @@ import (
"code.gitea.io/gitea/modules/timeutil"
)
-func createTag(gitRepo *git.Repository, rel *models.Release) error {
+func createTag(gitRepo *git.Repository, rel *models.Release, msg string) error {
// Only actual create when publish.
if !rel.IsDraft {
if !gitRepo.IsTagExist(rel.TagName) {
@@ -28,7 +28,16 @@ func createTag(gitRepo *git.Repository, rel *models.Release) error {
// Trim '--' prefix to prevent command line argument vulnerability.
rel.TagName = strings.TrimPrefix(rel.TagName, "--")
- if err = gitRepo.CreateTag(rel.TagName, commit.ID.String()); err != nil {
+ if len(msg) > 0 {
+ if err = gitRepo.CreateAnnotatedTag(rel.TagName, msg, commit.ID.String()); err != nil {
+ if strings.Contains(err.Error(), "is not a valid tag name") {
+ return models.ErrInvalidTagName{
+ TagName: rel.TagName,
+ }
+ }
+ return err
+ }
+ } else if err = gitRepo.CreateTag(rel.TagName, commit.ID.String()); err != nil {
if strings.Contains(err.Error(), "is not a valid tag name") {
return models.ErrInvalidTagName{
TagName: rel.TagName,
@@ -77,7 +86,7 @@ func createTag(gitRepo *git.Repository, rel *models.Release) error {
}
// CreateRelease creates a new release of repository.
-func CreateRelease(gitRepo *git.Repository, rel *models.Release, attachmentUUIDs []string) error {
+func CreateRelease(gitRepo *git.Repository, rel *models.Release, attachmentUUIDs []string, msg string) error {
isExist, err := models.IsReleaseExist(rel.RepoID, rel.TagName)
if err != nil {
return err
@@ -87,7 +96,7 @@ func CreateRelease(gitRepo *git.Repository, rel *models.Release, attachmentUUIDs
}
}
- if err = createTag(gitRepo, rel); err != nil {
+ if err = createTag(gitRepo, rel, msg); err != nil {
return err
}
@@ -107,9 +116,47 @@ func CreateRelease(gitRepo *git.Repository, rel *models.Release, attachmentUUIDs
return nil
}
+// CreateNewTag creates a new repository tag
+func CreateNewTag(doer *models.User, repo *models.Repository, commit, tagName, msg string) error {
+ isExist, err := models.IsReleaseExist(repo.ID, tagName)
+ if err != nil {
+ return err
+ } else if isExist {
+ return models.ErrTagAlreadyExists{
+ TagName: tagName,
+ }
+ }
+
+ gitRepo, err := git.OpenRepository(repo.RepoPath())
+ if err != nil {
+ return err
+ }
+ defer gitRepo.Close()
+
+ rel := &models.Release{
+ RepoID: repo.ID,
+ PublisherID: doer.ID,
+ TagName: tagName,
+ Target: commit,
+ IsDraft: false,
+ IsPrerelease: false,
+ IsTag: true,
+ }
+
+ if err = createTag(gitRepo, rel, msg); err != nil {
+ return err
+ }
+
+ if err = models.InsertRelease(rel); err != nil {
+ return err
+ }
+
+ return err
+}
+
// UpdateReleaseOrCreatReleaseFromTag updates information of a release or create release from tag.
func UpdateReleaseOrCreatReleaseFromTag(doer *models.User, gitRepo *git.Repository, rel *models.Release, attachmentUUIDs []string, isCreate bool) (err error) {
- if err = createTag(gitRepo, rel); err != nil {
+ if err = createTag(gitRepo, rel, ""); err != nil {
return err
}
rel.LowerTagName = strings.ToLower(rel.TagName)
diff --git a/services/release/release_test.go b/services/release/release_test.go
index f50fca71ec..deb0618832 100644
--- a/services/release/release_test.go
+++ b/services/release/release_test.go
@@ -40,7 +40,7 @@ func TestRelease_Create(t *testing.T) {
IsDraft: false,
IsPrerelease: false,
IsTag: false,
- }, nil))
+ }, nil, ""))
assert.NoError(t, CreateRelease(gitRepo, &models.Release{
RepoID: repo.ID,
@@ -52,7 +52,7 @@ func TestRelease_Create(t *testing.T) {
IsDraft: false,
IsPrerelease: false,
IsTag: false,
- }, nil))
+ }, nil, ""))
assert.NoError(t, CreateRelease(gitRepo, &models.Release{
RepoID: repo.ID,
@@ -64,7 +64,7 @@ func TestRelease_Create(t *testing.T) {
IsDraft: false,
IsPrerelease: false,
IsTag: false,
- }, nil))
+ }, nil, ""))
assert.NoError(t, CreateRelease(gitRepo, &models.Release{
RepoID: repo.ID,
@@ -76,7 +76,7 @@ func TestRelease_Create(t *testing.T) {
IsDraft: true,
IsPrerelease: false,
IsTag: false,
- }, nil))
+ }, nil, ""))
assert.NoError(t, CreateRelease(gitRepo, &models.Release{
RepoID: repo.ID,
@@ -88,7 +88,7 @@ func TestRelease_Create(t *testing.T) {
IsDraft: false,
IsPrerelease: true,
IsTag: false,
- }, nil))
+ }, nil, ""))
assert.NoError(t, CreateRelease(gitRepo, &models.Release{
RepoID: repo.ID,
@@ -100,7 +100,7 @@ func TestRelease_Create(t *testing.T) {
IsDraft: false,
IsPrerelease: false,
IsTag: true,
- }, nil))
+ }, nil, "test"))
}
func TestRelease_Update(t *testing.T) {
@@ -125,7 +125,7 @@ func TestRelease_Update(t *testing.T) {
IsDraft: false,
IsPrerelease: false,
IsTag: false,
- }, nil))
+ }, nil, ""))
release, err := models.GetRelease(repo.ID, "v1.1.1")
assert.NoError(t, err)
releaseCreatedUnix := release.CreatedUnix
@@ -147,7 +147,7 @@ func TestRelease_Update(t *testing.T) {
IsDraft: true,
IsPrerelease: false,
IsTag: false,
- }, nil))
+ }, nil, ""))
release, err = models.GetRelease(repo.ID, "v1.2.1")
assert.NoError(t, err)
releaseCreatedUnix = release.CreatedUnix
@@ -169,7 +169,7 @@ func TestRelease_Update(t *testing.T) {
IsDraft: false,
IsPrerelease: true,
IsTag: false,
- }, nil))
+ }, nil, ""))
release, err = models.GetRelease(repo.ID, "v1.3.1")
assert.NoError(t, err)
releaseCreatedUnix = release.CreatedUnix
@@ -205,12 +205,12 @@ func TestRelease_createTag(t *testing.T) {
IsPrerelease: false,
IsTag: false,
}
- assert.NoError(t, createTag(gitRepo, release))
+ assert.NoError(t, createTag(gitRepo, release, ""))
assert.NotEmpty(t, release.CreatedUnix)
releaseCreatedUnix := release.CreatedUnix
time.Sleep(2 * time.Second) // sleep 2 seconds to ensure a different timestamp
release.Note = "Changed note"
- assert.NoError(t, createTag(gitRepo, release))
+ assert.NoError(t, createTag(gitRepo, release, ""))
assert.Equal(t, int64(releaseCreatedUnix), int64(release.CreatedUnix))
// Test a changed draft
@@ -225,11 +225,11 @@ func TestRelease_createTag(t *testing.T) {
IsPrerelease: false,
IsTag: false,
}
- assert.NoError(t, createTag(gitRepo, release))
+ assert.NoError(t, createTag(gitRepo, release, ""))
releaseCreatedUnix = release.CreatedUnix
time.Sleep(2 * time.Second) // sleep 2 seconds to ensure a different timestamp
release.Title = "Changed title"
- assert.NoError(t, createTag(gitRepo, release))
+ assert.NoError(t, createTag(gitRepo, release, ""))
assert.Less(t, int64(releaseCreatedUnix), int64(release.CreatedUnix))
// Test a changed pre-release
@@ -244,11 +244,20 @@ func TestRelease_createTag(t *testing.T) {
IsPrerelease: true,
IsTag: false,
}
- assert.NoError(t, createTag(gitRepo, release))
+ assert.NoError(t, createTag(gitRepo, release, ""))
releaseCreatedUnix = release.CreatedUnix
time.Sleep(2 * time.Second) // sleep 2 seconds to ensure a different timestamp
release.Title = "Changed title"
release.Note = "Changed note"
- assert.NoError(t, createTag(gitRepo, release))
+ assert.NoError(t, createTag(gitRepo, release, ""))
assert.Equal(t, int64(releaseCreatedUnix), int64(release.CreatedUnix))
}
+
+func TestCreateNewTag(t *testing.T) {
+ assert.NoError(t, models.PrepareTestDatabase())
+ user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
+ repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
+
+ assert.NoError(t, CreateNewTag(user, repo, "master", "v2.0",
+ "v2.0 is released \n\n BUGFIX: .... \n\n 123"))
+}