mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
perf: drop wasted requests and reuse markdown renderers
pr edit with reviewers and other fields reads the pull request once, org delete counts only repositories, org view counts concurrently, team list skips the repository count of all-repository teams, pr diff --web builds the URL locally, pr list re-reads pull requests concurrently for review fields, and glamour renderers are built once per width and color. Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
8 files changed
+104
-47
No files matched your search
@@ -94,7 +94,8 @@ type OrganizationSize struct {
|
||||
Members, Teams, Repositories *int
|
||||
}
|
||||
|
||||
// Size counts the members, teams and repositories of an organization.
|
||||
// Size counts the members, teams and repositories of an organization; a count the account cannot
|
||||
// read is nil.
|
||||
func (s Organizations) Size(ctx context.Context, name string) (OrganizationSize, error) {
|
||||
first := listOptions(1, 1)
|
||||
counts := []func(context.Context, *gitea.Client) (*gitea.Response, error){
|
||||
@@ -106,24 +107,40 @@ func (s Organizations) Size(ctx context.Context, name string) (OrganizationSize,
|
||||
_, resp, err := c.Organizations.ListOrgTeams(ctx, name, gitea.ListTeamsOptions{ListOptions: first})
|
||||
return resp, err
|
||||
},
|
||||
func(ctx context.Context, c *gitea.Client) (*gitea.Response, error) {
|
||||
_, resp, err := c.Repositories.ListOrgRepos(ctx, name, gitea.ListOrgReposOptions{ListOptions: first})
|
||||
return resp, err
|
||||
},
|
||||
orgRepos(name),
|
||||
}
|
||||
totals := make([]*int, len(counts))
|
||||
for i, count := range counts {
|
||||
total, err := listTotal(ctx, s.Client, count)
|
||||
if ctx.Err() != nil {
|
||||
return OrganizationSize{}, ctx.Err()
|
||||
}
|
||||
if err == nil {
|
||||
totals[i] = total
|
||||
}
|
||||
_ = concurrently(len(counts), func(i int) error {
|
||||
totals[i], _ = listTotal(ctx, s.Client, counts[i])
|
||||
return nil
|
||||
})
|
||||
if ctx.Err() != nil {
|
||||
return OrganizationSize{}, ctx.Err()
|
||||
}
|
||||
return OrganizationSize{Members: totals[0], Teams: totals[1], Repositories: totals[2]}, nil
|
||||
}
|
||||
|
||||
// RepositoryCount counts the repositories of an organization, or returns nil when the account
|
||||
// cannot read the count.
|
||||
func (s Organizations) RepositoryCount(ctx context.Context, name string) (*int, error) {
|
||||
total, err := listTotal(ctx, s.Client, orgRepos(name))
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// orgRepos requests the first repository of an organization, to count them.
|
||||
func orgRepos(name string) func(context.Context, *gitea.Client) (*gitea.Response, error) {
|
||||
return func(ctx context.Context, c *gitea.Client) (*gitea.Response, error) {
|
||||
_, resp, err := c.Repositories.ListOrgRepos(ctx, name, gitea.ListOrgReposOptions{ListOptions: listOptions(1, 1)})
|
||||
return resp, err
|
||||
}
|
||||
}
|
||||
|
||||
// listTotal returns the collection size a list request reports, or nil when the server does not count it.
|
||||
func listTotal(ctx context.Context, client *transport.Client, list func(context.Context, *gitea.Client) (*gitea.Response, error)) (*int, error) {
|
||||
_, resp, err := call(ctx, client, func(ctx context.Context, c *gitea.Client) (struct{}, *gitea.Response, error) {
|
||||
|
||||
Reference in new issue
Block a user