mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 19:11:25 +01:00
- `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>
83 lines
3.1 KiB
Go
83 lines
3.1 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package services
|
|
|
|
import (
|
|
"context"
|
|
|
|
gitea "gitea.dev/sdk"
|
|
"gitea.dev/tea/internal/transport"
|
|
)
|
|
|
|
// Comment is the public representation shared by issue and pull request comments.
|
|
type Comment struct {
|
|
ID int64 `json:"id"`
|
|
Body string `json:"body"`
|
|
URL string `json:"url"`
|
|
Author string `json:"author"`
|
|
CreatedAt string `json:"created_at"`
|
|
UpdatedAt string `json:"updated_at"`
|
|
}
|
|
|
|
func commentFrom(comment *gitea.Comment) Comment {
|
|
return Comment{
|
|
ID: comment.ID, Body: comment.Body, URL: comment.HTMLURL, Author: login(comment.Poster),
|
|
CreatedAt: timestamp(comment.Created), UpdatedAt: timestamp(comment.Updated),
|
|
}
|
|
}
|
|
|
|
// Comments implements discussion comments under their owning repository resource.
|
|
type Comments struct {
|
|
Client *transport.Client
|
|
Repository string
|
|
}
|
|
|
|
// List returns every discussion comment; the API does not paginate this collection.
|
|
func (s Comments) List(ctx context.Context, n int64) ([]Comment, error) {
|
|
comments, err := repoCall(ctx, s.Client, s.Repository, func(ctx context.Context, c *gitea.Client, owner, repo string) ([]*gitea.Comment, *gitea.Response, error) {
|
|
return c.Issues.ListIssueComments(ctx, owner, repo, n, gitea.ListIssueCommentOptions{Page: -1})
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return convertAll(comments, commentFrom), nil
|
|
}
|
|
|
|
// Page applies CLI pagination to the unpaginated comment collection.
|
|
func (s Comments) Page(ctx context.Context, n int64, limit, page int) ([]Comment, error) {
|
|
items, err := s.List(ctx, n)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return pageOf(items, limit, page)
|
|
}
|
|
|
|
// View retrieves a comment by its repository-scoped ID.
|
|
func (s Comments) View(ctx context.Context, id int64) (Comment, error) {
|
|
return repoConvert(ctx, s.Client, s.Repository, commentFrom, func(ctx context.Context, c *gitea.Client, owner, repo string) (*gitea.Comment, *gitea.Response, error) {
|
|
return c.Issues.GetIssueComment(ctx, owner, repo, id)
|
|
})
|
|
}
|
|
|
|
// Create adds a comment to an issue or pull request.
|
|
func (s Comments) Create(ctx context.Context, n int64, body string) (Comment, error) {
|
|
return repoConvert(ctx, s.Client, s.Repository, commentFrom, func(ctx context.Context, c *gitea.Client, owner, repo string) (*gitea.Comment, *gitea.Response, error) {
|
|
return c.Issues.CreateIssueComment(ctx, owner, repo, n, gitea.CreateIssueCommentOption{Body: body})
|
|
})
|
|
}
|
|
|
|
// Edit replaces the body of a repository comment.
|
|
func (s Comments) Edit(ctx context.Context, id int64, body string) (Comment, error) {
|
|
return repoConvert(ctx, s.Client, s.Repository, commentFrom, func(ctx context.Context, c *gitea.Client, owner, repo string) (*gitea.Comment, *gitea.Response, error) {
|
|
return c.Issues.EditIssueComment(ctx, owner, repo, id, gitea.EditIssueCommentOption{Body: body})
|
|
})
|
|
}
|
|
|
|
// Delete removes a repository comment.
|
|
func (s Comments) Delete(ctx context.Context, id int64) error {
|
|
return repoDo(ctx, s.Client, s.Repository, func(ctx context.Context, c *gitea.Client, owner, repo string) (*gitea.Response, error) {
|
|
return c.Issues.DeleteIssueComment(ctx, owner, repo, id)
|
|
})
|
|
}
|