Skip to content

Commit 57aef6d

Browse files
committed
fix tag-only button logic
1 parent f68bee5 commit 57aef6d

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

routers/web/repo/release.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ func NewRelease(ctx *context.Context) {
360360
return
361361
}
362362

363+
ctx.Data["ShowCreateTagOnlyButton"] = true
364+
363365
// pre-fill the form with the tag name, target branch and the existing release (if exists)
364366
ctx.Data["tag_target"] = ctx.Repo.Repository.DefaultBranch
365367
if tagName := ctx.FormString("tag"); tagName != "" {
@@ -376,7 +378,7 @@ func NewRelease(ctx *context.Context) {
376378
return
377379
}
378380

379-
ctx.Data["TagNameReleaseExists"] = true
381+
ctx.Data["ShowCreateTagOnlyButton"] = false
380382
ctx.Data["tag_name"] = rel.TagName
381383
ctx.Data["tag_target"] = rel.Target
382384
ctx.Data["title"] = rel.Title
@@ -397,14 +399,17 @@ func NewReleasePost(ctx *context.Context) {
397399

398400
form := web.GetForm(ctx).(*forms.NewReleaseForm)
399401

400-
// first, check whether the release exists,
401-
// it should be done before the form error check, because the tmpl needs "TagNameReleaseExists" to show/hide the "tag only" button
402+
// first, check whether the release exists, and prepare "ShowCreateTagOnlyButton"
403+
// the logic should be done before the form error check to make the tmpl has correct variables
402404
rel, err := repo_model.GetRelease(ctx, ctx.Repo.Repository.ID, form.TagName)
403405
if err != nil && !repo_model.IsErrReleaseNotExist(err) {
404406
ctx.ServerError("GetRelease", err)
405407
return
406408
}
407-
ctx.Data["TagNameReleaseExists"] = rel != nil
409+
410+
// we should still show the "tag only" button if the user clicks it, no matter the release exists or not.
411+
// because if error occurs, end users need to have the chance to edit the name and submit the form with "tag-only" again.
412+
ctx.Data["ShowCreateTagOnlyButton"] = form.TagOnly || rel == nil
408413

409414
// do some form checks
410415
if ctx.HasError() {

routers/web/repo/release_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ func TestNewReleasePost(t *testing.T) {
3434

3535
t.Run("NewReleasePage", func(t *testing.T) {
3636
ctx := get(t, "v1.1")
37-
assert.NotEmpty(t, ctx.Data["TagNameReleaseExists"])
37+
assert.Empty(t, ctx.Data["ShowCreateTagOnlyButton"])
3838
ctx = get(t, "new-tag-name")
39-
assert.Empty(t, ctx.Data["TagNameReleaseExists"])
39+
assert.NotEmpty(t, ctx.Data["ShowCreateTagOnlyButton"])
4040
})
4141

4242
post := func(t *testing.T, form forms.NewReleaseForm) *context.Context {

templates/repo/release/new.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
<button class="ui small primary button">{{ctx.Locale.Tr "repo.release.edit_release"}}</button>
116116
{{end}}
117117
{{else}}
118-
{{if not .TagNameReleaseExists}}
118+
{{if .ShowCreateTagOnlyButton}}
119119
<button class="ui small button" name="tag_only" value="1">{{ctx.Locale.Tr "repo.release.add_tag"}}</button>
120120
{{end}}
121121
<button class="ui small button" name="draft" value="1">{{ctx.Locale.Tr "repo.release.save_draft"}}</button>

0 commit comments

Comments
 (0)