Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate that the tag doesn't exist when creating a tag via the web #33241

Merged
merged 17 commits into from
Jan 14, 2025

Conversation

kemzeb
Copy link
Contributor

@kemzeb kemzeb commented Jan 13, 2025

Found while investigating #33210.

This line no longer makes sense because the form field "TagName" is required, so this would mean that this code path would never be covered. Because it isn't covered, we end up going down the "update release" logic where we eventually set Release.IsTag to false (meaning it will now be treated as a release instead of a tag).

This snapshot rewrites the condition to ensure that we aren't trying to create a tag that already exists.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jan 13, 2025
@pull-request-size pull-request-size bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Jan 13, 2025
@github-actions github-actions bot added the modifies/go Pull requests that update Go code label Jan 13, 2025
@kemzeb
Copy link
Contributor Author

kemzeb commented Jan 13, 2025

To reproduce the bug, you can do the following:

  • Create a tag using the Web UI
  • Create the tag again using the Web UI
  • The tag will now be considered a release

@wxiaoguang
Copy link
Contributor

To reproduce the bug, you can do the following:

* Create a tag using the Web UI

* Create the tag again using the Web UI

* The tag will now be considered a release

Is it possible to write a test for this case?

routers/web/repo/release.go Outdated Show resolved Hide resolved
Copy link
Contributor

@wxiaoguang wxiaoguang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "type" magic could be left to the future ....... (to make it easier to backport)

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jan 13, 2025
@wxiaoguang wxiaoguang added the backport/v1.23 This PR should be backported to Gitea 1.23 label Jan 13, 2025
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Jan 13, 2025
@wxiaoguang
Copy link
Contributor

2 approvals now, would you like to merge it as-is, or there are still changes to do?

@kemzeb
Copy link
Contributor Author

kemzeb commented Jan 13, 2025

Implementing test and changes 👍

@pull-request-size pull-request-size bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Jan 13, 2025
@pull-request-size pull-request-size bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jan 13, 2025
@wxiaoguang
Copy link
Contributor

Added your finding as comment in 7a5595c

@kemzeb
Copy link
Contributor Author

kemzeb commented Jan 13, 2025

Screenshot 2025-01-13 at 12 57 29 AM

Hmm just realized the Create Tag Only button does not appear when the validation error occurs.

But it also disappears with other form validation errors (while running my dev instance on the main branch):
Screenshot 2025-01-13 at 1 00 43 AM

It looks like it's a combination of:

  • Passing the tag_name form field to the page that is sent back to the client
  • This line in the template file

@wxiaoguang
Copy link
Contributor

If we need to handle the "error form", I think we can use "form-fetch-action", then we do not need to reload the page or re-fill the form, then the logic could be simpler?

@wxiaoguang

This comment was marked as outdated.

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Jan 13, 2025

{{if not .tag_name}} was added by "Create tag on ui (#13467)", at that time, the {{if .PageIsEditRelease}} check doesn't control the "tag only" button, so it needs to hide the "tag only" button for "edit tag" (non-empty tag_name means "edit")

But now, the "tag only" button is in {{if .PageIsEditRelease}}{{else}} tag only {{end}}, so it doesn't need to be controlled by "tag_name" anymore.

@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jan 13, 2025
@github-actions github-actions bot added the modifies/templates This PR modifies the template files label Jan 13, 2025
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Jan 13, 2025

By reading the code again, I found there are too many legacy bugs and I think it's worth to rewrite it. Major changes:

  1. always prepare tmpl data correctly (even if error occurs)
  2. correctly show/hide the "tag only" button (it is only shown for "new" page when there is no existing release)
  3. correctly handle create/update errors and page render

Maybe it's not easy to backport, if we need the backport, we could take the simple fix before the large change ....... hmm, simple fix seems not quite right.

@wxiaoguang
Copy link
Contributor

Done from my side, added some tests and comments, does the new logic look good to you?

return
}
ctx.Data["Tags"] = tags
ctx.Data["TagNameReleaseExists"] = rel != nil
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Screenshot 2025-01-13 at 10 43 07 AM

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:

Suggested change
ctx.Data["TagNameReleaseExists"] = rel != nil
ctx.Data["TagNameReleaseExists"] = rel != nil && !rel.IsTag

Copy link
Contributor

@wxiaoguang wxiaoguang Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think the problem is: we shouldn't stop users from creating new "tag-only" if the error occurs, no matter whether the release exists. So I think this line should be completely removed.

Copy link
Contributor

@wxiaoguang wxiaoguang Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made some changes in 57aef6d

// 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.
ctx.Data["ShowCreateTagOnlyButton"] = form.TagOnly || rel == nil

Does it sound right?

Copy link
Contributor Author

@kemzeb kemzeb Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it sounds about right

Hmm, I think the problem is: we shouldn't stop users from creating new "tag-only" if the error occurs, no matter whether the release exists. So I think this line should be completely removed.

Hmm it looks like the only case where we do hide it is when a tag_name field is passed which refers to an existing tag (I guess we hide it to avoid any confusion by users). I think it is fine to follow your alternative approach and have the button always appear since the user would be shown an error in case they do press it, but it's up to you

@wxiaoguang wxiaoguang merged commit ecd463c into go-gitea:main Jan 14, 2025
26 checks passed
@GiteaBot GiteaBot added this to the 1.24.0 milestone Jan 14, 2025
@wxiaoguang
Copy link
Contributor

Not easy to backport and the changes are quite a lot.

Maybe we only keep this fix in 1.24? (If there is a strong requirement to backport .... I could try to manually backport .....)

@wxiaoguang wxiaoguang removed the backport/v1.23 This PR should be backported to Gitea 1.23 label Jan 14, 2025
zjjhot added a commit to zjjhot/gitea that referenced this pull request Jan 14, 2025
* giteaofficial/main: (21 commits)
  Support public code/issue access for private repositories (go-gitea#33127)
  Validate that the tag doesn't exist when creating a tag via the web (go-gitea#33241)
  [skip ci] Updated translations via Crowdin
  Switch back to `vue-tsc` (go-gitea#33248)
  Let API create and edit system webhooks, attempt 2 (go-gitea#33180)
  Fix incorrect ref "blob" (go-gitea#33240)
  Refactor RefName (go-gitea#33234)
  Refactor context RefName and RepoAssignment (go-gitea#33226)
  [skip ci] Updated translations via Crowdin
  Fix upload file form (go-gitea#33230)
  Fix mirror bug (go-gitea#33224)
  Remove unused CSS styles and move some styles to proper files (go-gitea#33217)
  Refactor context repository (go-gitea#33202)
  [skip ci] Updated translations via Crowdin
  Fix unpin hint on the pinned pull requests (go-gitea#33207)
  fix(cache): cache test triggered by non memory cache (go-gitea#33220)
  Update README.md (go-gitea#33149)
  Fix editor markdown not incrementing in a numbered list (go-gitea#33187)
  Some small refactors (go-gitea#33144)
  Fix sync fork for consistency (go-gitea#33147)
  ...
@kemzeb kemzeb deleted the fix/ui/release-post-with-tag-only branch January 16, 2025 02:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/go Pull requests that update Go code modifies/templates This PR modifies the template files size/L Denotes a PR that changes 100-499 lines, ignoring generated files. type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants