mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
refactor: add services layer on the Gitea Go SDK (#1125)
- `internal/services`: typed Gitea workflows built on `gitea.dev/sdk`, independent of CLI flags and terminal I/O: issues, pull requests, reviews, comments, labels, milestones, releases and assets, repositories, branches, wiki, Actions (workflows, runs, jobs, logs, artifacts, secrets, variables), organizations, teams, webhooks, notifications, SSH keys, tracked time, admin users, search and status. - SDK types are mapped to tea's own output types, so `--json` stays a stable contract. Requests the SDK cannot express yet (e.g. pull list filters, multi-status run lists, org label archiving) go through small raw helpers, each marked `TODO(sdk)` with the missing SDK piece. - Targets Gitea 28 or newer: no version or API-schema probing. List helpers fill pages capped by `MAX_RESPONSE_ITEMS`. Nothing imports this package yet. Reviewed-on: https://gitea.com/gitea/tea/pulls/1125 Reviewed-by: TheFox0x7 <95654+thefox0x7@noreply.gitea.com>
This commit is contained in:
1 parent
faea86cd94
commit
12e415a7a7
80 files changed
+9512
No files matched your search
@@ -0,0 +1,112 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
gitea "gitea.dev/sdk"
|
||||
)
|
||||
|
||||
// ReviewRequestInput names users and teams within the repository's organization.
|
||||
type ReviewRequestInput struct {
|
||||
Users []string `json:"reviewers,omitempty"`
|
||||
Teams []string `json:"team_reviewers,omitempty"`
|
||||
}
|
||||
|
||||
// ReviewRequestChanges adds and removes explicitly named review requests.
|
||||
type ReviewRequestChanges struct {
|
||||
Add, Remove ReviewRequestInput
|
||||
}
|
||||
|
||||
// Validate rejects empty, duplicated, and conflicting names before any request.
|
||||
func (c ReviewRequestChanges) Validate() error {
|
||||
if len(c.Add.Users)+len(c.Add.Teams)+len(c.Remove.Users)+len(c.Remove.Teams) == 0 {
|
||||
return errors.New("supply at least one reviewer or team")
|
||||
}
|
||||
for _, pair := range []struct {
|
||||
add, remove []string
|
||||
noun string
|
||||
}{{c.Add.Users, c.Remove.Users, "reviewer"}, {c.Add.Teams, c.Remove.Teams, "team"}} {
|
||||
seen := map[string]bool{}
|
||||
for _, names := range [][]string{pair.add, pair.remove} {
|
||||
for _, name := range names {
|
||||
if strings.TrimSpace(name) == "" || name != strings.TrimSpace(name) {
|
||||
return fmt.Errorf("%s names cannot be empty or have surrounding whitespace", pair.noun)
|
||||
}
|
||||
key := strings.ToLower(name)
|
||||
if seen[key] {
|
||||
return fmt.Errorf("%s %q is duplicated or present in both add and remove", pair.noun, name)
|
||||
}
|
||||
seen[key] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ChangeRequests returns the PR after changes and reports partial mutation failures.
|
||||
func (s Reviews) ChangeRequests(ctx context.Context, n int64, changes ReviewRequestChanges) (Pull, error) {
|
||||
if err := changes.Validate(); err != nil {
|
||||
return Pull{}, err
|
||||
}
|
||||
if len(changes.Add.Teams)+len(changes.Remove.Teams) > 0 {
|
||||
if err := s.CheckRequestTeams(ctx, append(append([]string{}, changes.Add.Teams...), changes.Remove.Teams...)); err != nil {
|
||||
return Pull{}, err
|
||||
}
|
||||
}
|
||||
mutated := false
|
||||
for _, action := range []struct {
|
||||
remove bool
|
||||
input ReviewRequestInput
|
||||
}{{true, changes.Remove}, {false, changes.Add}} {
|
||||
if len(action.input.Users)+len(action.input.Teams) == 0 {
|
||||
continue
|
||||
}
|
||||
option := gitea.PullReviewRequestOptions{Reviewers: action.input.Users, TeamReviewers: action.input.Teams}
|
||||
err := repoDo(ctx, s.Client, s.Repository, func(ctx context.Context, c *gitea.Client, owner, name string) (*gitea.Response, error) {
|
||||
if action.remove {
|
||||
return c.PullRequests.DeleteReviewRequests(ctx, owner, name, n, option)
|
||||
}
|
||||
return c.PullRequests.CreateReviewRequests(ctx, owner, name, n, option)
|
||||
})
|
||||
if err != nil {
|
||||
if mutated {
|
||||
return Pull{}, fmt.Errorf("reviewer removals succeeded, but adding reviewers failed: %w", err)
|
||||
}
|
||||
return Pull{}, fmt.Errorf("review request update failed; Gitea may have applied some requests: %w", err)
|
||||
}
|
||||
mutated = true
|
||||
}
|
||||
item, err := (Pulls{Client: s.Client, Repository: s.Repository}).View(ctx, n)
|
||||
if err != nil {
|
||||
return Pull{}, fmt.Errorf("review requests were updated, but reading the pull request failed: %w", err)
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
// CheckRequestTeams verifies that every named team exists in the repository's organization.
|
||||
func (s Reviews) CheckRequestTeams(ctx context.Context, names []string) error {
|
||||
owner, _, _ := strings.Cut(s.Repository, "/")
|
||||
_, isOrg, err := organization(ctx, s.Client, owner)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !isOrg {
|
||||
return errors.New("team reviews require an organization repository")
|
||||
}
|
||||
teams, err := (Teams{Client: s.Client}).ByName(ctx, owner)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, name := range names {
|
||||
if _, ok := teams[strings.ToLower(name)]; !ok {
|
||||
return fmt.Errorf("team %q is unavailable in organization %s", name, owner)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user