-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Pin repos on profile #30961
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
Open
carlosfelgueiras
wants to merge
18
commits into
go-gitea:main
Choose a base branch
from
carlosfelgueiras:feature-pin-repos
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Pin repos on profile #30961
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5f890b5
feat(pin): implemented base code for pins on the backend
carlosfelgueiras fd47c82
feat(pin): implemented the list of pinned repositories on the profile
carlosfelgueiras e8dca1e
feat(pin): implemented the pin/unpin button on repo main page
carlosfelgueiras 0754ed7
test(pin): created basic tests for the pin/unpin feature
carlosfelgueiras f8b1756
fix(lint): fixed and added copyright and license comment on top of files
carlosfelgueiras 5fba27c
fix(table name): Changed repository pins database name
DanielMatiasCarvalho 0887277
Update templates/repo/pin_unpin.tmpl
carlosfelgueiras c9d2f49
fix(pin): removed octicon-custom-pin-off and replaced the places that…
carlosfelgueiras dae3c8a
feat(pin): made the gap between pin card half the previous size
carlosfelgueiras 2140062
fix(user pin service): converted max number of pins to a constant
DanielMatiasCarvalho b941b04
fix(user pin service): fixed CanPin function to return an error
DanielMatiasCarvalho a019e44
fix(pin db): added a function to get the count of pinned repos
DanielMatiasCarvalho 3704d23
fix(view): Added a missing return function
DanielMatiasCarvalho 805dbc7
fix(transaction): changed the use of the transaction to WithTx instea…
DanielMatiasCarvalho 859b1ce
refactor(pin): changed method used to check if pin exists from Get to…
carlosfelgueiras 764b08b
fix(pin): pins not being deleted when user gets deleted
carlosfelgueiras a392779
Merge branch 'main' into feature-pin-repos
carlosfelgueiras 9479423
Merge branch 'main' into feature-pin-repos
carlosfelgueiras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package repo | ||
|
||
import ( | ||
"context" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/timeutil" | ||
) | ||
|
||
type Pin struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
UID int64 `xorm:"UNIQUE(s)"` | ||
RepoID int64 `xorm:"UNIQUE(s)"` | ||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` | ||
} | ||
|
||
// TableName sets the table name for the pin struct | ||
func (s *Pin) TableName() string { | ||
return "repo_pin" | ||
carlosfelgueiras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func init() { | ||
db.RegisterModel(new(Pin)) | ||
} | ||
|
||
func IsPinned(ctx context.Context, userID, repoID int64) bool { | ||
exists, _ := db.GetEngine(ctx).Get(&Pin{UID: userID, RepoID: repoID}) | ||
carlosfelgueiras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return exists | ||
} | ||
|
||
func PinRepo(ctx context.Context, doer *user_model.User, repo *Repository, pin bool) error { | ||
ctx, commiter, err := db.TxContext(ctx) | ||
carlosfelgueiras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
defer commiter.Close() | ||
pinned := IsPinned(ctx, doer.ID, repo.ID) | ||
|
||
if pin { | ||
// Already pinned, nothing to do | ||
if pinned { | ||
return nil | ||
} | ||
|
||
if err = db.Insert(ctx, &Pin{UID: doer.ID, RepoID: repo.ID}); err != nil { | ||
return err | ||
} | ||
} else { | ||
// Not pinned, nothing to do | ||
if !pinned { | ||
return nil | ||
} | ||
|
||
if _, err = db.DeleteByBean(ctx, &Pin{UID: doer.ID, RepoID: repo.ID}); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return commiter.Commit() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package repo_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
repo_model "code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/models/unittest" | ||
user_model "code.gitea.io/gitea/models/user" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPinRepoFunctionality(t *testing.T) { | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
|
||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) | ||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) | ||
|
||
unittest.AssertNotExistsBean(t, &repo_model.Pin{UID: user.ID, RepoID: repo.ID}) | ||
assert.NoError(t, repo_model.PinRepo(db.DefaultContext, user, repo, true)) | ||
unittest.AssertExistsAndLoadBean(t, &repo_model.Pin{UID: user.ID, RepoID: repo.ID}) | ||
assert.NoError(t, repo_model.PinRepo(db.DefaultContext, user, repo, true)) | ||
unittest.AssertExistsAndLoadBean(t, &repo_model.Pin{UID: user.ID, RepoID: repo.ID}) | ||
assert.NoError(t, repo_model.PinRepo(db.DefaultContext, user, repo, false)) | ||
unittest.AssertNotExistsBean(t, &repo_model.Star{UID: user.ID, RepoID: repo.ID}) | ||
} | ||
|
||
func TestIsPinned(t *testing.T) { | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
|
||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) | ||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) | ||
|
||
unittest.AssertNotExistsBean(t, &repo_model.Pin{UID: user.ID, RepoID: repo.ID}) | ||
assert.NoError(t, repo_model.PinRepo(db.DefaultContext, user, repo, true)) | ||
unittest.AssertExistsAndLoadBean(t, &repo_model.Pin{UID: user.ID, RepoID: repo.ID}) | ||
|
||
assert.True(t, repo_model.IsPinned(db.DefaultContext, user.ID, repo.ID)) | ||
|
||
assert.NoError(t, repo_model.PinRepo(db.DefaultContext, user, repo, false)) | ||
unittest.AssertNotExistsBean(t, &repo_model.Star{UID: user.ID, RepoID: repo.ID}) | ||
|
||
assert.False(t, repo_model.IsPinned(db.DefaultContext, user.ID, repo.ID)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not
UserID
? i do not think UID is a good name. It looks like UUID