Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
* upstream/main:
  Replace a few fontawesome icons with svg (go-gitea#23602)
  Fix pagination on `/notifications/watching` (go-gitea#23564)
  Fix `.locale.Tr` function not found in delete modal (go-gitea#23468)
  fix submodule is nil panic (go-gitea#23588)
  `Publish Review` buttons should indicate why they are disabled (go-gitea#23598)
  Improve template error reporting (go-gitea#23396)
  Polyfill the window.customElements (go-gitea#23592)
  Add CHANGELOG for 1.19.0 (go-gitea#23583)
  • Loading branch information
zjjhot committed Mar 21, 2023
2 parents 025c6c0 + 34a2cf5 commit d4f3fc7
Show file tree
Hide file tree
Showing 16 changed files with 477 additions and 32 deletions.
350 changes: 350 additions & 0 deletions CHANGELOG.md

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (
"net/http"
"net/url"
"path"
"regexp"
"strconv"
"strings"
texttemplate "text/template"
"time"

"code.gitea.io/gitea/models/db"
Expand Down Expand Up @@ -213,6 +215,8 @@ func (ctx *Context) RedirectToFirst(location ...string) {
ctx.Redirect(setting.AppSubURL + "/")
}

var templateExecutingErr = regexp.MustCompile(`^template: (.*):([1-9][0-9]*):([1-9][0-9]*): executing (?:"(.*)" at <(.*)>: )?`)

// HTML calls Context.HTML and renders the template to HTTP response
func (ctx *Context) HTML(status int, name base.TplName) {
log.Debug("Template: %s", name)
Expand All @@ -228,6 +232,34 @@ func (ctx *Context) HTML(status int, name base.TplName) {
ctx.PlainText(http.StatusInternalServerError, "Unable to find status/500 template")
return
}
if execErr, ok := err.(texttemplate.ExecError); ok {
if groups := templateExecutingErr.FindStringSubmatch(err.Error()); len(groups) > 0 {
errorTemplateName, lineStr, posStr := groups[1], groups[2], groups[3]
target := ""
if len(groups) == 6 {
target = groups[5]
}
line, _ := strconv.Atoi(lineStr) // Cannot error out as groups[2] is [1-9][0-9]*
pos, _ := strconv.Atoi(posStr) // Cannot error out as groups[3] is [1-9][0-9]*
filename, filenameErr := templates.GetAssetFilename("templates/" + errorTemplateName + ".tmpl")
if filenameErr != nil {
filename = "(template) " + errorTemplateName
}
if errorTemplateName != string(name) {
filename += " (subtemplate of " + string(name) + ")"
}
err = fmt.Errorf("%w\nin template file %s:\n%s", err, filename, templates.GetLineFromTemplate(errorTemplateName, line, target, pos))
} else {
filename, filenameErr := templates.GetAssetFilename("templates/" + execErr.Name + ".tmpl")
if filenameErr != nil {
filename = "(template) " + execErr.Name
}
if execErr.Name != string(name) {
filename += " (subtemplate of " + string(name) + ")"
}
err = fmt.Errorf("%w\nin template file %s", err, filename)
}
}
ctx.ServerError("Render failed", err)
}
}
Expand Down
43 changes: 25 additions & 18 deletions modules/templates/htmlrenderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func handleGenericTemplateError(err error) (string, []interface{}) {

lineNumber, _ := strconv.Atoi(lineNumberStr)

line := getLineFromAsset(templateName, lineNumber, "")
line := GetLineFromTemplate(templateName, lineNumber, "", -1)

return "PANIC: Unable to compile templates!\n%s in template file %s at line %d:\n\n%s\nStacktrace:\n\n%s", []interface{}{message, filename, lineNumber, log.NewColoredValue(line, log.Reset), log.Stack(2)}
}
Expand All @@ -140,7 +140,7 @@ func handleNotDefinedPanicError(err error) (string, []interface{}) {

lineNumber, _ := strconv.Atoi(lineNumberStr)

line := getLineFromAsset(templateName, lineNumber, functionName)
line := GetLineFromTemplate(templateName, lineNumber, functionName, -1)

return "PANIC: Unable to compile templates!\nUndefined function %q in template file %s at line %d:\n\n%s", []interface{}{functionName, filename, lineNumber, log.NewColoredValue(line, log.Reset)}
}
Expand All @@ -161,7 +161,7 @@ func handleUnexpected(err error) (string, []interface{}) {

lineNumber, _ := strconv.Atoi(lineNumberStr)

line := getLineFromAsset(templateName, lineNumber, unexpected)
line := GetLineFromTemplate(templateName, lineNumber, unexpected, -1)

return "PANIC: Unable to compile templates!\nUnexpected %q in template file %s at line %d:\n\n%s", []interface{}{unexpected, filename, lineNumber, log.NewColoredValue(line, log.Reset)}
}
Expand All @@ -181,14 +181,15 @@ func handleExpectedEnd(err error) (string, []interface{}) {

lineNumber, _ := strconv.Atoi(lineNumberStr)

line := getLineFromAsset(templateName, lineNumber, unexpected)
line := GetLineFromTemplate(templateName, lineNumber, unexpected, -1)

return "PANIC: Unable to compile templates!\nMissing end with unexpected %q in template file %s at line %d:\n\n%s", []interface{}{unexpected, filename, lineNumber, log.NewColoredValue(line, log.Reset)}
}

const dashSeparator = "----------------------------------------------------------------------\n"

func getLineFromAsset(templateName string, targetLineNum int, target string) string {
// GetLineFromTemplate returns a line from a template with some context
func GetLineFromTemplate(templateName string, targetLineNum int, target string, position int) string {
bs, err := GetAsset("templates/" + templateName + ".tmpl")
if err != nil {
return fmt.Sprintf("(unable to read template file: %v)", err)
Expand Down Expand Up @@ -229,23 +230,29 @@ func getLineFromAsset(templateName string, targetLineNum int, target string) str
// If there is a provided target to look for in the line add a pointer to it
// e.g. ^^^^^^^
if target != "" {
idx := bytes.Index(lineBs, []byte(target))

if idx >= 0 {
// take the current line and replace preceding text with whitespace (except for tab)
for i := range lineBs[:idx] {
if lineBs[i] != '\t' {
lineBs[i] = ' '
}
targetPos := bytes.Index(lineBs, []byte(target))
if targetPos >= 0 {
position = targetPos
}
}
if position >= 0 {
// take the current line and replace preceding text with whitespace (except for tab)
for i := range lineBs[:position] {
if lineBs[i] != '\t' {
lineBs[i] = ' '
}
}

// write the preceding "space"
_, _ = sb.Write(lineBs[:idx])
// write the preceding "space"
_, _ = sb.Write(lineBs[:position])

// Now write the ^^ pointer
_, _ = sb.WriteString(strings.Repeat("^", len(target)))
_ = sb.WriteByte('\n')
// Now write the ^^ pointer
targetLen := len(target)
if targetLen == 0 {
targetLen = 1
}
_, _ = sb.WriteString(strings.Repeat("^", targetLen))
_ = sb.WriteByte('\n')
}

// Finally write the footer
Expand Down
2 changes: 2 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2265,7 +2265,9 @@ diff.review.header = Submit review
diff.review.placeholder = Review comment
diff.review.comment = Comment
diff.review.approve = Approve
diff.review.self_reject = Pull request authors can't request changes on their own pull request
diff.review.reject = Request changes
diff.review.self_approve = Pull request authors can't approve their own pull request
diff.committed_by = committed by
diff.protected = Protected
diff.image.side_by_side = Side by Side
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@mcaptcha/vanilla-glue": "0.1.0-alpha-3",
"@primer/octicons": "18.2.0",
"@vue/compiler-sfc": "3.2.47",
"@webcomponents/custom-elements": "1.5.1",
"add-asset-webpack-plugin": "2.0.1",
"ansi-to-html": "0.7.2",
"asciinema-player": "3.2.0",
Expand Down
5 changes: 4 additions & 1 deletion routers/web/user/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ func NotificationWatching(ctx *context.Context) {
page = 1
}

keyword := ctx.FormTrim("q")
ctx.Data["Keyword"] = keyword

var orderBy db.SearchOrderBy
ctx.Data["SortType"] = ctx.FormString("sort")
switch ctx.FormString("sort") {
Expand Down Expand Up @@ -378,7 +381,7 @@ func NotificationWatching(ctx *context.Context) {
Page: page,
},
Actor: ctx.Doer,
Keyword: ctx.FormTrim("q"),
Keyword: keyword,
OrderBy: orderBy,
Private: ctx.IsSigned,
WatchedByID: ctx.Doer.ID,
Expand Down
4 changes: 3 additions & 1 deletion services/repository/files/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
if err != nil {
return nil, err
}
contentsResponse.SubmoduleGitURL = &submodule.URL
if submodule != nil && submodule.URL != "" {
contentsResponse.SubmoduleGitURL = &submodule.URL
}
}
// Handle links
if entry.IsRegular() || entry.IsLink() {
Expand Down
22 changes: 20 additions & 2 deletions templates/admin/repo/unadopted.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@
<input type="hidden" name="action" value="adopt">
<input type="hidden" name="q" value="{{$.Keyword}}">
<input type="hidden" name="page" value="{{$.CurrentPage}}">
{{template "base/delete_modal_actions" .}}
<div class="actions">
<button class="ui red basic inverted cancel button">
{{svg "octicon-trash" 16 "gt-mr-2"}}
{{$.locale.Tr "modal.no"}}
</button>
<button class="ui green basic inverted ok button">
{{svg "octicon-check" 16 "gt-mr-2"}}
{{$.locale.Tr "modal.yes"}}
</button>
</div>
</form>
</div>
<button class="ui button submit tiny red delete show-modal" data-modal="#delete-unadopted-modal-{{$dirI}}"><span class="icon">{{svg "octicon-x"}}</span><span class="label">{{$.locale.Tr "repo.delete_preexisting_label"}}</span></button>
Expand All @@ -61,7 +70,16 @@
<input type="hidden" name="action" value="delete">
<input type="hidden" name="q" value="{{$.Keyword}}">
<input type="hidden" name="page" value="{{$.CurrentPage}}">
{{template "base/delete_modal_actions" .}}
<div class="actions">
<button class="ui red basic inverted cancel button">
{{svg "octicon-trash" 16 "gt-mr-2"}}
{{$.locale.Tr "modal.no"}}
</button>
<button class="ui green basic inverted ok button">
{{svg "octicon-check" 16 "gt-mr-2"}}
{{$.locale.Tr "modal.yes"}}
</button>
</div>
</form>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/org/team/repositories.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@

<div class="ui small basic addall modal">
<div class="ui icon header">
<i class="globe icon"></i>
{{svg "octicon-globe"}}
{{.locale.Tr "org.teams.add_all_repos_title"}}
</div>
<div class="content">
Expand Down
17 changes: 15 additions & 2 deletions templates/repo/diff/new_review.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,22 @@
</div>
{{end}}
<div class="ui divider"></div>
<button type="submit" name="type" value="approve" {{if and $.IsSigned ($.Issue.IsPoster $.SignedUser.ID)}} disabled {{end}} class="ui submit green tiny button btn-submit">{{$.locale.Tr "repo.diff.review.approve"}}</button>
{{$showSelfTooltip := (and $.IsSigned ($.Issue.IsPoster $.SignedUser.ID))}}
{{if $showSelfTooltip}}
<span class="gt-dib tooltip" data-content="{{$.locale.Tr "repo.diff.review.self_approve"}}">
<button type="submit" name="type" value="approve" disabled class="ui submit green tiny button btn-submit">{{$.locale.Tr "repo.diff.review.approve"}}</button>
</span>
{{else}}
<button type="submit" name="type" value="approve" class="ui submit green tiny button btn-submit">{{$.locale.Tr "repo.diff.review.approve"}}</button>
{{end}}
<button type="submit" name="type" value="comment" class="ui submit tiny basic button btn-submit">{{$.locale.Tr "repo.diff.review.comment"}}</button>
<button type="submit" name="type" value="reject" {{if and $.IsSigned ($.Issue.IsPoster $.SignedUser.ID)}} disabled {{end}} class="ui submit red tiny button btn-submit">{{$.locale.Tr "repo.diff.review.reject"}}</button>
{{if $showSelfTooltip}}
<span class="gt-dib tooltip" data-content="{{$.locale.Tr "repo.diff.review.self_reject"}}">
<button type="submit" name="type" value="reject" disabled class="ui submit red tiny button btn-submit">{{$.locale.Tr "repo.diff.review.reject"}}</button>
</span>
{{else}}
<button type="submit" name="type" value="reject" class="ui submit red tiny button btn-submit">{{$.locale.Tr "repo.diff.review.reject"}}</button>
{{end}}
</form>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions templates/repo/editor/edit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@

<div class="ui small basic modal" id="edit-empty-content-modal">
<div class="ui icon header">
<i class="file icon"></i>
{{svg "octicon-file"}}
{{.locale.Tr "repo.editor.commit_empty_file_header"}}
</div>
<div class="center content">
<p>{{.locale.Tr "repo.editor.commit_empty_file_text"}}</p>
</div>
<div class="actions">
<button class="ui red basic cancel inverted button">
<i class="remove icon"></i>
{{svg "octicon-x"}}
{{.locale.Tr "repo.editor.cancel"}}
</button>
<button class="ui green basic ok inverted button">
<i class="save icon"></i>
{{svg "fontawesome-save"}}
{{.locale.Tr "repo.editor.commit_changes"}}
</button>
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/editor/patch.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<div class="ui small basic modal" id="edit-empty-content-modal">
<div class="ui icon header">
<i class="file icon"></i>
{{svg "octicon-file"}}
{{.locale.Tr "repo.editor.commit_empty_file_header"}}
</div>
<div class="center content">
Expand Down
2 changes: 1 addition & 1 deletion templates/user/auth/webauthn.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{{.locale.Tr "twofa"}}
</h3>
<div class="ui attached segment">
<i class="huge key icon"></i>
{{svg "octicon-key" 56}}
<h3>{{.locale.Tr "webauthn_insert_key"}}</h3>
{{template "base/alert" .}}
<p>{{.locale.Tr "webauthn_sign_in"}}</p>
Expand Down
13 changes: 11 additions & 2 deletions templates/user/settings/repos.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@
{{$.CsrfTokenHtml}}
<input type="hidden" name="id" value="{{$dir}}">
<input type="hidden" name="action" value="adopt">
{{template "base/delete_modal_actions" .}}
<div class="actions">
<button class="ui red basic inverted cancel button">
{{svg "octicon-x"}}
{{$.locale.Tr "modal.no"}}
</button>
<button class="ui green basic inverted ok button">
{{svg "octicon-check"}}
{{$.locale.Tr "modal.yes"}}
</button>
</div>
</form>
</div>
{{end}}
Expand All @@ -68,7 +77,7 @@
{{$.CsrfTokenHtml}}
<input type="hidden" name="id" value="{{$dir}}">
<input type="hidden" name="action" value="delete">
{{template "base/delete_modal_actions" .}}
{{template "base/delete_modal_actions" $}}
</form>
</div>
{{end}}
Expand Down
2 changes: 2 additions & 0 deletions web_src/js/webcomponents/GiteaOriginUrl.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import '@webcomponents/custom-elements'; // automatically adds custom elements for older browsers that don't support it

// this is a Gitea's private HTML component, it converts an absolute or relative URL to an absolute URL with the current origin
window.customElements.define('gitea-origin-url', class extends HTMLElement {
connectedCallback() {
Expand Down

0 comments on commit d4f3fc7

Please sign in to comment.