-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
API Endpoints for collaborators #375
Merged
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -11,8 +11,72 @@ import ( | |
"code.gitea.io/gitea/modules/context" | ||
) | ||
|
||
// ListCollaborators list a repository's collaborators | ||
func ListCollaborators(ctx *context.APIContext) { | ||
access, err := models.AccessLevel(ctx.User, ctx.Repo.Repository) | ||
if err != nil { | ||
ctx.Error(500, "AccessLevel", err) | ||
return | ||
} | ||
if access < models.AccessModeWrite { | ||
ctx.Error(403, "", "User does not have push access") | ||
return | ||
} | ||
collaborators, err := ctx.Repo.Repository.GetCollaborators() | ||
if err != nil { | ||
ctx.Error(500, "ListCollaborators", err) | ||
return | ||
} | ||
users := make([]*api.User, len(collaborators)) | ||
for i, collaborator := range collaborators { | ||
users[i] = collaborator.APIFormat() | ||
} | ||
ctx.JSON(200, users) | ||
} | ||
|
||
// IsCollaborator check if a user is a collaborator of a repository | ||
func IsCollaborator(ctx *context.APIContext) { | ||
access, err := models.AccessLevel(ctx.User, ctx.Repo.Repository) | ||
if err != nil { | ||
ctx.Error(500, "AccessLevel", err) | ||
return | ||
} | ||
if access < models.AccessModeWrite { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
ctx.Error(403, "", "User does not have push access") | ||
return | ||
} | ||
user, err := models.GetUserByName(ctx.Params(":collaborator")) | ||
if err != nil { | ||
if models.IsErrUserNotExist(err) { | ||
ctx.Error(422, "", err) | ||
} else { | ||
ctx.Error(500, "GetUserByName", err) | ||
} | ||
return | ||
} | ||
isColab, err := ctx.Repo.Repository.IsCollaborator(user.ID) | ||
if err != nil { | ||
ctx.Error(500, "IsCollaborator", err) | ||
return | ||
} | ||
if isColab { | ||
ctx.Status(204) | ||
} else { | ||
ctx.Status(404) | ||
} | ||
} | ||
|
||
// AddCollaborator add a collaborator of a repository | ||
func AddCollaborator(ctx *context.APIContext, form api.AddCollaboratorOption) { | ||
access, err := models.AccessLevel(ctx.User, ctx.Repo.Repository) | ||
if err != nil { | ||
ctx.Error(500, "AccessLevel", err) | ||
return | ||
} | ||
if access < models.AccessModeWrite { | ||
ctx.Error(403, "", "User does not have push access") | ||
return | ||
} | ||
collaborator, err := models.GetUserByName(ctx.Params(":collaborator")) | ||
if err != nil { | ||
if models.IsErrUserNotExist(err) { | ||
|
@@ -37,3 +101,32 @@ func AddCollaborator(ctx *context.APIContext, form api.AddCollaboratorOption) { | |
|
||
ctx.Status(204) | ||
} | ||
|
||
// DeleteCollaborator delete a collaborator from a repository | ||
func DeleteCollaborator(ctx *context.APIContext) { | ||
access, err := models.AccessLevel(ctx.User, ctx.Repo.Repository) | ||
if err != nil { | ||
ctx.Error(500, "AccessLevel", err) | ||
return | ||
} | ||
if access < models.AccessModeWrite { | ||
ctx.Error(403, "", "User does not have push access") | ||
return | ||
} | ||
|
||
collaborator, err := models.GetUserByName(ctx.Params(":collaborator")) | ||
if err != nil { | ||
if models.IsErrUserNotExist(err) { | ||
ctx.Error(422, "", err) | ||
} else { | ||
ctx.Error(500, "GetUserByName", err) | ||
} | ||
return | ||
} | ||
|
||
if err := ctx.Repo.Repository.DeleteCollaboration(collaborator.ID); err != nil { | ||
ctx.Error(500, "DeleteCollaboration", err) | ||
return | ||
} | ||
ctx.Status(204) | ||
} |
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.
This should only be checked for private repos. For public repos anyone can fetch the list of collaborators :)
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.
It is always checked in the Github API, even for public repos.
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.
ooh nvm then 😄