mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
refactor(completion): complete assignees and tags through the SDK
Completion fetched repository assignees and tags with raw GETs. List them through new Repositories.Assignees and Repositories.Tags service methods on the SDK, and remove the raw fetch helper. Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
3 files changed
+46
-32
No files matched your search
@@ -5,10 +5,8 @@ package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"maps"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -18,7 +16,6 @@ import (
|
||||
"gitea.dev/tea/internal/services"
|
||||
"gitea.dev/tea/internal/settings"
|
||||
"gitea.dev/tea/internal/terminal"
|
||||
"gitea.dev/tea/internal/transport"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
@@ -337,16 +334,6 @@ func (a *application) completionTarget(ctx context.Context, requireRepo bool) (t
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// fetchJSON decodes the response to a GET of endpoint, for values that no service lists.
|
||||
func fetchJSON(ctx context.Context, client *transport.Client, endpoint string, out any) error {
|
||||
return services.API{Client: client}.Visit(ctx, services.APIOptions{Method: http.MethodGet, Endpoint: endpoint}, func(response *http.Response) error {
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil // Visit reports the status.
|
||||
}
|
||||
return json.NewDecoder(response.Body).Decode(out)
|
||||
})
|
||||
}
|
||||
|
||||
// completeIssues completes issue numbers in state (open, closed or all) with their titles.
|
||||
func (a *application) completeIssues(state string) cobra.CompletionFunc {
|
||||
return a.apiCompletion(true, func(ctx context.Context, t target, _ string) ([]cobra.Completion, error) {
|
||||
@@ -390,13 +377,8 @@ func (a *application) completeMilestones() cobra.CompletionFunc {
|
||||
// completeUsers completes comma-separated usernames that can be assigned in the repository.
|
||||
func (a *application) completeUsers() cobra.CompletionFunc {
|
||||
return commaSeparated(a.apiCompletion(true, func(ctx context.Context, t target, _ string) ([]cobra.Completion, error) {
|
||||
type user struct {
|
||||
Login string `json:"login"`
|
||||
FullName string `json:"full_name"`
|
||||
}
|
||||
var users []user
|
||||
err := fetchJSON(ctx, t.client, "repos/"+t.Repo+"/assignees", &users)
|
||||
return describe(users, func(item user) (string, string) { return item.Login, item.FullName }), err
|
||||
users, err := services.Repositories{Client: t.client}.Assignees(ctx, t.Repo)
|
||||
return describe(users, func(item services.Assignee) (string, string) { return item.Username, item.FullName }), err
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -416,12 +398,8 @@ func (a *application) completeBranches() cobra.CompletionFunc {
|
||||
// completeTags completes the repository's tags.
|
||||
func (a *application) completeTags() cobra.CompletionFunc {
|
||||
return a.apiCompletion(true, func(ctx context.Context, t target, _ string) ([]cobra.Completion, error) {
|
||||
type tag struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
var tags []tag
|
||||
err := fetchJSON(ctx, t.client, "repos/"+t.Repo+"/tags?limit="+strconv.Itoa(completionLimit), &tags)
|
||||
return describe(tags, func(item tag) (string, string) { return item.Name, "" }), err
|
||||
tags, err := services.Repositories{Client: t.client}.Tags(ctx, t.Repo, completionLimit, 1)
|
||||
return describe(tags, func(name string) (string, string) { return name, "" }), err
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user