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 11 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", ctx.Repo.BranchName)
link := &feeds.Link{Href: repo.HTMLURL() + "/" + ctx.Repo.BranchNameSubURL()}

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(strings.Split(commit.Message(), "\n")[0]),
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)
}
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", ctx.Repo.TreePath)
link := &feeds.Link{Href: repo.HTMLURL() + "/" + ctx.Repo.BranchNameSubURL() + "/" + ctx.Repo.TreePath}
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(strings.Split(commit.Message(), "\n")[0]),
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)
}
54 changes: 54 additions & 0 deletions routers/web/feed/render.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"

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) {
if !setting.EnableFeed {
return
}
isFeed, _, showFeedType := GetFeedType(ctx.Params(":reponame"), ctx.Req)
if !isFeed {
ctx.Data["EnableFeed"] = true
ctx.Data["FeedURL"] = ctx.Repo.Repository.HTMLURL()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC #23904 already injects the EnableFeed and FeedURL data for all repo handlers

And this function is only called by Home, is it better to revert to old code structure?

eg:

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that makes sense. I've reverted to the old code structure.

There is still (albeit refactored) feed.RenderBranchFeed which handles rendering the feeds for branches / files. Is this ok or would you prefer to remove this function and either in-line or handle through the Home handler?

Copy link
Contributor

@wxiaoguang wxiaoguang Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean render? I guess it could be simplified a little (and in-lining seems good enough)

	switch {
	case ctx.Link == fmt.Sprintf("%s.%s", ctx.Repo.RepoLink, showFeedType):
		ShowRepoFeed(....)
	case ctx.Repo.TreePath == "":
		ShowRepoFeed(....)
	default: // for files
		ShowRepoFeed(....)
	}

What do you think ?

return
}
render(ctx, showFeedType)
}

// render
func render(ctx *context.Context, showFeedType string) {
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
}
renderer(ctx, ctx.Repo.Repository, showFeedType)
}
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,15 +698,9 @@ 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()
feed.RenderRepoFeed(ctx)
if ctx.Written() {
return
}

checkHomeCodeViewable(ctx)
Expand Down
3 changes: 3 additions & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,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
5 changes: 5 additions & 0 deletions templates/repo/home.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
</button>
{{end}}
{{if and .EnableFeed (eq $n 0) (ne .Repository.DefaultBranch $.BranchName)}}
<a role="button" class="ui primary compact button" href="{{$.FeedURL}}/rss/{{$.BranchNameSubURL}}">
<svg viewBox="0 2 16 16" class="svg octicon-rss" width="18" height="18" aria-hidden="true"><path d="M2.002 2.725a.75.75 0 0 1 .797-.699C8.79 2.42 13.58 7.21 13.974 13.201a.75.75 0 0 1-1.497.098 10.502 10.502 0 0 0-9.776-9.776.747.747 0 0 1-.7-.798ZM2.84 7.05h-.002a7.002 7.002 0 0 1 6.113 6.111.75.75 0 0 1-1.49.178 5.503 5.503 0 0 0-4.8-4.8.75.75 0 0 1 .179-1.489ZM2 13a1 1 0 1 1 2 0 1 1 0 0 1-2 0Z"></path></svg>
jladbrook marked this conversation as resolved.
Show resolved Hide resolved
</a>
{{end}}
{{if and (eq $n 0) (.Repository.IsTemplate)}}
<a role="button" class="ui primary compact button" href="{{AppSubUrl}}/repo/create?template_id={{.Repository.ID}}">
{{.locale.Tr "repo.use_template"}}
Expand Down
5 changes: 5 additions & 0 deletions templates/repo/view_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
{{end}}
{{if not .ReadmeInList}}
<div class="ui buttons gt-mr-2">
{{if .EnableFeed}}
<a class="ui mini basic button" href="{{$.FeedURL}}/rss/{{$.BranchNameSubURL}}{{range $i, $v := .TreeNames}}/{{$v}}{{end}}">
<svg viewBox="0 2 14 14" class="svg octicon-rss" width="12" height="12" aria-hidden="true"><path d="M2.002 2.725a.75.75 0 0 1 .797-.699C8.79 2.42 13.58 7.21 13.974 13.201a.75.75 0 0 1-1.497.098 10.502 10.502 0 0 0-9.776-9.776.747.747 0 0 1-.7-.798ZM2.84 7.05h-.002a7.002 7.002 0 0 1 6.113 6.111.75.75 0 0 1-1.49.178 5.503 5.503 0 0 0-4.8-4.8.75.75 0 0 1 .179-1.489ZM2 13a1 1 0 1 1 2 0 1 1 0 0 1-2 0Z"></path></svg>
</a>
{{end}}
<a class="ui mini basic button" href="{{$.RawFileLink}}">{{.locale.Tr "repo.file_raw"}}</a>
{{if not .IsViewCommit}}
<a class="ui mini basic button" href="{{.RepoLink}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}">{{.locale.Tr "repo.file_permalink"}}</a>
Expand Down