From e9780b4e861718c5d709d8e321ee4b465b253ba1 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 17 Dec 2020 01:02:52 +0100 Subject: [PATCH 1/2] rm GetOrganizations --- integrations/org_count_test.go | 5 ++- models/user.go | 53 -------------------------- routers/user/home.go | 7 ++-- templates/user/dashboard/repolist.tmpl | 2 +- 4 files changed, 8 insertions(+), 59 deletions(-) diff --git a/integrations/org_count_test.go b/integrations/org_count_test.go index 755ee3cee59f0..20917dc17e0c5 100644 --- a/integrations/org_count_test.go +++ b/integrations/org_count_test.go @@ -114,11 +114,12 @@ func doCheckOrgCounts(username string, orgCounts map[string]int, strict bool, ca Name: username, }).(*models.User) - user.GetOrganizations(&models.SearchOrganizationsOptions{All: true}) + orgs, err := models.GetOrgsByUserID(user.ID, true) + assert.NoError(t, err) calcOrgCounts := map[string]int{} - for _, org := range user.Orgs { + for _, org := range orgs { calcOrgCounts[org.LowerName] = org.NumRepos count, ok := canonicalCounts[org.LowerName] if ok { diff --git a/models/user.go b/models/user.go index e2b2593006170..b0c2dbb2dd898 100644 --- a/models/user.go +++ b/models/user.go @@ -112,7 +112,6 @@ type User struct { LoginName string Type UserType OwnedOrgs []*User `xorm:"-"` - Orgs []*User `xorm:"-"` Repos []*Repository `xorm:"-"` Location string Website string @@ -537,58 +536,6 @@ func (u *User) GetOwnedOrganizations() (err error) { return err } -// GetOrganizations returns paginated organizations that user belongs to. -// TODO: does not respect All and show orgs you privately participate -func (u *User) GetOrganizations(opts *SearchOrganizationsOptions) error { - sess := x.NewSession() - defer sess.Close() - - schema, err := x.TableInfo(new(User)) - if err != nil { - return err - } - groupByCols := &strings.Builder{} - for _, col := range schema.Columns() { - fmt.Fprintf(groupByCols, "`%s`.%s,", schema.Name, col.Name) - } - groupByStr := groupByCols.String() - groupByStr = groupByStr[0 : len(groupByStr)-1] - - sess.Select("`user`.*, count(repo_id) as org_count"). - Table("user"). - Join("INNER", "org_user", "`org_user`.org_id=`user`.id"). - Join("LEFT", builder. - Select("id as repo_id, owner_id as repo_owner_id"). - From("repository"). - Where(accessibleRepositoryCondition(u)), "`repository`.repo_owner_id = `org_user`.org_id"). - And("`org_user`.uid=?", u.ID). - GroupBy(groupByStr) - if opts.PageSize != 0 { - sess = opts.setSessionPagination(sess) - } - type OrgCount struct { - User `xorm:"extends"` - OrgCount int - } - orgCounts := make([]*OrgCount, 0, 10) - - if err := sess. - Asc("`user`.name"). - Find(&orgCounts); err != nil { - return err - } - - orgs := make([]*User, len(orgCounts)) - for i, orgCount := range orgCounts { - orgCount.User.NumRepos = orgCount.OrgCount - orgs[i] = &orgCount.User - } - - u.Orgs = orgs - - return nil -} - // DisplayName returns full name if it's not empty, // returns username otherwise. func (u *User) DisplayName() string { diff --git a/routers/user/home.go b/routers/user/home.go index 779971ca97d40..40269ebf80e7d 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -56,11 +56,12 @@ func getDashboardContextUser(ctx *context.Context) *models.User { } ctx.Data["ContextUser"] = ctxUser - if err := ctx.User.GetOrganizations(&models.SearchOrganizationsOptions{All: true}); err != nil { - ctx.ServerError("GetOrganizations", err) + orgs, err := models.GetOrgsByUserID(ctx.User.ID, true) + if err != nil { + ctx.ServerError("GetOrgsByUserID", err) return nil } - ctx.Data["Orgs"] = ctx.User.Orgs + ctx.Data["Orgs"] = orgs return ctxUser } diff --git a/templates/user/dashboard/repolist.tmpl b/templates/user/dashboard/repolist.tmpl index 49ea869bf1f46..5f27377c8eac6 100644 --- a/templates/user/dashboard/repolist.tmpl +++ b/templates/user/dashboard/repolist.tmpl @@ -6,7 +6,7 @@ :more-repos-link="'{{.ContextUser.HomeLink}}'" {{if not .ContextUser.IsOrganization}} :organizations="[ - {{range .ContextUser.Orgs}} + {{range .Orgs}} {name: '{{.Name}}', num_repos: '{{.NumRepos}}'}, {{end}} ]" From 3113ec32df0ffe6372e7ae8c1381b9b3f8400eb2 Mon Sep 17 00:00:00 2001 From: zeripath Date: Thu, 17 Dec 2020 12:26:22 +0000 Subject: [PATCH 2/2] Cause NotifyMigrateRepository to emit a repo create webhook (#14004) * Cause NotifyMigrateRepository to emit a repo create webhook This PR simply makes NotifyMigrateRepository emit a Create Repo webhook. The reason for no new payload is that the information sent to NotifyMigrateRepository is only essentially the same as NotifyCreateRepository Fix #13996 Signed-off-by: Andrew Thornton --- modules/notification/webhook/webhook.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/modules/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go index 2a06eba219059..4b159f6248142 100644 --- a/modules/notification/webhook/webhook.go +++ b/modules/notification/webhook/webhook.go @@ -122,6 +122,18 @@ func (m *webhookNotifier) NotifyDeleteRepository(doer *models.User, repo *models } } +func (m *webhookNotifier) NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) { + // Add to hook queue for created repo after session commit. + if err := webhook_services.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{ + Action: api.HookRepoCreated, + Repository: convert.ToRepo(repo, models.AccessModeOwner), + Organization: convert.ToUser(u, false, false), + Sender: convert.ToUser(doer, false, false), + }); err != nil { + log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) + } +} + func (m *webhookNotifier) NotifyIssueChangeAssignee(doer *models.User, issue *models.Issue, assignee *models.User, removed bool, comment *models.Comment) { if issue.IsPull { mode, _ := models.AccessLevelUnit(doer, issue.Repo, models.UnitTypePullRequests)