-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Conversation
Wouldn't matter for |
Hmm, yes, since there are already many
🤣 |
Yeah it's no problem to keep using |
func ParseTemplatesFromDefaultBranch(repo *repo.Repository, gitRepo *git.Repository) (ret struct { | ||
IssueTemplates []*api.IssueTemplate | ||
TemplateErrors map[string]error | ||
}, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)) |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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")) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
I was unable to create a backport for 1.21. @wxiaoguang, please send one manually. 🍵
|
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
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`
* 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) ...
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`
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`
Automatically locked because of our CONTRIBUTING guidelines |
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 functionGetIssueTemplates
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 incheckDeprecatedAuthMethods
, because non-standard HTTP headers should haveX-
prefix, and it is also consistent with the new code inGetIssueTemplates