@@ -14,6 +14,7 @@ import (
14
14
"code.gitea.io/gitea/modules/log"
15
15
"code.gitea.io/gitea/modules/private"
16
16
"code.gitea.io/gitea/modules/setting"
17
+ repo_service "code.gitea.io/gitea/services/repository"
17
18
18
19
"gitea.com/macaron/macaron"
19
20
)
@@ -98,44 +99,44 @@ func ServCommand(ctx *macaron.Context) {
98
99
}
99
100
100
101
// Now get the Repository and set the results section
102
+ repoExist := true
101
103
repo , err := models .GetRepositoryByOwnerAndName (results .OwnerName , results .RepoName )
102
104
if err != nil {
103
105
if models .IsErrRepoNotExist (err ) {
104
- ctx .JSON (http .StatusNotFound , map [string ]interface {}{
106
+ repoExist = false
107
+ } else {
108
+ log .Error ("Unable to get repository: %s/%s Error: %v" , results .OwnerName , results .RepoName , err )
109
+ ctx .JSON (http .StatusInternalServerError , map [string ]interface {}{
105
110
"results" : results ,
106
- "type" : "ErrRepoNotExist " ,
107
- "err" : fmt .Sprintf ("Cannot find repository %s/%s" , results .OwnerName , results .RepoName ),
111
+ "type" : "InternalServerError " ,
112
+ "err" : fmt .Sprintf ("Unable to get repository: %s/%s %v " , results .OwnerName , results .RepoName , err ),
108
113
})
109
114
return
110
115
}
111
- log .Error ("Unable to get repository: %s/%s Error: %v" , results .OwnerName , results .RepoName , err )
112
- ctx .JSON (http .StatusInternalServerError , map [string ]interface {}{
113
- "results" : results ,
114
- "type" : "InternalServerError" ,
115
- "err" : fmt .Sprintf ("Unable to get repository: %s/%s %v" , results .OwnerName , results .RepoName , err ),
116
- })
117
- return
118
116
}
119
- repo .OwnerName = ownerName
120
- results .RepoID = repo .ID
121
117
122
- if repo .IsBeingCreated () {
123
- ctx .JSON (http .StatusInternalServerError , map [string ]interface {}{
124
- "results" : results ,
125
- "type" : "InternalServerError" ,
126
- "err" : "Repository is being created, you could retry after it finished" ,
127
- })
128
- return
129
- }
118
+ if repoExist {
119
+ repo .OwnerName = ownerName
120
+ results .RepoID = repo .ID
130
121
131
- // We can shortcut at this point if the repo is a mirror
132
- if mode > models .AccessModeRead && repo .IsMirror {
133
- ctx .JSON (http .StatusUnauthorized , map [string ]interface {}{
134
- "results" : results ,
135
- "type" : "ErrMirrorReadOnly" ,
136
- "err" : fmt .Sprintf ("Mirror Repository %s/%s is read-only" , results .OwnerName , results .RepoName ),
137
- })
138
- return
122
+ if repo .IsBeingCreated () {
123
+ ctx .JSON (http .StatusInternalServerError , map [string ]interface {}{
124
+ "results" : results ,
125
+ "type" : "InternalServerError" ,
126
+ "err" : "Repository is being created, you could retry after it finished" ,
127
+ })
128
+ return
129
+ }
130
+
131
+ // We can shortcut at this point if the repo is a mirror
132
+ if mode > models .AccessModeRead && repo .IsMirror {
133
+ ctx .JSON (http .StatusUnauthorized , map [string ]interface {}{
134
+ "results" : results ,
135
+ "type" : "ErrMirrorReadOnly" ,
136
+ "err" : fmt .Sprintf ("Mirror Repository %s/%s is read-only" , results .OwnerName , results .RepoName ),
137
+ })
138
+ return
139
+ }
139
140
}
140
141
141
142
// Get the Public Key represented by the keyID
@@ -161,6 +162,16 @@ func ServCommand(ctx *macaron.Context) {
161
162
results .KeyID = key .ID
162
163
results .UserID = key .OwnerID
163
164
165
+ // If repo doesn't exist, deploy key doesn't make sense
166
+ if ! repoExist && key .Type == models .KeyTypeDeploy {
167
+ ctx .JSON (http .StatusNotFound , map [string ]interface {}{
168
+ "results" : results ,
169
+ "type" : "ErrRepoNotExist" ,
170
+ "err" : fmt .Sprintf ("Cannot find repository %s/%s" , results .OwnerName , results .RepoName ),
171
+ })
172
+ return
173
+ }
174
+
164
175
// Deploy Keys have ownerID set to 0 therefore we can't use the owner
165
176
// So now we need to check if the key is a deploy key
166
177
// We'll keep hold of the deploy key here for permissions checking
@@ -220,7 +231,7 @@ func ServCommand(ctx *macaron.Context) {
220
231
}
221
232
222
233
// Don't allow pushing if the repo is archived
223
- if mode > models .AccessModeRead && repo .IsArchived {
234
+ if repoExist && mode > models .AccessModeRead && repo .IsArchived {
224
235
ctx .JSON (http .StatusUnauthorized , map [string ]interface {}{
225
236
"results" : results ,
226
237
"type" : "ErrRepoIsArchived" ,
@@ -230,7 +241,7 @@ func ServCommand(ctx *macaron.Context) {
230
241
}
231
242
232
243
// Permissions checking:
233
- if mode > models .AccessModeRead || repo .IsPrivate || setting .Service .RequireSignInView {
244
+ if repoExist && ( mode > models .AccessModeRead || repo .IsPrivate || setting .Service .RequireSignInView ) {
234
245
if key .Type == models .KeyTypeDeploy {
235
246
if deployKey .Mode < mode {
236
247
ctx .JSON (http .StatusUnauthorized , map [string ]interface {}{
@@ -265,6 +276,48 @@ func ServCommand(ctx *macaron.Context) {
265
276
}
266
277
}
267
278
279
+ // We already know we aren't using a deploy key
280
+ if ! repoExist {
281
+ owner , err := models .GetUserByName (ownerName )
282
+ if err != nil {
283
+ ctx .JSON (http .StatusInternalServerError , map [string ]interface {}{
284
+ "results" : results ,
285
+ "type" : "InternalServerError" ,
286
+ "err" : fmt .Sprintf ("Unable to get owner: %s %v" , results .OwnerName , err ),
287
+ })
288
+ return
289
+ }
290
+
291
+ if owner .IsOrganization () && ! setting .Repository .EnablePushCreateOrg {
292
+ ctx .JSON (http .StatusForbidden , map [string ]interface {}{
293
+ "results" : results ,
294
+ "type" : "ErrForbidden" ,
295
+ "err" : "Push to create is not enabled for organizations." ,
296
+ })
297
+ return
298
+ }
299
+ if ! owner .IsOrganization () && ! setting .Repository .EnablePushCreateUser {
300
+ ctx .JSON (http .StatusForbidden , map [string ]interface {}{
301
+ "results" : results ,
302
+ "type" : "ErrForbidden" ,
303
+ "err" : "Push to create is not enabled for users." ,
304
+ })
305
+ return
306
+ }
307
+
308
+ repo , err = repo_service .PushCreateRepo (user , owner , results .RepoName )
309
+ if err != nil {
310
+ log .Error ("pushCreateRepo: %v" , err )
311
+ ctx .JSON (http .StatusNotFound , map [string ]interface {}{
312
+ "results" : results ,
313
+ "type" : "ErrRepoNotExist" ,
314
+ "err" : fmt .Sprintf ("Cannot find repository: %s/%s" , results .OwnerName , results .RepoName ),
315
+ })
316
+ return
317
+ }
318
+ results .RepoID = repo .ID
319
+ }
320
+
268
321
// Finally if we're trying to touch the wiki we should init it
269
322
if results .IsWiki {
270
323
if err = repo .InitWiki (); err != nil {
0 commit comments