|
| 1 | +package admin |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/url" |
| 6 | + "regexp" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/modules/log" |
| 12 | + |
| 13 | + issue_model "code.gitea.io/gitea/models/issues" |
| 14 | + repo_model "code.gitea.io/gitea/models/repo" |
| 15 | + "code.gitea.io/gitea/modules/setting" |
| 16 | + "code.gitea.io/gitea/modules/templates" |
| 17 | + "code.gitea.io/gitea/models/db" |
| 18 | + "code.gitea.io/gitea/services/context" |
| 19 | + user_model "code.gitea.io/gitea/models/user" |
| 20 | +) |
| 21 | + |
| 22 | +const tplIssuesWithLinks templates.TplName = "admin/issues_with_links" |
| 23 | + |
| 24 | +type linkItem struct { |
| 25 | + Type string |
| 26 | + Content string |
| 27 | + User *user_model.User |
| 28 | + UserCreated time.Time |
| 29 | + RepoName string |
| 30 | + RepoLink string |
| 31 | + ItemLink string |
| 32 | + Created time.Time |
| 33 | + Updated time.Time |
| 34 | +} |
| 35 | + |
| 36 | +func IssuesWithLinks(ctx *context.Context) { |
| 37 | + ctx.Data["Title"] = ctx.Tr("admin.issues_with_links") |
| 38 | + ctx.Data["PageIsIssuesWithLinks"] = true |
| 39 | + |
| 40 | + page := ctx.FormInt("page") |
| 41 | + if page <= 1 { |
| 42 | + page = 1 |
| 43 | + } |
| 44 | + |
| 45 | + sortField := ctx.FormString("sort") |
| 46 | + sortOrder := strings.ToLower(ctx.FormString("order")) // asc or desc |
| 47 | + if sortOrder != "asc" { |
| 48 | + sortOrder = "desc" |
| 49 | + } |
| 50 | + ctx.Data["Sort"] = sortField |
| 51 | + ctx.Data["Order"] = sortOrder |
| 52 | + |
| 53 | + // fetch recent issues and comments |
| 54 | + limit := setting.UI.Admin.UserPagingNum |
| 55 | + issues, err := issue_model.GetRecentIssues(ctx, &db.ListOptions{Page: page, PageSize: limit}) |
| 56 | + if err != nil { |
| 57 | + ctx.ServerError("GetRecentIssues", err) |
| 58 | + return |
| 59 | + } |
| 60 | + comments, err := issue_model.GetRecentComments(ctx, &db.ListOptions{Page: page, PageSize: limit}) |
| 61 | + if err != nil { |
| 62 | + ctx.ServerError("GetRecentComments", err) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + var excludedDomains = []string{ |
| 67 | + getDomain(setting.AppURL), |
| 68 | + "github.com", |
| 69 | + } |
| 70 | + |
| 71 | + var items []linkItem |
| 72 | + appendIfHasLink := func(typeLabel, content, itemLink, repoName string, repoLink string, created time.Time, updated time.Time, u *user_model.User) { |
| 73 | + links := extractAllLinks(content) |
| 74 | + if len(links) == 0 { |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + var validLinks []string |
| 79 | + for _, link := range links { |
| 80 | + if !containsExcludedDomain(link, excludedDomains) { |
| 81 | + validLinks = append(validLinks, link) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if len(validLinks) == 0 { |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + items = append(items, linkItem{ |
| 90 | + Type: typeLabel, |
| 91 | + Content: strings.Join(validLinks, ", "), |
| 92 | + User: u, |
| 93 | + UserCreated: u.CreatedUnix.AsTime(), |
| 94 | + RepoName: repoName, |
| 95 | + RepoLink: repoLink, |
| 96 | + ItemLink: itemLink, |
| 97 | + Created: created, |
| 98 | + Updated: updated, |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + for _, issue := range issues { |
| 103 | + if issue.Content == "" { |
| 104 | + continue |
| 105 | + } |
| 106 | + if issue.Repo == nil { |
| 107 | + issue.Repo, err = repo_model.GetRepositoryByID(ctx, issue.RepoID) |
| 108 | + if err != nil { |
| 109 | + log.Warn("Could not load repo for issue %d: %v", issue.ID, err) |
| 110 | + continue |
| 111 | + } |
| 112 | + } |
| 113 | + u, err := user_model.GetUserByID(ctx, issue.PosterID) |
| 114 | + if err != nil { |
| 115 | + continue |
| 116 | + } |
| 117 | + appendIfHasLink("Issue", issue.Content, issue.HTMLURL(), issue.Repo.Name, issue.Repo.HTMLURL(), issue.CreatedUnix.AsTime(), issue.UpdatedUnix.AsTime(), u) |
| 118 | + } |
| 119 | + |
| 120 | + for _, comment := range comments { |
| 121 | + if comment.Issue == nil { |
| 122 | + comment.Issue, err = issue_model.GetIssueByID(ctx, comment.IssueID) |
| 123 | + if err != nil { |
| 124 | + log.Warn("Could not load issue for comment %d: %v", comment.ID, err) |
| 125 | + continue |
| 126 | + } |
| 127 | + } |
| 128 | + if comment.Issue.Repo == nil { |
| 129 | + comment.Issue.Repo, err = repo_model.GetRepositoryByID(ctx, comment.Issue.RepoID) |
| 130 | + if err != nil { |
| 131 | + log.Warn("Could not load repo for issue %d: %v", comment.Issue.ID, err) |
| 132 | + continue |
| 133 | + } |
| 134 | + } |
| 135 | + if comment.Content == "" { |
| 136 | + continue |
| 137 | + } |
| 138 | + u, err := user_model.GetUserByID(ctx, comment.PosterID) |
| 139 | + if err != nil { |
| 140 | + continue |
| 141 | + } |
| 142 | + appendIfHasLink("Comment", comment.Content, comment.HTMLURL(ctx), comment.Issue.Repo.Name, comment.Issue.Repo.HTMLURL(), comment.CreatedUnix.AsTime(), comment.UpdatedUnix.AsTime(), u) |
| 143 | + } |
| 144 | + |
| 145 | + // Sort |
| 146 | + switch sortField { |
| 147 | + case "usercreated": |
| 148 | + sort.Slice(items, func(i, j int) bool { |
| 149 | + if sortOrder == "asc" { |
| 150 | + return items[i].UserCreated.Before(items[j].UserCreated) |
| 151 | + } |
| 152 | + return items[i].UserCreated.After(items[j].UserCreated) |
| 153 | + }) |
| 154 | + case "created": |
| 155 | + sort.Slice(items, func(i, j int) bool { |
| 156 | + if sortOrder == "asc" { |
| 157 | + return items[i].Created.Before(items[j].Created) |
| 158 | + } |
| 159 | + return items[i].Created.After(items[j].Created) |
| 160 | + }) |
| 161 | + default: // fallback to descending by UserCreated |
| 162 | + sort.Slice(items, func(i, j int) bool { |
| 163 | + return items[i].UserCreated.After(items[j].UserCreated) |
| 164 | + }) |
| 165 | + } |
| 166 | + |
| 167 | + total := len(items) |
| 168 | + start := (page - 1) * limit |
| 169 | + end := start + limit |
| 170 | + if start > total { |
| 171 | + start = total |
| 172 | + } |
| 173 | + if end > total { |
| 174 | + end = total |
| 175 | + } |
| 176 | + paged := items[start:end] |
| 177 | + |
| 178 | + ctx.Data["Items"] = paged |
| 179 | + ctx.Data["Total"] = total |
| 180 | + |
| 181 | + pager := context.NewPagination(total, limit, page, 5) |
| 182 | + pager.AddParamFromRequest(ctx.Req) |
| 183 | + ctx.Data["Page"] = pager |
| 184 | + |
| 185 | + ctx.HTML(http.StatusOK, tplIssuesWithLinks) |
| 186 | +} |
| 187 | + |
| 188 | +// extractAllLinks returns all http(s) URLs from raw text and Markdown-style links. |
| 189 | +func extractAllLinks(text string) []string { |
| 190 | + // Match Markdown-style links: [label](http://example.com) |
| 191 | + mdLinkRegex := regexp.MustCompile(`\[[^\]]*\]\((https?://[^\s\)]+)\)`) |
| 192 | + // Match raw URLs |
| 193 | + rawURLRegex := regexp.MustCompile(`https?://[^\s<>"')\]]+`) |
| 194 | + |
| 195 | + seen := make(map[string]bool) |
| 196 | + var links []string |
| 197 | + |
| 198 | + // Extract Markdown links |
| 199 | + for _, match := range mdLinkRegex.FindAllStringSubmatch(text, -1) { |
| 200 | + if len(match) > 1 { |
| 201 | + url := match[1] |
| 202 | + if !seen[url] { |
| 203 | + seen[url] = true |
| 204 | + links = append(links, url) |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + // Extract raw URLs |
| 210 | + for _, url := range rawURLRegex.FindAllString(text, -1) { |
| 211 | + if !seen[url] { |
| 212 | + seen[url] = true |
| 213 | + links = append(links, url) |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + return links |
| 218 | +} |
| 219 | + |
| 220 | +// containsExcludedDomain returns true if any domain in the list matches the link |
| 221 | +func containsExcludedDomain(link string, excluded []string) bool { |
| 222 | + u, err := url.Parse(link) |
| 223 | + if err != nil || u.Host == "" { |
| 224 | + return false |
| 225 | + } |
| 226 | + for _, d := range excluded { |
| 227 | + if strings.EqualFold(u.Hostname(), d) { |
| 228 | + return true |
| 229 | + } |
| 230 | + } |
| 231 | + return false |
| 232 | +} |
| 233 | + |
| 234 | +// getDomain extracts domain from full AppURL (e.g. https://example.com) |
| 235 | +func getDomain(raw string) string { |
| 236 | + u, err := url.Parse(raw) |
| 237 | + if err != nil { |
| 238 | + return "" |
| 239 | + } |
| 240 | + return u.Hostname() |
| 241 | +} |
0 commit comments