Skip to content

Add API endpoint for accessing repo topics #7963

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

Merged
merged 35 commits into from
Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ded028c
Create API endpoints for repo topics.
davidsvantesson Aug 24, 2019
6965aa8
Generate swagger
davidsvantesson Aug 24, 2019
5a87bab
Add documentation to functions
davidsvantesson Aug 24, 2019
0839b96
Grammar fix
davidsvantesson Aug 24, 2019
244cbd3
Fix function comment
davidsvantesson Aug 24, 2019
7c721f9
Merge branch 'master' into api-repo-topics
davidsvantesson Aug 24, 2019
f0f49bd
Can't use FindTopics when looking for a single repo topic, as it does…
davidsvantesson Aug 24, 2019
6ec5a0c
Add PUT ​/repos​/{owner}​/{repo}​/topics and remove GET ​/repos​/{own…
davidsvantesson Aug 25, 2019
8daf641
Ignore if topic is sent twice in same request, refactoring.
davidsvantesson Aug 25, 2019
9fdab25
Fix topic dropdown with api changes.
davidsvantesson Aug 25, 2019
1cb206c
Style fix
davidsvantesson Aug 25, 2019
c13a297
Update API documentation
davidsvantesson Aug 25, 2019
4a53681
Better way to handle duplicate topics in slice
davidsvantesson Aug 26, 2019
3705219
Make response element TopicName an array of strings, instead of using…
davidsvantesson Aug 26, 2019
ac93677
Add test cases for API Repo Topics.
davidsvantesson Aug 26, 2019
8bc836c
Fix format of tests
davidsvantesson Aug 26, 2019
3af43e2
Fix comments
davidsvantesson Aug 26, 2019
0e671f0
Merge branch 'master' into api-repo-topics
davidsvantesson Aug 26, 2019
5cdbbbc
Fix unit tests after adding some more topics to the test fixture.
davidsvantesson Aug 26, 2019
db3f6f3
Update models/topic.go
davidsvantesson Aug 27, 2019
2ef6904
Engine as first parameter in function
davidsvantesson Aug 27, 2019
99eb479
Replace magic numbers with http status code constants.
davidsvantesson Aug 27, 2019
ed7604a
Fix variable scope
davidsvantesson Aug 27, 2019
1088eac
Test one read with login and one with token
davidsvantesson Aug 27, 2019
098af67
Add some more tests
davidsvantesson Aug 27, 2019
41b6276
Apply suggestions from code review
davidsvantesson Aug 27, 2019
b654ebf
Add test case to check access for user with write access
davidsvantesson Aug 28, 2019
48989c3
Fix access, repo admin required to change topics
davidsvantesson Aug 28, 2019
db48d14
Correct first test to be without token
davidsvantesson Aug 28, 2019
fa8aa5d
Any repo reader should be able to access topics.
davidsvantesson Aug 29, 2019
1a6a8fc
Merge branch 'master' into api-repo-topics
davidsvantesson Aug 29, 2019
9d9f8dc
No need for string pointer
davidsvantesson Aug 29, 2019
6d9152f
Merge branch 'master' into api-repo-topics
davidsvantesson Aug 29, 2019
189d900
Merge branch 'master' into api-repo-topics
lafriks Aug 30, 2019
0b8409a
Merge branch 'master' into api-repo-topics
sapk Sep 3, 2019
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
Prev Previous commit
Next Next commit
Can't use FindTopics when looking for a single repo topic, as it does…
…nt use exact match

Signed-off-by: David Svantesson <[email protected]>
  • Loading branch information
davidsvantesson committed Aug 24, 2019
commit f0f49bd42c8eedde0d50a611069f1ba6834d392d
28 changes: 16 additions & 12 deletions models/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,37 +149,41 @@ func FindTopics(opts *FindTopicOptions) (topics []*Topic, err error) {
return topics, sess.Desc("topic.repo_count").Find(&topics)
}

// GetRepoTopicByName retrives topic from name for a repo if it exist
func GetRepoTopicByName(repoID int64, topicName string) (topic *Topic, err error) {
sess := x.Select("topic.*").Where("repo_topic.repo_id = ?", repoID).And("topic.name = ?", topicName)
sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id")
has, err := sess.Get(&topic)
if has {
return topic, err
}
return nil, err
}

// AddTopic adds a topic name to a repository (if it does not already have it)
func AddTopic(repoID int64, topicName string) (*Topic, error) {
topics, err := FindTopics(&FindTopicOptions{
RepoID: repoID,
Keyword: topicName,
})
topic, err := GetRepoTopicByName(repoID, topicName)
if err != nil {
return nil, err
}
if len(topics) != 0 {
if topic != nil {
// Repo already have topic
return topics[0], nil
return topic, nil
}

return addTopicByNameToRepo(repoID, topicName, x)
}

// DeleteTopic removes a topic name from a repository (if it has it)
func DeleteTopic(repoID int64, topicName string) (*Topic, error) {
topics, err := FindTopics(&FindTopicOptions{
RepoID: repoID,
Keyword: topicName,
})
topic, err := GetRepoTopicByName(repoID, topicName)
if err != nil {
return nil, err
}
if len(topics) == 0 {
if topic == nil {
// Repo doesn't have topic, can't be removed
return nil, nil
}
topic := topics[0]

err = removeTopicFromRepo(repoID, topic, x)

Expand Down
9 changes: 3 additions & 6 deletions routers/api/v1/repo/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ func HasTopic(ctx *context.APIContext) {
// "$ref": "#/responses/empty"
topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic")))

topics, err := models.FindTopics(&models.FindTopicOptions{
RepoID: ctx.Repo.Repository.ID,
Keyword: topicName,
})
topic, err := models.GetRepoTopicByName(ctx.Repo.Repository.ID, topicName)
if err != nil {
log.Error("HasTopic failed: %v", err)
ctx.JSON(500, map[string]interface{}{
Expand All @@ -99,12 +96,12 @@ func HasTopic(ctx *context.APIContext) {
return
}

if len(topics) == 0 {
if topic == nil {
ctx.NotFound()
}

ctx.JSON(200, map[string]interface{}{
"topic": convert.ToTopicResponse(topics[0]),
"topic": convert.ToTopicResponse(topic),
})
}

Expand Down