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

Fix so that user can still fork his own repository to his organizations #2699

Merged
merged 9 commits into from Oct 15, 2017

Conversation

lafriks
Copy link
Member

@lafriks lafriks commented Oct 13, 2017

Fixes #2698

Allows forking to organization even when user owns repository or has already forked to his user but still limits single fork per user/organization

@lafriks lafriks added issue/regression Issue needs no code to be fixed, only a description on how to fix it yourself backport/v1.2 labels Oct 13, 2017
@lafriks lafriks added this to the 1.3.0 milestone Oct 13, 2017
@codecov-io
Copy link

codecov-io commented Oct 13, 2017

Codecov Report

Merging #2699 into master will decrease coverage by 0.02%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #2699      +/-   ##
=========================================
- Coverage   27.12%   27.1%   -0.03%     
=========================================
  Files          87      87              
  Lines       17176   17191      +15     
=========================================
  Hits         4659    4659              
- Misses      11837   11852      +15     
  Partials      680     680
Impacted Files Coverage Δ
models/repo.go 12.95% <0%> (-0.12%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 32ca299...ea6d8eb. Read the comment docs.

@tboerger tboerger added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Oct 13, 2017
models/repo.go Outdated
return false, err
}
for _, org := range user.Orgs {
if !org.HasForkedRepo(repo.ID) {
Copy link
Member

Choose a reason for hiding this comment

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

maybe

if org.HasForkedRepo(repo.ID) {
    return false
}
}
return true

Copy link
Member

Choose a reason for hiding this comment

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

Not possible, since it would stop looping of all left organizations if first has already forked the repository.

Copy link
Member Author

Choose a reason for hiding this comment

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

@lunny No, that would not work correct. We are looking in this code to check if user has at least one organization that has not forked repository yet. And if user has forked && all orgs has forks than it is not possible to fork anymore - so return false

Copy link
Member

Choose a reason for hiding this comment

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

@lafriks OK. I see.

@lunny
Copy link
Member

lunny commented Oct 14, 2017

Otherwise LGTM

@tboerger tboerger added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Oct 14, 2017
models/repo.go Outdated
if repo.OwnerID != user.ID && !user.HasForkedRepo(repo.ID) {
return true, nil
}
if err := user.GetOrganizations(true); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

This does retrieve all organizations not respecting access rights to organization.
There should also be a check that user is owner of org at least.

Copy link
Member

Choose a reason for hiding this comment

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

I think user.GetOwnedOrganizations() can be used.

@@ -61,6 +61,8 @@ func getForkRepository(ctx *context.Context) *models.Repository {
ctx.Data["repo_name"] = forkRepo.Name
ctx.Data["description"] = forkRepo.Description
ctx.Data["IsPrivate"] = forkRepo.IsPrivate
canForkToUser := !ctx.User.HasForkedRepo(forkRepo.ID)
ctx.Data["CanForkToUser"] = canForkToUser
Copy link
Member

Choose a reason for hiding this comment

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

Should a user be able to fork his own repo into his user?

models/repo.go Outdated
if err := user.GetOrganizations(true); err != nil {
return false, err
}
for _, org := range user.Orgs {
Copy link
Member

Choose a reason for hiding this comment

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

user.OwnedOrgs (with previous use of user.GetOwnedOrganizations())

@@ -73,7 +75,19 @@ func getForkRepository(ctx *context.Context) *models.Repository {
ctx.Handle(500, "GetOrganizations", err)
return nil
}
ctx.Data["Orgs"] = ctx.User.Orgs
var orgs []*models.User
for _, org := range ctx.User.Orgs {
Copy link
Member

Choose a reason for hiding this comment

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

Same as above (https://github.com/go-gitea/gitea/pull/2699/files#diff-d4e2553a87eb8b329efd7335f028903cR648) as Orgs includes also organizations where you are in team that have only read rights.

@ethantkoenig
Copy link
Member

Can we please add a test to prevent another regression?

@lafriks
Copy link
Member Author

lafriks commented Oct 14, 2017

@lunny @Morlinest @daviian fixed all suggestions

@lafriks
Copy link
Member Author

lafriks commented Oct 14, 2017

@ethantkoenig integration test added

"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func testRepoFork(t *testing.T, session *TestSession) *TestResponse {
func testRepoFork(t *testing.T, session *TestSession, ownerName, repoName, forkOwnerName, forkOwnerID, forkRepoName string) *TestResponse {
Copy link
Member

Choose a reason for hiding this comment

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

nit: instead of requiring caller to specify forkOwnerID, we can infer it from forkOwnerName:

forkOwner := AssertExistsAndLoadBean(t, &models.User{Name: forkOwnerName}).(*models.User)
...
forkOwner.ID

Copy link
Member Author

Choose a reason for hiding this comment

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

@ethantkoenig thanks, done

ctx.Data["Orgs"] = orgs

if canForkToUser {
ctx.Data["ContextUser"] = ctx.User
Copy link
Member

Choose a reason for hiding this comment

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

In both places where getForkRepository() is called (Fork and ForkPost in routers/repo/pull.go), ctx.Data["ContextUser"] is immediately overwritten after this function is called.

Copy link
Member Author

Choose a reason for hiding this comment

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

@ethantkoenig it is not overwritten in Fork handler only in ForkPost. It is intentional so - getForkRepository fills in default ctx.Data["ContextUser"] value and in ForkPost it is overwritten by actually selected one from dropdown box.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I missed that you had removed that line

ctx.Data["Orgs"] = orgs

if canForkToUser {
ctx.Data["ContextUser"] = ctx.User
Copy link
Member

Choose a reason for hiding this comment

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

Oh, I missed that you had removed that line

@ethantkoenig
Copy link
Member

LGTM

@tboerger tboerger added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Oct 14, 2017
@lunny
Copy link
Member

lunny commented Oct 15, 2017

@daviian @Morlinest needs your approvals.

@lafriks
Copy link
Member Author

lafriks commented Oct 15, 2017

Make LG-TM work again

@lafriks lafriks merged commit 1ec4dc6 into go-gitea:master Oct 15, 2017
@lafriks lafriks deleted the feat/fork_own_to_org branch October 15, 2017 15:06
lafriks added a commit to lafriks-fork/gitea that referenced this pull request Oct 15, 2017
…ions (go-gitea#2699)

* Fix so that user can still fork his own repository to his organizations

* Fix to only use owned organizations

* Add integration test for forking own repository to owned organization
@lafriks lafriks added the backport/done All backports for this PR have been created label Oct 15, 2017
lafriks added a commit that referenced this pull request Oct 15, 2017
…ions (#2699) (#2707)

* Fix so that user can still fork his own repository to his organizations

* Fix to only use owned organizations

* Add integration test for forking own repository to owned organization
@go-gitea go-gitea locked and limited conversation to collaborators Nov 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backport/done All backports for this PR have been created issue/regression Issue needs no code to be fixed, only a description on how to fix it yourself lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression: no longer possible to fork own repo
7 participants