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

Prevent duplicate records in organizations list when creating a repository #11303

Merged

Conversation

6543
Copy link
Member

@6543 6543 commented May 5, 2020

prevent double entries in results of GetOrgsCanCreateRepoByUserID

I first try to only add GroupBy directly but xorm return broken user objects ...

... solution was to just query related UserIDs(OrgIDs) first and return OrgUsers based on this IDs

close #11258

prefent dubble entrys
@zeripath zeripath changed the title FIX GetOrgsCanCreateRepoByUserID Prevent multiple listings of organization when creating a repository May 5, 2020
@6543
Copy link
Member Author

6543 commented May 5, 2020

I think I hit another xorm issue :(

GetOrgsCanCreateRepoByUserID: mssql: Cannot call methods on nvarchar.
GetOrgsCanCreateRepoByUserID: pq: syntax error at or near "."

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label May 5, 2020
models/org.go Outdated Show resolved Hide resolved
@zeripath
Copy link
Contributor

zeripath commented May 5, 2020

How about:

// GetOrgsCanCreateRepoByUserID returns a list of organizations where given user ID
// are allowed to create repos.
func GetOrgsCanCreateRepoByUserID(userID int64) ([]*User, error) {
	orgs := make([]*User, 0, 10)

	return orgs, x.Where("id IN ("+
		"SELECT `user`.id FROM `user` "+
		"INNER JOIN `team_user` ON `team_user`.org_id = `user`.id "+
		"INNER JOIN `team` on `team`.id = `team_user`.team_id "+
		"WHERE `team_user`.uid = ? AND "+
		"(`team`.authorize = ? OR `team`.can_create_org_repo = ?) "+
		"GROUP BY `user`.id)", userID, AccessModeOwner, true).
		Desc("`user`.updated_unix").
		Find(&orgs)
}

Fixed some spelling mistakes on my behalf.

models/org.go Outdated Show resolved Hide resolved
models/org.go Outdated Show resolved Hide resolved
@GiteaBot GiteaBot 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 May 5, 2020
@codecov-io
Copy link

Codecov Report

Merging #11303 into master will increase coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #11303      +/-   ##
==========================================
+ Coverage   43.82%   43.85%   +0.03%     
==========================================
  Files         607      607              
  Lines       86899    86902       +3     
==========================================
+ Hits        38080    38115      +35     
+ Misses      44115    44084      -31     
+ Partials     4704     4703       -1     
Impacted Files Coverage Δ
models/org.go 70.46% <100.00%> (+0.14%) ⬆️
modules/indexer/stats/queue.go 62.50% <0.00%> (-18.75%) ⬇️
modules/indexer/stats/db.go 40.62% <0.00%> (-9.38%) ⬇️
modules/git/utils.go 65.67% <0.00%> (-4.48%) ⬇️
modules/log/event.go 64.61% <0.00%> (-1.03%) ⬇️
modules/git/repo.go 49.79% <0.00%> (-0.84%) ⬇️
models/issue.go 52.41% <0.00%> (+0.46%) ⬆️
modules/notification/webhook/webhook.go 43.59% <0.00%> (+0.58%) ⬆️
services/mirror/mirror.go 19.40% <0.00%> (+0.65%) ⬆️
routers/repo/view.go 36.82% <0.00%> (+0.67%) ⬆️
... and 7 more

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 0396fcf...25a3e85. Read the comment docs.

@lafriks lafriks added this to the 1.12.0 milestone May 5, 2020
@lafriks
Copy link
Member

lafriks commented May 5, 2020

I would prefer that builder would be used to build subselect to be passed to in condition

@6543
Copy link
Member Author

6543 commented May 5, 2020

@lafriks done

models/org.go Outdated Show resolved Hide resolved
@6543
Copy link
Member Author

6543 commented May 6, 2020

something with the CI unrelated went wrong

@GiteaBot GiteaBot 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 May 7, 2020
@zeripath zeripath merged commit 507d0ec into go-gitea:master May 7, 2020
@lunny
Copy link
Member

lunny commented May 7, 2020

Please send back port to v1.11

@6543 6543 deleted the fix-GetOrgsCanCreateRepoByUserID_11258 branch May 7, 2020 15:23
@6543
Copy link
Member Author

6543 commented May 7, 2020

BP now :)

6543 added a commit to 6543-forks/gitea that referenced this pull request May 7, 2020
…o-gitea#11303)

prevent double entries in results of GetOrgsCanCreateRepoByUserID

I first try to only add GroupBy directly but xorm return broken user objects ...

... solution was to just query related UserIDs(OrgIDs) first and return OrgUsers based on this IDs

close go-gitea#11258

Co-authored-by: zeripath <art27@cantab.net>
@6543
Copy link
Member Author

6543 commented May 7, 2020

-> #11325

@lafriks lafriks added the backport/done All backports for this PR have been created label May 7, 2020
zeripath added a commit that referenced this pull request May 7, 2020
…11303) (#11325)

Backport #11303 

Prevent multiple listings of organization when creating a repository (#11303)

prevent double entries in results of GetOrgsCanCreateRepoByUserID

I first try to only add GroupBy directly but xorm return broken user objects ...

... solution was to just query related UserIDs(OrgIDs) first and return OrgUsers based on this IDs

close #11258

Co-authored-by: zeripath <art27@cantab.net>
@zeripath zeripath changed the title Prevent multiple listings of organization when creating a repository Prevent duplicate records in organizations list when creating a repository May 8, 2020
ydelafollye pushed a commit to ydelafollye/gitea that referenced this pull request Jul 31, 2020
…o-gitea#11303)

prevent double entries in results of GetOrgsCanCreateRepoByUserID

I first try to only add GroupBy directly but xorm return broken user objects ...

... solution was to just query related UserIDs(OrgIDs) first and return OrgUsers based on this IDs

close go-gitea#11258

Co-authored-by: zeripath <art27@cantab.net>
@go-gitea go-gitea locked and limited conversation to collaborators Nov 24, 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 lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

organization listed multiple times when creating a repository
7 participants