mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 19:11:25 +01:00
fix(wiki,notification): guard wiki edit renames, show real revision totals and tidy notification view
Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
6 files changed
+145
-24
No files matched your search
+39
-11
@@ -150,22 +150,45 @@ func (s Wiki) Create(ctx context.Context, opts WikiOptions) (WikiPage, error) {
|
||||
item, err := s.page(ctx, func(ctx context.Context, c *gitea.Client, owner, repo string) (*gitea.WikiPage, *gitea.Response, error) {
|
||||
return c.Wiki.CreatePage(ctx, owner, repo, wikiOptions(opts))
|
||||
})
|
||||
if IsNotFound(err) {
|
||||
// Gitea answers a bare 404 when the wiki is disabled or the repository is missing, which
|
||||
// says nothing about the page being created.
|
||||
repository, checkErr := (Repositories{Client: s.Client}).get(ctx, s.Repository)
|
||||
if checkErr != nil {
|
||||
return WikiPage{}, checkErr
|
||||
}
|
||||
if !repository.HasWiki {
|
||||
return WikiPage{}, fmt.Errorf("the wiki of %s is disabled; turn it on with: tea repo edit --enable-wiki -R %s", s.Repository, s.Repository)
|
||||
}
|
||||
}
|
||||
return item, Classify(err, "wiki page", strconv.Quote(opts.Title), s.Repository)
|
||||
}
|
||||
|
||||
// Edit replaces page content and optionally changes its title.
|
||||
func (s Wiki) Edit(ctx context.Context, name string, opts WikiOptions) (WikiPage, error) {
|
||||
// Edit replaces page content and optionally changes its title. It reports whether the title
|
||||
// changed. Gitea's edit creates a page that does not exist and overwrites one the new title
|
||||
// names, so both are checked first.
|
||||
func (s Wiki) Edit(ctx context.Context, name string, opts WikiOptions) (WikiPage, bool, error) {
|
||||
current, err := s.View(ctx, name)
|
||||
if err != nil {
|
||||
return WikiPage{}, false, err
|
||||
}
|
||||
renamed := opts.Title != "" && opts.Title != current.Title
|
||||
if opts.Title == "" {
|
||||
current, err := s.View(ctx, name)
|
||||
if err != nil {
|
||||
return WikiPage{}, err
|
||||
}
|
||||
opts.Title = current.Title
|
||||
}
|
||||
if renamed {
|
||||
existing, err := s.View(ctx, opts.Title)
|
||||
if err == nil && existing.Title != current.Title {
|
||||
return WikiPage{}, false, &ConflictError{Reason: ReasonExists, Kind: "wiki page", Key: strconv.Quote(opts.Title), Scope: s.Repository}
|
||||
}
|
||||
if err != nil && !IsNotFound(err) {
|
||||
return WikiPage{}, false, err
|
||||
}
|
||||
}
|
||||
item, err := s.page(ctx, func(ctx context.Context, c *gitea.Client, owner, repo string) (*gitea.WikiPage, *gitea.Response, error) {
|
||||
return c.Wiki.EditPage(ctx, owner, repo, name, wikiOptions(opts))
|
||||
})
|
||||
return item, Classify(err, "wiki page", strconv.Quote(name), s.Repository)
|
||||
return item, renamed, Classify(err, "wiki page", strconv.Quote(name), s.Repository)
|
||||
}
|
||||
|
||||
// Delete removes a named wiki page.
|
||||
@@ -176,13 +199,18 @@ func (s Wiki) Delete(ctx context.Context, name string) error {
|
||||
return Classify(err, "wiki page", strconv.Quote(name), s.Repository)
|
||||
}
|
||||
|
||||
// Revisions returns one server-paginated page of wiki commits.
|
||||
func (s Wiki) Revisions(ctx context.Context, name string, page int) ([]WikiRevision, error) {
|
||||
// wikiRevisionPageSize is Gitea's default number of wiki commits per page ([git] COMMITS_RANGE_SIZE).
|
||||
const wikiRevisionPageSize = 50
|
||||
|
||||
// Revisions returns one server-paginated page of wiki commits and the total the server counted.
|
||||
func (s Wiki) Revisions(ctx context.Context, name string, page int) (Page[WikiRevision], error) {
|
||||
revisions, err := repoCall(ctx, s.Client, s.Repository, func(ctx context.Context, c *gitea.Client, owner, repo string) (*gitea.WikiCommitList, *gitea.Response, error) {
|
||||
return c.Wiki.GetPageRevisions(ctx, owner, repo, name, gitea.ListWikiPageRevisionsOptions{Page: page})
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return Page[WikiRevision]{}, err
|
||||
}
|
||||
return convertAll(revisions.WikiCommits, wikiRevisionFrom), nil
|
||||
items := convertAll(revisions.WikiCommits, wikiRevisionFrom)
|
||||
total := int(revisions.Count)
|
||||
return Page[WikiRevision]{Items: items, Total: total, Counted: true, More: (page-1)*wikiRevisionPageSize+len(items) < total}, nil
|
||||
}
|
||||
Reference in new issue
Block a user