-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
auto-create repository on push #2478
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
"testing" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
api "code.gitea.io/sdk/gitea" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
@@ -74,3 +75,45 @@ func TestViewRepo1CloneLinkAuthorized(t *testing.T) { | |
sshURL := fmt.Sprintf("%s@%s:user2/repo1.git", setting.RunUser, setting.SSH.Domain) | ||
assert.Equal(t, sshURL, link) | ||
} | ||
|
||
func TestAutoCreateRepo(t *testing.T) { | ||
prepareTestEnv(t) | ||
|
||
session := loginUser(t, "user2") | ||
|
||
// repo should not exist | ||
req := NewRequest(t, "GET", "/api/v1/repos/user2/autocreated") | ||
MakeRequest(t, req, http.StatusNotFound) | ||
|
||
// | ||
// if 'AUTO_CREATE = false', the repo should not be created if it doesn't exist | ||
// | ||
setting.Repository.Upload.AutoCreate = false | ||
req = NewRequest(t, "GET", "/user2/autocreated.git/info/refs?service=git-receive-pack") | ||
session.MakeRequest(t, req, http.StatusNotFound) | ||
|
||
req = NewRequest(t, "GET", "/api/v1/repos/user2/autocreated") | ||
MakeRequest(t, req, http.StatusNotFound) | ||
|
||
// | ||
// if 'AUTO_CREATE = true', the repos should be created | ||
// | ||
setting.Repository.Upload.AutoCreate = true | ||
req = NewRequest(t, "GET", "/user2/autocreated.git/info/refs?service=git-receive-pack") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if logged user is not same user as in url? You should check both options. |
||
// we don't get a successful response code here, because we don't execute a real 'git push ...' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you do real push? (I don't know how git requests look) |
||
session.MakeRequest(t, req, NoExpectedStatus) | ||
|
||
// repo should exist | ||
req = NewRequest(t, "GET", "/api/v1/repos/user2/autocreated") | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
|
||
var repo api.Repository | ||
DecodeJSON(t, resp, &repo) | ||
assert.Equal(t, repo.Name, "autocreated") | ||
|
||
// | ||
// cleanup | ||
// | ||
req = NewRequest(t, "DELETE", "/api/v1/repos/user2/autocreated") | ||
session.MakeRequest(t, req, http.StatusNoContent) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,6 +176,7 @@ var ( | |
AllowedTypes []string `delim:"|"` | ||
FileMaxSize int64 | ||
MaxFiles int | ||
AutoCreate bool | ||
} `ini:"-"` | ||
|
||
// Repository local settings | ||
|
@@ -208,12 +209,14 @@ var ( | |
AllowedTypes []string `delim:"|"` | ||
FileMaxSize int64 | ||
MaxFiles int | ||
AutoCreate bool | ||
}{ | ||
Enabled: true, | ||
TempPath: "data/tmp/uploads", | ||
AllowedTypes: []string{}, | ||
FileMaxSize: 3, | ||
MaxFiles: 5, | ||
AutoCreate: false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}, | ||
|
||
// Repository local settings | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,13 @@ func HTTP(ctx *context.Context) { | |
} | ||
|
||
repo, err := models.GetRepositoryByName(repoUser.ID, reponame) | ||
if models.IsErrRepoNotExist(err) && setting.Repository.Upload.AutoCreate { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't look good to me. You should check err for each function. Later err check block is expecting only
Edit: Updated if condition in example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also you are creating repository on any action, you should check if user is doing push or pull. I think you can use Edit: Or use |
||
|
||
// if 'CreateRepository' fails, the error are handled in the next 'if err != nil' block | ||
repo, err = models.CreateRepository(repoUser, repoUser, models.CreateRepoOptions{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about access rights? Logged user don't need to be same as |
||
Name: reponame, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about private/public repo? I think repository should be private by default. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should it hard-coded private or with an extra bool setting - like 'AutoCreateAsPrivate'? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it should be Auto Create specific but probably more generic like 'NewRepositoryDefaultPrivate` and this setting should be reused to set private check box to this default when creating new repository also in UI There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point - i make a extra pr for this tomorrow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lafriks Actually I think it is more secure to set it private by default. In UI you can tell what you want, but if you autocreate repository, you don't have any option. Also if you use some general variable, you know about it as admin, but not as user. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Morlinest yes but that does not mean that can't be configurable (as I need myself to to have such option so that I don't have remember to check that check box when creating new repository :) ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we can add configurable variable, but we should not mix UI and some kind of auto create function. |
||
}) | ||
} | ||
if err != nil { | ||
if models.IsErrRepoNotExist(err) { | ||
ctx.Handle(http.StatusNotFound, "GetRepositoryByName", nil) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe don't use empty comment line.