Skip to content

[need-help] Add pinned mod for repo #12498

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

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5433a8c
Add pinned mod for repo
a1012112796 Aug 19, 2020
156635a
Merge branch 'master' into feature/Pinned_repo
a1012112796 Aug 19, 2020
3bd284a
Merge branch 'master' into feature/Pinned_repo
a1012112796 Aug 25, 2020
184f140
Apply suggestions from code review, Thanks @lunny
a1012112796 Aug 26, 2020
c26b4f5
Merge branch 'master' into feature/Pinned_repo
a1012112796 Aug 26, 2020
484ffc4
simplify ui
a1012112796 Aug 26, 2020
eb02288
remove unused files
a1012112796 Aug 26, 2020
bd4b543
fix logic
a1012112796 Aug 27, 2020
ca15649
ui 2
a1012112796 Aug 27, 2020
bfea801
Merge branch 'master' into feature/Pinned_repo
a1012112796 Aug 27, 2020
69b376b
fix lint
a1012112796 Aug 27, 2020
0ca9d90
fix test
a1012112796 Aug 27, 2020
b03738c
ui
a1012112796 Aug 28, 2020
f72e959
Merge branch 'master' into feature/Pinned_repo
a1012112796 Aug 28, 2020
bb87239
Update options/locale/locale_en-US.ini
a1012112796 Aug 29, 2020
4f28c45
Update models/user_pinnedrepo.go
a1012112796 Aug 30, 2020
cb72ef1
Update migration
a1012112796 Aug 30, 2020
cab00cc
Merge branch 'master' into feature/Pinned_repo
a1012112796 Aug 30, 2020
1356eb9
ui
a1012112796 Aug 30, 2020
6dcddf8
Merge branch 'master' into feature/Pinned_repo
a1012112796 Sep 9, 2020
0d400fd
Apply suggestions from code review
a1012112796 Sep 10, 2020
73ab596
try simplify logic
a1012112796 Sep 10, 2020
c88a9b8
Merge branch 'master' into feature/Pinned_repo
a1012112796 Sep 10, 2020
56b6f99
fix delete repo
a1012112796 Sep 10, 2020
e1f22fe
Merge branch 'master' into feature/Pinned_repo
a1012112796 Nov 26, 2020
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
32 changes: 32 additions & 0 deletions models/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,38 @@ func (err ErrReachLimitOfRepo) Error() string {
return fmt.Sprintf("user has reached maximum limit of repositories [limit: %d]", err.Limit)
}

// ErrUserPinnedRepoAlreadyExist represents a "UserPinnedRepo already exist" kind of error.
type ErrUserPinnedRepoAlreadyExist struct {
UID int64
RepoID int64
}

// IsErrUserPinnedRepoAlreadyExist checks if an error is a ErrUserPinnedRepoAlreadyExist.
func IsErrUserPinnedRepoAlreadyExist(err error) bool {
_, ok := err.(ErrUserPinnedRepoAlreadyExist)
return ok
}

func (err ErrUserPinnedRepoAlreadyExist) Error() string {
return fmt.Sprintf("user has pinned this repo [uid: %d, repo_id: %d]", err.UID, err.RepoID)
}

// ErrUserPinnedRepoNotExist represents a "UserPinnedRepo not exist" kind of error.
type ErrUserPinnedRepoNotExist struct {
UID int64
RepoID int64
}

// IsErrUserPinnedRepoNotExist checks if an error is a ErrUserPinnedRepoNotExist.
func IsErrUserPinnedRepoNotExist(err error) bool {
_, ok := err.(ErrUserPinnedRepoNotExist)
return ok
}

func (err ErrUserPinnedRepoNotExist) Error() string {
return fmt.Sprintf("user hasn't pinned this repo [uid: %d, repo_id: %d]", err.UID, err.RepoID)
}

// __ __.__ __ .__
// / \ / \__| | _|__|
// \ \/\/ / | |/ / |
Expand Down
15 changes: 15 additions & 0 deletions models/fixtures/user_pinned_repo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-
uid: 2
repo_id: 1

-
uid: 6
repo_id: 1

-
uid: 2
repo_id: 4

-
uid: 2
repo_id: 16
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ var migrations = []Migration{
NewMigration("code comment replies should have the commitID of the review they are replying to", updateCodeCommentReplies),
// v159 -> v160
NewMigration("update reactions constraint", updateReactionConstraint),
// v160 -> v161
NewMigration("Add user_pinned_repo table", addUserPinnedRepoTable),
}

// GetCurrentDBVersion returns the current db version
Expand Down
19 changes: 19 additions & 0 deletions models/migrations/v160.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
"xorm.io/xorm"
)

func addUserPinnedRepoTable(x *xorm.Engine) error {

type UserPinnedRepo struct {
UID int64 `xorm:"pk INDEX NOT NULL"`
RepoID int64 `xorm:"pk NOT NULL"`
}

return x.Sync2(new(UserPinnedRepo))
}
1 change: 1 addition & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func init() {
new(Project),
new(ProjectBoard),
new(ProjectIssue),
new(UserPinnedRepo),
)

gonicNames := []string{"SSL", "UID"}
Expand Down
3 changes: 3 additions & 0 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ type Repository struct {
// Avatar: ID(10-20)-md5(32) - must fit into 64 symbols
Avatar string `xorm:"VARCHAR(64)"`

IsPinned bool `xorm:"-"`

CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
}
Expand Down Expand Up @@ -1696,6 +1698,7 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
&LanguageStat{RepoID: repoID},
&Comment{RefRepoID: repoID},
&Task{RepoID: repoID},
&UserPinnedRepo{RepoID: repoID},
); err != nil {
return fmt.Errorf("deleteBeans: %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,7 @@ func deleteUser(e *xorm.Session, u *User) error {
&TeamUser{UID: u.ID},
&Collaboration{UserID: u.ID},
&Stopwatch{UserID: u.ID},
&UserPinnedRepo{UID: u.ID},
); err != nil {
return fmt.Errorf("deleteBeans: %v", err)
}
Expand Down
82 changes: 82 additions & 0 deletions models/user_pinnedrepo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models

import (
"code.gitea.io/gitea/modules/structs"

"xorm.io/builder"
)

// UserPinnedRepo represents a pinned repo by an user or org.
type UserPinnedRepo struct {
UID int64 `xorm:"pk INDEX NOT NULL"`
RepoID int64 `xorm:"pk NOT NULL"`
}

// AddPinnedRepo add a pinned repo
func (u *User) AddPinnedRepo(repo *Repository) (err error) {
exist := false
if exist, err = u.IsPinnedRepoExist(repo.ID); err != nil {
return
}

if exist {
return ErrUserPinnedRepoAlreadyExist{UID: u.ID, RepoID: repo.ID}
}

r := &UserPinnedRepo{
UID: u.ID,
RepoID: repo.ID,
}

_, err = x.Insert(r)
return
}

// RemovePinnedRepo remove a pinned repo
func (u *User) RemovePinnedRepo(repoID int64) (err error) {
exist := false
if exist, err = u.IsPinnedRepoExist(repoID); err != nil {
return
}

if !exist {
return ErrUserPinnedRepoNotExist{UID: u.ID, RepoID: repoID}
}

_, err = x.Delete(&UserPinnedRepo{UID: u.ID, RepoID: repoID})
return
}

// IsPinnedRepoExist check if this repo is pinned
func (u *User) IsPinnedRepoExist(repoID int64) (isExist bool, err error) {
return x.Exist(&UserPinnedRepo{UID: u.ID, RepoID: repoID})
}

// GetPinnedRepoIDs get repos id
func (u *User) GetPinnedRepoIDs(actor *User) (results []int64, err error) {
var cond = builder.NewCond()
results = make([]int64, 0, 10)

if actor == nil {
if u.IsOrganization() && u.Visibility != structs.VisibleTypePublic {
return
}
cond = cond.And(builder.Eq{"is_private": false})
} else if actor.ID != u.ID && !actor.IsAdmin {
// OK we're in the context of a User
cond = cond.And(accessibleRepositoryCondition(actor))
}

idBuilder := builder.Select("repo_id").
From("user_pinned_repo").
Where(builder.Eq{"uid": u.ID})

cond = cond.And(builder.In("id", idBuilder))

err = x.Table("repository").Cols("id").Where(cond).Find(&results)
return
}
64 changes: 64 additions & 0 deletions models/user_pinnedrepo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestAddPinnedRepo(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
repo10 := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)

assert.NoError(t, user2.AddPinnedRepo(repo10))
AssertExistsAndLoadBean(t, &UserPinnedRepo{UID: user2.ID, RepoID: repo10.ID})

assert.EqualError(t, user2.AddPinnedRepo(repo1),
ErrUserPinnedRepoAlreadyExist{UID: user2.ID, RepoID: repo1.ID}.Error())
}

func TestRemovePinnedRepo(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)

assert.NoError(t, user2.RemovePinnedRepo(1))

assert.EqualError(t, user2.RemovePinnedRepo(3),
ErrUserPinnedRepoNotExist{UID: user2.ID, RepoID: 3}.Error())
}

func TestIsPinnedRepoExist(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)

exist, err := user2.IsPinnedRepoExist(1)
assert.NoError(t, err)
assert.Equal(t, true, exist)

exist, err = user2.IsPinnedRepoExist(5)
assert.NoError(t, err)
assert.Equal(t, false, exist)
}

func TestGetPinnedRepos(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)

repoIDs, err := user2.GetPinnedRepoIDs(user2)
assert.NoError(t, err)
assert.Equal(t, []int64{1, 4, 16}, repoIDs)

repoIDs, err = user2.GetPinnedRepoIDs(nil)
assert.NoError(t, err)
assert.Equal(t, []int64{1, 4}, repoIDs)
}
7 changes: 7 additions & 0 deletions modules/auth/user_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,10 @@ type U2FDeleteForm struct {
func (f *U2FDeleteForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
return validate(errs, ctx.Data, f, ctx.Locale)
}

// RepoPinnedForm form for changing repository pinned settings
type RepoPinnedForm struct {
RepoFullName string `binding:"Required" form:"name"`
Status string `binding:"Required;In(pinned,unpinned)"`
IsBtn bool
}
8 changes: 8 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ issues.in_your_repos = In your repositories

[explore]
repos = Repositories
pinned_repos = Pinned repositories
users = Users
organizations = Organizations
search = Search
Expand Down Expand Up @@ -461,6 +462,13 @@ uploaded_avatar_not_a_image = The uploaded file is not an image.
uploaded_avatar_is_too_big = The uploaded file has exceeded the maximum size.
update_avatar_success = Your avatar has been updated.

pinned_repo = pinned this repo
unpinned_repo = unpinned this repo
pinned_other_repo_des = Repo full name
pinned_other_repo = Pinned other repo
pinned_btn = Pinned
unpinned_btn = Unpinned

change_password = Update Password
old_password = Current Password
new_password = New Password
Expand Down
48 changes: 48 additions & 0 deletions routers/org/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,54 @@ func Home(ctx *context.Context) {
return
}

pinnedRepos := make([]*models.Repository, 0, 10)
pinnedRepoIDs, err := ctx.Org.Organization.GetPinnedRepoIDs(ctx.User)
if err != nil {
ctx.ServerError("GetPinnedRepos", err)
return
}

pinnedRepos2 := make(models.RepositoryList, 0, 5)
for _, pinnedRepoID := range pinnedRepoIDs {
has := false
for _, repo := range repos {
if repo.ID == pinnedRepoID {
has = true
repo.IsPinned = true
pinnedRepos = append(pinnedRepos, repo)
break
}
}

if !has {
repo, err := models.GetRepositoryByID(pinnedRepoID)
if err != nil && !models.IsErrRepoNotExist(err) {
ctx.ServerError("GetRepositoryByID", err)
return
}

if repo != nil {
repo.IsPinned = true
pinnedRepos2 = append(pinnedRepos2, repo)
}
}
}

if len(pinnedRepos2) > 0 {
if err = pinnedRepos2.LoadAttributes(); err != nil {
ctx.ServerError("pinnedRepos2.LoadAttributes()", err)
return
}
pinnedRepos = append(pinnedRepos, pinnedRepos2...)
}

ctx.Data["PinnedRepos"] = pinnedRepos
ctx.Data["PinnedReposNum"] = len(pinnedRepos)
ctx.Data["CanConfigPinnedRepos"] = ctx.IsSigned && ctx.Org.IsOwner
if ctx.IsSigned && ctx.Org.IsOwner {
ctx.Data["ConfigPinnedReposLink"] = ctx.Org.OrgLink + "/settings/pinned_repo"
}

ctx.Data["Owner"] = org
ctx.Data["Repos"] = repos
ctx.Data["Total"] = count
Expand Down
31 changes: 31 additions & 0 deletions routers/org/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,34 @@ func Labels(ctx *context.Context) {
ctx.Data["LabelTemplates"] = models.LabelTemplates
ctx.HTML(200, tplSettingsLabels)
}

// PinnedRepoPost response pinned repo settings
func PinnedRepoPost(ctx *context.Context, form auth.RepoPinnedForm) {
repo, err := models.GetRepositoryByOwnerAndName(ctx.Org.Organization.Name, form.RepoFullName)
if err != nil {
if models.IsErrRepoNotExist(err) {
ctx.Status(404)
return
}

ctx.ServerError("models.GetRepositoryByOwnerAndName", err)
return
}

if form.Status == "unpinned" {
if err = ctx.Org.Organization.RemovePinnedRepo(repo.ID); err != nil && !models.IsErrUserPinnedRepoNotExist(err) {
ctx.ServerError("ctx.Org.Organization.RemovePinnedRepo", err)
return
}

ctx.Status(200)
return
}

if err = ctx.Org.Organization.AddPinnedRepo(repo); err != nil && !models.IsErrUserPinnedRepoAlreadyExist(err) {
ctx.ServerError("ctx.Org.Organization.AddPinnedRepo", err)
return
}

ctx.Status(200)
}
Loading