Browse Source

repo: able to perform initial commit on behave of actual user

pull/4240/head
Unknwon 8 years ago
parent
commit
87f0ce793d
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 14
      models/action.go
  2. 66
      models/repo.go
  3. 4
      routers/api/v1/repo/repo.go
  4. 2
      routers/repo/pull.go
  5. 4
      routers/repo/repo.go
  6. 2
      templates/user/dashboard/dashboard.tmpl

14
models/action.go

@ -172,26 +172,26 @@ func (a *Action) GetIssueContent() string {
return issue.Content return issue.Content
} }
func newRepoAction(e Engine, u *User, repo *Repository) (err error) { func newRepoAction(e Engine, doer, owner *User, repo *Repository) (err error) {
if err = notifyWatchers(e, &Action{ if err = notifyWatchers(e, &Action{
ActUserID: u.ID, ActUserID: doer.ID,
ActUserName: u.Name, ActUserName: doer.Name,
OpType: ACTION_CREATE_REPO, OpType: ACTION_CREATE_REPO,
RepoID: repo.ID, RepoID: repo.ID,
RepoUserName: repo.Owner.Name, RepoUserName: repo.Owner.Name,
RepoName: repo.Name, RepoName: repo.Name,
IsPrivate: repo.IsPrivate, IsPrivate: repo.IsPrivate,
}); err != nil { }); err != nil {
return fmt.Errorf("notify watchers '%d/%d': %v", u.ID, repo.ID, err) return fmt.Errorf("notify watchers '%d/%d': %v", owner.ID, repo.ID, err)
} }
log.Trace("action.newRepoAction: %s/%s", u.Name, repo.Name) log.Trace("action.newRepoAction: %s/%s", owner.Name, repo.Name)
return err return err
} }
// NewRepoAction adds new action for creating repository. // NewRepoAction adds new action for creating repository.
func NewRepoAction(u *User, repo *Repository) (err error) { func NewRepoAction(doer, owner *User, repo *Repository) (err error) {
return newRepoAction(x, u, repo) return newRepoAction(x, doer, owner, repo)
} }
func renameRepoAction(e Engine, actUser *User, oldRepoName string, repo *Repository) (err error) { func renameRepoAction(e Engine, actUser *User, oldRepoName string, repo *Repository) (err error) {

66
models/repo.go

@ -628,8 +628,8 @@ func wikiRemoteURL(remote string) string {
} }
// MigrateRepository migrates a existing repository from other project hosting. // MigrateRepository migrates a existing repository from other project hosting.
func MigrateRepository(u *User, opts MigrateRepoOptions) (*Repository, error) { func MigrateRepository(doer, owner *User, opts MigrateRepoOptions) (*Repository, error) {
repo, err := CreateRepository(u, CreateRepoOptions{ repo, err := CreateRepository(doer, owner, CreateRepoOptions{
Name: opts.Name, Name: opts.Name,
Description: opts.Description, Description: opts.Description,
IsPrivate: opts.IsPrivate, IsPrivate: opts.IsPrivate,
@ -639,11 +639,11 @@ func MigrateRepository(u *User, opts MigrateRepoOptions) (*Repository, error) {
return nil, err return nil, err
} }
repoPath := RepoPath(u.Name, opts.Name) repoPath := RepoPath(owner.Name, opts.Name)
wikiPath := WikiPath(u.Name, opts.Name) wikiPath := WikiPath(owner.Name, opts.Name)
if u.IsOrganization() { if owner.IsOrganization() {
t, err := u.GetOwnerTeam() t, err := owner.GetOwnerTeam()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -882,8 +882,8 @@ func prepareRepoCommit(repo *Repository, tmpDir, repoPath string, opts CreateRep
return nil return nil
} }
// InitRepository initializes README and .gitignore if needed. // initRepository performs initial commit with chosen setup files on behave of doer.
func initRepository(e Engine, repoPath string, u *User, repo *Repository, opts CreateRepoOptions) (err error) { func initRepository(e Engine, repoPath string, doer *User, repo *Repository, opts CreateRepoOptions) (err error) {
// Somehow the directory could exist. // Somehow the directory could exist.
if com.IsExist(repoPath) { if com.IsExist(repoPath) {
return fmt.Errorf("initRepository: path already exists: %s", repoPath) return fmt.Errorf("initRepository: path already exists: %s", repoPath)
@ -908,7 +908,7 @@ func initRepository(e Engine, repoPath string, u *User, repo *Repository, opts C
} }
// Apply changes and commit. // Apply changes and commit.
if err = initRepoCommit(tmpDir, u.NewGitSig()); err != nil { if err = initRepoCommit(tmpDir, doer.NewGitSig()); err != nil {
return fmt.Errorf("initRepoCommit: %v", err) return fmt.Errorf("initRepoCommit: %v", err)
} }
} }
@ -941,32 +941,32 @@ func IsUsableRepoName(name string) error {
return isUsableName(reservedRepoNames, reservedRepoPatterns, name) return isUsableName(reservedRepoNames, reservedRepoPatterns, name)
} }
func createRepository(e *xorm.Session, u *User, repo *Repository) (err error) { func createRepository(e *xorm.Session, doer, owner *User, repo *Repository) (err error) {
if err = IsUsableRepoName(repo.Name); err != nil { if err = IsUsableRepoName(repo.Name); err != nil {
return err return err
} }
has, err := isRepositoryExist(e, u, repo.Name) has, err := isRepositoryExist(e, owner, repo.Name)
if err != nil { if err != nil {
return fmt.Errorf("IsRepositoryExist: %v", err) return fmt.Errorf("IsRepositoryExist: %v", err)
} else if has { } else if has {
return ErrRepoAlreadyExist{u.Name, repo.Name} return ErrRepoAlreadyExist{owner.Name, repo.Name}
} }
if _, err = e.Insert(repo); err != nil { if _, err = e.Insert(repo); err != nil {
return err return err
} }
u.NumRepos++ owner.NumRepos++
// Remember visibility preference. // Remember visibility preference.
u.LastRepoVisibility = repo.IsPrivate owner.LastRepoVisibility = repo.IsPrivate
if err = updateUser(e, u); err != nil { if err = updateUser(e, owner); err != nil {
return fmt.Errorf("updateUser: %v", err) return fmt.Errorf("updateUser: %v", err)
} }
// Give access to all members in owner team. // Give access to all members in owner team.
if u.IsOrganization() { if owner.IsOrganization() {
t, err := u.getOwnerTeam(e) t, err := owner.getOwnerTeam(e)
if err != nil { if err != nil {
return fmt.Errorf("getOwnerTeam: %v", err) return fmt.Errorf("getOwnerTeam: %v", err)
} else if err = t.addRepository(e, repo); err != nil { } else if err = t.addRepository(e, repo); err != nil {
@ -979,9 +979,9 @@ func createRepository(e *xorm.Session, u *User, repo *Repository) (err error) {
} }
} }
if err = watchRepo(e, u.ID, repo.ID, true); err != nil { if err = watchRepo(e, owner.ID, repo.ID, true); err != nil {
return fmt.Errorf("watchRepo: %v", err) return fmt.Errorf("watchRepo: %v", err)
} else if err = newRepoAction(e, u, repo); err != nil { } else if err = newRepoAction(e, doer, owner, repo); err != nil {
return fmt.Errorf("newRepoAction: %v", err) return fmt.Errorf("newRepoAction: %v", err)
} }
@ -989,14 +989,14 @@ func createRepository(e *xorm.Session, u *User, repo *Repository) (err error) {
} }
// CreateRepository creates a repository for given user or organization. // CreateRepository creates a repository for given user or organization.
func CreateRepository(u *User, opts CreateRepoOptions) (_ *Repository, err error) { func CreateRepository(doer, owner *User, opts CreateRepoOptions) (_ *Repository, err error) {
if !u.CanCreateRepo() { if !owner.CanCreateRepo() {
return nil, ErrReachLimitOfRepo{u.MaxRepoCreation} return nil, ErrReachLimitOfRepo{owner.MaxRepoCreation}
} }
repo := &Repository{ repo := &Repository{
OwnerID: u.ID, OwnerID: owner.ID,
Owner: u, Owner: owner,
Name: opts.Name, Name: opts.Name,
LowerName: strings.ToLower(opts.Name), LowerName: strings.ToLower(opts.Name),
Description: opts.Description, Description: opts.Description,
@ -1012,14 +1012,14 @@ func CreateRepository(u *User, opts CreateRepoOptions) (_ *Repository, err error
return nil, err return nil, err
} }
if err = createRepository(sess, u, repo); err != nil { if err = createRepository(sess, doer, owner, repo); err != nil {
return nil, err return nil, err
} }
// No need for init mirror. // No need for init mirror.
if !opts.IsMirror { if !opts.IsMirror {
repoPath := RepoPath(u.Name, repo.Name) repoPath := RepoPath(owner.Name, repo.Name)
if err = initRepository(sess, repoPath, u, repo, opts); err != nil { if err = initRepository(sess, repoPath, doer, repo, opts); err != nil {
RemoveAllWithNotice("Delete repository for initialization failure", repoPath) RemoveAllWithNotice("Delete repository for initialization failure", repoPath)
return nil, fmt.Errorf("initRepository: %v", err) return nil, fmt.Errorf("initRepository: %v", err)
} }
@ -2168,10 +2168,10 @@ func HasForkedRepo(ownerID, repoID int64) (*Repository, bool) {
return repo, has return repo, has
} }
func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Repository, err error) { func ForkRepository(doer, owner *User, oldRepo *Repository, name, desc string) (_ *Repository, err error) {
repo := &Repository{ repo := &Repository{
OwnerID: u.ID, OwnerID: owner.ID,
Owner: u, Owner: owner,
Name: name, Name: name,
LowerName: strings.ToLower(name), LowerName: strings.ToLower(name),
Description: desc, Description: desc,
@ -2187,7 +2187,7 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Reposit
return nil, err return nil, err
} }
if err = createRepository(sess, u, repo); err != nil { if err = createRepository(sess, doer, owner, repo); err != nil {
return nil, err return nil, err
} }
@ -2195,9 +2195,9 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Reposit
return nil, err return nil, err
} }
repoPath := RepoPath(u.Name, repo.Name) repoPath := RepoPath(owner.Name, repo.Name)
_, stderr, err := process.ExecTimeout(10*time.Minute, _, stderr, err := process.ExecTimeout(10*time.Minute,
fmt.Sprintf("ForkRepository 'git clone': %s/%s", u.Name, repo.Name), fmt.Sprintf("ForkRepository 'git clone': %s/%s", owner.Name, repo.Name),
"git", "clone", "--bare", oldRepo.RepoPath(), repoPath) "git", "clone", "--bare", oldRepo.RepoPath(), repoPath)
if err != nil { if err != nil {
return nil, fmt.Errorf("git clone: %v", stderr) return nil, fmt.Errorf("git clone: %v", stderr)

4
routers/api/v1/repo/repo.go

@ -149,7 +149,7 @@ func ListOrgRepositories(ctx *context.APIContext) {
} }
func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateRepoOption) { func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateRepoOption) {
repo, err := models.CreateRepository(owner, models.CreateRepoOptions{ repo, err := models.CreateRepository(ctx.User, owner, models.CreateRepoOptions{
Name: opt.Name, Name: opt.Name,
Description: opt.Description, Description: opt.Description,
Gitignores: opt.Gitignores, Gitignores: opt.Gitignores,
@ -256,7 +256,7 @@ func Migrate(ctx *context.APIContext, f form.MigrateRepo) {
return return
} }
repo, err := models.MigrateRepository(ctxUser, models.MigrateRepoOptions{ repo, err := models.MigrateRepository(ctx.User, ctxUser, models.MigrateRepoOptions{
Name: f.RepoName, Name: f.RepoName,
Description: f.Description, Description: f.Description,
IsPrivate: f.Private || setting.Repository.ForcePrivate, IsPrivate: f.Private || setting.Repository.ForcePrivate,

2
routers/repo/pull.go

@ -124,7 +124,7 @@ func ForkPost(ctx *context.Context, f form.CreateRepo) {
return return
} }
repo, err := models.ForkRepository(ctxUser, forkRepo, f.RepoName, f.Description) repo, err := models.ForkRepository(ctx.User, ctxUser, forkRepo, f.RepoName, f.Description)
if err != nil { if err != nil {
ctx.Data["Err_RepoName"] = true ctx.Data["Err_RepoName"] = true
switch { switch {

4
routers/repo/repo.go

@ -120,7 +120,7 @@ func CreatePost(ctx *context.Context, f form.CreateRepo) {
return return
} }
repo, err := models.CreateRepository(ctxUser, models.CreateRepoOptions{ repo, err := models.CreateRepository(ctx.User, ctxUser, models.CreateRepoOptions{
Name: f.RepoName, Name: f.RepoName,
Description: f.Description, Description: f.Description,
Gitignores: f.Gitignores, Gitignores: f.Gitignores,
@ -194,7 +194,7 @@ func MigratePost(ctx *context.Context, f form.MigrateRepo) {
return return
} }
repo, err := models.MigrateRepository(ctxUser, models.MigrateRepoOptions{ repo, err := models.MigrateRepository(ctx.User, ctxUser, models.MigrateRepoOptions{
Name: f.RepoName, Name: f.RepoName,
Description: f.Description, Description: f.Description,
IsPrivate: f.Private || setting.Repository.ForcePrivate, IsPrivate: f.Private || setting.Repository.ForcePrivate,

2
templates/user/dashboard/dashboard.tmpl

@ -18,7 +18,7 @@
<h4 class="ui top attached header"> <h4 class="ui top attached header">
{{.i18n.Tr "home.my_repos"}} <span class="ui grey label">{{.ContextUser.NumRepos}}</span> {{.i18n.Tr "home.my_repos"}} <span class="ui grey label">{{.ContextUser.NumRepos}}</span>
<div class="ui right"> <div class="ui right">
<a class="poping up" href="{{AppSubUrl}}/repo/create" data-content="{{.i18n.Tr "new_repo"}}" data-variation="tiny inverted" data-position="left center"> <a class="poping up" href="{{AppSubUrl}}/repo/create{{if .ContextUser.IsOrganization}}?org={{.ContextUser.ID}}{{end}}" data-content="{{.i18n.Tr "new_repo"}}" data-variation="tiny inverted" data-position="left center">
<i class="plus icon"></i> <i class="plus icon"></i>
<span class="sr-only">{{.i18n.Tr "new_repo"}}</span> <span class="sr-only">{{.i18n.Tr "new_repo"}}</span>
</a> </a>

Loading…
Cancel
Save