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

Refactor issue template parsing and fix API endpoint #29069

Merged
merged 3 commits into from
Feb 12, 2024

Conversation

wxiaoguang
Copy link
Contributor

@wxiaoguang wxiaoguang commented Feb 6, 2024

The old code GetTemplatesFromDefaultBranch(...) ([]*api.IssueTemplate, map[string]error) doesn't really follow Golang's habits, then the second returned value might be misused. For example, the API function GetIssueTemplates incorrectly checked the second returned value and always responds 500 error.

This PR refactors GetTemplatesFromDefaultBranch to ParseTemplatesFromDefaultBranch and clarify its behavior, and fix the API endpoint bug, and add some tests.

And by the way, add proper prefix X- for the header generated in checkDeprecatedAuthMethods, because non-standard HTTP headers should have X- prefix, and it is also consistent with the new code in GetIssueTemplates

@wxiaoguang wxiaoguang added type/bug type/refactoring Existing code has been cleaned up. There should be no new functionality. backport/v1.21 This PR should be backported to Gitea 1.21 labels Feb 6, 2024
@wxiaoguang wxiaoguang added this to the 1.22.0 milestone Feb 6, 2024
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Feb 6, 2024
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Feb 6, 2024
@github-actions github-actions bot added the modifies/api This PR adds API routes or modifies them label Feb 6, 2024
@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 Feb 8, 2024
@silverwind
Copy link
Member

silverwind commented Feb 8, 2024

non-standard HTTP headers should have X- prefix

X- headers are deprecated sind 2012 as per MDN:

Custom proprietary headers have historically been used with an X- prefix, but this convention was deprecated in June 2012 because of the inconveniences it caused when nonstandard fields became standard in RFC 6648; others are listed in an IANA registry, whose original content was defined in RFC 4229. IANA also maintains a registry of proposed new HTTP headers.

Wouldn't matter for X-Gitea-Warning at least, because it's not aiming to become a web standard.

@wxiaoguang
Copy link
Contributor Author

non-standard HTTP headers should have X- prefix

X- headers are deprecated sind 2012 as per MDN:
Wouldn't matter for X-Gitea-Warning at least, because it's not aiming to become a web standard.

Hmm, yes, since there are already many X- headers, I guess it's fine to follow it:

  • X-Gitea-OTP
  • X-Gitea-Object-Type
  • X-Total-Count
  • X-Content-Type-Options
  • X-Csrf-Token
  • X-Page
  • ....

🤣

@silverwind
Copy link
Member

silverwind commented Feb 8, 2024

Yeah it's no problem to keep using X- and some of these headers will never be changed. It's just forbidden for new "standard" headers to be introduced with X-, but I guess we can make an exception for exiting X-Gitea for the sake of consistency.

Comment on lines +114 to +117
func ParseTemplatesFromDefaultBranch(repo *repo.Repository, gitRepo *git.Repository) (ret struct {
IssueTemplates []*api.IssueTemplate
TemplateErrors map[string]error
},
Copy link
Member

Choose a reason for hiding this comment

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

I think I'd rather define a concrete struct type above, even if not exported.
I don't love named returns, much less an anonymous named return.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I'd rather define a concrete struct type above, even if not exported.

That's impossible to pass the lint, it violates "do not export private names"

I don't love named returns, much less an anonymous named return.

But I do love this usage. Anonymous struct is perfect use case for such function.

Copy link
Member

Choose a reason for hiding this comment

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

This is nearly the same thing, I'd rather nolint this case than use an anonymous struct, personally.

Copy link
Contributor Author

@wxiaoguang wxiaoguang Feb 9, 2024

Choose a reason for hiding this comment

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

Sorry but I didn't get the point why "anonymous struct" is bad, we use it a lot.

The code is readable enough and there should be no maintainability problem, and no one needs to "grep" the name.


If you have better ideas, feel free to edit this PR directly.

return
ret := issue.ParseTemplatesFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo)
if cnt := len(ret.TemplateErrors); cnt != 0 {
ctx.Resp.Header().Add("X-Gitea-Warning", "error occurs when parsing issue template: count="+strconv.Itoa(cnt))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ctx.Resp.Header().Add("X-Gitea-Warning", "error occurs when parsing issue template: count="+strconv.Itoa(cnt))
ctx.Resp.Header().Add("X-Gitea-Warning", fmt.Sprintf("errors occurred when parsing issue templates: %v", ret.TemplateErrors))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think there is no end user really needs this error message, so I'd like to keep the message simple, and to avoid putting anything uncontrolled into the header.

So I'd like to keep the old code, if there is any user really needs the detailed error message, I will propose a complete solution in the future.


// ParseTemplatesFromDefaultBranch parses the issue templates in the repo's default branch,
// returns valid templates and the errors of invalid template files (the errors map is guaranteed to be non-nil).
func ParseTemplatesFromDefaultBranch(repo *repo.Repository, gitRepo *git.Repository) (ret struct {
Copy link
Member

Choose a reason for hiding this comment

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

Could we perhaps not define an inline return struct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See: #29069 (comment)

Anonymous struct is perfect use case for such function, I prefer to use this syntax here.

But if you have better ideas, feel free to edit this PR directly.

DecodeJSON(t, resp, &issueTemplates)
assert.Len(t, issueTemplates, 1)
assert.Equal(t, "foo", issueTemplates[0].Name)
assert.Equal(t, "error occurs when parsing issue template: count=2", resp.Header().Get("X-Gitea-Warning"))
Copy link
Member

Choose a reason for hiding this comment

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

That line should be fixed as per my suggestion above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #29069 (comment)

I'd like to keep things simple at the moment, until someone really needs it, then it needs a complete solution for "warning" messages.

@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 Feb 9, 2024
@wxiaoguang wxiaoguang enabled auto-merge (squash) February 12, 2024 04:52
@wxiaoguang wxiaoguang merged commit ee242a0 into go-gitea:main Feb 12, 2024
26 checks passed
@GiteaBot
Copy link
Contributor

I was unable to create a backport for 1.21. @wxiaoguang, please send one manually. 🍵

go run ./contrib/backport 29069
...  // fix git conflicts if any
go run ./contrib/backport --continue

@GiteaBot GiteaBot added the backport/manual No power to the bots! Create your backport yourself! label Feb 12, 2024
@wxiaoguang wxiaoguang deleted the refactor-issue-template branch February 12, 2024 05:09
wxiaoguang added a commit to wxiaoguang/gitea that referenced this pull request Feb 12, 2024
The old code `GetTemplatesFromDefaultBranch(...) ([]*api.IssueTemplate,
map[string]error)` doesn't really follow Golang's habits, then the
second returned value might be misused. For example, the API function
`GetIssueTemplates` incorrectly checked the second returned value and
always responds 500 error.

This PR refactors GetTemplatesFromDefaultBranch to
ParseTemplatesFromDefaultBranch and clarifies its behavior, and fixes the
API endpoint bug, and adds some tests.

And by the way, add proper prefix `X-` for the header generated in
`checkDeprecatedAuthMethods`, because non-standard HTTP headers should
have `X-` prefix, and it is also consistent with the new code in
`GetIssueTemplates`
# Conflicts:
#	routers/web/repo/issue.go
@wxiaoguang wxiaoguang added the backport/done All backports for this PR have been created label Feb 12, 2024
silverwind pushed a commit that referenced this pull request Feb 14, 2024
Backport #29069

The old code `GetTemplatesFromDefaultBranch(...) ([]*api.IssueTemplate,
map[string]error)` doesn't really follow Golang's habits, then the
second returned value might be misused. For example, the API function
`GetIssueTemplates` incorrectly checked the second returned value and
always responds 500 error.

This PR refactors GetTemplatesFromDefaultBranch to
ParseTemplatesFromDefaultBranch and clarifies its behavior, and fixes
the API endpoint bug, and adds some tests.

And by the way, add proper prefix `X-` for the header generated in
`checkDeprecatedAuthMethods`, because non-standard HTTP headers should
have `X-` prefix, and it is also consistent with the new code in
`GetIssueTemplates`
zjjhot added a commit to zjjhot/gitea that referenced this pull request Feb 14, 2024
* giteaofficial/main: (38 commits)
  Document how the TOC election process works (go-gitea#29135)
  Runner tokens are multi use (go-gitea#29153)
  Fix Gitpod logic of setting ROOT_URL (go-gitea#29162)
  Remove jQuery from the user search form in admin page (go-gitea#29151)
  Dont load Review if Comment is CommentTypeReviewRequest (go-gitea#28551)
  Show `View at this point in history` for every commit (go-gitea#29122)
  [skip ci] Updated translations via Crowdin
  Add merge style `fast-forward-only` (go-gitea#28954)
  Use Markdown alert syntax for notes in README (go-gitea#29150)
  Refactor issue template parsing and fix API endpoint (go-gitea#29069)
  [skip ci] Updated translations via Crowdin
  Update some translations and fix markdown formatting (go-gitea#29099)
  Show more settings for empty repositories (go-gitea#29130)
  Update JS and PY dependencies (go-gitea#29127)
  Add alert blocks in markdown (go-gitea#29121)
  Remove obsolete border-radius on comment content (go-gitea#29128)
  Make blockquote border size less aggressive (go-gitea#29124)
  Drop "@" from email sender to avoid spam filters (go-gitea#29109)
  [skip ci] Updated translations via Crowdin
  Disallow duplicate storage paths (go-gitea#26484)
  ...
silverwind pushed a commit to silverwind/gitea that referenced this pull request Feb 15, 2024
The old code `GetTemplatesFromDefaultBranch(...) ([]*api.IssueTemplate,
map[string]error)` doesn't really follow Golang's habits, then the
second returned value might be misused. For example, the API function
`GetIssueTemplates` incorrectly checked the second returned value and
always responds 500 error.

This PR refactors GetTemplatesFromDefaultBranch to
ParseTemplatesFromDefaultBranch and clarifies its behavior, and fixes the
API endpoint bug, and adds some tests.

And by the way, add proper prefix `X-` for the header generated in
`checkDeprecatedAuthMethods`, because non-standard HTTP headers should
have `X-` prefix, and it is also consistent with the new code in
`GetIssueTemplates`
silverwind pushed a commit to silverwind/gitea that referenced this pull request Feb 20, 2024
The old code `GetTemplatesFromDefaultBranch(...) ([]*api.IssueTemplate,
map[string]error)` doesn't really follow Golang's habits, then the
second returned value might be misused. For example, the API function
`GetIssueTemplates` incorrectly checked the second returned value and
always responds 500 error.

This PR refactors GetTemplatesFromDefaultBranch to
ParseTemplatesFromDefaultBranch and clarifies its behavior, and fixes the
API endpoint bug, and adds some tests.

And by the way, add proper prefix `X-` for the header generated in
`checkDeprecatedAuthMethods`, because non-standard HTTP headers should
have `X-` prefix, and it is also consistent with the new code in
`GetIssueTemplates`
Copy link

github-actions bot commented Mar 1, 2024

Automatically locked because of our CONTRIBUTING guidelines

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backport/done All backports for this PR have been created backport/manual No power to the bots! Create your backport yourself! backport/v1.21 This PR should be backported to Gitea 1.21 lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/api This PR adds API routes or modifies them size/L Denotes a PR that changes 100-499 lines, ignoring generated files. type/bug type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants