]> source.dussan.org Git - gitea.git/commitdiff
Create tag on ui (#13467)
authora1012112796 <1012112796@qq.com>
Sun, 28 Feb 2021 19:57:45 +0000 (03:57 +0800)
committerGitHub <noreply@github.com>
Sun, 28 Feb 2021 19:57:45 +0000 (19:57 +0000)
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>
13 files changed:
modules/forms/repo_branch_form.go
modules/forms/repo_form.go
modules/git/repo_tag_test.go
options/locale/locale_en-US.ini
routers/api/v1/repo/release.go
routers/repo/branch.go
routers/repo/release.go
services/mirror/mirror_test.go
services/release/release.go
services/release/release_test.go
templates/repo/branch_dropdown.tmpl
templates/repo/release/new.tmpl
web_src/js/index.js

index 5c631f20d4b19751a17b39c9abf0dda958944517..88a069b8310c8d1c0b5bde2d2a128ac1cae28683 100644 (file)
@@ -16,6 +16,7 @@ import (
 // NewBranchForm form for creating a new branch
 type NewBranchForm struct {
        NewBranchName string `binding:"Required;MaxSize(100);GitRefName"`
+       CreateTag     bool
 }
 
 // Validate validates the fields
index 48af3450f371d8715815287623574a14d894fdfa..2793acdd5b74069cfb7fc647060f2fe400a67f76 100644 (file)
@@ -642,7 +642,9 @@ type NewReleaseForm struct {
        Title      string `binding:"Required;MaxSize(255)"`
        Content    string
        Draft      string
+       TagOnly    string
        Prerelease bool
+       AddTagMsg  bool
        Files      []string
 }
 
index 1dee29ba57357c5c7507ea3a3ea57ccadae64579..6feae8d9132561d6e30a2ab923246a20ed9c93d3 100644 (file)
@@ -43,7 +43,7 @@ func TestRepository_GetTag(t *testing.T) {
 
        aTagCommitID := "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0"
        aTagName := "annotatedTag"
-       aTagMessage := "my annotated message"
+       aTagMessage := "my annotated message \n - test two line"
        bareRepo1.CreateAnnotatedTag(aTagName, aTagMessage, aTagCommitID)
        aTagID, _ := bareRepo1.GetTagID(aTagName)
 
index d6c8aa6f7101db21a63b414a4ac2c66f29084643..99f07b9050334cd90a91c5bc982e992b97aa9be3 100644 (file)
@@ -1895,6 +1895,8 @@ release.tag_name_invalid = The tag name is not valid.
 release.tag_already_exist = This tag name already exists.
 release.downloads = Downloads
 release.download_count = Downloads: %s
+release.add_tag_msg = Use the title and content of release as tag message.
+release.add_tag = Create Tag Only
 
 branch.name = Branch Name
 branch.search = Search branches
@@ -1922,6 +1924,9 @@ branch.download = Download Branch '%s'
 branch.included_desc = This branch is part of the default branch
 branch.included = Included
 
+tag.create_tag = Create tag <strong>%s</strong>
+tag.create_success = Tag '%s' has been created.
+
 topic.manage_topics = Manage Topics
 topic.done = Done
 topic.count_prompt = You can not select more than 25 topics
index 08d92e6c0a86c7fb6a39fc1439fdb8dbde433e39..e9eb4e0f9eed6856cc6c2005658de0f73a5185cd 100644 (file)
@@ -179,7 +179,7 @@ func CreateRelease(ctx *context.APIContext) {
                        IsTag:        false,
                        Repo:         ctx.Repo.Repository,
                }
-               if err := releaseservice.CreateRelease(ctx.Repo.GitRepo, rel, nil); err != nil {
+               if err := releaseservice.CreateRelease(ctx.Repo.GitRepo, rel, nil, ""); err != nil {
                        if models.IsErrReleaseAlreadyExist(err) {
                                ctx.Error(http.StatusConflict, "ReleaseAlreadyExist", err)
                        } else {
index cf6abc08df52577cd8ef8c4eb11ed1667e0e7f02..ac6b7a1bed080d0773075e0581832a201d3a14fd 100644 (file)
@@ -20,6 +20,7 @@ import (
        "code.gitea.io/gitea/modules/util"
        "code.gitea.io/gitea/modules/web"
        "code.gitea.io/gitea/routers/utils"
+       release_service "code.gitea.io/gitea/services/release"
        repo_service "code.gitea.io/gitea/services/repository"
 )
 
@@ -383,7 +384,14 @@ func CreateBranch(ctx *context.Context) {
        }
 
        var err error
-       if ctx.Repo.IsViewBranch {
+
+       if form.CreateTag {
+               if ctx.Repo.IsViewTag {
+                       err = release_service.CreateNewTag(ctx.User, ctx.Repo.Repository, ctx.Repo.CommitID, form.NewBranchName, "")
+               } else {
+                       err = release_service.CreateNewTag(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName, "")
+               }
+       } else if ctx.Repo.IsViewBranch {
                err = repo_module.CreateNewBranch(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
        } else if ctx.Repo.IsViewTag {
                err = repo_module.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.CommitID, form.NewBranchName)
@@ -432,6 +440,12 @@ func CreateBranch(ctx *context.Context) {
                return
        }
 
+       if form.CreateTag {
+               ctx.Flash.Success(ctx.Tr("repo.tag.create_success", form.NewBranchName))
+               ctx.Redirect(ctx.Repo.RepoLink + "/src/tag/" + util.PathEscapeSegments(form.NewBranchName))
+               return
+       }
+
        ctx.Flash.Success(ctx.Tr("repo.branch.create_success", form.NewBranchName))
        ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(form.NewBranchName))
 }
index b4370bb09b9606e57039e20eb17724b03c785dcc..d89a173669e5215f04c3d099eec6b2c9e9117de0 100644 (file)
@@ -262,6 +262,29 @@ func NewReleasePost(ctx *context.Context) {
                        return
                }
 
+               msg := ""
+               if len(form.Title) > 0 && form.AddTagMsg {
+                       msg = form.Title + "\n\n" + form.Content
+               }
+
+               if len(form.TagOnly) > 0 {
+                       if err = releaseservice.CreateNewTag(ctx.User, ctx.Repo.Repository, form.Target, form.TagName, msg); err != nil {
+                               if models.IsErrTagAlreadyExists(err) {
+                                       e := err.(models.ErrTagAlreadyExists)
+                                       ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName))
+                                       ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
+                                       return
+                               }
+
+                               ctx.ServerError("releaseservice.CreateNewTag", err)
+                               return
+                       }
+
+                       ctx.Flash.Success(ctx.Tr("repo.tag.create_success", form.TagName))
+                       ctx.Redirect(ctx.Repo.RepoLink + "/src/tag/" + form.TagName)
+                       return
+               }
+
                rel = &models.Release{
                        RepoID:       ctx.Repo.Repository.ID,
                        PublisherID:  ctx.User.ID,
@@ -274,7 +297,7 @@ func NewReleasePost(ctx *context.Context) {
                        IsTag:        false,
                }
 
-               if err = releaseservice.CreateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil {
+               if err = releaseservice.CreateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs, msg); err != nil {
                        ctx.Data["Err_TagName"] = true
                        switch {
                        case models.IsErrReleaseAlreadyExist(err):
index ddfb6c676b6a7249cab66ac9146b99a4f43c7f6a..57628aa68dec6d3dc4f88db80042a5c519382b56 100644 (file)
@@ -69,7 +69,7 @@ func TestRelease_MirrorDelete(t *testing.T) {
                IsDraft:      false,
                IsPrerelease: false,
                IsTag:        true,
-       }, nil))
+       }, nil, ""))
 
        err = mirror.GetMirror()
        assert.NoError(t, err)
index fd821cc813a4d000becb758cdbbcea9bcbc0b84d..d04e538c92ab09e39e247ff27d6746ee1e2cc7cf 100644 (file)
@@ -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)
index f50fca71ec0647c9429f895aa9cf82614ec597a3..deb0618832bab9b3fc31f5271b53285a05ade989 100644 (file)
@@ -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"))
+}
index 5dd0553d480f7397763783dcf1bcdb757ac9f0b5..ca805fa5877d6ad9cb3db51a339fc3c6856060b0 100644 (file)
                        <div class="header branch-tag-choice">
                                <div class="ui grid">
                                        <div class="two column row">
-                                               <a class="reference column" href="#" @click="mode = 'branches'; focusSearchField()">
+                                               <a class="reference column" href="#" @click="createTag = false; mode = 'branches'; focusSearchField()">
                                                        <span class="text" :class="{black: mode == 'branches'}">
                                                                {{svg "octicon-git-branch" 16 "mr-2"}}{{.i18n.Tr "repo.branches"}}
                                                        </span>
                                                </a>
-                                               <a class="reference column" href="#" @click="mode = 'tags'; focusSearchField()">
+                                               <a class="reference column" href="#" @click="createTag = true; mode = 'tags'; focusSearchField()">
                                                        <span class="text" :class="{black: mode == 'tags'}">
                                                                {{svg "octicon-tag" 16 "mr-2"}}{{.i18n.Tr "repo.tags"}}
                                                        </span>
                                <div v-for="(item, index) in filteredItems" :key="item.name" class="item" :class="{selected: item.selected, active: active == index}" @click="selectItem(item)" :ref="'listItem' + index">${ item.name }</div>
                                <div class="item" v-if="showCreateNewBranch" :class="{active: active == filteredItems.length}" :ref="'listItem' + filteredItems.length">
                                        <a href="#" @click="createNewBranch()">
-                                               <div>
+                                               <div v-show="createTag">
+                                                       <i class="reference tags icon"></i>
+                                                       {{.i18n.Tr "repo.tag.create_tag" `${ searchTerm }` | Safe}}
+                                               </div>
+                                               <div v-show="!createTag">
                                                        {{svg "octicon-git-branch"}}
                                                        {{.i18n.Tr "repo.branch.create_branch" `${ searchTerm }` | Safe}}
                                                </div>
@@ -56,6 +60,7 @@
                                        <form ref="newBranchForm" action="{{.RepoLink}}/branches/_new/{{EscapePound .BranchNameSubURL}}" method="post">
                                                {{.CsrfTokenHtml}}
                                                <input type="hidden" name="new_branch_name" v-model="searchTerm">
+                                               <input type="hidden" name="create_tag" v-model="createTag">
                                        </form>
                                </div>
                        </div>
index 473257a2107b800d93245921dab04c72b4e0f5f9..ef9115d1e235ca15b771722d3806d290b7ac1ab5 100644 (file)
                        <div class="ui container">
                                <div class="ui divider"></div>
                                <div class="ui text right">
+                                       {{if not .PageIsEditRelease}}
+                                               <div class="tag-message field">
+                                                       <div class="ui checkbox">
+                                                               <input type="checkbox" name="add_tag_msg">
+                                                               <label><strong>{{.i18n.Tr "repo.release.add_tag_msg"}}</strong></label>
+                                                       </div>
+                                               </div>
+                                       {{else}}
+                                               <input type="hidden" name="add_tag_msg" value="false">
+                                       {{end}}
                                        <div class="prerelease field">
                                                <div class="ui checkbox">
                                                        <input type="checkbox" name="prerelease" {{if .prerelease}}checked{{end}}>
                                                                </button>
                                                        {{end}}
                                                {{else}}
+                                                       {{if not .tag_name}}
+                                                               <input class="ui grey button" type="submit" name="tag_only" value="{{.i18n.Tr "repo.release.add_tag"}}"/>
+                                                       {{end}}
                                                        <input class="ui button" type="submit" name="draft" value="{{.i18n.Tr "repo.release.save_draft"}}"/>
                                                        <button class="ui primary button">
                                                                {{.i18n.Tr "repo.release.publish"}}
index 7df170b930c52762201808e5d18efbcd5644831c..cda08870a5f728b5359f0a8668b1a8ea903ab78f 100644 (file)
@@ -3310,6 +3310,7 @@ function initFilterBranchTagDropdown(selector) {
       noResults: '',
       canCreateBranch: false,
       menuVisible: false,
+      createTag: false,
       active: 0
     };
     $data.find('.item').each(function () {
@@ -3341,7 +3342,7 @@ function initFilterBranchTagDropdown(selector) {
           return this.filteredItems.length === 0 && !this.showCreateNewBranch;
         },
         showCreateNewBranch() {
-          if (!this.canCreateBranch || !this.searchTerm || this.mode === 'tags') {
+          if (!this.canCreateBranch || !this.searchTerm) {
             return false;
           }