Skip to content

Commit 5bc6a71

Browse files
committed
Allow administrator to create repository for any organization
1 parent 4a8ee0b commit 5bc6a71

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

integrations/api_repo_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,26 @@ func TestAPIRepoMigrate(t *testing.T) {
262262
session.MakeRequest(t, req, testCase.expectedStatus)
263263
}
264264
}
265+
266+
func TestAPIOrgRepoCreate(t *testing.T) {
267+
testCases := []struct {
268+
ctxUserID int64
269+
orgName, repoName string
270+
expectedStatus int
271+
}{
272+
{ctxUserID: 1, orgName: "user3", repoName: "repo-admin", expectedStatus: http.StatusCreated},
273+
{ctxUserID: 2, orgName: "user3", repoName: "repo-own", expectedStatus: http.StatusCreated},
274+
{ctxUserID: 2, orgName: "user6", repoName: "repo-bad-org", expectedStatus: http.StatusForbidden},
275+
}
276+
277+
prepareTestEnv(t)
278+
for _, testCase := range testCases {
279+
user := models.AssertExistsAndLoadBean(t, &models.User{ID: testCase.ctxUserID}).(*models.User)
280+
session := loginUser(t, user.Name)
281+
282+
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/org/%s/repos", testCase.orgName), &api.CreateRepoOption{
283+
Name: testCase.repoName,
284+
})
285+
session.MakeRequest(t, req, testCase.expectedStatus)
286+
}
287+
}

routers/api/v1/repo/repo.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,15 @@ func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) {
257257
return
258258
}
259259

260-
isOwner, err := org.IsOwnedBy(ctx.User.ID)
261-
if err != nil {
262-
ctx.ServerError("IsOwnedBy", err)
263-
return
264-
} else if !isOwner {
265-
ctx.Error(403, "", "Given user is not owner of organization.")
266-
return
260+
if !ctx.User.IsAdmin {
261+
isOwner, err := org.IsOwnedBy(ctx.User.ID)
262+
if err != nil {
263+
ctx.ServerError("IsOwnedBy", err)
264+
return
265+
} else if !isOwner {
266+
ctx.Error(403, "", "Given user is not owner of organization.")
267+
return
268+
}
267269
}
268270
CreateUserRepo(ctx, org, opt)
269271
}

0 commit comments

Comments
 (0)