Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RSS Feeds for branches and files #22719

Merged
merged 36 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a43e056
implementing the rss feed for branches not connected to router yet
jladbrook Feb 1, 2023
bd6f6e5
adding feed generation for a file
jladbrook Feb 1, 2023
6279b02
refactored feed rendering and updated home implementation
jladbrook Feb 1, 2023
b95ff66
Merge branch 'main' into rss-22228
jladbrook Feb 1, 2023
41db639
Merge branch 'main' into rss-22228
jladbrook Feb 2, 2023
d277548
responding to feedback removing .rss suffix from branches
jladbrook Feb 2, 2023
955bc04
Merge branch 'main' into rss-22228
jladbrook Feb 2, 2023
a9095e1
Merge branch 'main' into rss-22228
6543 Apr 2, 2023
ca1d6b7
Merge branch 'main' into rss-22228
jladbrook Apr 3, 2023
32579ed
addressing comments from feedback
jladbrook Apr 3, 2023
b29d1ef
updating view templates adding feed buttons
jladbrook Apr 3, 2023
f796c36
don't inline svg use the svg helper instead
jladbrook Apr 3, 2023
20cb77f
Merge branch 'main' into rss-22228
6543 Apr 9, 2023
6590760
Merge branch 'main' into rss-22228
silverwind Apr 9, 2023
f573e75
Merge branch 'main' into rss-22228
6543 Apr 12, 2023
54d2304
Merge branch 'main' into rss-22228
6543 Apr 13, 2023
b195361
Merge branch 'main' into rss-22228
6543 Apr 13, 2023
e2bf743
Merge branch 'main' into rss-22228
jladbrook Apr 17, 2023
e9126e7
updating ui as per feedback
jladbrook Apr 17, 2023
abdf073
relocate buttons on home and view_file
jladbrook Apr 17, 2023
9d763f2
adding feed buttons on the branches page
jladbrook Apr 18, 2023
e461fbc
add rss feed links to the drop-down menu and remove from the main page
jladbrook Apr 18, 2023
a674624
Merge branch 'main' into rss-22228
jladbrook Apr 18, 2023
8ab322d
Merge branch 'main' into rss-22228
jladbrook Apr 18, 2023
081b40d
revert changes in response to #24904 and fix rss path from dropdown box
jladbrook Apr 18, 2023
3d63b4c
fix: lint-frontend failure
jladbrook Apr 18, 2023
b32be23
Merge branch 'main' into rss-22228
jladbrook Apr 24, 2023
4a30a23
Merge branch 'main' into rss-22228
jladbrook Apr 24, 2023
30e6d1d
mute link in branch dropdown
silverwind Apr 24, 2023
d5a6055
Merge branch 'main' into rss-22228
silverwind Apr 24, 2023
9591255
Merge branch 'main' into rss-22228
jladbrook Apr 25, 2023
8b225c5
remove reference to setting.EnableFeed
jladbrook Apr 25, 2023
b9669b2
remove custom rule and use ui.right
jladbrook Apr 25, 2023
dcd6f9d
remove unecessary jump from button
jladbrook Apr 25, 2023
172280d
addressing reviewers feedback
jladbrook Apr 25, 2023
f1b03c5
Merge branch 'main' into rss-22228
GiteaBot Apr 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions routers/web/feed/branch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package feed

import (
"fmt"
"strings"
"time"

"code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/context"

"github.com/gorilla/feeds"
)

// ShowBranchFeed shows tags and/or releases on the repo as RSS / Atom feed
func ShowBranchFeed(ctx *context.Context, repo *repo.Repository, formatType string) {
commits, err := ctx.Repo.Commit.CommitsByRange(0, 10)
if err != nil {
ctx.ServerError("ShowBranchFeed %s", err)
return
}

title := fmt.Sprintf("Latest commits for branch %s", strings.TrimSpace(ctx.Repo.BranchName))
jladbrook marked this conversation as resolved.
Show resolved Hide resolved
link := &feeds.Link{Href: repo.HTMLURL() + "/branch/" + ctx.Repo.BranchName}
jladbrook marked this conversation as resolved.
Show resolved Hide resolved

feed := &feeds.Feed{
Title: title,
Link: link,
Description: repo.Description,
Created: time.Now(),
}

for _, commit := range commits {
feed.Items = append(feed.Items, &feeds.Item{
Id: commit.ID.String(),
Title: strings.TrimSpace(commit.Message()),
Link: &feeds.Link{Href: repo.HTMLURL() + "/commit/" + commit.ID.String()},
Author: &feeds.Author{
Name: commit.Author.Name,
Email: commit.Author.Email,
},
Description: commit.Message(),
Content: commit.Message(),
jladbrook marked this conversation as resolved.
Show resolved Hide resolved
})
}

writeFeed(ctx, feed, formatType)
}
54 changes: 54 additions & 0 deletions routers/web/feed/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package feed

import (
"fmt"
"strings"
"time"

"code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/context"

"github.com/gorilla/feeds"
)

// ShowFileFeed shows tags and/or releases on the repo as RSS / Atom feed
func ShowFileFeed(ctx *context.Context, repo *repo.Repository, formatType string) {
fileName := ctx.Repo.TreePath
if len(fileName) == 0 {
return
}
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(ctx.Repo.RefName, fileName, 1)
if err != nil {
ctx.ServerError("ShowBranchFeed %s", err)
return
}

title := fmt.Sprintf("Latest commits for file %s", strings.TrimSpace(ctx.Repo.TreePath))
link := &feeds.Link{Href: repo.HTMLURL() + "/branch/" + ctx.Repo.BranchName}

feed := &feeds.Feed{
Title: title,
Link: link,
Description: repo.Description,
Created: time.Now(),
}

for _, commit := range commits {
feed.Items = append(feed.Items, &feeds.Item{
Id: commit.ID.String(),
Title: strings.TrimSpace(commit.Message()),
Link: &feeds.Link{Href: repo.HTMLURL() + "/commit/" + commit.ID.String()},
Author: &feeds.Author{
Name: commit.Author.Name,
Email: commit.Author.Email,
},
Description: commit.Message(),
Content: commit.Message(),
})
}

writeFeed(ctx, feed, formatType)
}
64 changes: 64 additions & 0 deletions routers/web/feed/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package feed

import (
"fmt"

model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
)

// RenderBranchFeedRss render rss format for branch feed
func RenderBranchFeedRss(ctx *context.Context) {
render(ctx, "rss")
}

// RenderBranchFeedAtom render atom format for branch feed
func RenderBranchFeedAtom(ctx *context.Context) {
render(ctx, "atom")
}

// RenderRepoFeed handles if an RSS feed should be rendered, injects variables into context if not.
//
// The logic for rendering as a rss / atom feed checks against:
// * existence of Accept header containing application/rss+xml or application/atom+xml
// * support for the {repo}.rss url
func RenderRepoFeed(ctx *context.Context) bool {
if !setting.EnableFeed {
return false
}
isFeed, _, showFeedType := GetFeedType(ctx.Params(":reponame"), ctx.Req)
if !isFeed {
return false
}
return render(ctx, showFeedType)
}

// render
func render(ctx *context.Context, showFeedType string) bool {
6543 marked this conversation as resolved.
Show resolved Hide resolved
var renderer func(ctx *context.Context, repo *model.Repository, formatType string)
switch {
case ctx.Link == fmt.Sprintf("%s.%s", ctx.Repo.RepoLink, showFeedType):
renderer = ShowRepoFeed
case ctx.Repo.TreePath == "":
renderer = ShowBranchFeed
case ctx.Repo.TreePath != "":
renderer = ShowFileFeed
default:
return false
6543 marked this conversation as resolved.
Show resolved Hide resolved
}
renderer(ctx, ctx.Repo.Repository, showFeedType)
return true
}

// InjectContextVariables adds required context variables to allow feed to be displayed in the UI
func InjectContextVariables(ctx *context.Context) {
jladbrook marked this conversation as resolved.
Show resolved Hide resolved
if !setting.EnableFeed {
return
}
ctx.Data["EnableFeed"] = true
ctx.Data["FeedURL"] = ctx.Repo.Repository.HTMLURL()
}
6 changes: 6 additions & 0 deletions routers/web/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/utils"
"code.gitea.io/gitea/routers/web/feed"
"code.gitea.io/gitea/services/forms"
release_service "code.gitea.io/gitea/services/release"
repo_service "code.gitea.io/gitea/services/repository"
Expand Down Expand Up @@ -340,6 +341,11 @@ func getDeletedBranches(ctx *context.Context) ([]*Branch, error) {
return branches, nil
}

// BranchFeedRSS get feeds for tags in RSS format
func BranchFeedRSS(ctx *context.Context) {
feed.ShowBranchFeed(ctx, ctx.Repo.Repository, "rss")
}

// CreateBranch creates new branch in repository
func CreateBranch(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.NewBranchForm)
Expand Down
12 changes: 3 additions & 9 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,17 +698,11 @@ func checkCitationFile(ctx *context.Context, entry *git.TreeEntry) {

// Home render repository home page
func Home(ctx *context.Context) {
if setting.EnableFeed {
isFeed, _, showFeedType := feed.GetFeedType(ctx.Params(":reponame"), ctx.Req)
if isFeed {
feed.ShowRepoFeed(ctx, ctx.Repo.Repository, showFeedType)
return
}

ctx.Data["EnableFeed"] = true
ctx.Data["FeedURL"] = ctx.Repo.Repository.Link()
if feed.RenderRepoFeed(ctx) {
jladbrook marked this conversation as resolved.
Show resolved Hide resolved
return
}

feed.InjectContextVariables(ctx)
checkHomeCodeViewable(ctx)
if ctx.Written() {
return
Expand Down
3 changes: 3 additions & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,9 @@ func RegisterRoutes(m *web.Route) {
m.Get("/cherry-pick/{sha:([a-f0-9]{7,40})$}", repo.SetEditorconfigIfExists, repo.CherryPick)
}, repo.MustBeNotEmpty, context.RepoRef(), reqRepoCodeReader)

m.Get("/rss/branch/*", context.RepoRefByType(context.RepoRefBranch), feed.RenderBranchFeedRss)
m.Get("/atom/branch/*", context.RepoRefByType(context.RepoRefBranch), feed.RenderBranchFeedAtom)

m.Group("/src", func() {
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.Home)
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.Home)
Expand Down