@@ -8,33 +8,29 @@ import (
88
99// listUserRepos - List the repositories owned by the given user.
1010func listUserRepos (ctx * context.APIContext , u * models.User ) {
11- userID := u .ID
12- showPrivateRepos := ctx .IsSigned && (ctx .User .ID == userID || ctx .User .IsAdmin )
13- ownRepos , err := models .GetUserRepositories (userID , showPrivateRepos , 1 , u .NumRepos , "" )
11+ showPrivateRepos := ctx .IsSigned && (ctx .User .ID == u .ID || ctx .User .IsAdmin )
12+ repos , err := models .GetUserRepositories (u .ID , showPrivateRepos , 1 , u .NumRepos , "" )
1413 if err != nil {
1514 ctx .Error (500 , "GetUserRepositories" , err )
1615 return
1716 }
18- var accessibleRepos []* api.Repository
17+ apiRepos := make ([]* api.Repository , len (repos ))
18+ var ctxUserID int64
1919 if ctx .User != nil {
20- accessibleRepos , err = getAccessibleRepos (ctx )
20+ ctxUserID = ctx .User .ID
21+ }
22+ for i := range repos {
23+ access , err := models .AccessLevel (ctxUserID , repos [i ])
2124 if err != nil {
22- ctx .Error (500 , "GetAccessibleRepos" , err )
25+ ctx .Error (500 , "AccessLevel" , err )
26+ return
2327 }
24- }
25- apiRepos := make ([]* api.Repository , len (ownRepos )+ len (accessibleRepos ))
26- // Set owned repositories.
27- for i := range ownRepos {
28- apiRepos [i ] = ownRepos [i ].APIFormat (models .AccessModeOwner )
29- }
30- // Set repositories user has access to.
31- for i := 0 ; i < len (accessibleRepos ); i ++ {
32- apiRepos [i + len (ownRepos )] = accessibleRepos [i ]
28+ apiRepos [i ] = repos [i ].APIFormat (access )
3329 }
3430 ctx .JSON (200 , & apiRepos )
3531}
3632
37- // ListUserRepos - list the repos owned and accessible by the given user.
33+ // ListUserRepos - list the repos owned by the given user.
3834func ListUserRepos (ctx * context.APIContext ) {
3935 // swagger:route GET /users/{username}/repos userListRepos
4036 //
@@ -52,7 +48,7 @@ func ListUserRepos(ctx *context.APIContext) {
5248 listUserRepos (ctx , user )
5349}
5450
55- // ListMyRepos - list the repositories owned by you .
51+ // ListMyRepos - list the repositories you own or have access to .
5652func ListMyRepos (ctx * context.APIContext ) {
5753 // swagger:route GET /user/repos userCurrentListRepos
5854 //
@@ -62,21 +58,25 @@ func ListMyRepos(ctx *context.APIContext) {
6258 // Responses:
6359 // 200: RepositoryList
6460 // 500: error
65-
66- listUserRepos (ctx , ctx .User )
67- }
68-
69- // getAccessibleRepos - Get the repositories a user has access to.
70- func getAccessibleRepos (ctx * context.APIContext ) ([]* api.Repository , error ) {
71- accessibleRepos , err := ctx .User .GetRepositoryAccesses ()
61+ ownRepos , err := models .GetUserRepositories (ctx .User .ID , true , 1 , ctx .User .NumRepos , "" )
62+ if err != nil {
63+ ctx .Error (500 , "GetUserRepositories" , err )
64+ return
65+ }
66+ accessibleReposMap , err := ctx .User .GetRepositoryAccesses ()
7267 if err != nil {
73- return nil , err
68+ ctx .Error (500 , "GetRepositoryAccesses" , err )
69+ return
7470 }
75- i := 0
76- repos := make ([]* api.Repository , len (accessibleRepos ))
77- for repo , access := range accessibleRepos {
78- repos [i ] = repo .APIFormat (access )
71+
72+ apiRepos := make ([]* api.Repository , len (ownRepos )+ len (accessibleReposMap ))
73+ for i := range ownRepos {
74+ apiRepos [i ] = ownRepos [i ].APIFormat (models .AccessModeOwner )
75+ }
76+ i := len (ownRepos )
77+ for repo , access := range accessibleReposMap {
78+ apiRepos [i ] = repo .APIFormat (access )
7979 i ++
8080 }
81- return repos , nil
81+ ctx . JSON ( 200 , & apiRepos )
8282}
0 commit comments