|
| 1 | +// Copyright 2023 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package templates |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "html/template" |
| 10 | + "mime" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + activities_model "code.gitea.io/gitea/models/activities" |
| 16 | + repo_model "code.gitea.io/gitea/models/repo" |
| 17 | + "code.gitea.io/gitea/modules/git" |
| 18 | + giturl "code.gitea.io/gitea/modules/git/url" |
| 19 | + "code.gitea.io/gitea/modules/json" |
| 20 | + "code.gitea.io/gitea/modules/log" |
| 21 | + "code.gitea.io/gitea/modules/repository" |
| 22 | + "code.gitea.io/gitea/modules/svg" |
| 23 | + |
| 24 | + "github.com/editorconfig/editorconfig-core-go/v2" |
| 25 | +) |
| 26 | + |
| 27 | +func SortArrow(normSort, revSort, urlSort string, isDefault bool) template.HTML { |
| 28 | + // if needed |
| 29 | + if len(normSort) == 0 || len(urlSort) == 0 { |
| 30 | + return "" |
| 31 | + } |
| 32 | + |
| 33 | + if len(urlSort) == 0 && isDefault { |
| 34 | + // if sort is sorted as default add arrow tho this table header |
| 35 | + if isDefault { |
| 36 | + return svg.RenderHTML("octicon-triangle-down", 16) |
| 37 | + } |
| 38 | + } else { |
| 39 | + // if sort arg is in url test if it correlates with column header sort arguments |
| 40 | + // the direction of the arrow should indicate the "current sort order", up means ASC(normal), down means DESC(rev) |
| 41 | + if urlSort == normSort { |
| 42 | + // the table is sorted with this header normal |
| 43 | + return svg.RenderHTML("octicon-triangle-up", 16) |
| 44 | + } else if urlSort == revSort { |
| 45 | + // the table is sorted with this header reverse |
| 46 | + return svg.RenderHTML("octicon-triangle-down", 16) |
| 47 | + } |
| 48 | + } |
| 49 | + // the table is NOT sorted with this header |
| 50 | + return "" |
| 51 | +} |
| 52 | + |
| 53 | +// IsMultilineCommitMessage checks to see if a commit message contains multiple lines. |
| 54 | +func IsMultilineCommitMessage(msg string) bool { |
| 55 | + return strings.Count(strings.TrimSpace(msg), "\n") >= 1 |
| 56 | +} |
| 57 | + |
| 58 | +// Actioner describes an action |
| 59 | +type Actioner interface { |
| 60 | + GetOpType() activities_model.ActionType |
| 61 | + GetActUserName() string |
| 62 | + GetRepoUserName() string |
| 63 | + GetRepoName() string |
| 64 | + GetRepoPath() string |
| 65 | + GetRepoLink() string |
| 66 | + GetBranch() string |
| 67 | + GetContent() string |
| 68 | + GetCreate() time.Time |
| 69 | + GetIssueInfos() []string |
| 70 | +} |
| 71 | + |
| 72 | +// ActionIcon accepts an action operation type and returns an icon class name. |
| 73 | +func ActionIcon(opType activities_model.ActionType) string { |
| 74 | + switch opType { |
| 75 | + case activities_model.ActionCreateRepo, activities_model.ActionTransferRepo, activities_model.ActionRenameRepo: |
| 76 | + return "repo" |
| 77 | + case activities_model.ActionCommitRepo, activities_model.ActionPushTag, activities_model.ActionDeleteTag, activities_model.ActionDeleteBranch: |
| 78 | + return "git-commit" |
| 79 | + case activities_model.ActionCreateIssue: |
| 80 | + return "issue-opened" |
| 81 | + case activities_model.ActionCreatePullRequest: |
| 82 | + return "git-pull-request" |
| 83 | + case activities_model.ActionCommentIssue, activities_model.ActionCommentPull: |
| 84 | + return "comment-discussion" |
| 85 | + case activities_model.ActionMergePullRequest, activities_model.ActionAutoMergePullRequest: |
| 86 | + return "git-merge" |
| 87 | + case activities_model.ActionCloseIssue, activities_model.ActionClosePullRequest: |
| 88 | + return "issue-closed" |
| 89 | + case activities_model.ActionReopenIssue, activities_model.ActionReopenPullRequest: |
| 90 | + return "issue-reopened" |
| 91 | + case activities_model.ActionMirrorSyncPush, activities_model.ActionMirrorSyncCreate, activities_model.ActionMirrorSyncDelete: |
| 92 | + return "mirror" |
| 93 | + case activities_model.ActionApprovePullRequest: |
| 94 | + return "check" |
| 95 | + case activities_model.ActionRejectPullRequest: |
| 96 | + return "diff" |
| 97 | + case activities_model.ActionPublishRelease: |
| 98 | + return "tag" |
| 99 | + case activities_model.ActionPullReviewDismissed: |
| 100 | + return "x" |
| 101 | + default: |
| 102 | + return "question" |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// ActionContent2Commits converts action content to push commits |
| 107 | +func ActionContent2Commits(act Actioner) *repository.PushCommits { |
| 108 | + push := repository.NewPushCommits() |
| 109 | + |
| 110 | + if act == nil || act.GetContent() == "" { |
| 111 | + return push |
| 112 | + } |
| 113 | + |
| 114 | + if err := json.Unmarshal([]byte(act.GetContent()), push); err != nil { |
| 115 | + log.Error("json.Unmarshal:\n%s\nERROR: %v", act.GetContent(), err) |
| 116 | + } |
| 117 | + |
| 118 | + if push.Len == 0 { |
| 119 | + push.Len = len(push.Commits) |
| 120 | + } |
| 121 | + |
| 122 | + return push |
| 123 | +} |
| 124 | + |
| 125 | +// DiffLineTypeToStr returns diff line type name |
| 126 | +func DiffLineTypeToStr(diffType int) string { |
| 127 | + switch diffType { |
| 128 | + case 2: |
| 129 | + return "add" |
| 130 | + case 3: |
| 131 | + return "del" |
| 132 | + case 4: |
| 133 | + return "tag" |
| 134 | + } |
| 135 | + return "same" |
| 136 | +} |
| 137 | + |
| 138 | +// MigrationIcon returns a SVG name matching the service an issue/comment was migrated from |
| 139 | +func MigrationIcon(hostname string) string { |
| 140 | + switch hostname { |
| 141 | + case "github.com": |
| 142 | + return "octicon-mark-github" |
| 143 | + default: |
| 144 | + return "gitea-git" |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +type remoteAddress struct { |
| 149 | + Address string |
| 150 | + Username string |
| 151 | + Password string |
| 152 | +} |
| 153 | + |
| 154 | +func mirrorRemoteAddress(ctx context.Context, m *repo_model.Repository, remoteName string, ignoreOriginalURL bool) remoteAddress { |
| 155 | + a := remoteAddress{} |
| 156 | + |
| 157 | + remoteURL := m.OriginalURL |
| 158 | + if ignoreOriginalURL || remoteURL == "" { |
| 159 | + var err error |
| 160 | + remoteURL, err = git.GetRemoteAddress(ctx, m.RepoPath(), remoteName) |
| 161 | + if err != nil { |
| 162 | + log.Error("GetRemoteURL %v", err) |
| 163 | + return a |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + u, err := giturl.Parse(remoteURL) |
| 168 | + if err != nil { |
| 169 | + log.Error("giturl.Parse %v", err) |
| 170 | + return a |
| 171 | + } |
| 172 | + |
| 173 | + if u.Scheme != "ssh" && u.Scheme != "file" { |
| 174 | + if u.User != nil { |
| 175 | + a.Username = u.User.Username() |
| 176 | + a.Password, _ = u.User.Password() |
| 177 | + } |
| 178 | + u.User = nil |
| 179 | + } |
| 180 | + a.Address = u.String() |
| 181 | + |
| 182 | + return a |
| 183 | +} |
| 184 | + |
| 185 | +func FilenameIsImage(filename string) bool { |
| 186 | + mimeType := mime.TypeByExtension(filepath.Ext(filename)) |
| 187 | + return strings.HasPrefix(mimeType, "image/") |
| 188 | +} |
| 189 | + |
| 190 | +func TabSizeClass(ec interface{}, filename string) string { |
| 191 | + var ( |
| 192 | + value *editorconfig.Editorconfig |
| 193 | + ok bool |
| 194 | + ) |
| 195 | + if ec != nil { |
| 196 | + if value, ok = ec.(*editorconfig.Editorconfig); !ok || value == nil { |
| 197 | + return "tab-size-8" |
| 198 | + } |
| 199 | + def, err := value.GetDefinitionForFilename(filename) |
| 200 | + if err != nil { |
| 201 | + log.Error("tab size class: getting definition for filename: %v", err) |
| 202 | + return "tab-size-8" |
| 203 | + } |
| 204 | + if def.TabWidth > 0 { |
| 205 | + return fmt.Sprintf("tab-size-%d", def.TabWidth) |
| 206 | + } |
| 207 | + } |
| 208 | + return "tab-size-8" |
| 209 | +} |
0 commit comments