diff --git a/models/repo.go b/models/repo.go index 397b4380d67ac..897ceeede9793 100644 --- a/models/repo.go +++ b/models/repo.go @@ -294,6 +294,36 @@ func CanUserForkRepo(user *user_model.User, repo *repo_model.Repository) (bool, return false, nil } +// GetForksByUserAndOrgs return forked repos of the user and owned orgs +func GetForksByUserAndOrgs(user *user_model.User, repo *repo_model.Repository) ([]*repo_model.Repository, error) { + var repoList []*repo_model.Repository + if user == nil { + return repoList, nil + } + var forkedRepo *repo_model.Repository + forkedRepo, err := repo_model.GetUserFork(repo.ID, user.ID) + if err != nil { + return repoList, err + } + if forkedRepo != nil { + repoList = append(repoList, forkedRepo) + } + canCreateRepos, err := GetOrgsCanCreateRepoByUserID(user.ID) + if err != nil { + return repoList, err + } + for _, org := range canCreateRepos { + forkedRepo, err := repo_model.GetUserFork(repo.ID, org.ID) + if err != nil { + return repoList, err + } + if forkedRepo != nil { + repoList = append(repoList, forkedRepo) + } + } + return repoList, nil +} + // CanUserDelete returns true if user could delete the repository func CanUserDelete(repo *repo_model.Repository, user *user_model.User) (bool, error) { if user.IsAdmin || user.ID == repo.OwnerID { diff --git a/modules/context/repo.go b/modules/context/repo.go index b54401f348479..010d3b7f81814 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -499,10 +499,24 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { ctx.Data["CanWriteIssues"] = ctx.Repo.CanWrite(unit_model.TypeIssues) ctx.Data["CanWritePulls"] = ctx.Repo.CanWrite(unit_model.TypePullRequests) - if ctx.Data["CanSignedUserFork"], err = models.CanUserForkRepo(ctx.User, ctx.Repo.Repository); err != nil { - ctx.ServerError("CanSignedUserFork", err) + canSignedUserFork, err := models.CanUserForkRepo(ctx.User, ctx.Repo.Repository) + if err != nil { + ctx.ServerError("CanUserForkRepo", err) + return + } + ctx.Data["CanSignedUserFork"] = canSignedUserFork + + userAndOrgForks, err := models.GetForksByUserAndOrgs(ctx.User, ctx.Repo.Repository) + if err != nil { + ctx.ServerError("GetForksByUserAndOrgs", err) return } + ctx.Data["UserAndOrgForks"] = userAndOrgForks + + // canSignedUserFork is true if the current user doesn't have a fork of this repo yet or + // if he owns an org that doesn't have a fork of this repo yet + // If multiple forks are available or if the user can fork to another account, but there is already a fork: open selection dialog + ctx.Data["ShowForkModal"] = len(userAndOrgForks) > 1 || (canSignedUserFork && len(userAndOrgForks) > 0) ctx.Data["DisableSSH"] = setting.SSH.Disabled ctx.Data["ExposeAnonSSH"] = setting.SSH.ExposeAnonymous diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index b1f8b7994b3dc..c70002278cef5 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -785,6 +785,8 @@ visibility_fork_helper = (Changing this will affect all forks.) clone_helper = Need help cloning? Visit Help. fork_repo = Fork Repository fork_from = Fork From +already_forked = You've already forked %s +fork_to_different_account = Fork to a different account fork_visibility_helper = The visibility of a forked repository cannot be changed. use_template = Use this template clone_in_vsc = Clone in VS Code diff --git a/templates/repo/header.tmpl b/templates/repo/header.tmpl index 4fb5e1941b905..014623dc9ae24 100644 --- a/templates/repo/header.tmpl +++ b/templates/repo/header.tmpl @@ -85,10 +85,55 @@ {{end}} {{if and (not .IsEmpty) ($.Permission.CanRead $.UnitTypeCode)}} -
- +
+ {{svg "octicon-repo-forked"}}{{$.i18n.Tr "repo.fork"}} + {{CountFmt .NumForks}} diff --git a/web_src/less/_repository.less b/web_src/less/_repository.less index dab9b6ad00897..94ab67ef47642 100644 --- a/web_src/less/_repository.less +++ b/web_src/less/_repository.less @@ -2746,7 +2746,8 @@ #delete-repo-modal, #delete-wiki-modal, #convert-fork-repo-modal, -#convert-mirror-repo-modal { +#convert-mirror-repo-modal, +#fork-repo-modal { .ui.message { width: 100% !important; }