@@ -11,8 +11,72 @@ import (
1111 "code.gitea.io/gitea/modules/context"
1212)
1313
14+ // ListCollaborators list a repository's collaborators
15+ func ListCollaborators (ctx * context.APIContext ) {
16+ access , err := models .AccessLevel (ctx .User , ctx .Repo .Repository )
17+ if err != nil {
18+ ctx .Error (500 , "AccessLevel" , err )
19+ return
20+ }
21+ if access < models .AccessModeWrite {
22+ ctx .Error (403 , "" , "User does not have push access" )
23+ return
24+ }
25+ collaborators , err := ctx .Repo .Repository .GetCollaborators ()
26+ if err != nil {
27+ ctx .Error (500 , "ListCollaborators" , err )
28+ return
29+ }
30+ users := make ([]* api.User , len (collaborators ))
31+ for i , collaborator := range collaborators {
32+ users [i ] = collaborator .APIFormat ()
33+ }
34+ ctx .JSON (200 , users )
35+ }
36+
37+ // IsCollaborator check if a user is a collaborator of a repository
38+ func IsCollaborator (ctx * context.APIContext ) {
39+ access , err := models .AccessLevel (ctx .User , ctx .Repo .Repository )
40+ if err != nil {
41+ ctx .Error (500 , "AccessLevel" , err )
42+ return
43+ }
44+ if access < models .AccessModeWrite {
45+ ctx .Error (403 , "" , "User does not have push access" )
46+ return
47+ }
48+ user , err := models .GetUserByName (ctx .Params (":collaborator" ))
49+ if err != nil {
50+ if models .IsErrUserNotExist (err ) {
51+ ctx .Error (422 , "" , err )
52+ } else {
53+ ctx .Error (500 , "GetUserByName" , err )
54+ }
55+ return
56+ }
57+ isColab , err := ctx .Repo .Repository .IsCollaborator (user .ID )
58+ if err != nil {
59+ ctx .Error (500 , "IsCollaborator" , err )
60+ return
61+ }
62+ if isColab {
63+ ctx .Status (204 )
64+ } else {
65+ ctx .Status (404 )
66+ }
67+ }
68+
1469// AddCollaborator add a collaborator of a repository
1570func AddCollaborator (ctx * context.APIContext , form api.AddCollaboratorOption ) {
71+ access , err := models .AccessLevel (ctx .User , ctx .Repo .Repository )
72+ if err != nil {
73+ ctx .Error (500 , "AccessLevel" , err )
74+ return
75+ }
76+ if access < models .AccessModeWrite {
77+ ctx .Error (403 , "" , "User does not have push access" )
78+ return
79+ }
1680 collaborator , err := models .GetUserByName (ctx .Params (":collaborator" ))
1781 if err != nil {
1882 if models .IsErrUserNotExist (err ) {
@@ -37,3 +101,32 @@ func AddCollaborator(ctx *context.APIContext, form api.AddCollaboratorOption) {
37101
38102 ctx .Status (204 )
39103}
104+
105+ // DeleteCollaborator delete a collaborator from a repository
106+ func DeleteCollaborator (ctx * context.APIContext ) {
107+ access , err := models .AccessLevel (ctx .User , ctx .Repo .Repository )
108+ if err != nil {
109+ ctx .Error (500 , "AccessLevel" , err )
110+ return
111+ }
112+ if access < models .AccessModeWrite {
113+ ctx .Error (403 , "" , "User does not have push access" )
114+ return
115+ }
116+
117+ collaborator , err := models .GetUserByName (ctx .Params (":collaborator" ))
118+ if err != nil {
119+ if models .IsErrUserNotExist (err ) {
120+ ctx .Error (422 , "" , err )
121+ } else {
122+ ctx .Error (500 , "GetUserByName" , err )
123+ }
124+ return
125+ }
126+
127+ if err := ctx .Repo .Repository .DeleteCollaboration (collaborator .ID ); err != nil {
128+ ctx .Error (500 , "DeleteCollaboration" , err )
129+ return
130+ }
131+ ctx .Status (204 )
132+ }
0 commit comments