Repository: go-gitea/gitea
File: routers/web/repo/release.go (Go)

Code before:
			ctx.Data["title"] = rel.Title
			ctx.Data["content"] = rel.Note
			ctx.Data["attachments"] = rel.Attachments
		}
	}

	ctx.HTML(http.StatusOK, tplReleaseNew)
}

// NewReleasePost response for creating a release
func NewReleasePost(ctx *context.Context) {
	newReleaseCommon(ctx)
	if ctx.Written() {
		return
	}

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

	// first, check whether the release exists,
	// it should be done before the form error check, because the tmpl needs "TagNameReleaseExists" to show/hide the "tag only" button
	rel, err := repo_model.GetRelease(ctx, ctx.Repo.Repository.ID, form.TagName)
	if err != nil && !repo_model.IsErrReleaseNotExist(err) {
		ctx.ServerError("GetRelease", err)
		return
	}
	ctx.Data["TagNameReleaseExists"] = rel != nil

	// do some form checks
	if ctx.HasError() {
		ctx.HTML(http.StatusOK, tplReleaseNew)
		return
	}

	if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
		ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), tplReleaseNew, &form)
		return
	}

	if !form.TagOnly && form.Title == "" {
		// if not "tag only", then the title of the release cannot be empty
		ctx.RenderWithErr(ctx.Tr("repo.release.title_empty"), tplReleaseNew, &form)
		return
	}

	handleTagReleaseError := func(err error) {
		ctx.Data["Err_TagName"] = true
		switch {
		case release_service.IsErrTagAlreadyExists(err):
			ctx.RenderWithErr(ctx.Tr("repo.branch.tag_collision", form.TagName), tplReleaseNew, &form)
		case repo_model.IsErrReleaseAlreadyExist(err):
			ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), tplReleaseNew, &form)

Review comment (refactor):
Big fan of the test refactor. Everything looks good to me, the only problem that I see now is the "Create Tag Only" problem we discussed earlier:

<img width="1296" alt="Screenshot 2025-01-13 at 10 43 07 AM" src="https://github.com/user-attachments/assets/20b3f67c-8943-479a-9d6a-42ae29469b69" />


To reproduce:
- Have an existing tag e.g. "v1.0"
- Try creating a tag with the same name


It looks like this happens because the "error form" page we return now has a `tag_name` that points to an existing `Release`, which would set the highlighted line to `true`.

Would it make sense to set this to something like:
```suggestion
			ctx.Data["TagNameReleaseExists"] = rel != nil && !rel.IsTag
```


Code after:
			ctx.Data["tag_name"] = rel.TagName
			ctx.Data["tag_target"] = rel.Target
			ctx.Data["title"] = rel.Title
			ctx.Data["content"] = rel.Note
			ctx.Data["attachments"] = rel.Attachments
		}
	}

	ctx.HTML(http.StatusOK, tplReleaseNew)
}

// NewReleasePost response for creating a release
func NewReleasePost(ctx *context.Context) {
	newReleaseCommon(ctx)
	if ctx.Written() {
		return
	}

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

	// first, check whether the release exists, and prepare "ShowCreateTagOnlyButton"
	// the logic should be done before the form error check to make the tmpl has correct variables
	rel, err := repo_model.GetRelease(ctx, ctx.Repo.Repository.ID, form.TagName)
	if err != nil && !repo_model.IsErrReleaseNotExist(err) {
		ctx.ServerError("GetRelease", err)
		return
	}

	// We should still show the "tag only" button if the user clicks it, no matter the release exists or not.
	// Because if error occurs, end users need to have the chance to edit the name and submit the form with "tag-only" again.
	// It is still not completely right, because there could still be cases like this:
	// * user visit "new release" page, see the "tag only" button
	// * input something, click other buttons but not "tag only"
	// * error occurs, the "new release" page is rendered again, but the "tag only" button is gone
	// Such cases are not able to be handled by current code, it needs frontend code to toggle the "tag-only" button if the input changes.
	// Or another choice is "always show the tag-only button" if error occurs.
	ctx.Data["ShowCreateTagOnlyButton"] = form.TagOnly || rel == nil

	// do some form checks
	if ctx.HasError() {
		ctx.HTML(http.StatusOK, tplReleaseNew)
		return
	}

	if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
		ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), tplReleaseNew, &form)
		return
	}

	if !form.TagOnly && form.Title == "" {
		// if not "tag only", then the title of the release cannot be empty