Skip to content

Commit c3eea2f

Browse files
qwerty2876543silverwindwxiaoguanglunny
authored
Improve behavior of "Fork" button (#17288)
* Improbe behaviour of fork button * Apply suggestions from code review * Remove old lines * Apply suggestions * Fix test * Remove unnecessary or * Update templates/repo/header.tmpl Co-authored-by: silverwind <[email protected]> * Add comment * Fix situation if you can't fork but don't have forks * Fix lint * Apply changes from #17783 * fmt * fmt * Apply tweaks Co-authored by: silverwind <[email protected]> * Rm dupl css * Fix build Co-authored-by: 6543 <[email protected]> Co-authored-by: silverwind <[email protected]> Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent e0118b0 commit c3eea2f

File tree

5 files changed

+97
-5
lines changed

5 files changed

+97
-5
lines changed

models/repo.go

+30
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,36 @@ func CanUserForkRepo(user *user_model.User, repo *repo_model.Repository) (bool,
294294
return false, nil
295295
}
296296

297+
// GetForksByUserAndOrgs return forked repos of the user and owned orgs
298+
func GetForksByUserAndOrgs(user *user_model.User, repo *repo_model.Repository) ([]*repo_model.Repository, error) {
299+
var repoList []*repo_model.Repository
300+
if user == nil {
301+
return repoList, nil
302+
}
303+
var forkedRepo *repo_model.Repository
304+
forkedRepo, err := repo_model.GetUserFork(repo.ID, user.ID)
305+
if err != nil {
306+
return repoList, err
307+
}
308+
if forkedRepo != nil {
309+
repoList = append(repoList, forkedRepo)
310+
}
311+
canCreateRepos, err := GetOrgsCanCreateRepoByUserID(user.ID)
312+
if err != nil {
313+
return repoList, err
314+
}
315+
for _, org := range canCreateRepos {
316+
forkedRepo, err := repo_model.GetUserFork(repo.ID, org.ID)
317+
if err != nil {
318+
return repoList, err
319+
}
320+
if forkedRepo != nil {
321+
repoList = append(repoList, forkedRepo)
322+
}
323+
}
324+
return repoList, nil
325+
}
326+
297327
// CanUserDelete returns true if user could delete the repository
298328
func CanUserDelete(repo *repo_model.Repository, user *user_model.User) (bool, error) {
299329
if user.IsAdmin || user.ID == repo.OwnerID {

modules/context/repo.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,24 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
499499
ctx.Data["CanWriteIssues"] = ctx.Repo.CanWrite(unit_model.TypeIssues)
500500
ctx.Data["CanWritePulls"] = ctx.Repo.CanWrite(unit_model.TypePullRequests)
501501

502-
if ctx.Data["CanSignedUserFork"], err = models.CanUserForkRepo(ctx.User, ctx.Repo.Repository); err != nil {
503-
ctx.ServerError("CanSignedUserFork", err)
502+
canSignedUserFork, err := models.CanUserForkRepo(ctx.User, ctx.Repo.Repository)
503+
if err != nil {
504+
ctx.ServerError("CanUserForkRepo", err)
505+
return
506+
}
507+
ctx.Data["CanSignedUserFork"] = canSignedUserFork
508+
509+
userAndOrgForks, err := models.GetForksByUserAndOrgs(ctx.User, ctx.Repo.Repository)
510+
if err != nil {
511+
ctx.ServerError("GetForksByUserAndOrgs", err)
504512
return
505513
}
514+
ctx.Data["UserAndOrgForks"] = userAndOrgForks
515+
516+
// canSignedUserFork is true if the current user doesn't have a fork of this repo yet or
517+
// if he owns an org that doesn't have a fork of this repo yet
518+
// If multiple forks are available or if the user can fork to another account, but there is already a fork: open selection dialog
519+
ctx.Data["ShowForkModal"] = len(userAndOrgForks) > 1 || (canSignedUserFork && len(userAndOrgForks) > 0)
506520

507521
ctx.Data["DisableSSH"] = setting.SSH.Disabled
508522
ctx.Data["ExposeAnonSSH"] = setting.SSH.ExposeAnonymous

options/locale/locale_en-US.ini

+2
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,8 @@ visibility_fork_helper = (Changing this will affect all forks.)
785785
clone_helper = Need help cloning? Visit <a target="_blank" rel="noopener noreferrer" href="%s">Help</a>.
786786
fork_repo = Fork Repository
787787
fork_from = Fork From
788+
already_forked = You've already forked %s
789+
fork_to_different_account = Fork to a different account
788790
fork_visibility_helper = The visibility of a forked repository cannot be changed.
789791
use_template = Use this template
790792
clone_in_vsc = Clone in VS Code

templates/repo/header.tmpl

+47-2
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,55 @@
8585
</form>
8686
{{end}}
8787
{{if and (not .IsEmpty) ($.Permission.CanRead $.UnitTypeCode)}}
88-
<div class="ui labeled button{{if not $.CanSignedUserFork}} tooltip disabled{{end}}"{{if and (not $.CanSignedUserFork) $.IsSigned}} data-content="{{$.i18n.Tr "repo.fork_from_self"}}" {{else if not $.IsSigned}} data-content="{{$.i18n.Tr "repo.fork_guest_user"}}"{{end}} data-position="top center" tabindex="0">
89-
<a class="ui compact small basic button"{{if $.CanSignedUserFork}} href="{{AppSubUrl}}/repo/fork/{{.ID}}"{{end}}>
88+
<div class="ui labeled button
89+
{{if or (not $.IsSigned) (and (not $.CanSignedUserFork) (eq (len $.UserAndOrgForks) 0))}}
90+
tooltip disabled
91+
{{end}}"
92+
{{if not $.IsSigned}}
93+
data-content="{{$.i18n.Tr "repo.fork_guest_user"}}"
94+
{{else if and (not $.CanSignedUserFork) (eq (len $.UserAndOrgForks) 0)}}
95+
data-content="{{$.i18n.Tr "repo.fork_from_self"}}"
96+
{{end}}
97+
data-position="top center" data-variation="tiny" tabindex="0">
98+
<a class="ui compact{{if $.ShowForkModal}} show-modal{{end}} small basic button"
99+
{{if not $.CanSignedUserFork}}
100+
{{if gt (len $.UserAndOrgForks) 1}}
101+
data-modal="#fork-repo-modal"
102+
{{else if eq (len $.UserAndOrgForks) 1}}
103+
href="{{AppSubUrl}}/{{(index $.UserAndOrgForks 0).FullName}}"
104+
{{/*else is not required here, because the button shouldn't link to any site if you can't create a fork*/}}
105+
{{end}}
106+
{{else if eq (len $.UserAndOrgForks) 0}}
107+
href="{{AppSubUrl}}/repo/fork/{{.ID}}"
108+
{{else}}
109+
data-modal="#fork-repo-modal"
110+
{{end}}
111+
>
90112
{{svg "octicon-repo-forked"}}{{$.i18n.Tr "repo.fork"}}
91113
</a>
114+
<div class="ui small modal" id="fork-repo-modal">
115+
{{svg "octicon-x" 16 "close inside"}}
116+
<div class="header">
117+
{{$.i18n.Tr "repo.already_forked" .Name}}
118+
</div>
119+
<div class="content tl">
120+
<div class="ui list">
121+
{{range $.UserAndOrgForks}}
122+
<div class="ui item py-3">
123+
<a href="{{.Link}}">
124+
{{svg "octicon-repo-forked" 16 "mr-3"}}{{.FullName}}
125+
</a>
126+
</div>
127+
{{end}}
128+
</div>
129+
{{if $.CanSignedUserFork}}
130+
<div class="ui divider"></div>
131+
<a href="{{AppSubUrl}}/repo/fork/{{.ID}}">
132+
{{$.i18n.Tr "repo.fork_to_different_account"}}
133+
</a>
134+
{{end}}
135+
</div>
136+
</div>
92137
<a class="ui basic label" href="{{.Link}}/forks">
93138
{{CountFmt .NumForks}}
94139
</a>

web_src/less/_repository.less

+2-1
Original file line numberDiff line numberDiff line change
@@ -2746,7 +2746,8 @@
27462746
#delete-repo-modal,
27472747
#delete-wiki-modal,
27482748
#convert-fork-repo-modal,
2749-
#convert-mirror-repo-modal {
2749+
#convert-mirror-repo-modal,
2750+
#fork-repo-modal {
27502751
.ui.message {
27512752
width: 100% !important;
27522753
}

0 commit comments

Comments
 (0)