-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add RSS Feeds for branches and files #22719
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
Changes from 11 commits
a43e056
bd6f6e5
6279b02
b95ff66
41db639
d277548
955bc04
a9095e1
ca1d6b7
32579ed
b29d1ef
f796c36
20cb77f
6590760
f573e75
54d2304
b195361
e2bf743
e9126e7
abdf073
9d763f2
e461fbc
a674624
8ab322d
081b40d
3d63b4c
b32be23
4a30a23
30e6d1d
d5a6055
9591255
8b225c5
b9669b2
dcd6f9d
172280d
f1b03c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} |
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} | ||
|
||
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) | ||
} |
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC #23904 already injects the And this function is only called by eg: func Home(ctx *context.Context) {
if setting.EnableFeed {
isFeed, _, showFeedType := feed.GetFeedType(ctx.Params(":reponame"), ctx.Req)
if isFeed {
render(...)
return
}
... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean 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) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.