-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
models/repo.go
Outdated
return false, err | ||
} | ||
for _, org := range user.Orgs { | ||
if !org.HasForkedRepo(repo.ID) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lafriks OK. I see.
Otherwise LGTM |
models/repo.go
Outdated
if repo.OwnerID != user.ID && !user.HasForkedRepo(repo.ID) { | ||
return true, nil | ||
} | ||
if err := user.GetOrganizations(true); err != nil { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
routers/repo/pull.go
Outdated
@@ -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 |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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()
)
routers/repo/pull.go
Outdated
@@ -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 { |
There was a problem hiding this comment.
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.
f7ddb53
to
4e27da8
Compare
Can we please add a test to prevent another regression? |
4e27da8
to
0f894db
Compare
@lunny @Morlinest @daviian fixed all suggestions |
@ethantkoenig integration test added |
integrations/repo_fork_test.go
Outdated
"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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
LGTM |
@daviian @Morlinest needs your approvals. |
Make LG-TM work again |
…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
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