mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01: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
@@ -193,7 +193,7 @@ func (a *application) notificationWatchCommand(scope func(*cobra.Command) (notif
|
||||
Long: `Print notifications as they arrive or change, until Ctrl-C or --timeout. On a terminal
|
||||
it runs until stopped; otherwise it stops after a minute unless --timeout says otherwise.
|
||||
With --json it prints one JSON object per line.`,
|
||||
Example: "$ tea notification watch\n# Stream new pull request notifications to a script\n$ tea notification watch --type pull --json id,repository,title | ./notify.sh",
|
||||
Example: "$ tea notification watch\n# Stream pull request notifications to a script, starting with the current ones\n$ tea notification watch --type pull --json id,repository,title --timeout 0 | ./notify.sh",
|
||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||
listed, err := a.notificationOptions(opts, updated)
|
||||
if err != nil {
|
||||
@@ -284,10 +284,14 @@ pinned.`,
|
||||
if err := writeView(a, cmd, item, func(item services.Notification) terminal.View { return notificationView(item, comment, a.display()) }); err != nil {
|
||||
return err
|
||||
}
|
||||
// Marking is a side effect of viewing: a token without write:notification, or an
|
||||
// administrator reading another user's thread, still gets the view.
|
||||
if item.Unread && !item.Pinned {
|
||||
_, err = s.Mark(cmd.Context(), id, "read")
|
||||
if _, err := s.Mark(cmd.Context(), id, "read"); err != nil {
|
||||
a.warn(cmd, "Could not mark notification %d as read: %v", id, err)
|
||||
}
|
||||
}
|
||||
return err
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -380,17 +384,24 @@ func notificationView(item services.Notification, comment *services.Comment, d t
|
||||
Title: item.Title, Meta: meta, Kind: notificationKinds[item.Type], URL: item.URL,
|
||||
Badges: []terminal.Cell{{Text: titleCase(item.State), Color: stateColors[item.State]}, {Text: notificationStatus(item), Muted: true}},
|
||||
Fields: []terminal.Field{{Label: "Notification", Value: strconv.FormatInt(item.ID, 10)}, {Label: "Type", Value: notificationKinds[item.Type]}, {Label: "Repository", Value: item.Repository}},
|
||||
Body: "_No comments yet._",
|
||||
}
|
||||
if d.TTY {
|
||||
view.Body = "_No comments yet._"
|
||||
}
|
||||
view.Fields = append(view.Fields,
|
||||
terminal.Field{Label: "State", Value: item.State, PipedOnly: true},
|
||||
terminal.Field{Label: "Status", Value: notificationStatus(item), PipedOnly: true})
|
||||
if item.Number != 0 {
|
||||
view.ID = "#" + strconv.FormatInt(item.Number, 10)
|
||||
}
|
||||
if comment != nil {
|
||||
when := ""
|
||||
if created := terminal.ParseTime(comment.CreatedAt); !created.IsZero() {
|
||||
if created := terminal.ParseTime(comment.CreatedAt); d.TTY && !created.IsZero() {
|
||||
when = ", " + terminal.RelativeTime(d.Now, created)
|
||||
}
|
||||
view.Fields = append(view.Fields, terminal.Field{Label: "Latest comment", Value: comment.Author + when})
|
||||
commented := timeField("Commented", comment.CreatedAt, d)
|
||||
commented.PipedOnly = true
|
||||
view.Fields = append(view.Fields, terminal.Field{Label: "Latest comment", Value: comment.Author + when}, commented)
|
||||
view.Body = comment.Body
|
||||
}
|
||||
return view
|
||||
|
||||
@@ -155,3 +155,23 @@ func TestNotificationMarkAllNamesTheBeforeCutoff(t *testing.T) {
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"notification", "mark-read", "--all", "--before=1h"}))
|
||||
require.Contains(t, withoutANSI(diagnostics.String()), "No unread notifications updated before 2028-02-01 11:00 in your inbox")
|
||||
}
|
||||
|
||||
func TestNotificationViewSurvivesARefusedMark(t *testing.T) {
|
||||
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodPatch:
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
_, _ = w.Write([]byte(`{"message":"token does not have write:notification scope"}`))
|
||||
default:
|
||||
_, _ = w.Write([]byte(`{"id":12,"unread":true,"repository":{"full_name":"alice/project"},"subject":{"title":"Improve greeting","type":"Pull","state":"open","url":"https://gitea.test/api/v1/repos/alice/project/issues/17"}}`))
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
deps, out, diagnostics := serverDeps(t, server.URL)
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"notification", "view", "12"}))
|
||||
require.Contains(t, out.String(), "title:\tImprove greeting\n")
|
||||
require.Contains(t, out.String(), "state:\topen\n")
|
||||
require.Contains(t, out.String(), "status:\tunread\n")
|
||||
require.NotContains(t, out.String(), "_No comments yet._")
|
||||
require.Contains(t, diagnostics.String(), "Could not mark notification 12 as read")
|
||||
}
|
||||
@@ -120,7 +120,7 @@ func (a *application) wikiRevisionsCommand() *cobra.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
items, err := (services.Wiki{Client: target.client, Repository: target.Repo}).Revisions(cmd.Context(), args[0], page)
|
||||
revisions, err := (services.Wiki{Client: target.client, Repository: target.Repo}).Revisions(cmd.Context(), args[0], page)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -136,7 +136,7 @@ func (a *application) wikiRevisionsCommand() *cobra.Command {
|
||||
},
|
||||
Context: "revisions of wiki page " + strconv.Quote(args[0]), Empty: "no revisions of wiki page " + strconv.Quote(args[0]),
|
||||
}
|
||||
return writeTable(a, cmd, items, spec, listPage{Number: page})
|
||||
return writeTable(a, cmd, revisions.Items, spec, servicePage(revisions, page))
|
||||
},
|
||||
}
|
||||
positiveIntVar(command, &page, "page", 1, math.MaxInt, "Revision page (server controls page size)")
|
||||
@@ -280,11 +280,11 @@ $ tea wiki edit Home --editor`,
|
||||
}
|
||||
}
|
||||
}
|
||||
item, err := service.Edit(cmd.Context(), args[0], services.WikiOptions{Title: content.title, Body: body, Message: content.message})
|
||||
item, renamed, err := service.Edit(cmd.Context(), args[0], services.WikiOptions{Title: content.title, Body: body, Message: content.message})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if item.Title != args[0] {
|
||||
if renamed {
|
||||
a.done(cmd, "Renamed wiki page %q to %q in %s", args[0], item.Title, target.Repo)
|
||||
} else {
|
||||
a.done(cmd, "Edited wiki page %q in %s", item.Title, target.Repo)
|
||||
|
||||
@@ -20,12 +20,19 @@ func TestWikiEditRenamesAndKeepsTheContent(t *testing.T) {
|
||||
_, _ = w.Write([]byte(`{"title":"New Title","content_base64":"S2VlcCBtZQ=="}`))
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/api/v1/repos/alice/x/wiki/page/New Title" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"title":"Old Title","content_base64":"S2VlcCBtZQ=="}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
deps, out, diagnostics := repoTTYDeps(t, server.URL)
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"wiki", "edit", "Old Title", "--title", "New Title", "-R", "alice/x", "--no-input"}))
|
||||
require.Equal(t, []string{"GET /api/v1/repos/alice/x/wiki/page/Old Title", "PATCH /api/v1/repos/alice/x/wiki/page/Old Title"}, requests)
|
||||
require.Equal(t, []string{
|
||||
"GET /api/v1/repos/alice/x/wiki/page/Old Title", "GET /api/v1/repos/alice/x/wiki/page/Old Title",
|
||||
"GET /api/v1/repos/alice/x/wiki/page/New Title", "PATCH /api/v1/repos/alice/x/wiki/page/Old Title",
|
||||
}, requests)
|
||||
require.Empty(t, out.String())
|
||||
require.Equal(t, "✓ Renamed wiki page \"Old Title\" to \"New Title\" in alice/x\n", diagnostics.String())
|
||||
err := Execute(context.Background(), deps, []string{"wiki", "edit", "Home", "-R", "alice/x"})
|
||||
@@ -65,3 +72,43 @@ func TestWikiViewAndCreate(t *testing.T) {
|
||||
require.Equal(t, "https://gitea.test/alice/x/wiki/Page-Two\n", out.String())
|
||||
require.Equal(t, "✓ Created wiki page \"Page Two\" in alice/x\n", diagnostics.String())
|
||||
}
|
||||
|
||||
func TestWikiEditRefusesToOverwriteOrCreate(t *testing.T) {
|
||||
patched := 0
|
||||
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.Method == http.MethodPatch:
|
||||
patched++
|
||||
_, _ = w.Write([]byte(`{"title":"Getting Started","content_base64":"eA=="}`))
|
||||
case r.URL.Path == "/api/v1/repos/alice/x/wiki/page/Draft":
|
||||
_, _ = w.Write([]byte(`{"title":"Draft","content_base64":"ZHJhZnQ="}`))
|
||||
case r.URL.Path == "/api/v1/repos/alice/x/wiki/page/Page B":
|
||||
_, _ = w.Write([]byte(`{"title":"Page B","content_base64":"Yg=="}`))
|
||||
case r.URL.Path == "/api/v1/repos/alice/x/wiki/page/Getting-Started":
|
||||
_, _ = w.Write([]byte(`{"title":"Getting Started","content_base64":"Zw=="}`))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
deps, _, diagnostics := repoTTYDeps(t, server.URL)
|
||||
err := Execute(context.Background(), deps, []string{"wiki", "edit", "Draft", "--title", "Page B", "-R", "alice/x", "--no-input"})
|
||||
require.ErrorContains(t, err, `wiki page "Page B" already exists in alice/x`)
|
||||
err = Execute(context.Background(), deps, []string{"wiki", "edit", "Drfat", "--body", "x", "-R", "alice/x", "--no-input"})
|
||||
require.ErrorContains(t, err, `wiki page "Drfat" not found`)
|
||||
require.Zero(t, patched, "neither refused edit may reach the PATCH")
|
||||
// Editing by the URL slug is not a rename.
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"wiki", "edit", "Getting-Started", "--body", "x", "-R", "alice/x", "--no-input"}))
|
||||
require.Equal(t, "✓ Edited wiki page \"Getting Started\" in alice/x\n", diagnostics.String())
|
||||
require.Equal(t, 1, patched)
|
||||
}
|
||||
|
||||
func TestWikiRevisionsCountsTheWholeHistory(t *testing.T) {
|
||||
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write([]byte(`{"commits":[{"sha":"abc","message":"Edit","author":{"name":"alice","date":"2026-10-11T18:00:00Z"}}],"count":56}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
deps, out, _ := repoTTYDeps(t, server.URL)
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"wiki", "revisions", "Home", "-R", "alice/x"}))
|
||||
require.Contains(t, out.String(), "Showing 1 of 56 revisions of wiki page \"Home\" · next: --page 2")
|
||||
}
|
||||
Reference in new issue
Block a user