code.gitea.io/gitea@v1.21.7/routers/web/feed/file.go (about)

     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/git"
    14  	"code.gitea.io/gitea/modules/util"
    15  
    16  	"github.com/gorilla/feeds"
    17  )
    18  
    19  // ShowFileFeed shows tags and/or releases on the repo as RSS / Atom feed
    20  func ShowFileFeed(ctx *context.Context, repo *repo.Repository, formatType string) {
    21  	fileName := ctx.Repo.TreePath
    22  	if len(fileName) == 0 {
    23  		return
    24  	}
    25  	commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(
    26  		git.CommitsByFileAndRangeOptions{
    27  			Revision: ctx.Repo.RefName,
    28  			File:     fileName,
    29  			Page:     1,
    30  		})
    31  	if err != nil {
    32  		ctx.ServerError("ShowBranchFeed", err)
    33  		return
    34  	}
    35  
    36  	title := fmt.Sprintf("Latest commits for file %s", ctx.Repo.TreePath)
    37  
    38  	link := &feeds.Link{Href: repo.HTMLURL() + "/" + ctx.Repo.BranchNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)}
    39  
    40  	feed := &feeds.Feed{
    41  		Title:       title,
    42  		Link:        link,
    43  		Description: repo.Description,
    44  		Created:     time.Now(),
    45  	}
    46  
    47  	for _, commit := range commits {
    48  		feed.Items = append(feed.Items, &feeds.Item{
    49  			Id:    commit.ID.String(),
    50  			Title: strings.TrimSpace(strings.Split(commit.Message(), "\n")[0]),
    51  			Link:  &feeds.Link{Href: repo.HTMLURL() + "/commit/" + commit.ID.String()},
    52  			Author: &feeds.Author{
    53  				Name:  commit.Author.Name,
    54  				Email: commit.Author.Email,
    55  			},
    56  			Description: commit.Message(),
    57  			Content:     commit.Message(),
    58  		})
    59  	}
    60  
    61  	writeFeed(ctx, feed, formatType)
    62  }