-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Changes from 26 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
ded028c
Create API endpoints for repo topics.
davidsvantesson 6965aa8
Generate swagger
davidsvantesson 5a87bab
Add documentation to functions
davidsvantesson 0839b96
Grammar fix
davidsvantesson 244cbd3
Fix function comment
davidsvantesson 7c721f9
Merge branch 'master' into api-repo-topics
davidsvantesson f0f49bd
Can't use FindTopics when looking for a single repo topic, as it does…
davidsvantesson 6ec5a0c
Add PUT /repos/{owner}/{repo}/topics and remove GET /repos/{own…
davidsvantesson 8daf641
Ignore if topic is sent twice in same request, refactoring.
davidsvantesson 9fdab25
Fix topic dropdown with api changes.
davidsvantesson 1cb206c
Style fix
davidsvantesson c13a297
Update API documentation
davidsvantesson 4a53681
Better way to handle duplicate topics in slice
davidsvantesson 3705219
Make response element TopicName an array of strings, instead of using…
davidsvantesson ac93677
Add test cases for API Repo Topics.
davidsvantesson 8bc836c
Fix format of tests
davidsvantesson 3af43e2
Fix comments
davidsvantesson 0e671f0
Merge branch 'master' into api-repo-topics
davidsvantesson 5cdbbbc
Fix unit tests after adding some more topics to the test fixture.
davidsvantesson db3f6f3
Update models/topic.go
davidsvantesson 2ef6904
Engine as first parameter in function
davidsvantesson 99eb479
Replace magic numbers with http status code constants.
davidsvantesson ed7604a
Fix variable scope
davidsvantesson 1088eac
Test one read with login and one with token
davidsvantesson 098af67
Add some more tests
davidsvantesson 41b6276
Apply suggestions from code review
davidsvantesson b654ebf
Add test case to check access for user with write access
davidsvantesson 48989c3
Fix access, repo admin required to change topics
davidsvantesson db48d14
Correct first test to be without token
davidsvantesson fa8aa5d
Any repo reader should be able to access topics.
davidsvantesson 1a6a8fc
Merge branch 'master' into api-repo-topics
davidsvantesson 9d9f8dc
No need for string pointer
davidsvantesson 6d9152f
Merge branch 'master' into api-repo-topics
davidsvantesson 189d900
Merge branch 'master' into api-repo-topics
lafriks 0b8409a
Merge branch 'master' into api-repo-topics
sapk 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,103 @@ | ||
// Copyright 2019 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 integrations | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models" | ||
api "code.gitea.io/gitea/modules/structs" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAPIRepoTopic(t *testing.T) { | ||
prepareTestEnv(t) | ||
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of repo1 | ||
repo2 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) | ||
|
||
// Get user2's token | ||
session := loginUser(t, user2.Name) | ||
token2 := getTokenForLoggedInUser(t, session) | ||
session = emptyTestSession(t) | ||
|
||
// Test read topics using login | ||
url := fmt.Sprintf("/api/v1/repos/%s/%s/topics?token=%s", user2.Name, repo2.Name, token2) | ||
req := NewRequest(t, "GET", url) | ||
res := session.MakeRequest(t, req, http.StatusOK) | ||
var topics *api.TopicName | ||
DecodeJSON(t, res, &topics) | ||
assert.ElementsMatch(t, []string{"topicname1", "topicname2"}, topics.TopicNames) | ||
|
||
// Test delete a topic | ||
url = fmt.Sprintf("/api/v1/repos/%s/%s/topics?token=%s", user2.Name, repo2.Name, token2) | ||
req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Topicname1", token2) | ||
res = session.MakeRequest(t, req, http.StatusNoContent) | ||
|
||
// Test add an existing topic | ||
req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Golang", token2) | ||
res = session.MakeRequest(t, req, http.StatusNoContent) | ||
|
||
// Test add a topic | ||
req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "topicName3", token2) | ||
res = session.MakeRequest(t, req, http.StatusNoContent) | ||
|
||
// Test read topics using token | ||
req = NewRequest(t, "GET", url) | ||
res = session.MakeRequest(t, req, http.StatusOK) | ||
DecodeJSON(t, res, &topics) | ||
assert.ElementsMatch(t, []string{"topicname2", "golang", "topicname3"}, topics.TopicNames) | ||
|
||
// Test replace topics | ||
newTopics := []string{" windows ", " ", "MAC "} | ||
req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{ | ||
Topics: newTopics, | ||
}) | ||
res = session.MakeRequest(t, req, http.StatusNoContent) | ||
req = NewRequest(t, "GET", url) | ||
res = session.MakeRequest(t, req, http.StatusOK) | ||
DecodeJSON(t, res, &topics) | ||
assert.ElementsMatch(t, []string{"windows", "mac"}, topics.TopicNames) | ||
|
||
// Test replace topics with something invalid | ||
newTopics = []string{"topicname1", "topicname2", "topicname!"} | ||
req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{ | ||
Topics: newTopics, | ||
}) | ||
res = session.MakeRequest(t, req, http.StatusUnprocessableEntity) | ||
req = NewRequest(t, "GET", url) | ||
res = session.MakeRequest(t, req, http.StatusOK) | ||
DecodeJSON(t, res, &topics) | ||
assert.ElementsMatch(t, []string{"windows", "mac"}, topics.TopicNames) | ||
|
||
// Test with some topics multiple times, less than 25 unique | ||
newTopics = []string{"t1", "t2", "t1", "t3", "t4", "t5", "t6", "t7", "t8", "t9", "t10", "t11", "t12", "t13", "t14", "t15", "t16", "17", "t18", "t19", "t20", "t21", "t22", "t23", "t24", "t25"} | ||
req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{ | ||
Topics: newTopics, | ||
}) | ||
res = session.MakeRequest(t, req, http.StatusNoContent) | ||
req = NewRequest(t, "GET", url) | ||
res = session.MakeRequest(t, req, http.StatusOK) | ||
DecodeJSON(t, res, &topics) | ||
assert.Equal(t, 25, len(topics.TopicNames)) | ||
|
||
// Test writing more topics than allowed | ||
newTopics = append(newTopics, "t26") | ||
req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{ | ||
Topics: newTopics, | ||
}) | ||
res = session.MakeRequest(t, req, http.StatusUnprocessableEntity) | ||
|
||
// Test add a topic when there is already maximum | ||
req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "t26", token2) | ||
res = session.MakeRequest(t, req, http.StatusUnprocessableEntity) | ||
|
||
// Test delete a topic that repo doesn't have | ||
req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Topicname1", token2) | ||
res = session.MakeRequest(t, req, http.StatusNotFound) | ||
|
||
} |
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 |
---|---|---|
|
@@ -17,3 +17,11 @@ | |
- | ||
repo_id: 33 | ||
topic_id: 4 | ||
|
||
- | ||
repo_id: 2 | ||
topic_id: 5 | ||
|
||
- | ||
repo_id: 2 | ||
topic_id: 6 |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Can you try to read topics without token and add the token for operation that need it ? (at least for the first try)
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.
To be more specific at L28
url := fmt.Sprintf("/api/v1/repos/%s/%s/topics", user2.Name, repo2.Name)
and at L36
url = fmt.Sprintf("/api/v1/repos/%s/%s/topics?token=%s", user2.Name, repo2.Name, token2)
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.
Tests updated.
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.
I may have read too quickly but I haven't found a read test without token.
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.
Sorry, I somehow missed to finish the change.