Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve behavior of "Fork" button #17288

Merged
merged 47 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
3b641de
Improbe behaviour of fork button
qwerty287 Oct 11, 2021
24b83ee
Merge branch 'main' into fork-dialog
qwerty287 Oct 11, 2021
4a1facf
Apply suggestions from code review
qwerty287 Oct 11, 2021
97fb7bd
Remove old lines
qwerty287 Oct 11, 2021
c822b22
Apply suggestions
qwerty287 Oct 11, 2021
5bb9f9f
Fix test
qwerty287 Oct 11, 2021
859f69c
Merge branch 'main' into fork-dialog
qwerty287 Oct 11, 2021
265d9cf
Remove unnecessary or
qwerty287 Oct 11, 2021
128c2e6
Merge branch 'main' into fork-dialog
qwerty287 Oct 12, 2021
9dd9098
Merge branch 'main' into fork-dialog
6543 Oct 12, 2021
0a25a69
Merge branch 'main' into fork-dialog
qwerty287 Oct 15, 2021
eb261c5
Merge branch 'fork-dialog' of github.com:qwerty287/gitea into fork-di…
qwerty287 Oct 16, 2021
125c1c8
Merge branch 'main' into fork-dialog
qwerty287 Oct 16, 2021
01a6e67
Update templates/repo/header.tmpl
qwerty287 Oct 16, 2021
ad23347
Add comment
qwerty287 Oct 16, 2021
1a89f5d
Merge branch 'fork-dialog' of github.com:qwerty287/gitea into fork-di…
qwerty287 Oct 16, 2021
6f86036
Merge branch 'main' into fork-dialog
qwerty287 Oct 17, 2021
654738f
Merge branch 'main' into fork-dialog
qwerty287 Oct 21, 2021
4fc7b08
Merge branch 'main' into fork-dialog
wxiaoguang Oct 21, 2021
5d9c5f9
Merge branch 'main' into fork-dialog
qwerty287 Oct 22, 2021
af026cb
Merge branch 'main' into fork-dialog
qwerty287 Oct 23, 2021
ae7e9fa
Merge branch 'main' into fork-dialog
qwerty287 Oct 26, 2021
9667754
Merge branch 'main' into fork-dialog
qwerty287 Oct 28, 2021
4097805
Merge branch 'main' into fork-dialog
qwerty287 Nov 1, 2021
6a4ab3a
Merge branch 'main' into fork-dialog
qwerty287 Nov 7, 2021
be1bfba
Merge branch 'main' into fork-dialog
qwerty287 Nov 12, 2021
d094c23
Merge branch 'main' into fork-dialog
qwerty287 Nov 15, 2021
3917a02
Merge branch 'main' into fork-dialog
qwerty287 Nov 16, 2021
d02ee5c
Merge branch 'main' into fork-dialog
qwerty287 Nov 21, 2021
3968d00
Merge branch 'main' into fork-dialog
qwerty287 Nov 23, 2021
85149a9
Fix situation if you can't fork but don't have forks
qwerty287 Nov 23, 2021
f9f224d
Fix lint
qwerty287 Nov 23, 2021
34641cf
Merge branch 'main' into fork-dialog
qwerty287 Nov 25, 2021
75ce8ac
Apply changes from #17783
qwerty287 Nov 25, 2021
862d39a
fmt
qwerty287 Nov 25, 2021
73cc7e8
fmt
qwerty287 Nov 25, 2021
813ead4
Merge branch 'main' into fork-dialog
qwerty287 Nov 26, 2021
f87c0dc
Apply tweaks
qwerty287 Nov 27, 2021
6621589
Merge branch 'main' into fork-dialog
qwerty287 Nov 27, 2021
214651a
Merge branch 'main' into fork-dialog
qwerty287 Nov 29, 2021
43c0cfd
Merge branch 'main' into fork-dialog
qwerty287 Dec 5, 2021
7a6ec83
Merge branch 'main' into fork-dialog
wxiaoguang Dec 10, 2021
5b41a0a
Rm dupl css
qwerty287 Dec 11, 2021
bbb458f
Merge branch 'main' into fork-dialog
qwerty287 Dec 11, 2021
45d7872
Merge branch 'main' into fork-dialog
qwerty287 Dec 12, 2021
71dfc97
Fix build
qwerty287 Dec 12, 2021
c85aff6
Merge branch 'main' into fork-dialog
lunny Dec 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,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 (repo *Repository) GetForksByUserAndOrgs(user *user_model.User) ([]*Repository, error) {
var repoList []*Repository
if user == nil {
return repoList, nil
}
var forkedRepo *Repository
forkedRepo, err := repo.GetUserFork(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.GetUserFork(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 {
Expand Down
18 changes: 16 additions & 2 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ctx.Repo.Repository.GetForksByUserAndOrgs(ctx.User)
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
Expand Down
2 changes: 2 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 47 additions & 2 deletions templates/repo/header.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,55 @@
</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}}"
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
{{/*else is not required here, because the button shouldn't link to any site if you can't create a fork*/}}
{{end}}
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
{{else if eq (len $.UserAndOrgForks) 0}}
href="{{AppSubUrl}}/repo/fork/{{.ID}}"
{{else}}
data-modal="#fork-repo-modal"
{{end}}
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
>
{{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>
Expand Down
3 changes: 2 additions & 1 deletion web_src/less/_repository.less
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just very curious to ask ... why do we need this css style? I use !important for only one or two times in all my projects, but I see !important in Gitea css code everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added another one, because every dialog/modal is listed in this css style... (but I don't know why there is this style)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the modal dialog work without this style? If it works alright, we can get rid of this strange styling ....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it doesn't work without this style

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

important is needed because of semanatic/fomantic css that is not very though out and uses important all over the place so we also have to use important to style it :/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anytime you need to override vendor CSS, it's a good idea to use !important. We should of course reduce unnecessary use, of which there are also a few cases.

Copy link
Member

@silverwind silverwind Oct 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, there is a w-100 helper CSS class for width: 100% !important;, please use that instead.

.w-100 { width: 100% !important; }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@silverwind If I remove the fork-repo-modal and add the w-100 class to the modal, it doesn't work (the content of the fork-repo-modal class is .ui.message { width: 100% !important; } and not width: 100% !important;)

.ui.message {
width: 100% !important;
}
Expand Down