You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

531 lines
14 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
6 years ago
Pull request review/approval and comment on code (#3748) * 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>
6 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Copyright 2014 The Gogs Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package templates
  6. import (
  7. "bytes"
  8. "container/list"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "html"
  13. "html/template"
  14. "mime"
  15. "net/url"
  16. "path/filepath"
  17. "runtime"
  18. "strings"
  19. "time"
  20. "code.gitea.io/gitea/modules/util"
  21. "code.gitea.io/gitea/models"
  22. "code.gitea.io/gitea/modules/base"
  23. "code.gitea.io/gitea/modules/log"
  24. "code.gitea.io/gitea/modules/markup"
  25. "code.gitea.io/gitea/modules/setting"
  26. "golang.org/x/net/html/charset"
  27. "golang.org/x/text/transform"
  28. "gopkg.in/editorconfig/editorconfig-core-go.v1"
  29. )
  30. // NewFuncMap returns functions for injecting to templates
  31. func NewFuncMap() []template.FuncMap {
  32. return []template.FuncMap{map[string]interface{}{
  33. "GoVer": func() string {
  34. return strings.Title(runtime.Version())
  35. },
  36. "UseHTTPS": func() bool {
  37. return strings.HasPrefix(setting.AppURL, "https")
  38. },
  39. "AppName": func() string {
  40. return setting.AppName
  41. },
  42. "AppSubUrl": func() string {
  43. return setting.AppSubURL
  44. },
  45. "AppUrl": func() string {
  46. return setting.AppURL
  47. },
  48. "AppVer": func() string {
  49. return setting.AppVer
  50. },
  51. "AppBuiltWith": func() string {
  52. return setting.AppBuiltWith
  53. },
  54. "AppDomain": func() string {
  55. return setting.Domain
  56. },
  57. "DisableGravatar": func() bool {
  58. return setting.DisableGravatar
  59. },
  60. "DefaultShowFullName": func() bool {
  61. return setting.UI.DefaultShowFullName
  62. },
  63. "ShowFooterTemplateLoadTime": func() bool {
  64. return setting.ShowFooterTemplateLoadTime
  65. },
  66. "LoadTimes": func(startTime time.Time) string {
  67. return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
  68. },
  69. "AvatarLink": base.AvatarLink,
  70. "Safe": Safe,
  71. "SafeJS": SafeJS,
  72. "Str2html": Str2html,
  73. "TimeSince": base.TimeSince,
  74. "TimeSinceUnix": base.TimeSinceUnix,
  75. "RawTimeSince": base.RawTimeSince,
  76. "FileSize": base.FileSize,
  77. "Subtract": base.Subtract,
  78. "EntryIcon": base.EntryIcon,
  79. "Add": func(a, b int) int {
  80. return a + b
  81. },
  82. "ActionIcon": ActionIcon,
  83. "DateFmtLong": func(t time.Time) string {
  84. return t.Format(time.RFC1123Z)
  85. },
  86. "DateFmtShort": func(t time.Time) string {
  87. return t.Format("Jan 02, 2006")
  88. },
  89. "SizeFmt": func(s int64) string {
  90. return base.FileSize(s)
  91. },
  92. "List": List,
  93. "SubStr": func(str string, start, length int) string {
  94. if len(str) == 0 {
  95. return ""
  96. }
  97. end := start + length
  98. if length == -1 {
  99. end = len(str)
  100. }
  101. if len(str) < end {
  102. return str
  103. }
  104. return str[start:end]
  105. },
  106. "EllipsisString": base.EllipsisString,
  107. "DiffTypeToStr": DiffTypeToStr,
  108. "DiffLineTypeToStr": DiffLineTypeToStr,
  109. "Sha1": Sha1,
  110. "ShortSha": base.ShortSha,
  111. "MD5": base.EncodeMD5,
  112. "ActionContent2Commits": ActionContent2Commits,
  113. "PathEscape": url.PathEscape,
  114. "EscapePound": func(str string) string {
  115. return strings.NewReplacer("%", "%25", "#", "%23", " ", "%20", "?", "%3F").Replace(str)
  116. },
  117. "PathEscapeSegments": util.PathEscapeSegments,
  118. "URLJoin": util.URLJoin,
  119. "RenderCommitMessage": RenderCommitMessage,
  120. "RenderCommitMessageLink": RenderCommitMessageLink,
  121. "RenderCommitBody": RenderCommitBody,
  122. "IsMultilineCommitMessage": IsMultilineCommitMessage,
  123. "ThemeColorMetaTag": func() string {
  124. return setting.UI.ThemeColorMetaTag
  125. },
  126. "MetaAuthor": func() string {
  127. return setting.UI.Meta.Author
  128. },
  129. "MetaDescription": func() string {
  130. return setting.UI.Meta.Description
  131. },
  132. "MetaKeywords": func() string {
  133. return setting.UI.Meta.Keywords
  134. },
  135. "FilenameIsImage": func(filename string) bool {
  136. mimeType := mime.TypeByExtension(filepath.Ext(filename))
  137. return strings.HasPrefix(mimeType, "image/")
  138. },
  139. "TabSizeClass": func(ec *editorconfig.Editorconfig, filename string) string {
  140. if ec != nil {
  141. def := ec.GetDefinitionForFilename(filename)
  142. if def.TabWidth > 0 {
  143. return fmt.Sprintf("tab-size-%d", def.TabWidth)
  144. }
  145. }
  146. return "tab-size-8"
  147. },
  148. "SubJumpablePath": func(str string) []string {
  149. var path []string
  150. index := strings.LastIndex(str, "/")
  151. if index != -1 && index != len(str) {
  152. path = append(path, str[0:index+1])
  153. path = append(path, str[index+1:])
  154. } else {
  155. path = append(path, str)
  156. }
  157. return path
  158. },
  159. "JsonPrettyPrint": func(in string) string {
  160. var out bytes.Buffer
  161. err := json.Indent(&out, []byte(in), "", " ")
  162. if err != nil {
  163. return ""
  164. }
  165. return out.String()
  166. },
  167. "DisableGitHooks": func() bool {
  168. return setting.DisableGitHooks
  169. },
  170. "DisableImportLocal": func() bool {
  171. return !setting.ImportLocalPaths
  172. },
  173. "TrN": TrN,
  174. "Dict": func(values ...interface{}) (map[string]interface{}, error) {
  175. if len(values)%2 != 0 {
  176. return nil, errors.New("invalid dict call")
  177. }
  178. dict := make(map[string]interface{}, len(values)/2)
  179. for i := 0; i < len(values); i += 2 {
  180. key, ok := values[i].(string)
  181. if !ok {
  182. return nil, errors.New("dict keys must be strings")
  183. }
  184. dict[key] = values[i+1]
  185. }
  186. return dict, nil
  187. },
  188. "Printf": fmt.Sprintf,
  189. "Escape": Escape,
  190. "Sec2Time": models.SecToTime,
  191. "ParseDeadline": func(deadline string) []string {
  192. return strings.Split(deadline, "|")
  193. },
  194. "DefaultTheme": func() string {
  195. return setting.UI.DefaultTheme
  196. },
  197. "dict": func(values ...interface{}) (map[string]interface{}, error) {
  198. if len(values) == 0 {
  199. return nil, errors.New("invalid dict call")
  200. }
  201. dict := make(map[string]interface{})
  202. for i := 0; i < len(values); i++ {
  203. switch key := values[i].(type) {
  204. case string:
  205. i++
  206. if i == len(values) {
  207. return nil, errors.New("specify the key for non array values")
  208. }
  209. dict[key] = values[i]
  210. case map[string]interface{}:
  211. m := values[i].(map[string]interface{})
  212. for i, v := range m {
  213. dict[i] = v
  214. }
  215. default:
  216. return nil, errors.New("dict values must be maps")
  217. }
  218. }
  219. return dict, nil
  220. },
  221. "percentage": func(n int, values ...int) float32 {
  222. var sum = 0
  223. for i := 0; i < len(values); i++ {
  224. sum += values[i]
  225. }
  226. return float32(n) * 100 / float32(sum)
  227. },
  228. }}
  229. }
  230. // Safe render raw as HTML
  231. func Safe(raw string) template.HTML {
  232. return template.HTML(raw)
  233. }
  234. // SafeJS renders raw as JS
  235. func SafeJS(raw string) template.JS {
  236. return template.JS(raw)
  237. }
  238. // Str2html render Markdown text to HTML
  239. func Str2html(raw string) template.HTML {
  240. return template.HTML(markup.Sanitize(raw))
  241. }
  242. // Escape escapes a HTML string
  243. func Escape(raw string) string {
  244. return html.EscapeString(raw)
  245. }
  246. // List traversings the list
  247. func List(l *list.List) chan interface{} {
  248. e := l.Front()
  249. c := make(chan interface{})
  250. go func() {
  251. for e != nil {
  252. c <- e.Value
  253. e = e.Next()
  254. }
  255. close(c)
  256. }()
  257. return c
  258. }
  259. // Sha1 returns sha1 sum of string
  260. func Sha1(str string) string {
  261. return base.EncodeSha1(str)
  262. }
  263. // ToUTF8WithErr converts content to UTF8 encoding
  264. func ToUTF8WithErr(content []byte) (string, error) {
  265. charsetLabel, err := base.DetectEncoding(content)
  266. if err != nil {
  267. return "", err
  268. } else if charsetLabel == "UTF-8" {
  269. return string(base.RemoveBOMIfPresent(content)), nil
  270. }
  271. encoding, _ := charset.Lookup(charsetLabel)
  272. if encoding == nil {
  273. return string(content), fmt.Errorf("Unknown encoding: %s", charsetLabel)
  274. }
  275. // If there is an error, we concatenate the nicely decoded part and the
  276. // original left over. This way we won't lose data.
  277. result, n, err := transform.Bytes(encoding.NewDecoder(), content)
  278. if err != nil {
  279. result = append(result, content[n:]...)
  280. }
  281. result = base.RemoveBOMIfPresent(result)
  282. return string(result), err
  283. }
  284. // ToUTF8WithFallback detects the encoding of content and coverts to UTF-8 if possible
  285. func ToUTF8WithFallback(content []byte) []byte {
  286. charsetLabel, err := base.DetectEncoding(content)
  287. if err != nil || charsetLabel == "UTF-8" {
  288. return base.RemoveBOMIfPresent(content)
  289. }
  290. encoding, _ := charset.Lookup(charsetLabel)
  291. if encoding == nil {
  292. return content
  293. }
  294. // If there is an error, we concatenate the nicely decoded part and the
  295. // original left over. This way we won't lose data.
  296. result, n, err := transform.Bytes(encoding.NewDecoder(), content)
  297. if err != nil {
  298. return append(result, content[n:]...)
  299. }
  300. return base.RemoveBOMIfPresent(result)
  301. }
  302. // ToUTF8 converts content to UTF8 encoding and ignore error
  303. func ToUTF8(content string) string {
  304. res, _ := ToUTF8WithErr([]byte(content))
  305. return res
  306. }
  307. // ReplaceLeft replaces all prefixes 'old' in 's' with 'new'.
  308. func ReplaceLeft(s, old, new string) string {
  309. oldLen, newLen, i, n := len(old), len(new), 0, 0
  310. for ; i < len(s) && strings.HasPrefix(s[i:], old); n++ {
  311. i += oldLen
  312. }
  313. // simple optimization
  314. if n == 0 {
  315. return s
  316. }
  317. // allocating space for the new string
  318. curLen := n*newLen + len(s[i:])
  319. replacement := make([]byte, curLen, curLen)
  320. j := 0
  321. for ; j < n*newLen; j += newLen {
  322. copy(replacement[j:j+newLen], new)
  323. }
  324. copy(replacement[j:], s[i:])
  325. return string(replacement)
  326. }
  327. // RenderCommitMessage renders commit message with XSS-safe and special links.
  328. func RenderCommitMessage(msg, urlPrefix string, metas map[string]string) template.HTML {
  329. return RenderCommitMessageLink(msg, urlPrefix, "", metas)
  330. }
  331. // RenderCommitMessageLink renders commit message as a XXS-safe link to the provided
  332. // default url, handling for special links.
  333. func RenderCommitMessageLink(msg, urlPrefix, urlDefault string, metas map[string]string) template.HTML {
  334. cleanMsg := template.HTMLEscapeString(msg)
  335. // we can safely assume that it will not return any error, since there
  336. // shouldn't be any special HTML.
  337. fullMessage, err := markup.RenderCommitMessage([]byte(cleanMsg), urlPrefix, urlDefault, metas)
  338. if err != nil {
  339. log.Error("RenderCommitMessage: %v", err)
  340. return ""
  341. }
  342. msgLines := strings.Split(strings.TrimSpace(string(fullMessage)), "\n")
  343. if len(msgLines) == 0 {
  344. return template.HTML("")
  345. }
  346. return template.HTML(msgLines[0])
  347. }
  348. // RenderCommitBody extracts the body of a commit message without its title.
  349. func RenderCommitBody(msg, urlPrefix string, metas map[string]string) template.HTML {
  350. cleanMsg := template.HTMLEscapeString(msg)
  351. fullMessage, err := markup.RenderCommitMessage([]byte(cleanMsg), urlPrefix, "", metas)
  352. if err != nil {
  353. log.Error("RenderCommitMessage: %v", err)
  354. return ""
  355. }
  356. body := strings.Split(strings.TrimSpace(string(fullMessage)), "\n")
  357. if len(body) == 0 {
  358. return template.HTML("")
  359. }
  360. return template.HTML(strings.Join(body[1:], "\n"))
  361. }
  362. // IsMultilineCommitMessage checks to see if a commit message contains multiple lines.
  363. func IsMultilineCommitMessage(msg string) bool {
  364. return strings.Count(strings.TrimSpace(msg), "\n") >= 1
  365. }
  366. // Actioner describes an action
  367. type Actioner interface {
  368. GetOpType() models.ActionType
  369. GetActUserName() string
  370. GetRepoUserName() string
  371. GetRepoName() string
  372. GetRepoPath() string
  373. GetRepoLink() string
  374. GetBranch() string
  375. GetContent() string
  376. GetCreate() time.Time
  377. GetIssueInfos() []string
  378. }
  379. // ActionIcon accepts an action operation type and returns an icon class name.
  380. func ActionIcon(opType models.ActionType) string {
  381. switch opType {
  382. case models.ActionCreateRepo, models.ActionTransferRepo:
  383. return "repo"
  384. case models.ActionCommitRepo, models.ActionPushTag, models.ActionDeleteTag, models.ActionDeleteBranch:
  385. return "git-commit"
  386. case models.ActionCreateIssue:
  387. return "issue-opened"
  388. case models.ActionCreatePullRequest:
  389. return "git-pull-request"
  390. case models.ActionCommentIssue:
  391. return "comment-discussion"
  392. case models.ActionMergePullRequest:
  393. return "git-merge"
  394. case models.ActionCloseIssue, models.ActionClosePullRequest:
  395. return "issue-closed"
  396. case models.ActionReopenIssue, models.ActionReopenPullRequest:
  397. return "issue-reopened"
  398. case models.ActionMirrorSyncPush, models.ActionMirrorSyncCreate, models.ActionMirrorSyncDelete:
  399. return "repo-clone"
  400. default:
  401. return "invalid type"
  402. }
  403. }
  404. // ActionContent2Commits converts action content to push commits
  405. func ActionContent2Commits(act Actioner) *models.PushCommits {
  406. push := models.NewPushCommits()
  407. if err := json.Unmarshal([]byte(act.GetContent()), push); err != nil {
  408. log.Error("json.Unmarshal:\n%s\nERROR: %v", act.GetContent(), err)
  409. }
  410. return push
  411. }
  412. // DiffTypeToStr returns diff type name
  413. func DiffTypeToStr(diffType int) string {
  414. diffTypes := map[int]string{
  415. 1: "add", 2: "modify", 3: "del", 4: "rename",
  416. }
  417. return diffTypes[diffType]
  418. }
  419. // DiffLineTypeToStr returns diff line type name
  420. func DiffLineTypeToStr(diffType int) string {
  421. switch diffType {
  422. case 2:
  423. return "add"
  424. case 3:
  425. return "del"
  426. case 4:
  427. return "tag"
  428. }
  429. return "same"
  430. }
  431. // Language specific rules for translating plural texts
  432. var trNLangRules = map[string]func(int64) int{
  433. "en-US": func(cnt int64) int {
  434. if cnt == 1 {
  435. return 0
  436. }
  437. return 1
  438. },
  439. "lv-LV": func(cnt int64) int {
  440. if cnt%10 == 1 && cnt%100 != 11 {
  441. return 0
  442. }
  443. return 1
  444. },
  445. "ru-RU": func(cnt int64) int {
  446. if cnt%10 == 1 && cnt%100 != 11 {
  447. return 0
  448. }
  449. return 1
  450. },
  451. "zh-CN": func(cnt int64) int {
  452. return 0
  453. },
  454. "zh-HK": func(cnt int64) int {
  455. return 0
  456. },
  457. "zh-TW": func(cnt int64) int {
  458. return 0
  459. },
  460. "fr-FR": func(cnt int64) int {
  461. if cnt > -2 && cnt < 2 {
  462. return 0
  463. }
  464. return 1
  465. },
  466. }
  467. // TrN returns key to be used for plural text translation
  468. func TrN(lang string, cnt interface{}, key1, keyN string) string {
  469. var c int64
  470. if t, ok := cnt.(int); ok {
  471. c = int64(t)
  472. } else if t, ok := cnt.(int16); ok {
  473. c = int64(t)
  474. } else if t, ok := cnt.(int32); ok {
  475. c = int64(t)
  476. } else if t, ok := cnt.(int64); ok {
  477. c = t
  478. } else {
  479. return keyN
  480. }
  481. ruleFunc, ok := trNLangRules[lang]
  482. if !ok {
  483. ruleFunc = trNLangRules["en-US"]
  484. }
  485. if ruleFunc(c) == 0 {
  486. return key1
  487. }
  488. return keyN
  489. }