Skip to content

Commit f09a125

Browse files
committed
Fix so that user can still fork his own repository to his organizations
1 parent 8863e74 commit f09a125

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

models/repo.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,22 @@ func (repo *Repository) CanBeForked() bool {
634634
return !repo.IsBare && repo.UnitEnabled(UnitTypeCode)
635635
}
636636

637+
// CanUserFork returns true if specified user can fork repository.
638+
func (repo *Repository) CanUserFork(user *User) (bool, error) {
639+
if repo.OwnerID != user.ID && !user.HasForkedRepo(repo.ID) {
640+
return true, nil
641+
}
642+
if err := user.GetOrganizations(true); err != nil {
643+
return false, err
644+
}
645+
for _, org := range user.Orgs {
646+
if !org.HasForkedRepo(repo.ID) {
647+
return true, nil
648+
}
649+
}
650+
return false, nil
651+
}
652+
637653
// CanEnablePulls returns true if repository meets the requirements of accepting pulls.
638654
func (repo *Repository) CanEnablePulls() bool {
639655
return !repo.IsMirror && !repo.IsBare

modules/context/repo.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ func RepoAssignment() macaron.Handler {
337337
ctx.Data["IsRepositoryAdmin"] = ctx.Repo.IsAdmin()
338338
ctx.Data["IsRepositoryWriter"] = ctx.Repo.IsWriter()
339339

340+
if ctx.Data["CanSignedUserFork"], err = ctx.Repo.Repository.CanUserFork(ctx.User); err != nil {
341+
ctx.Handle(500, "CanUserFork", err)
342+
return
343+
}
344+
340345
ctx.Data["DisableSSH"] = setting.SSH.Disabled
341346
ctx.Data["ExposeAnonSSH"] = setting.SSH.ExposeAnonymous
342347
ctx.Data["DisableHTTP"] = setting.Repository.DisableHTTPGit

routers/repo/pull.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ func getForkRepository(ctx *context.Context) *models.Repository {
6161
ctx.Data["repo_name"] = forkRepo.Name
6262
ctx.Data["description"] = forkRepo.Description
6363
ctx.Data["IsPrivate"] = forkRepo.IsPrivate
64+
canForkToUser := !ctx.User.HasForkedRepo(forkRepo.ID)
65+
ctx.Data["CanForkToUser"] = canForkToUser
6466

6567
if err = forkRepo.GetOwner(); err != nil {
6668
ctx.Handle(500, "GetOwner", err)
@@ -73,7 +75,19 @@ func getForkRepository(ctx *context.Context) *models.Repository {
7375
ctx.Handle(500, "GetOrganizations", err)
7476
return nil
7577
}
76-
ctx.Data["Orgs"] = ctx.User.Orgs
78+
var orgs []*models.User
79+
for _, org := range ctx.User.Orgs {
80+
if !org.HasForkedRepo(forkRepo.ID) {
81+
orgs = append(orgs, org)
82+
}
83+
}
84+
ctx.Data["Orgs"] = orgs
85+
86+
if canForkToUser {
87+
ctx.Data["ContextUser"] = ctx.User
88+
} else if len(orgs) > 0 {
89+
ctx.Data["ContextUser"] = orgs[0]
90+
}
7791

7892
return forkRepo
7993
}
@@ -87,23 +101,23 @@ func Fork(ctx *context.Context) {
87101
return
88102
}
89103

90-
ctx.Data["ContextUser"] = ctx.User
91104
ctx.HTML(200, tplFork)
92105
}
93106

94107
// ForkPost response for forking a repository
95108
func ForkPost(ctx *context.Context, form auth.CreateRepoForm) {
96109
ctx.Data["Title"] = ctx.Tr("new_fork")
97110

98-
forkRepo := getForkRepository(ctx)
111+
ctxUser := checkContextUser(ctx, form.UID)
99112
if ctx.Written() {
100113
return
101114
}
102115

103-
ctxUser := checkContextUser(ctx, form.UID)
116+
forkRepo := getForkRepository(ctx)
104117
if ctx.Written() {
105118
return
106119
}
120+
107121
ctx.Data["ContextUser"] = ctxUser
108122

109123
if ctx.HasError() {

templates/repo/header.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</div>
3333
{{if .CanBeForked}}
3434
<div class="ui compact labeled button" tabindex="0">
35-
<a class="ui compact button {{if eq .OwnerID $.SignedUserID}}poping up{{end}}" {{if not (eq .OwnerID $.SignedUserID)}}href="{{AppSubUrl}}/repo/fork/{{.ID}}"{{else}} data-content="{{$.i18n.Tr "repo.fork_from_self"}}" data-position="top center" data-variation="tiny"{{end}}>
35+
<a class="ui compact button {{if not $.CanSignedUserFork}}poping up{{end}}" {{if $.CanSignedUserFork}}href="{{AppSubUrl}}/repo/fork/{{.ID}}"{{else}} data-content="{{$.i18n.Tr "repo.fork_from_self"}}" data-position="top center" data-variation="tiny"{{end}}>
3636
<i class="octicon octicon-repo-forked"></i>{{$.i18n.Tr "repo.fork"}}
3737
</a>
3838
<a class="ui basic label" href="{{.Link}}/forks">

templates/repo/pulls/fork.tmpl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
</span>
2020
<i class="dropdown icon"></i>
2121
<div class="menu">
22-
<div class="item" data-value="{{.SignedUser.ID}}">
23-
<img class="ui mini image" src="{{.SignedUser.RelAvatarLink}}">
24-
{{.SignedUser.ShortName 20}}
25-
</div>
22+
{{if .CanForkToUser}}
23+
<div class="item" data-value="{{.SignedUser.ID}}">
24+
<img class="ui mini image" src="{{.SignedUser.RelAvatarLink}}">
25+
{{.SignedUser.ShortName 20}}
26+
</div>
27+
{{end}}
2628
{{range .Orgs}}
2729
{{if and (.IsOwnedBy $.SignedUser.ID) (ne .ID $.ForkFromOwnerID)}}
2830
<div class="item" data-value="{{.ID}}">

0 commit comments

Comments
 (0)