gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package feed
  4. import (
  5. "strings"
  6. "time"
  7. "code.gitea.io/gitea/models/repo"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/util"
  10. "code.gitea.io/gitea/services/context"
  11. "github.com/gorilla/feeds"
  12. )
  13. // ShowFileFeed shows tags and/or releases on the repo as RSS / Atom feed
  14. func ShowFileFeed(ctx *context.Context, repo *repo.Repository, formatType string) {
  15. fileName := ctx.Repo.TreePath
  16. if len(fileName) == 0 {
  17. return
  18. }
  19. commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(
  20. git.CommitsByFileAndRangeOptions{
  21. Revision: ctx.Repo.RefFullName.ShortName(), // FIXME: legacy code used ShortName
  22. File: fileName,
  23. Page: 1,
  24. })
  25. if err != nil {
  26. ctx.ServerError("ShowBranchFeed", err)
  27. return
  28. }
  29. title := "Latest commits for file " + ctx.Repo.TreePath
  30. link := &feeds.Link{Href: repo.HTMLURL() + "/" + ctx.Repo.RefTypeNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)}
  31. feed := &feeds.Feed{
  32. Title: title,
  33. Link: link,
  34. Description: repo.Description,
  35. Created: time.Now(),
  36. }
  37. for _, commit := range commits {
  38. feed.Items = append(feed.Items, &feeds.Item{
  39. Id: commit.ID.String(),
  40. Title: strings.TrimSpace(strings.Split(commit.Message(), "\n")[0]),
  41. Link: &feeds.Link{Href: repo.HTMLURL() + "/commit/" + commit.ID.String()},
  42. Author: &feeds.Author{
  43. Name: commit.Author.Name,
  44. Email: commit.Author.Email,
  45. },
  46. Description: commit.Message(),
  47. Content: commit.Message(),
  48. Created: commit.Committer.When,
  49. })
  50. }
  51. writeFeed(ctx, feed, formatType)
  52. }