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