]> source.dussan.org Git - gitea.git/commitdiff
Improve behavior of "Fork" button (#17288)
authorqwerty287 <80460567+qwerty287@users.noreply.github.com>
Mon, 13 Dec 2021 01:59:09 +0000 (02:59 +0100)
committerGitHub <noreply@github.com>
Mon, 13 Dec 2021 01:59:09 +0000 (09:59 +0800)
* 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 <me@silverwind.io>
* 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 <me@silverwind.io>

* Rm dupl css

* Fix build

Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
models/repo.go
modules/context/repo.go
options/locale/locale_en-US.ini
templates/repo/header.tmpl
web_src/less/_repository.less

index 397b4380d67acd43d845675231e82d96357cf7eb..897ceeede97939cebb178109286ce913391ca4d2 100644 (file)
@@ -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 {
index b54401f3484790c1ab3c6bd67ff2284538ddc249..010d3b7f818146d7f2978cdea34d1efe03d21f1d 100644 (file)
@@ -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
index b1f8b7994b3dc929dc61ac0d34985b3e392c7223..c70002278cef5d6109cb897506b663f31219d5e3 100644 (file)
@@ -785,6 +785,8 @@ visibility_fork_helper = (Changing this will affect all forks.)
 clone_helper = Need help cloning? Visit <a target="_blank" rel="noopener noreferrer" href="%s">Help</a>.
 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
index 4fb5e1941b9058191bb2046fc4be593721874011..014623dc9ae24cfc28cfff533b567fee8d758fad 100644 (file)
                                                </form>
                                        {{end}}
                                        {{if and (not .IsEmpty) ($.Permission.CanRead $.UnitTypeCode)}}
-                                               <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">
-                                                       <a class="ui compact small basic button"{{if $.CanSignedUserFork}} href="{{AppSubUrl}}/repo/fork/{{.ID}}"{{end}}>
+                                               <div class="ui labeled button
+                                                       {{if or (not $.IsSigned) (and (not $.CanSignedUserFork) (eq (len $.UserAndOrgForks) 0))}}
+                                                               tooltip disabled
+                                                       {{end}}"
+                                                       {{if not $.IsSigned}}
+                                                               data-content="{{$.i18n.Tr "repo.fork_guest_user"}}"
+                                                       {{else if and (not $.CanSignedUserFork) (eq (len $.UserAndOrgForks) 0)}}
+                                                               data-content="{{$.i18n.Tr "repo.fork_from_self"}}"
+                                                       {{end}}
+                                               data-position="top center" data-variation="tiny" tabindex="0">
+                                                       <a class="ui compact{{if $.ShowForkModal}} show-modal{{end}} small basic button"
+                                                               {{if not $.CanSignedUserFork}}
+                                                                       {{if gt (len $.UserAndOrgForks) 1}}
+                                                                               data-modal="#fork-repo-modal"
+                                                                       {{else if eq (len $.UserAndOrgForks) 1}}
+                                                                               href="{{AppSubUrl}}/{{(index $.UserAndOrgForks 0).FullName}}"
+                                                                       {{/*else is not required here, because the button shouldn't link to any site if you can't create a fork*/}}
+                                                                       {{end}}
+                                                               {{else if eq (len $.UserAndOrgForks) 0}}
+                                                                       href="{{AppSubUrl}}/repo/fork/{{.ID}}"
+                                                               {{else}}
+                                                                       data-modal="#fork-repo-modal"
+                                                               {{end}}
+                                                       >
                                                                {{svg "octicon-repo-forked"}}{{$.i18n.Tr "repo.fork"}}
                                                        </a>
+                                                       <div class="ui small modal" id="fork-repo-modal">
+                                                               {{svg "octicon-x" 16 "close inside"}}
+                                                               <div class="header">
+                                                                       {{$.i18n.Tr "repo.already_forked" .Name}}
+                                                               </div>
+                                                               <div class="content tl">
+                                                                       <div class="ui list">
+                                                                               {{range $.UserAndOrgForks}}
+                                                                                       <div class="ui item py-3">
+                                                                                               <a href="{{.Link}}">
+                                                                                                       {{svg "octicon-repo-forked" 16 "mr-3"}}{{.FullName}}
+                                                                                               </a>
+                                                                                       </div>
+                                                                               {{end}}
+                                                                       </div>
+                                                                       {{if $.CanSignedUserFork}}
+                                                                       <div class="ui divider"></div>
+                                                                       <a href="{{AppSubUrl}}/repo/fork/{{.ID}}">
+                                                                               {{$.i18n.Tr "repo.fork_to_different_account"}}
+                                                                       </a>
+                                                                       {{end}}
+                                                               </div>
+                                                       </div>
                                                        <a class="ui basic label" href="{{.Link}}/forks">
                                                                {{CountFmt .NumForks}}
                                                        </a>
index dab9b6ad00897fa3b119e19c7c0ee526fe37a43a..94ab67ef476421f2b64c1c1da4573f8f408f81a3 100644 (file)
 #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;
   }