-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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 topic support #3711
Merged
Merged
Add topic support #3711
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d07c1d5
add topic models and unit tests
lunny c10a04b
fix comments
lunny 1539511
fix comment
lunny a405d40
add the UI to show or add topics for a repo
lunny b634af1
show topics on repositories list
lunny cc5dc73
fix test
lunny 4605ee5
don't show manage topics link when no permission
lunny 7373966
use green basic as topic label
lunny 0af3841
fix topic label color
lunny 0b8190f
remove trace content
lunny be00702
remove debug function
lunny d62b876
Merge branch 'master' into lunny/topic
lunny 4a1f5a3
Merge branch 'master' into lunny/topic
lunny 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 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,11 @@ | ||
- | ||
repo_id: 1 | ||
topic_id: 1 | ||
|
||
- | ||
repo_id: 1 | ||
topic_id: 2 | ||
|
||
- | ||
repo_id: 1 | ||
topic_id: 3 |
This file contains 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,13 @@ | ||
- | ||
id: 1 | ||
name: golang | ||
repo_count: 1 | ||
|
||
- | ||
id: 2 | ||
name: database | ||
repo_count: 1 | ||
|
||
- id: 3 | ||
name: SQL | ||
repo_count: 1 |
This file contains 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 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,192 @@ | ||
// Copyright 2018 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 ( | ||
"fmt" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/util" | ||
|
||
"github.com/go-xorm/builder" | ||
) | ||
|
||
func init() { | ||
tables = append(tables, | ||
new(Topic), | ||
new(RepoTopic), | ||
) | ||
} | ||
|
||
// Topic represents a topic of repositories | ||
type Topic struct { | ||
ID int64 | ||
Name string `xorm:"unique"` | ||
RepoCount int | ||
CreatedUnix util.TimeStamp `xorm:"INDEX created"` | ||
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"` | ||
} | ||
|
||
// RepoTopic represents associated repositories and topics | ||
type RepoTopic struct { | ||
RepoID int64 `xorm:"unique(s)"` | ||
TopicID int64 `xorm:"unique(s)"` | ||
} | ||
|
||
// ErrTopicNotExist represents an error that a topic is not exist | ||
type ErrTopicNotExist struct { | ||
Name string | ||
} | ||
|
||
// IsErrTopicNotExist checks if an error is an ErrTopicNotExist. | ||
func IsErrTopicNotExist(err error) bool { | ||
_, ok := err.(ErrTopicNotExist) | ||
return ok | ||
} | ||
|
||
// Error implements error interface | ||
func (err ErrTopicNotExist) Error() string { | ||
return fmt.Sprintf("topic is not exist [name: %s]", err.Name) | ||
} | ||
|
||
// GetTopicByName retrieves topic by name | ||
func GetTopicByName(name string) (*Topic, error) { | ||
var topic Topic | ||
if has, err := x.Where("name = ?", name).Get(&topic); err != nil { | ||
return nil, err | ||
} else if !has { | ||
return nil, ErrTopicNotExist{name} | ||
} | ||
return &topic, nil | ||
} | ||
|
||
// FindTopicOptions represents the options when fdin topics | ||
type FindTopicOptions struct { | ||
RepoID int64 | ||
Keyword string | ||
Limit int | ||
Page int | ||
} | ||
|
||
func (opts *FindTopicOptions) toConds() builder.Cond { | ||
var cond = builder.NewCond() | ||
if opts.RepoID > 0 { | ||
cond = cond.And(builder.Eq{"repo_topic.repo_id": opts.RepoID}) | ||
} | ||
|
||
if opts.Keyword != "" { | ||
cond = cond.And(builder.Like{"topic.name", opts.Keyword}) | ||
} | ||
|
||
return cond | ||
} | ||
|
||
// FindTopics retrieves the topics via FindTopicOptions | ||
func FindTopics(opts *FindTopicOptions) (topics []*Topic, err error) { | ||
sess := x.Select("topic.*").Where(opts.toConds()) | ||
if opts.RepoID > 0 { | ||
sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id") | ||
} | ||
if opts.Limit > 0 { | ||
sess.Limit(opts.Limit, opts.Page*opts.Limit) | ||
} | ||
return topics, sess.Desc("topic.repo_count").Find(&topics) | ||
} | ||
|
||
// SaveTopics save topics to a repository | ||
func SaveTopics(repoID int64, topicNames ...string) error { | ||
topics, err := FindTopics(&FindTopicOptions{ | ||
RepoID: repoID, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sess := x.NewSession() | ||
defer sess.Close() | ||
|
||
if err := sess.Begin(); err != nil { | ||
return err | ||
} | ||
|
||
var addedTopicNames []string | ||
for _, topicName := range topicNames { | ||
if strings.TrimSpace(topicName) == "" { | ||
continue | ||
} | ||
|
||
var found bool | ||
for _, t := range topics { | ||
if strings.EqualFold(topicName, t.Name) { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
addedTopicNames = append(addedTopicNames, topicName) | ||
} | ||
} | ||
|
||
var removeTopics []*Topic | ||
for _, t := range topics { | ||
var found bool | ||
for _, topicName := range topicNames { | ||
if strings.EqualFold(topicName, t.Name) { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
removeTopics = append(removeTopics, t) | ||
} | ||
} | ||
|
||
for _, topicName := range addedTopicNames { | ||
var topic Topic | ||
if has, err := sess.Where("name = ?", topicName).Get(&topic); err != nil { | ||
return err | ||
} else if !has { | ||
topic.Name = topicName | ||
topic.RepoCount = 1 | ||
if _, err := sess.Insert(&topic); err != nil { | ||
return err | ||
} | ||
} else { | ||
topic.RepoCount++ | ||
if _, err := sess.ID(topic.ID).Cols("repo_count").Update(&topic); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if _, err := sess.Insert(&RepoTopic{ | ||
RepoID: repoID, | ||
TopicID: topic.ID, | ||
}); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
for _, topic := range removeTopics { | ||
topic.RepoCount-- | ||
if _, err := sess.ID(topic.ID).Cols("repo_count").Update(topic); err != nil { | ||
return err | ||
} | ||
|
||
if _, err := sess.Delete(&RepoTopic{ | ||
RepoID: repoID, | ||
TopicID: topic.ID, | ||
}); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if _, err := sess.ID(repoID).Cols("topics").Update(&Repository{ | ||
Topics: topicNames, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return sess.Commit() | ||
} |
This file contains 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,57 @@ | ||
// Copyright 2018 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 TestAddTopic(t *testing.T) { | ||
assert.NoError(t, PrepareTestDatabase()) | ||
|
||
topics, err := FindTopics(&FindTopicOptions{}) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 3, len(topics)) | ||
|
||
topics, err = FindTopics(&FindTopicOptions{ | ||
Limit: 2, | ||
}) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 2, len(topics)) | ||
|
||
topics, err = FindTopics(&FindTopicOptions{ | ||
RepoID: 1, | ||
}) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 3, len(topics)) | ||
|
||
assert.NoError(t, SaveTopics(2, "golang")) | ||
topics, err = FindTopics(&FindTopicOptions{}) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 3, len(topics)) | ||
|
||
topics, err = FindTopics(&FindTopicOptions{ | ||
RepoID: 2, | ||
}) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 1, len(topics)) | ||
|
||
assert.NoError(t, SaveTopics(2, "golang", "gitea")) | ||
topic, err := GetTopicByName("gitea") | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 1, topic.RepoCount) | ||
|
||
topics, err = FindTopics(&FindTopicOptions{}) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 4, len(topics)) | ||
|
||
topics, err = FindTopics(&FindTopicOptions{ | ||
RepoID: 2, | ||
}) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 2, len(topics)) | ||
} |
This file contains 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 is this saved to database?
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.
For better performance when we list all topics for every repository on repositories list UI.