Skip to content

Commit b8b12dc

Browse files
add ability to search for repository activity based on repo groups
1 parent 0ba8191 commit b8b12dc

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

models/activities/action.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616

1717
"code.gitea.io/gitea/models/db"
18+
group_model "code.gitea.io/gitea/models/group"
1819
issues_model "code.gitea.io/gitea/models/issues"
1920
"code.gitea.io/gitea/models/organization"
2021
repo_model "code.gitea.io/gitea/models/repo"
@@ -435,6 +436,7 @@ type GetFeedsOptions struct {
435436
db.ListOptions
436437
RequestedUser *user_model.User // the user we want activity for
437438
RequestedTeam *organization.Team // the team we want activity for
439+
RequestedGroup *group_model.Group // the group we want activity for
438440
RequestedRepo *repo_model.Repository // the repo we want activity for
439441
Actor *user_model.User // the user viewing the activity
440442
IncludePrivate bool // include private actions
@@ -514,8 +516,16 @@ func ActivityQueryCondition(ctx context.Context, opts GetFeedsOptions) (builder.
514516
if opts.Actor == nil || !opts.Actor.IsAdmin {
515517
cond = cond.And(builder.In("repo_id", repo_model.AccessibleRepoIDsQuery(opts.Actor)))
516518
}
517-
518-
if opts.RequestedRepo != nil {
519+
if opts.RequestedGroup != nil {
520+
cond = cond.And(builder.In("`action`.repo_id",
521+
builder.Select("id").
522+
From("repository").
523+
Where(builder.Or(
524+
builder.In("`repository`.group_id", group_model.ChildGroupCond(opts.RequestedGroup.ID)),
525+
builder.Eq{"`repository`.group_id": opts.RequestedGroup.ID}),
526+
),
527+
))
528+
} else if opts.RequestedRepo != nil {
519529
// repo's actions could have duplicate items, see the comment of NotifyWatchers
520530
// so here we only filter the "original items", aka: user_id == act_user_id
521531
cond = cond.And(

models/group/group.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,23 @@ func ParentGroupCond(ctx context.Context, idStr string, groupID int64) builder.C
359359
return builder.In(idStr, groupList)
360360
}
361361

362+
// ChildGroupCond returns a condition recursively matching a group and its descendants
363+
func ChildGroupCond(firstParent int64) builder.Cond {
364+
if firstParent < 0 {
365+
firstParent = 0
366+
}
367+
return builder.Expr(`with recursive groups as (
368+
select * from repo_group
369+
WHERE parent_group_id = ?
370+
371+
union all
372+
373+
select subgroup.*
374+
from repo_group subgroup
375+
join groups g on g.id = subgroup.parent_group_id
376+
) select g.id from groups g`, firstParent)
377+
}
378+
362379
func UpdateGroup(ctx context.Context, group *Group) error {
363380
sess := db.GetEngine(ctx)
364381
_, err := sess.Table(group.TableName()).ID(group.ID).Update(group)

0 commit comments

Comments
 (0)