mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-29 09:31:11 -05:00
6e64f9db8e
* Initial ui components for pull request review * Add Review Add IssueComment types Signed-off-by: Jonas Franz <info@jonasfranz.software> (cherry picked from commit 2b4daab) Signed-off-by: Jonas Franz <info@jonasfranz.software> * Replace ReviewComment with Content Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add load functions Add ReviewID to findComments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add create review comment implementation Add migration for review Other small changes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Simplified create and find functions for review Signed-off-by: Jonas Franz <info@jonasfranz.software> * Moved "Pending" to first position Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add GetCurrentReview to simplify fetching current review Signed-off-by: Jonas Franz <info@jonasfranz.software> * Preview for listing comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Move new comment form to its own file Signed-off-by: Jonas Franz <info@jonasfranz.software> * Implement Review form Show Review comments on comment stream Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for single comments Showing buttons in context Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add pending tag to pending review comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add unit tests for Review Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fetch all review ids at once Add unit tests Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Improved comment rendering in "Files" view by adding Comments to DiffLine Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for invalidating comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Switched back to code.gitea.io/git Signed-off-by: Jonas Franz <info@jonasfranz.software> * Moved review migration from v64 to v65 Signed-off-by: Jonas Franz <info@jonasfranz.software> * Rebuild css Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Improve translations Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests by updating fixtures and updating outdated test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Comments will be shown at the right place now Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for deleting CodeComments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix problems caused by files in subdirectories Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for showing code comments of reviews in conversation Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for "Show/Hide outdated" Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update code.gitea.io/git Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add support for new webhooks Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update comparison Signed-off-by: Jonas Franz <info@jonasfranz.software> * Resolve conflicts Signed-off-by: Jonas Franz <info@jonasfranz.software> * Minor UI improvements * update code.gitea.io/git * Fix ui bug reported by @lunny causing wrong position of add button Add functionality to "Cancel" button Add scale effects to add button Hide "Cancel" button for existing comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Prepare solving conflicts Signed-off-by: Jonas Franz <info@jonasfranz.software> * Show add button only if no comments already exist for the line Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing vendor files Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if reviewer is nil Signed-off-by: Jonas Franz <info@jonasfranz.software> * Show forms only to users who are logged in Signed-off-by: Jonas Franz <info@jonasfranz.software> * Revert "Show forms only to users who are logged in" This reverts commit c083682 Signed-off-by: Jonas Franz <info@jonasfranz.software> * Save patch in comment Render patch for code comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add link to comment in code Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add reply form to comment list Show forms only to signed in users Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add 'Reply' as translatable Add CODE_COMMENT_LINES setting Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix problems introduced by checking for singed in user Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add v70 Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update generated stylesheet Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix preview Beginn with new review comment patch system Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add new algo to generate diff for line range Remove old algo used for cutting big diffs (it was very buggy) * Add documentation and example for CutDiffAroundLine * Fix example of CutDiffAroundLine * Fix some comment UI rendering bugs * Add code comment edit mode * Send notifications / actions to users until review gets published Fix diff generation bug Fix wrong hashtag * Fix vet errors * Send notifications also for single comments * Fix some notification bugs, fix link * Fix: add comment icon is only shown on code lines * Add lint comment * Add unit tests for git diff * Add more error messages * Regenerated css Signed-off-by: Jonas Franz <info@jonasfranz.software> * fmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Regenerated CSS with latest less version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by updating comment type to new ID Signed-off-by: Jonas Franz <info@jonasfranz.software> * Introducing CodeComments as type for map[string]map[int64][]*Comment Other minor code improvements Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix data-tab issues Signed-off-by: Jonas Franz <info@jonasfranz.software> * Remove unnecessary change Signed-off-by: Jonas Franz <info@jonasfranz.software> * refactored checkForInvalidation Signed-off-by: Jonas Franz <info@jonasfranz.software> * Append comments instead of setting Signed-off-by: Jonas Franz <info@jonasfranz.software> * Use HeadRepo instead of BaseRepo Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update migration Signed-off-by: Jonas Franz <info@jonasfranz.de> * Regenerated CSS Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add copyright Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update index.css Signed-off-by: Jonas Franz <info@jonasfranz.software>
481 lines
12 KiB
Go
481 lines
12 KiB
Go
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package templates
|
|
|
|
import (
|
|
"bytes"
|
|
"container/list"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"html"
|
|
"html/template"
|
|
"mime"
|
|
"net/url"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/models"
|
|
"code.gitea.io/gitea/modules/base"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/markup"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"golang.org/x/net/html/charset"
|
|
"golang.org/x/text/transform"
|
|
"gopkg.in/editorconfig/editorconfig-core-go.v1"
|
|
)
|
|
|
|
// NewFuncMap returns functions for injecting to templates
|
|
func NewFuncMap() []template.FuncMap {
|
|
return []template.FuncMap{map[string]interface{}{
|
|
"GoVer": func() string {
|
|
return strings.Title(runtime.Version())
|
|
},
|
|
"UseHTTPS": func() bool {
|
|
return strings.HasPrefix(setting.AppURL, "https")
|
|
},
|
|
"AppName": func() string {
|
|
return setting.AppName
|
|
},
|
|
"AppSubUrl": func() string {
|
|
return setting.AppSubURL
|
|
},
|
|
"AppUrl": func() string {
|
|
return setting.AppURL
|
|
},
|
|
"AppVer": func() string {
|
|
return setting.AppVer
|
|
},
|
|
"AppBuiltWith": func() string {
|
|
return setting.AppBuiltWith
|
|
},
|
|
"AppDomain": func() string {
|
|
return setting.Domain
|
|
},
|
|
"DisableGravatar": func() bool {
|
|
return setting.DisableGravatar
|
|
},
|
|
"ShowFooterTemplateLoadTime": func() bool {
|
|
return setting.ShowFooterTemplateLoadTime
|
|
},
|
|
"LoadTimes": func(startTime time.Time) string {
|
|
return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
|
|
},
|
|
"AvatarLink": base.AvatarLink,
|
|
"Safe": Safe,
|
|
"SafeJS": SafeJS,
|
|
"Str2html": Str2html,
|
|
"TimeSince": base.TimeSince,
|
|
"TimeSinceUnix": base.TimeSinceUnix,
|
|
"RawTimeSince": base.RawTimeSince,
|
|
"FileSize": base.FileSize,
|
|
"Subtract": base.Subtract,
|
|
"EntryIcon": base.EntryIcon,
|
|
"Add": func(a, b int) int {
|
|
return a + b
|
|
},
|
|
"ActionIcon": ActionIcon,
|
|
"DateFmtLong": func(t time.Time) string {
|
|
return t.Format(time.RFC1123Z)
|
|
},
|
|
"DateFmtShort": func(t time.Time) string {
|
|
return t.Format("Jan 02, 2006")
|
|
},
|
|
"SizeFmt": func(s int64) string {
|
|
return base.FileSize(s)
|
|
},
|
|
"List": List,
|
|
"SubStr": func(str string, start, length int) string {
|
|
if len(str) == 0 {
|
|
return ""
|
|
}
|
|
end := start + length
|
|
if length == -1 {
|
|
end = len(str)
|
|
}
|
|
if len(str) < end {
|
|
return str
|
|
}
|
|
return str[start:end]
|
|
},
|
|
"EllipsisString": base.EllipsisString,
|
|
"DiffTypeToStr": DiffTypeToStr,
|
|
"DiffLineTypeToStr": DiffLineTypeToStr,
|
|
"Sha1": Sha1,
|
|
"ShortSha": base.ShortSha,
|
|
"MD5": base.EncodeMD5,
|
|
"ActionContent2Commits": ActionContent2Commits,
|
|
"PathEscape": url.PathEscape,
|
|
"EscapePound": func(str string) string {
|
|
return strings.NewReplacer("%", "%25", "#", "%23", " ", "%20", "?", "%3F").Replace(str)
|
|
},
|
|
"RenderCommitMessage": RenderCommitMessage,
|
|
"RenderCommitMessageLink": RenderCommitMessageLink,
|
|
"RenderCommitBody": RenderCommitBody,
|
|
"IsMultilineCommitMessage": IsMultilineCommitMessage,
|
|
"ThemeColorMetaTag": func() string {
|
|
return setting.UI.ThemeColorMetaTag
|
|
},
|
|
"MetaAuthor": func() string {
|
|
return setting.UI.Meta.Author
|
|
},
|
|
"MetaDescription": func() string {
|
|
return setting.UI.Meta.Description
|
|
},
|
|
"MetaKeywords": func() string {
|
|
return setting.UI.Meta.Keywords
|
|
},
|
|
"FilenameIsImage": func(filename string) bool {
|
|
mimeType := mime.TypeByExtension(filepath.Ext(filename))
|
|
return strings.HasPrefix(mimeType, "image/")
|
|
},
|
|
"TabSizeClass": func(ec *editorconfig.Editorconfig, filename string) string {
|
|
if ec != nil {
|
|
def := ec.GetDefinitionForFilename(filename)
|
|
if def.TabWidth > 0 {
|
|
return fmt.Sprintf("tab-size-%d", def.TabWidth)
|
|
}
|
|
}
|
|
return "tab-size-8"
|
|
},
|
|
"SubJumpablePath": func(str string) []string {
|
|
var path []string
|
|
index := strings.LastIndex(str, "/")
|
|
if index != -1 && index != len(str) {
|
|
path = append(path, str[0:index+1])
|
|
path = append(path, str[index+1:])
|
|
} else {
|
|
path = append(path, str)
|
|
}
|
|
return path
|
|
},
|
|
"JsonPrettyPrint": func(in string) string {
|
|
var out bytes.Buffer
|
|
err := json.Indent(&out, []byte(in), "", " ")
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return out.String()
|
|
},
|
|
"DisableGitHooks": func() bool {
|
|
return setting.DisableGitHooks
|
|
},
|
|
"TrN": TrN,
|
|
"Dict": func(values ...interface{}) (map[string]interface{}, error) {
|
|
if len(values)%2 != 0 {
|
|
return nil, errors.New("invalid dict call")
|
|
}
|
|
dict := make(map[string]interface{}, len(values)/2)
|
|
for i := 0; i < len(values); i += 2 {
|
|
key, ok := values[i].(string)
|
|
if !ok {
|
|
return nil, errors.New("dict keys must be strings")
|
|
}
|
|
dict[key] = values[i+1]
|
|
}
|
|
return dict, nil
|
|
},
|
|
"Printf": fmt.Sprintf,
|
|
"Escape": Escape,
|
|
"Sec2Time": models.SecToTime,
|
|
"ParseDeadline": func(deadline string) []string {
|
|
return strings.Split(deadline, "|")
|
|
},
|
|
"DefaultTheme": func() string {
|
|
return setting.UI.DefaultTheme
|
|
},
|
|
"dict": func(values ...interface{}) (map[string]interface{}, error) {
|
|
if len(values) == 0 {
|
|
return nil, errors.New("invalid dict call")
|
|
}
|
|
|
|
dict := make(map[string]interface{})
|
|
|
|
for i := 0; i < len(values); i++ {
|
|
switch key := values[i].(type) {
|
|
case string:
|
|
i++
|
|
if i == len(values) {
|
|
return nil, errors.New("specify the key for non array values")
|
|
}
|
|
dict[key] = values[i]
|
|
case map[string]interface{}:
|
|
m := values[i].(map[string]interface{})
|
|
for i, v := range m {
|
|
dict[i] = v
|
|
}
|
|
default:
|
|
return nil, errors.New("dict values must be maps")
|
|
}
|
|
}
|
|
return dict, nil
|
|
},
|
|
}}
|
|
}
|
|
|
|
// Safe render raw as HTML
|
|
func Safe(raw string) template.HTML {
|
|
return template.HTML(raw)
|
|
}
|
|
|
|
// SafeJS renders raw as JS
|
|
func SafeJS(raw string) template.JS {
|
|
return template.JS(raw)
|
|
}
|
|
|
|
// Str2html render Markdown text to HTML
|
|
func Str2html(raw string) template.HTML {
|
|
return template.HTML(markup.Sanitize(raw))
|
|
}
|
|
|
|
// Escape escapes a HTML string
|
|
func Escape(raw string) string {
|
|
return html.EscapeString(raw)
|
|
}
|
|
|
|
// List traversings the list
|
|
func List(l *list.List) chan interface{} {
|
|
e := l.Front()
|
|
c := make(chan interface{})
|
|
go func() {
|
|
for e != nil {
|
|
c <- e.Value
|
|
e = e.Next()
|
|
}
|
|
close(c)
|
|
}()
|
|
return c
|
|
}
|
|
|
|
// Sha1 returns sha1 sum of string
|
|
func Sha1(str string) string {
|
|
return base.EncodeSha1(str)
|
|
}
|
|
|
|
// ToUTF8WithErr converts content to UTF8 encoding
|
|
func ToUTF8WithErr(content []byte) (string, error) {
|
|
charsetLabel, err := base.DetectEncoding(content)
|
|
if err != nil {
|
|
return "", err
|
|
} else if charsetLabel == "UTF-8" {
|
|
return string(content), nil
|
|
}
|
|
|
|
encoding, _ := charset.Lookup(charsetLabel)
|
|
if encoding == nil {
|
|
return string(content), fmt.Errorf("Unknown encoding: %s", charsetLabel)
|
|
}
|
|
|
|
// If there is an error, we concatenate the nicely decoded part and the
|
|
// original left over. This way we won't loose data.
|
|
result, n, err := transform.String(encoding.NewDecoder(), string(content))
|
|
if err != nil {
|
|
result = result + string(content[n:])
|
|
}
|
|
|
|
return result, err
|
|
}
|
|
|
|
// ToUTF8 converts content to UTF8 encoding and ignore error
|
|
func ToUTF8(content string) string {
|
|
res, _ := ToUTF8WithErr([]byte(content))
|
|
return res
|
|
}
|
|
|
|
// ReplaceLeft replaces all prefixes 'old' in 's' with 'new'.
|
|
func ReplaceLeft(s, old, new string) string {
|
|
oldLen, newLen, i, n := len(old), len(new), 0, 0
|
|
for ; i < len(s) && strings.HasPrefix(s[i:], old); n++ {
|
|
i += oldLen
|
|
}
|
|
|
|
// simple optimization
|
|
if n == 0 {
|
|
return s
|
|
}
|
|
|
|
// allocating space for the new string
|
|
curLen := n*newLen + len(s[i:])
|
|
replacement := make([]byte, curLen, curLen)
|
|
|
|
j := 0
|
|
for ; j < n*newLen; j += newLen {
|
|
copy(replacement[j:j+newLen], new)
|
|
}
|
|
|
|
copy(replacement[j:], s[i:])
|
|
return string(replacement)
|
|
}
|
|
|
|
// RenderCommitMessage renders commit message with XSS-safe and special links.
|
|
func RenderCommitMessage(msg, urlPrefix string, metas map[string]string) template.HTML {
|
|
return RenderCommitMessageLink(msg, urlPrefix, "", metas)
|
|
}
|
|
|
|
// RenderCommitMessageLink renders commit message as a XXS-safe link to the provided
|
|
// default url, handling for special links.
|
|
func RenderCommitMessageLink(msg, urlPrefix, urlDefault string, metas map[string]string) template.HTML {
|
|
cleanMsg := template.HTMLEscapeString(msg)
|
|
// we can safely assume that it will not return any error, since there
|
|
// shouldn't be any special HTML.
|
|
fullMessage, err := markup.RenderCommitMessage([]byte(cleanMsg), urlPrefix, urlDefault, metas)
|
|
if err != nil {
|
|
log.Error(3, "RenderCommitMessage: %v", err)
|
|
return ""
|
|
}
|
|
msgLines := strings.Split(strings.TrimSpace(string(fullMessage)), "\n")
|
|
if len(msgLines) == 0 {
|
|
return template.HTML("")
|
|
}
|
|
return template.HTML(msgLines[0])
|
|
}
|
|
|
|
// RenderCommitBody extracts the body of a commit message without its title.
|
|
func RenderCommitBody(msg, urlPrefix string, metas map[string]string) template.HTML {
|
|
cleanMsg := template.HTMLEscapeString(msg)
|
|
fullMessage, err := markup.RenderCommitMessage([]byte(cleanMsg), urlPrefix, "", metas)
|
|
if err != nil {
|
|
log.Error(3, "RenderCommitMessage: %v", err)
|
|
return ""
|
|
}
|
|
body := strings.Split(strings.TrimSpace(string(fullMessage)), "\n")
|
|
if len(body) == 0 {
|
|
return template.HTML("")
|
|
}
|
|
return template.HTML(strings.Join(body[1:], "\n"))
|
|
}
|
|
|
|
// IsMultilineCommitMessage checks to see if a commit message contains multiple lines.
|
|
func IsMultilineCommitMessage(msg string) bool {
|
|
return strings.Count(strings.TrimSpace(msg), "\n") >= 1
|
|
}
|
|
|
|
// Actioner describes an action
|
|
type Actioner interface {
|
|
GetOpType() models.ActionType
|
|
GetActUserName() string
|
|
GetRepoUserName() string
|
|
GetRepoName() string
|
|
GetRepoPath() string
|
|
GetRepoLink() string
|
|
GetBranch() string
|
|
GetContent() string
|
|
GetCreate() time.Time
|
|
GetIssueInfos() []string
|
|
}
|
|
|
|
// ActionIcon accepts an action operation type and returns an icon class name.
|
|
func ActionIcon(opType models.ActionType) string {
|
|
switch opType {
|
|
case models.ActionCreateRepo, models.ActionTransferRepo:
|
|
return "repo"
|
|
case models.ActionCommitRepo, models.ActionPushTag, models.ActionDeleteTag, models.ActionDeleteBranch:
|
|
return "git-commit"
|
|
case models.ActionCreateIssue:
|
|
return "issue-opened"
|
|
case models.ActionCreatePullRequest:
|
|
return "git-pull-request"
|
|
case models.ActionCommentIssue:
|
|
return "comment-discussion"
|
|
case models.ActionMergePullRequest:
|
|
return "git-merge"
|
|
case models.ActionCloseIssue, models.ActionClosePullRequest:
|
|
return "issue-closed"
|
|
case models.ActionReopenIssue, models.ActionReopenPullRequest:
|
|
return "issue-reopened"
|
|
default:
|
|
return "invalid type"
|
|
}
|
|
}
|
|
|
|
// ActionContent2Commits converts action content to push commits
|
|
func ActionContent2Commits(act Actioner) *models.PushCommits {
|
|
push := models.NewPushCommits()
|
|
if err := json.Unmarshal([]byte(act.GetContent()), push); err != nil {
|
|
log.Error(4, "json.Unmarshal:\n%s\nERROR: %v", act.GetContent(), err)
|
|
}
|
|
return push
|
|
}
|
|
|
|
// DiffTypeToStr returns diff type name
|
|
func DiffTypeToStr(diffType int) string {
|
|
diffTypes := map[int]string{
|
|
1: "add", 2: "modify", 3: "del", 4: "rename",
|
|
}
|
|
return diffTypes[diffType]
|
|
}
|
|
|
|
// DiffLineTypeToStr returns diff line type name
|
|
func DiffLineTypeToStr(diffType int) string {
|
|
switch diffType {
|
|
case 2:
|
|
return "add"
|
|
case 3:
|
|
return "del"
|
|
case 4:
|
|
return "tag"
|
|
}
|
|
return "same"
|
|
}
|
|
|
|
// Language specific rules for translating plural texts
|
|
var trNLangRules = map[string]func(int64) int{
|
|
"en-US": func(cnt int64) int {
|
|
if cnt == 1 {
|
|
return 0
|
|
}
|
|
return 1
|
|
},
|
|
"lv-LV": func(cnt int64) int {
|
|
if cnt%10 == 1 && cnt%100 != 11 {
|
|
return 0
|
|
}
|
|
return 1
|
|
},
|
|
"ru-RU": func(cnt int64) int {
|
|
if cnt%10 == 1 && cnt%100 != 11 {
|
|
return 0
|
|
}
|
|
return 1
|
|
},
|
|
"zh-CN": func(cnt int64) int {
|
|
return 0
|
|
},
|
|
"zh-HK": func(cnt int64) int {
|
|
return 0
|
|
},
|
|
"zh-TW": func(cnt int64) int {
|
|
return 0
|
|
},
|
|
}
|
|
|
|
// TrN returns key to be used for plural text translation
|
|
func TrN(lang string, cnt interface{}, key1, keyN string) string {
|
|
var c int64
|
|
if t, ok := cnt.(int); ok {
|
|
c = int64(t)
|
|
} else if t, ok := cnt.(int16); ok {
|
|
c = int64(t)
|
|
} else if t, ok := cnt.(int32); ok {
|
|
c = int64(t)
|
|
} else if t, ok := cnt.(int64); ok {
|
|
c = t
|
|
} else {
|
|
return keyN
|
|
}
|
|
|
|
ruleFunc, ok := trNLangRules[lang]
|
|
if !ok {
|
|
ruleFunc = trNLangRules["en-US"]
|
|
}
|
|
|
|
if ruleFunc(c) == 0 {
|
|
return key1
|
|
}
|
|
return keyN
|
|
}
|