@@ -21,6 +21,26 @@ import (
2121 "github.com/gogits/gogs/modules/setting"
2222)
2323
24+ // ToApiRepository converts repository to API format.
25+ func ToApiRepository (owner * models.User , repo * models.Repository , permission api.Permission ) * api.Repository {
26+ sshUrlFmt := "%s@%s:%s/%s.git"
27+ if setting .SshPort != 22 {
28+ sshUrlFmt = "ssh://%s@%s:%d/%s/%s.git"
29+ }
30+ htmlUrl := setting .AppUrl + owner .Name + "/" + repo .Name
31+ return & api.Repository {
32+ Id : repo .Id ,
33+ Owner : * ToApiUser (owner ),
34+ FullName : owner .Name + "/" + repo .Name ,
35+ Private : repo .IsPrivate ,
36+ Fork : repo .IsFork ,
37+ HtmlUrl : htmlUrl ,
38+ SshUrl : fmt .Sprintf (sshUrlFmt , setting .RunUser , setting .Domain , owner .LowerName , repo .LowerName ),
39+ CloneUrl : htmlUrl + ".git" ,
40+ Permissions : permission ,
41+ }
42+ }
43+
2444func SearchRepos (ctx * middleware.Context ) {
2545 opt := models.SearchOption {
2646 Keyword : path .Base (ctx .Query ("q" )),
@@ -44,7 +64,7 @@ func SearchRepos(ctx *middleware.Context) {
4464 })
4565 return
4666 }
47- if u .IsOrganization () && u .IsOrgOwner (ctx .User .Id ) {
67+ if u .IsOrganization () && u .IsOwnedBy (ctx .User .Id ) {
4868 opt .Private = true
4969 }
5070 // FIXME: how about collaborators?
@@ -75,13 +95,66 @@ func SearchRepos(ctx *middleware.Context) {
7595 }
7696 }
7797
78- ctx .Render . JSON (200 , map [string ]interface {}{
98+ ctx .JSON (200 , map [string ]interface {}{
7999 "ok" : true ,
80100 "data" : results ,
81101 })
82102}
83103
84- func Migrate (ctx * middleware.Context , form auth.MigrateRepoForm ) {
104+ func createRepo (ctx * middleware.Context , owner * models.User , opt api.CreateRepoOption ) {
105+ repo , err := models .CreateRepository (owner , opt .Name , opt .Description ,
106+ opt .Gitignore , opt .License , opt .Private , false , opt .AutoInit )
107+ if err != nil {
108+ if err == models .ErrRepoAlreadyExist ||
109+ err == models .ErrRepoNameIllegal {
110+ ctx .JSON (422 , & base.ApiJsonErr {err .Error (), base .DOC_URL })
111+ } else {
112+ log .Error (4 , "CreateRepository: %v" , err )
113+ if repo != nil {
114+ if err = models .DeleteRepository (ctx .User .Id , repo .Id , ctx .User .Name ); err != nil {
115+ log .Error (4 , "DeleteRepository: %v" , err )
116+ }
117+ }
118+ ctx .Error (500 )
119+ }
120+ return
121+ }
122+
123+ ctx .JSON (200 , ToApiRepository (owner , repo , api.Permission {true , true , true }))
124+ }
125+
126+ // POST /user/repos
127+ // https://developer.github.com/v3/repos/#create
128+ func CreateRepo (ctx * middleware.Context , opt api.CreateRepoOption ) {
129+ // Shouldn't reach this condition, but just in case.
130+ if ctx .User .IsOrganization () {
131+ ctx .JSON (422 , "not allowed creating repository for organization" )
132+ return
133+ }
134+ createRepo (ctx , ctx .User , opt )
135+ }
136+
137+ // POST /orgs/:org/repos
138+ // https://developer.github.com/v3/repos/#create
139+ func CreateOrgRepo (ctx * middleware.Context , opt api.CreateRepoOption ) {
140+ org , err := models .GetOrgByName (ctx .Params (":org" ))
141+ if err != nil {
142+ if err == models .ErrUserNotExist {
143+ ctx .Error (404 )
144+ } else {
145+ ctx .Error (500 )
146+ }
147+ return
148+ }
149+
150+ if ! org .IsOwnedBy (ctx .User .Id ) {
151+ ctx .Error (403 )
152+ return
153+ }
154+ createRepo (ctx , org , opt )
155+ }
156+
157+ func MigrateRepo (ctx * middleware.Context , form auth.MigrateRepoForm ) {
85158 u , err := models .GetUserByName (ctx .Query ("username" ))
86159 if err != nil {
87160 ctx .JSON (500 , map [string ]interface {}{
@@ -103,17 +176,15 @@ func Migrate(ctx *middleware.Context, form auth.MigrateRepoForm) {
103176 if form .Uid != u .Id {
104177 org , err := models .GetUserById (form .Uid )
105178 if err != nil {
106- ctx .JSON (500 , map [string ]interface {}{
107- "ok" : false ,
108- "error" : err .Error (),
109- })
179+ log .Error (4 , "GetUserById: %v" , err )
180+ ctx .Error (500 )
110181 return
111182 }
112183 ctxUser = org
113184 }
114185
115186 if ctx .HasError () {
116- ctx .JSON (500 , map [string ]interface {}{
187+ ctx .JSON (422 , map [string ]interface {}{
117188 "ok" : false ,
118189 "error" : ctx .GetErrMsg (),
119190 })
@@ -122,7 +193,7 @@ func Migrate(ctx *middleware.Context, form auth.MigrateRepoForm) {
122193
123194 if ctxUser .IsOrganization () {
124195 // Check ownership of organization.
125- if ! ctxUser .IsOrgOwner (u .Id ) {
196+ if ! ctxUser .IsOwnedBy (u .Id ) {
126197 ctx .JSON (403 , map [string ]interface {}{
127198 "ok" : false ,
128199 "error" : "given user is not owner of organization" ,
@@ -173,54 +244,20 @@ func ListMyRepos(ctx *middleware.Context) {
173244 return
174245 }
175246
176- sshUrlFmt := "%s@%s:%s/%s.git"
177- if setting .SshPort != 22 {
178- sshUrlFmt = "ssh://%s@%s:%d/%s/%s.git"
179- }
180-
181247 repos := make ([]* api.Repository , numOwnRepos + len (collaRepos ))
182- // FIXME: make only one loop
183248 for i := range ownRepos {
184- repos [i ] = & api.Repository {
185- Id : ownRepos [i ].Id ,
186- Owner : api.User {
187- Id : ctx .User .Id ,
188- UserName : ctx .User .Name ,
189- AvatarUrl : string (setting .Protocol ) + ctx .User .AvatarLink (),
190- },
191- FullName : ctx .User .Name + "/" + ownRepos [i ].Name ,
192- Private : ownRepos [i ].IsPrivate ,
193- Fork : ownRepos [i ].IsFork ,
194- HtmlUrl : setting .AppUrl + ctx .User .Name + "/" + ownRepos [i ].Name ,
195- SshUrl : fmt .Sprintf (sshUrlFmt , setting .RunUser , setting .Domain , ctx .User .LowerName , ownRepos [i ].LowerName ),
196- Permissions : api.Permission {true , true , true },
197- }
198- repos [i ].CloneUrl = repos [i ].HtmlUrl + ".git"
249+ repos [i ] = ToApiRepository (ctx .User , ownRepos [i ], api.Permission {true , true , true })
199250 }
200251 for i := range collaRepos {
201252 if err = collaRepos [i ].GetOwner (); err != nil {
202253 ctx .JSON (500 , & base.ApiJsonErr {"GetOwner: " + err .Error (), base .DOC_URL })
203254 return
204255 }
205256 j := i + numOwnRepos
206- repos [j ] = & api.Repository {
207- Id : collaRepos [i ].Id ,
208- Owner : api.User {
209- Id : collaRepos [i ].Owner .Id ,
210- UserName : collaRepos [i ].Owner .Name ,
211- AvatarUrl : string (setting .Protocol ) + collaRepos [i ].Owner .AvatarLink (),
212- },
213- FullName : collaRepos [i ].Owner .Name + "/" + collaRepos [i ].Name ,
214- Private : collaRepos [i ].IsPrivate ,
215- Fork : collaRepos [i ].IsFork ,
216- HtmlUrl : setting .AppUrl + collaRepos [i ].Owner .Name + "/" + collaRepos [i ].Name ,
217- SshUrl : fmt .Sprintf (sshUrlFmt , setting .RunUser , setting .Domain , collaRepos [i ].Owner .LowerName , collaRepos [i ].LowerName ),
218- Permissions : api.Permission {false , collaRepos [i ].CanPush , true },
219- }
220- repos [j ].CloneUrl = repos [j ].HtmlUrl + ".git"
257+ repos [j ] = ToApiRepository (collaRepos [i ].Owner , collaRepos [i ].Repository , api.Permission {false , collaRepos [i ].CanPush , true })
221258
222259 // FIXME: cache result to reduce DB query?
223- if collaRepos [i ].Owner .IsOrganization () && collaRepos [i ].Owner .IsOrgOwner (ctx .User .Id ) {
260+ if collaRepos [i ].Owner .IsOrganization () && collaRepos [i ].Owner .IsOwnedBy (ctx .User .Id ) {
224261 repos [j ].Permissions .Admin = true
225262 }
226263 }
0 commit comments