mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
perf(pr,issue): load the interactive create form data concurrently
Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
7 files changed
+239
-38
No files matched your search
@@ -23,30 +23,77 @@ func (s Pulls) CreateSelectables(ctx context.Context) (CreateSelectables, error)
|
||||
return loadCreateSelectables(ctx, s.Client, s.Repository)
|
||||
}
|
||||
|
||||
// PullCreateChoices seed the interactive create form: the repository, its pull request template
|
||||
// on the default branch, and the metadata selectables.
|
||||
type PullCreateChoices struct {
|
||||
Repository Repository
|
||||
Template string
|
||||
CreateSelectables
|
||||
}
|
||||
|
||||
// CreateChoices fetches the repository once for its default branch and permissions, then loads
|
||||
// the pull request template and the selectables concurrently.
|
||||
func (s Pulls) CreateChoices(ctx context.Context) (PullCreateChoices, error) {
|
||||
item, err := (Repositories{Client: s.Client}).get(ctx, s.Repository)
|
||||
if err != nil {
|
||||
return PullCreateChoices{}, err
|
||||
}
|
||||
result := PullCreateChoices{Repository: repositoryFrom(item)}
|
||||
err = loadTogether(
|
||||
func() (err error) {
|
||||
result.Template, err = s.Template(ctx, result.Repository.DefaultBranch)
|
||||
return err
|
||||
},
|
||||
func() (err error) {
|
||||
result.CreateSelectables, err = createSelectablesFor(ctx, s.Client, s.Repository, permissionsFrom(item).Push)
|
||||
return err
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return PullCreateChoices{}, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func loadCreateSelectables(ctx context.Context, client *transport.Client, repository string) (CreateSelectables, error) {
|
||||
result := CreateSelectables{Assignees: []string{}, Milestones: []Milestone{}, Labels: []Label{}}
|
||||
permissions, err := (Repositories{Client: client}).viewPermissions(ctx, repository)
|
||||
if err != nil || !permissions.Push {
|
||||
return result, err
|
||||
if err != nil {
|
||||
return CreateSelectables{Assignees: []string{}, Milestones: []Milestone{}, Labels: []Label{}}, err
|
||||
}
|
||||
return createSelectablesFor(ctx, client, repository, permissions.Push)
|
||||
}
|
||||
|
||||
// createSelectablesFor loads assignees, milestones, and labels concurrently when canPush.
|
||||
func createSelectablesFor(ctx context.Context, client *transport.Client, repository string, canPush bool) (CreateSelectables, error) {
|
||||
result := CreateSelectables{Assignees: []string{}, Milestones: []Milestone{}, Labels: []Label{}}
|
||||
if !canPush {
|
||||
return result, nil
|
||||
}
|
||||
result.CanPush = true
|
||||
if result.Assignees, err = (Repositories{Client: client}).assignees(ctx, repository); err != nil {
|
||||
return result, err
|
||||
}
|
||||
if result.Milestones, err = listAll(func(page int) ([]Milestone, error) {
|
||||
return (Milestones{Client: client, Repository: repository}).List(ctx, "open", 100, page)
|
||||
}); err != nil {
|
||||
return result, err
|
||||
}
|
||||
if result.Labels, err = (Labels{Client: client, Repository: repository}).All(ctx); err != nil {
|
||||
return result, err
|
||||
}
|
||||
// Like --label, offer the labels shared by the organization that owns the repository.
|
||||
owner, _, _ := strings.Cut(repository, "/")
|
||||
if _, isOrg, err := organization(ctx, client, owner); err != nil || !isOrg {
|
||||
return result, err
|
||||
}
|
||||
orgLabels, err := (Labels{Client: client, Organization: owner}).All(ctx)
|
||||
result.Labels = append(result.Labels, orgLabels...)
|
||||
err := loadTogether(
|
||||
func() (err error) {
|
||||
result.Assignees, err = (Repositories{Client: client}).assignees(ctx, repository)
|
||||
return err
|
||||
},
|
||||
func() (err error) {
|
||||
result.Milestones, err = listAll(func(page int) ([]Milestone, error) {
|
||||
return (Milestones{Client: client, Repository: repository}).List(ctx, "open", 100, page)
|
||||
})
|
||||
return err
|
||||
},
|
||||
func() (err error) {
|
||||
if result.Labels, err = (Labels{Client: client, Repository: repository}).All(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
// Like --label, offer the labels shared by the organization that owns the repository.
|
||||
owner, _, _ := strings.Cut(repository, "/")
|
||||
if _, isOrg, err := organization(ctx, client, owner); err != nil || !isOrg {
|
||||
return err
|
||||
}
|
||||
orgLabels, err := (Labels{Client: client, Organization: owner}).All(ctx)
|
||||
result.Labels = append(result.Labels, orgLabels...)
|
||||
return err
|
||||
},
|
||||
)
|
||||
return result, err
|
||||
}
|
||||
Reference in new issue
Block a user