From 74047bee0d5b08a24fbe5f986758a47fc08fc4b6 Mon Sep 17 00:00:00 2001 From: Karl Heinz Marbaise Date: Tue, 8 Dec 2020 19:07:43 +0100 Subject: [PATCH 01/17] add subcomands for notifications Fix #243 Signed-off-by: Karl Heinz Marbaise --- cmd/notifications.go | 31 ++++++++++--------- cmd/notifications/list.go | 40 ++++++++++++++++++++++++ cmd/notifications/pinned.go | 41 +++++++++++++++++++++++++ cmd/notifications/read.go | 41 +++++++++++++++++++++++++ cmd/notifications/unread.go | 41 +++++++++++++++++++++++++ modules/task/notifications_list.go | 49 ++++++++++++++++++++++++++++++ 6 files changed, 228 insertions(+), 15 deletions(-) create mode 100644 cmd/notifications/list.go create mode 100644 cmd/notifications/pinned.go create mode 100644 cmd/notifications/read.go create mode 100644 cmd/notifications/unread.go create mode 100644 modules/task/notifications_list.go diff --git a/cmd/notifications.go b/cmd/notifications.go index 3cab988..b72d605 100644 --- a/cmd/notifications.go +++ b/cmd/notifications.go @@ -5,7 +5,9 @@ package cmd import ( - "code.gitea.io/tea/cmd/flags" + "log" + + "code.gitea.io/tea/cmd/notifications" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" @@ -18,27 +20,21 @@ var CmdNotifications = cli.Command{ Name: "notifications", Aliases: []string{"notification", "n"}, Usage: "Show notifications", - Description: "Show notifications, by default based of the current repo and unread one", + Description: "Show notifications, by default based of the current repo", Action: runNotifications, + Subcommands: []*cli.Command{ + ¬ifications.CmdNotificationsList, + ¬ifications.CmdNotificationsPinned, + ¬ifications.CmdNotificationsRead, + ¬ifications.CmdNotificationsUnread, + }, Flags: append([]cli.Flag{ &cli.BoolFlag{ Name: "all", Aliases: []string{"a"}, Usage: "show all notifications of related gitea instance", }, - &cli.BoolFlag{ - Name: "read", - Aliases: []string{"rd"}, - Usage: "show read notifications instead unread", - }, - &cli.BoolFlag{ - Name: "pinned", - Aliases: []string{"pd"}, - Usage: "show pinned notifications instead unread", - }, - &flags.PaginationPageFlag, - &flags.PaginationLimitFlag, - }, flags.AllDefaultFlags...), + }), } func runNotifications(cmd *cli.Context) error { @@ -80,3 +76,8 @@ func runNotifications(cmd *cli.Context) error { print.NotificationsList(news, ctx.Output, ctx.Bool("all")) return nil } + +func runNotificationsDetails(ctx *cli.Context) error { + log.Fatal("Not yet implemented.") + return nil +} diff --git a/cmd/notifications/list.go b/cmd/notifications/list.go new file mode 100644 index 0000000..caf5bb5 --- /dev/null +++ b/cmd/notifications/list.go @@ -0,0 +1,40 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package notifications + +import ( + "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/flags" + "code.gitea.io/tea/modules/task" + "github.com/urfave/cli/v2" +) + +// CmdNotificationsList represents a sub command of notifications to list notifications +var CmdNotificationsList = cli.Command{ + Name: "ls", + Aliases: []string{"list"}, + Usage: "List notifications....", + Description: `List notification....`, + Action: RunNotificationsList, + Flags: append([]cli.Flag{ + &cli.BoolFlag{ + Name: "all", + Aliases: []string{"a"}, + Usage: "show all notifications of related gitea instance", + }, + &cli.StringFlag{ + Name: "state", + Usage: "Filter by milestone state (all|open|closed)", + DefaultText: "open", + }, + &flags.PaginationPageFlag, + &flags.PaginationLimitFlag, + }, flags.AllDefaultFlags...), +} + +// RunNotificationsList list notifications +func RunNotificationsList(ctx *cli.Context) error { + return task.ListNotifications(ctx, []gitea.NotifyStatus{}) +} diff --git a/cmd/notifications/pinned.go b/cmd/notifications/pinned.go new file mode 100644 index 0000000..85432ed --- /dev/null +++ b/cmd/notifications/pinned.go @@ -0,0 +1,41 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package notifications + +import ( + "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/flags" + "code.gitea.io/tea/modules/task" + "github.com/urfave/cli/v2" +) + +// CmdNotificationsPinned represents a sub command of notifications to list pinned notifications +var CmdNotificationsPinned = cli.Command{ + Name: "pinned", + Aliases: []string{"pin"}, + Usage: "show pinned notifications", + Description: `show pinned notifications`, + Action: RunNotificationsPinned, + Flags: append([]cli.Flag{ + &cli.BoolFlag{ + Name: "all", + Aliases: []string{"a"}, + Usage: "show all notifications of related gitea instance", + }, + &cli.StringFlag{ + Name: "state", + Usage: "Filter by milestone state (all|open|closed)", + DefaultText: "open", + }, + &flags.PaginationPageFlag, + &flags.PaginationLimitFlag, + }, flags.AllDefaultFlags...), +} + +// RunNotificationsPinned will show notifications with status pinned. +func RunNotificationsPinned(ctx *cli.Context) error { + var statuses = []gitea.NotifyStatus{gitea.NotifyStatusPinned} + return task.ListNotifications(ctx, statuses) +} diff --git a/cmd/notifications/read.go b/cmd/notifications/read.go new file mode 100644 index 0000000..c4dc810 --- /dev/null +++ b/cmd/notifications/read.go @@ -0,0 +1,41 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package notifications + +import ( + "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/flags" + "code.gitea.io/tea/modules/task" + "github.com/urfave/cli/v2" +) + +// CmdNotificationsRead represents a sub command of notifications to list read notifications +var CmdNotificationsRead = cli.Command{ + Name: "read", + Aliases: []string{}, + Usage: "show read notifications instead", + Description: `show read notifications instead`, + Action: RunNotificationsRead, + Flags: append([]cli.Flag{ + &cli.BoolFlag{ + Name: "all", + Aliases: []string{"a"}, + Usage: "show all notifications of related gitea instance", + }, + &cli.StringFlag{ + Name: "state", + Usage: "Filter by milestone state (all|open|closed)", + DefaultText: "open", + }, + &flags.PaginationPageFlag, + &flags.PaginationLimitFlag, + }, flags.AllDefaultFlags...), +} + +// RunNotificationsList list notifications +func RunNotificationsRead(ctx *cli.Context) error { + var statuses = []gitea.NotifyStatus{gitea.NotifyStatusRead} + return task.ListNotifications(ctx, statuses) +} diff --git a/cmd/notifications/unread.go b/cmd/notifications/unread.go new file mode 100644 index 0000000..4a41756 --- /dev/null +++ b/cmd/notifications/unread.go @@ -0,0 +1,41 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package notifications + +import ( + "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/flags" + "code.gitea.io/tea/modules/task" + "github.com/urfave/cli/v2" +) + +// CmdNotificationsUnread represents a sub command of notifications to list unread notifications. +var CmdNotificationsUnread = cli.Command{ + Name: "unread", + Aliases: []string{}, + Usage: "show unread notifications", + Description: `show unread notifications`, + Action: RunNotificationsUnread, + Flags: append([]cli.Flag{ + &cli.BoolFlag{ + Name: "all", + Aliases: []string{"a"}, + Usage: "show all notifications of related gitea instance", + }, + &cli.StringFlag{ + Name: "state", + Usage: "Filter by milestone state (all|open|closed)", + DefaultText: "open", + }, + &flags.PaginationPageFlag, + &flags.PaginationLimitFlag, + }, flags.AllDefaultFlags...), +} + +// RunNotificationsList list notifications +func RunNotificationsUnread(ctx *cli.Context) error { + var statuses = []gitea.NotifyStatus{gitea.NotifyStatusUnread} + return task.ListNotifications(ctx, statuses) +} diff --git a/modules/task/notifications_list.go b/modules/task/notifications_list.go new file mode 100644 index 0000000..b27c2d3 --- /dev/null +++ b/modules/task/notifications_list.go @@ -0,0 +1,49 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package task + +import ( + "log" + + "code.gitea.io/tea/cmd/flags" + "code.gitea.io/tea/modules/config" + "code.gitea.io/tea/modules/print" + + "code.gitea.io/sdk/gitea" + "github.com/urfave/cli/v2" +) + +func ListNotifications(ctx *cli.Context, status []gitea.NotifyStatus) error { + + //TODO: What is the purpose of the following? + listOpts := flags.GetListOptions(ctx) + if listOpts.Page == 0 { + listOpts.Page = 1 + } + + var news []*gitea.NotificationThread + var err error + + var allRelated = ctx.Bool("all") + if allRelated { + login := config.InitCommandLoginOnly(flags.GlobalLoginValue) + news, _, err = login.Client().ListNotifications(gitea.ListNotificationOptions{ + ListOptions: listOpts, + Status: status, + }) + } else { + login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue) + news, _, err = login.Client().ListRepoNotifications(owner, repo, gitea.ListNotificationOptions{ + ListOptions: listOpts, + Status: status, + }) + } + if err != nil { + log.Fatal(err) + } + + print.NotificationsList(news, flags.GlobalOutputValue, allRelated) + return nil +} -- 2.40.1 From 75ad6b84848403a8303ef9af2de819b79533b455 Mon Sep 17 00:00:00 2001 From: Karl Heinz Marbaise Date: Tue, 8 Dec 2020 20:59:21 +0100 Subject: [PATCH 02/17] Added missing comment on exported function. --- modules/task/notifications_list.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/task/notifications_list.go b/modules/task/notifications_list.go index b27c2d3..58d6151 100644 --- a/modules/task/notifications_list.go +++ b/modules/task/notifications_list.go @@ -15,6 +15,7 @@ import ( "github.com/urfave/cli/v2" ) +//ListNotifications will get the notifications based on status func ListNotifications(ctx *cli.Context, status []gitea.NotifyStatus) error { //TODO: What is the purpose of the following? -- 2.40.1 From f9787139286e8a29a83d0ae02523577061389b68 Mon Sep 17 00:00:00 2001 From: Karl Heinz Marbaise Date: Tue, 8 Dec 2020 21:06:15 +0100 Subject: [PATCH 03/17] Fixed exported function comments. Signed-off-by: Karl Heinz Marbaise --- cmd/notifications/read.go | 2 +- cmd/notifications/unread.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/notifications/read.go b/cmd/notifications/read.go index c4dc810..95112ca 100644 --- a/cmd/notifications/read.go +++ b/cmd/notifications/read.go @@ -34,7 +34,7 @@ var CmdNotificationsRead = cli.Command{ }, flags.AllDefaultFlags...), } -// RunNotificationsList list notifications +// RunNotificationsRead will show notifications with status read. func RunNotificationsRead(ctx *cli.Context) error { var statuses = []gitea.NotifyStatus{gitea.NotifyStatusRead} return task.ListNotifications(ctx, statuses) diff --git a/cmd/notifications/unread.go b/cmd/notifications/unread.go index 4a41756..587a9ad 100644 --- a/cmd/notifications/unread.go +++ b/cmd/notifications/unread.go @@ -34,7 +34,7 @@ var CmdNotificationsUnread = cli.Command{ }, flags.AllDefaultFlags...), } -// RunNotificationsList list notifications +// RunNotificationsUnread will show notifications with status unread. func RunNotificationsUnread(ctx *cli.Context) error { var statuses = []gitea.NotifyStatus{gitea.NotifyStatusUnread} return task.ListNotifications(ctx, statuses) -- 2.40.1 From 91b5db3e420034e53e925e4d8b86692aab08f842 Mon Sep 17 00:00:00 2001 From: Karl Heinz Marbaise Date: Tue, 8 Dec 2020 21:30:09 +0100 Subject: [PATCH 04/17] Integrated review comments. Signed-off-by: Karl Heinz Marbaise --- cmd/flags/flags.go | 11 +++++++++++ cmd/notifications/list.go | 19 +++---------------- cmd/notifications/pinned.go | 17 ++--------------- cmd/notifications/read.go | 15 +-------------- cmd/notifications/unread.go | 15 +-------------- 5 files changed, 18 insertions(+), 59 deletions(-) diff --git a/cmd/flags/flags.go b/cmd/flags/flags.go index d9fe526..ac2b91a 100644 --- a/cmd/flags/flags.go +++ b/cmd/flags/flags.go @@ -91,3 +91,14 @@ var IssuePRFlags = append([]cli.Flag{ &PaginationPageFlag, &PaginationLimitFlag, }, AllDefaultFlags...) + +// NotificationFlags defines flags that should be available on notifications. +var NotificationFlags = append([]cli.Flag{ + &cli.BoolFlag{ + Name: "all", + Aliases: []string{"a"}, + Usage: "Show notifications across all your repositories instead of the current repository only", + }, + &PaginationPageFlag, + &PaginationLimitFlag, +}, AllDefaultFlags...) diff --git a/cmd/notifications/list.go b/cmd/notifications/list.go index caf5bb5..32f0fd9 100644 --- a/cmd/notifications/list.go +++ b/cmd/notifications/list.go @@ -15,23 +15,10 @@ import ( var CmdNotificationsList = cli.Command{ Name: "ls", Aliases: []string{"list"}, - Usage: "List notifications....", - Description: `List notification....`, + Usage: "List notifications", + Description: `List notifications`, Action: RunNotificationsList, - Flags: append([]cli.Flag{ - &cli.BoolFlag{ - Name: "all", - Aliases: []string{"a"}, - Usage: "show all notifications of related gitea instance", - }, - &cli.StringFlag{ - Name: "state", - Usage: "Filter by milestone state (all|open|closed)", - DefaultText: "open", - }, - &flags.PaginationPageFlag, - &flags.PaginationLimitFlag, - }, flags.AllDefaultFlags...), + Flags: flags.NotificationFlags, } // RunNotificationsList list notifications diff --git a/cmd/notifications/pinned.go b/cmd/notifications/pinned.go index 85432ed..0c8cef6 100644 --- a/cmd/notifications/pinned.go +++ b/cmd/notifications/pinned.go @@ -14,24 +14,11 @@ import ( // CmdNotificationsPinned represents a sub command of notifications to list pinned notifications var CmdNotificationsPinned = cli.Command{ Name: "pinned", - Aliases: []string{"pin"}, + Aliases: []string{"pd"}, Usage: "show pinned notifications", Description: `show pinned notifications`, Action: RunNotificationsPinned, - Flags: append([]cli.Flag{ - &cli.BoolFlag{ - Name: "all", - Aliases: []string{"a"}, - Usage: "show all notifications of related gitea instance", - }, - &cli.StringFlag{ - Name: "state", - Usage: "Filter by milestone state (all|open|closed)", - DefaultText: "open", - }, - &flags.PaginationPageFlag, - &flags.PaginationLimitFlag, - }, flags.AllDefaultFlags...), + Flags: flags.NotificationFlags, } // RunNotificationsPinned will show notifications with status pinned. diff --git a/cmd/notifications/read.go b/cmd/notifications/read.go index 95112ca..0636259 100644 --- a/cmd/notifications/read.go +++ b/cmd/notifications/read.go @@ -18,20 +18,7 @@ var CmdNotificationsRead = cli.Command{ Usage: "show read notifications instead", Description: `show read notifications instead`, Action: RunNotificationsRead, - Flags: append([]cli.Flag{ - &cli.BoolFlag{ - Name: "all", - Aliases: []string{"a"}, - Usage: "show all notifications of related gitea instance", - }, - &cli.StringFlag{ - Name: "state", - Usage: "Filter by milestone state (all|open|closed)", - DefaultText: "open", - }, - &flags.PaginationPageFlag, - &flags.PaginationLimitFlag, - }, flags.AllDefaultFlags...), + Flags: flags.NotificationFlags, } // RunNotificationsRead will show notifications with status read. diff --git a/cmd/notifications/unread.go b/cmd/notifications/unread.go index 587a9ad..1c35118 100644 --- a/cmd/notifications/unread.go +++ b/cmd/notifications/unread.go @@ -18,20 +18,7 @@ var CmdNotificationsUnread = cli.Command{ Usage: "show unread notifications", Description: `show unread notifications`, Action: RunNotificationsUnread, - Flags: append([]cli.Flag{ - &cli.BoolFlag{ - Name: "all", - Aliases: []string{"a"}, - Usage: "show all notifications of related gitea instance", - }, - &cli.StringFlag{ - Name: "state", - Usage: "Filter by milestone state (all|open|closed)", - DefaultText: "open", - }, - &flags.PaginationPageFlag, - &flags.PaginationLimitFlag, - }, flags.AllDefaultFlags...), + Flags: flags.NotificationFlags, } // RunNotificationsUnread will show notifications with status unread. -- 2.40.1 From 481132ffdb5e97fce4d160ad0cec109f1861c59d Mon Sep 17 00:00:00 2001 From: Karl Heinz Marbaise Date: Thu, 10 Dec 2020 08:01:05 +0100 Subject: [PATCH 05/17] Moved notifications_list from task into cmd/notifications Signed-off-by: Karl Heinz Marbaise --- cmd/notifications/list.go | 3 +-- {modules/task => cmd/notifications}/notifications_list.go | 8 ++++---- cmd/notifications/pinned.go | 3 +-- cmd/notifications/read.go | 3 +-- cmd/notifications/unread.go | 3 +-- 5 files changed, 8 insertions(+), 12 deletions(-) rename {modules/task => cmd/notifications}/notifications_list.go (86%) diff --git a/cmd/notifications/list.go b/cmd/notifications/list.go index 32f0fd9..f05961e 100644 --- a/cmd/notifications/list.go +++ b/cmd/notifications/list.go @@ -7,7 +7,6 @@ package notifications import ( "code.gitea.io/sdk/gitea" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/task" "github.com/urfave/cli/v2" ) @@ -23,5 +22,5 @@ var CmdNotificationsList = cli.Command{ // RunNotificationsList list notifications func RunNotificationsList(ctx *cli.Context) error { - return task.ListNotifications(ctx, []gitea.NotifyStatus{}) + return listNotifications(ctx, []gitea.NotifyStatus{}) } diff --git a/modules/task/notifications_list.go b/cmd/notifications/notifications_list.go similarity index 86% rename from modules/task/notifications_list.go rename to cmd/notifications/notifications_list.go index 58d6151..b0120a8 100644 --- a/modules/task/notifications_list.go +++ b/cmd/notifications/notifications_list.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package task +package notifications import ( "log" @@ -15,10 +15,10 @@ import ( "github.com/urfave/cli/v2" ) -//ListNotifications will get the notifications based on status -func ListNotifications(ctx *cli.Context, status []gitea.NotifyStatus) error { +//listNotifications will get the notifications based on status +func listNotifications(ctx *cli.Context, status []gitea.NotifyStatus) error { - //TODO: What is the purpose of the following? + //This enforces pagination. listOpts := flags.GetListOptions(ctx) if listOpts.Page == 0 { listOpts.Page = 1 diff --git a/cmd/notifications/pinned.go b/cmd/notifications/pinned.go index 0c8cef6..9904c2f 100644 --- a/cmd/notifications/pinned.go +++ b/cmd/notifications/pinned.go @@ -7,7 +7,6 @@ package notifications import ( "code.gitea.io/sdk/gitea" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/task" "github.com/urfave/cli/v2" ) @@ -24,5 +23,5 @@ var CmdNotificationsPinned = cli.Command{ // RunNotificationsPinned will show notifications with status pinned. func RunNotificationsPinned(ctx *cli.Context) error { var statuses = []gitea.NotifyStatus{gitea.NotifyStatusPinned} - return task.ListNotifications(ctx, statuses) + return listNotifications(ctx, statuses) } diff --git a/cmd/notifications/read.go b/cmd/notifications/read.go index 0636259..40cb428 100644 --- a/cmd/notifications/read.go +++ b/cmd/notifications/read.go @@ -7,7 +7,6 @@ package notifications import ( "code.gitea.io/sdk/gitea" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/task" "github.com/urfave/cli/v2" ) @@ -24,5 +23,5 @@ var CmdNotificationsRead = cli.Command{ // RunNotificationsRead will show notifications with status read. func RunNotificationsRead(ctx *cli.Context) error { var statuses = []gitea.NotifyStatus{gitea.NotifyStatusRead} - return task.ListNotifications(ctx, statuses) + return listNotifications(ctx, statuses) } diff --git a/cmd/notifications/unread.go b/cmd/notifications/unread.go index 1c35118..262665e 100644 --- a/cmd/notifications/unread.go +++ b/cmd/notifications/unread.go @@ -7,7 +7,6 @@ package notifications import ( "code.gitea.io/sdk/gitea" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/task" "github.com/urfave/cli/v2" ) @@ -24,5 +23,5 @@ var CmdNotificationsUnread = cli.Command{ // RunNotificationsUnread will show notifications with status unread. func RunNotificationsUnread(ctx *cli.Context) error { var statuses = []gitea.NotifyStatus{gitea.NotifyStatusUnread} - return task.ListNotifications(ctx, statuses) + return listNotifications(ctx, statuses) } -- 2.40.1 From 31bd1cc6b1d9c69751d4c4989346acb6b3e33d65 Mon Sep 17 00:00:00 2001 From: Karl Heinz Marbaise Date: Fri, 11 Dec 2020 20:50:46 +0100 Subject: [PATCH 06/17] WIP. Cleanuped / ... --- cmd/notifications.go | 7 ++++- cmd/notifications/list.go | 41 +++++++++++++++++++++++-- cmd/notifications/notifications_list.go | 8 +++-- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/cmd/notifications.go b/cmd/notifications.go index b72d605..73e3630 100644 --- a/cmd/notifications.go +++ b/cmd/notifications.go @@ -34,6 +34,11 @@ var CmdNotifications = cli.Command{ Aliases: []string{"a"}, Usage: "show all notifications of related gitea instance", }, + &cli.StringFlag{ + Name: "state", + Usage: "set milestone state (default is open)", + DefaultText: "open", + }, }), } @@ -78,6 +83,6 @@ func runNotifications(cmd *cli.Context) error { } func runNotificationsDetails(ctx *cli.Context) error { - log.Fatal("Not yet implemented.") + log.Fatal("Use tea notif --help to see all available commands.") return nil } diff --git a/cmd/notifications/list.go b/cmd/notifications/list.go index f05961e..3f18a1f 100644 --- a/cmd/notifications/list.go +++ b/cmd/notifications/list.go @@ -6,7 +6,7 @@ package notifications import ( "code.gitea.io/sdk/gitea" - "code.gitea.io/tea/cmd/flags" + "fmt" "github.com/urfave/cli/v2" ) @@ -17,10 +17,45 @@ var CmdNotificationsList = cli.Command{ Usage: "List notifications", Description: `List notifications`, Action: RunNotificationsList, - Flags: flags.NotificationFlags, + // Flags: flags.NotificationFlags, + Flags: append([]cli.Flag{ + &cli.BoolFlag{ + Name: "all", + Aliases: []string{"a"}, + Usage: "show all notifications of related gitea instance", + }, + &cli.StringFlag{ + Name: "state", + Usage: "set notification state (default is all), pinned,read,unread", + DefaultText: "xxx", + }, + }), } +// notif ls +// notif ls --state all +// notif ls --state pinned +// notif ls --state read +// notif ls --state unread + // RunNotificationsList list notifications func RunNotificationsList(ctx *cli.Context) error { - return listNotifications(ctx, []gitea.NotifyStatus{}) + // --states all + + states := []gitea.NotifyStatus{gitea.NotifyStatusRead, gitea.NotifyStatusPinned} + + switch ctx.String("state") { + case "all": + states = []gitea.NotifyStatus{gitea.NotifyStatusPinned, gitea.NotifyStatusRead, gitea.NotifyStatusUnread} + case "pinned": + states = []gitea.NotifyStatus{gitea.NotifyStatusPinned} + case "read": + states = []gitea.NotifyStatus{gitea.NotifyStatusRead} + case "unread": + states = []gitea.NotifyStatus{gitea.NotifyStatusUnread} + default: + return fmt.Errorf("invalid notification state type '%s'. valid: all, pinned,read,unread", ctx.String("state")) + } + + return listNotifications(ctx, states) } diff --git a/cmd/notifications/notifications_list.go b/cmd/notifications/notifications_list.go index b0120a8..d6cc060 100644 --- a/cmd/notifications/notifications_list.go +++ b/cmd/notifications/notifications_list.go @@ -5,6 +5,7 @@ package notifications import ( + "fmt" "log" "code.gitea.io/tea/cmd/flags" @@ -28,14 +29,17 @@ func listNotifications(ctx *cli.Context, status []gitea.NotifyStatus) error { var err error var allRelated = ctx.Bool("all") + fmt.Printf("allRelated: %t\n", allRelated) + + login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue) if allRelated { - login := config.InitCommandLoginOnly(flags.GlobalLoginValue) + fmt.Printf("login: %s owner: %s repo:%s\n", login.Name, owner, repo) news, _, err = login.Client().ListNotifications(gitea.ListNotificationOptions{ ListOptions: listOpts, Status: status, }) } else { - login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue) + fmt.Printf("login: %s owner: %s repo:%s\n", login.Name, owner, repo) news, _, err = login.Client().ListRepoNotifications(owner, repo, gitea.ListNotificationOptions{ ListOptions: listOpts, Status: status, -- 2.40.1 From abd32a70747f421ff14dc86eedcf5408dbeb90c4 Mon Sep 17 00:00:00 2001 From: Karl Heinz Marbaise Date: Sat, 12 Dec 2020 18:08:07 +0100 Subject: [PATCH 07/17] WIP - Improved. --- cmd/notifications/list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/notifications/list.go b/cmd/notifications/list.go index 3f18a1f..1ae8a76 100644 --- a/cmd/notifications/list.go +++ b/cmd/notifications/list.go @@ -42,7 +42,7 @@ var CmdNotificationsList = cli.Command{ func RunNotificationsList(ctx *cli.Context) error { // --states all - states := []gitea.NotifyStatus{gitea.NotifyStatusRead, gitea.NotifyStatusPinned} + states := []gitea.NotifyStatus{} switch ctx.String("state") { case "all": -- 2.40.1 From be0e857273e174c8f0bdd33d389754cb2b7764d6 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 16 Aug 2021 15:12:46 +0200 Subject: [PATCH 08/17] code dedub --- cmd/notifications.go | 57 +++------------------------------------ cmd/notifications/list.go | 3 ++- 2 files changed, 5 insertions(+), 55 deletions(-) diff --git a/cmd/notifications.go b/cmd/notifications.go index 3014751..cb8da88 100644 --- a/cmd/notifications.go +++ b/cmd/notifications.go @@ -5,13 +5,8 @@ package cmd import ( - "log" - "code.gitea.io/tea/cmd/notifications" - "code.gitea.io/tea/modules/context" - "code.gitea.io/tea/modules/print" - "code.gitea.io/sdk/gitea" "github.com/urfave/cli/v2" ) @@ -22,7 +17,7 @@ var CmdNotifications = cli.Command{ Category: catHelpers, Usage: "Show notifications", Description: "Show notifications, by default based of the current repo", - Action: runNotifications, + Action: notifications.RunNotificationsList, Subcommands: []*cli.Command{ ¬ifications.CmdNotificationsList, ¬ifications.CmdNotificationsPinned, @@ -36,54 +31,8 @@ var CmdNotifications = cli.Command{ Usage: "show all notifications of related gitea instance", }, &cli.StringFlag{ - Name: "state", - Usage: "set milestone state (default is open)", - DefaultText: "open", + Name: "state", + Usage: "set notification state (default is all), pinned,read,unread", }, }), } - -func runNotifications(cmd *cli.Context) error { - var news []*gitea.NotificationThread - var err error - - ctx := context.InitCommand(cmd) - client := ctx.Login.Client() - - listOpts := ctx.GetListOptions() - if listOpts.Page == 0 { - listOpts.Page = 1 - } - - var status []gitea.NotifyStatus - if ctx.Bool("read") { - status = []gitea.NotifyStatus{gitea.NotifyStatusRead} - } - if ctx.Bool("pinned") { - status = append(status, gitea.NotifyStatusPinned) - } - - if ctx.Bool("all") { - news, _, err = client.ListNotifications(gitea.ListNotificationOptions{ - ListOptions: listOpts, - Status: status, - }) - } else { - ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) - news, _, err = client.ListRepoNotifications(ctx.Owner, ctx.Repo, gitea.ListNotificationOptions{ - ListOptions: listOpts, - Status: status, - }) - } - if err != nil { - return err - } - - print.NotificationsList(news, ctx.Output, ctx.Bool("all")) - return nil -} - -func runNotificationsDetails(ctx *cli.Context) error { - log.Fatal("Use tea notif --help to see all available commands.") - return nil -} diff --git a/cmd/notifications/list.go b/cmd/notifications/list.go index 1ae8a76..a0247a4 100644 --- a/cmd/notifications/list.go +++ b/cmd/notifications/list.go @@ -5,8 +5,9 @@ package notifications import ( - "code.gitea.io/sdk/gitea" "fmt" + + "code.gitea.io/sdk/gitea" "github.com/urfave/cli/v2" ) -- 2.40.1 From 0f12c0a77f79b412581aea3e16c567f4b5bdfe29 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 16 Aug 2021 15:21:44 +0200 Subject: [PATCH 09/17] Next dedub --- cmd/notifications.go | 10 +++------- cmd/notifications/list.go | 27 ++++++++------------------- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/cmd/notifications.go b/cmd/notifications.go index cb8da88..906ed5f 100644 --- a/cmd/notifications.go +++ b/cmd/notifications.go @@ -5,6 +5,7 @@ package cmd import ( + "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/cmd/notifications" "github.com/urfave/cli/v2" @@ -24,15 +25,10 @@ var CmdNotifications = cli.Command{ ¬ifications.CmdNotificationsRead, ¬ifications.CmdNotificationsUnread, }, - Flags: append([]cli.Flag{ - &cli.BoolFlag{ - Name: "all", - Aliases: []string{"a"}, - Usage: "show all notifications of related gitea instance", - }, + Flags: append(flags.NotificationFlags, &cli.StringFlag{ Name: "state", Usage: "set notification state (default is all), pinned,read,unread", }, - }), + ), } diff --git a/cmd/notifications/list.go b/cmd/notifications/list.go index a0247a4..643447e 100644 --- a/cmd/notifications/list.go +++ b/cmd/notifications/list.go @@ -5,7 +5,7 @@ package notifications import ( - "fmt" + "code.gitea.io/tea/cmd/flags" "code.gitea.io/sdk/gitea" "github.com/urfave/cli/v2" @@ -18,19 +18,12 @@ var CmdNotificationsList = cli.Command{ Usage: "List notifications", Description: `List notifications`, Action: RunNotificationsList, - // Flags: flags.NotificationFlags, - Flags: append([]cli.Flag{ - &cli.BoolFlag{ - Name: "all", - Aliases: []string{"a"}, - Usage: "show all notifications of related gitea instance", - }, + Flags: append(flags.NotificationFlags, &cli.StringFlag{ - Name: "state", - Usage: "set notification state (default is all), pinned,read,unread", - DefaultText: "xxx", + Name: "state", + Usage: "set notification state (default is all), pinned,read,unread", }, - }), + ), } // notif ls @@ -41,21 +34,17 @@ var CmdNotificationsList = cli.Command{ // RunNotificationsList list notifications func RunNotificationsList(ctx *cli.Context) error { - // --states all - - states := []gitea.NotifyStatus{} + var states []gitea.NotifyStatus switch ctx.String("state") { - case "all": - states = []gitea.NotifyStatus{gitea.NotifyStatusPinned, gitea.NotifyStatusRead, gitea.NotifyStatusUnread} case "pinned": states = []gitea.NotifyStatus{gitea.NotifyStatusPinned} case "read": states = []gitea.NotifyStatus{gitea.NotifyStatusRead} case "unread": states = []gitea.NotifyStatus{gitea.NotifyStatusUnread} - default: - return fmt.Errorf("invalid notification state type '%s'. valid: all, pinned,read,unread", ctx.String("state")) + default: // all + states = []gitea.NotifyStatus{gitea.NotifyStatusPinned, gitea.NotifyStatusRead, gitea.NotifyStatusUnread} } return listNotifications(ctx, states) -- 2.40.1 From 908c6dfca0d4870e4d460327716573ea95d27116 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Thu, 19 Aug 2021 14:08:40 +0200 Subject: [PATCH 10/17] apply own codereview --- cmd/flags/flags.go | 4 ++-- cmd/notifications.go | 10 ++------- cmd/notifications/list.go | 30 ++++++++++++++----------- cmd/notifications/notifications_list.go | 7 +++--- 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/cmd/flags/flags.go b/cmd/flags/flags.go index b71fd4a..4ba3069 100644 --- a/cmd/flags/flags.go +++ b/cmd/flags/flags.go @@ -104,8 +104,8 @@ var IssuePRFlags = append([]cli.Flag{ // NotificationFlags defines flags that should be available on notifications. var NotificationFlags = append([]cli.Flag{ &cli.BoolFlag{ - Name: "all", - Aliases: []string{"a"}, + Name: "for-user", + Aliases: []string{"u"}, Usage: "Show notifications across all your repositories instead of the current repository only", }, &PaginationPageFlag, diff --git a/cmd/notifications.go b/cmd/notifications.go index 906ed5f..7efb4ac 100644 --- a/cmd/notifications.go +++ b/cmd/notifications.go @@ -5,7 +5,6 @@ package cmd import ( - "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/cmd/notifications" "github.com/urfave/cli/v2" @@ -17,7 +16,7 @@ var CmdNotifications = cli.Command{ Aliases: []string{"notification", "n"}, Category: catHelpers, Usage: "Show notifications", - Description: "Show notifications, by default based of the current repo", + Description: "Show notifications, by default based on the current repo if available", Action: notifications.RunNotificationsList, Subcommands: []*cli.Command{ ¬ifications.CmdNotificationsList, @@ -25,10 +24,5 @@ var CmdNotifications = cli.Command{ ¬ifications.CmdNotificationsRead, ¬ifications.CmdNotificationsUnread, }, - Flags: append(flags.NotificationFlags, - &cli.StringFlag{ - Name: "state", - Usage: "set notification state (default is all), pinned,read,unread", - }, - ), + Flags: notifications.CmdNotificationsList.Flags, } diff --git a/cmd/notifications/list.go b/cmd/notifications/list.go index 643447e..4d44c6d 100644 --- a/cmd/notifications/list.go +++ b/cmd/notifications/list.go @@ -5,6 +5,9 @@ package notifications import ( + "fmt" + "os" + "code.gitea.io/tea/cmd/flags" "code.gitea.io/sdk/gitea" @@ -18,33 +21,34 @@ var CmdNotificationsList = cli.Command{ Usage: "List notifications", Description: `List notifications`, Action: RunNotificationsList, - Flags: append(flags.NotificationFlags, + Flags: append([]cli.Flag{ &cli.StringFlag{ - Name: "state", - Usage: "set notification state (default is all), pinned,read,unread", + Name: "state", + Aliases: []string{"s"}, + Usage: "filter by notification state (pinned,read,unread,all)", + DefaultText: "pinned + unread", }, - ), + }, flags.NotificationFlags...), } -// notif ls -// notif ls --state all -// notif ls --state pinned -// notif ls --state read -// notif ls --state unread - // RunNotificationsList list notifications func RunNotificationsList(ctx *cli.Context) error { var states []gitea.NotifyStatus switch ctx.String("state") { + case "": + states = []gitea.NotifyStatus{} + case "unread": + states = []gitea.NotifyStatus{gitea.NotifyStatusUnread} case "pinned": states = []gitea.NotifyStatus{gitea.NotifyStatusPinned} case "read": states = []gitea.NotifyStatus{gitea.NotifyStatusRead} - case "unread": - states = []gitea.NotifyStatus{gitea.NotifyStatusUnread} - default: // all + case "all": states = []gitea.NotifyStatus{gitea.NotifyStatusPinned, gitea.NotifyStatusRead, gitea.NotifyStatusUnread} + default: + fmt.Printf("Unknown state '%s'\n", ctx.String("state")) + os.Exit(1) } return listNotifications(ctx, states) diff --git a/cmd/notifications/notifications_list.go b/cmd/notifications/notifications_list.go index 1a84624..90f6a8e 100644 --- a/cmd/notifications/notifications_list.go +++ b/cmd/notifications/notifications_list.go @@ -14,16 +14,16 @@ import ( "github.com/urfave/cli/v2" ) -//listNotifications will get the notifications based on status +// listNotifications will get the notifications based on status func listNotifications(cmd *cli.Context, status []gitea.NotifyStatus) error { var news []*gitea.NotificationThread var err error ctx := context.InitCommand(cmd) client := ctx.Login.Client() - all := ctx.Bool("all") + all := ctx.Bool("for-user") - //This enforces pagination. + // This enforces pagination (see https://github.com/go-gitea/gitea/issues/16733) listOpts := ctx.GetListOptions() if listOpts.Page == 0 { listOpts.Page = 1 @@ -33,6 +33,7 @@ func listNotifications(cmd *cli.Context, status []gitea.NotifyStatus) error { news, _, err = client.ListNotifications(gitea.ListNotificationOptions{ ListOptions: listOpts, Status: status, + // TODO: SubjectTypes }) } else { ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) -- 2.40.1 From ccb10bbbe42ccbc9e04f4d0048f99dab3358f76c Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Tue, 24 Aug 2021 02:01:57 +0200 Subject: [PATCH 11/17] tea n ls: add type filter --- cmd/notifications.go | 7 ++-- cmd/notifications/list.go | 29 +++++++++++++++-- cmd/notifications/mark_as.go | 43 +++++++++++++++++++++++++ cmd/notifications/notifications_list.go | 15 +++++---- cmd/notifications/pin.go | 36 +++++++++++++++++++++ cmd/notifications/pinned.go | 27 ---------------- cmd/notifications/read.go | 27 ---------------- cmd/notifications/unread.go | 27 ---------------- 8 files changed, 118 insertions(+), 93 deletions(-) create mode 100644 cmd/notifications/mark_as.go create mode 100644 cmd/notifications/pin.go delete mode 100644 cmd/notifications/pinned.go delete mode 100644 cmd/notifications/read.go delete mode 100644 cmd/notifications/unread.go diff --git a/cmd/notifications.go b/cmd/notifications.go index 7efb4ac..e7daf10 100644 --- a/cmd/notifications.go +++ b/cmd/notifications.go @@ -20,9 +20,10 @@ var CmdNotifications = cli.Command{ Action: notifications.RunNotificationsList, Subcommands: []*cli.Command{ ¬ifications.CmdNotificationsList, - ¬ifications.CmdNotificationsPinned, - ¬ifications.CmdNotificationsRead, - ¬ifications.CmdNotificationsUnread, + ¬ifications.CmdNotificationsPin, + ¬ifications.CmdNotificationsUnpin, + ¬ifications.CmdNotificationsMarkRead, + ¬ifications.CmdNotificationsMarkUnread, }, Flags: notifications.CmdNotificationsList.Flags, } diff --git a/cmd/notifications/list.go b/cmd/notifications/list.go index 4d44c6d..5b0a292 100644 --- a/cmd/notifications/list.go +++ b/cmd/notifications/list.go @@ -1,4 +1,4 @@ -// Copyright 2020 The Gitea Authors. All rights reserved. +// Copyright 2021 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -22,18 +22,27 @@ var CmdNotificationsList = cli.Command{ Description: `List notifications`, Action: RunNotificationsList, Flags: append([]cli.Flag{ + // FIXME: shouldn't --for-user only be applied to the ls command here, instead of all notification operations? &cli.StringFlag{ Name: "state", Aliases: []string{"s"}, Usage: "filter by notification state (pinned,read,unread,all)", DefaultText: "pinned + unread", }, + &cli.StringFlag{ + Name: "type", + Aliases: []string{"t"}, + Usage: "filter by subject type (repo,issue,pr,commit)", + }, }, flags.NotificationFlags...), } // RunNotificationsList list notifications func RunNotificationsList(ctx *cli.Context) error { var states []gitea.NotifyStatus + var types []gitea.NotifySubjectType + + // FIXME: make these commaseparated slice flags..? switch ctx.String("state") { case "": @@ -51,5 +60,21 @@ func RunNotificationsList(ctx *cli.Context) error { os.Exit(1) } - return listNotifications(ctx, states) + switch ctx.String("type") { + case "": + types = []gitea.NotifySubjectType{} + case "commit": + types = []gitea.NotifySubjectType{gitea.NotifySubjectCommit} + case "issue": + types = []gitea.NotifySubjectType{gitea.NotifySubjectIssue} + case "pull", "pr": + types = []gitea.NotifySubjectType{gitea.NotifySubjectPull} + case "repo": + types = []gitea.NotifySubjectType{gitea.NotifySubjectRepository} + default: + fmt.Printf("Unknown type '%s'\n", ctx.String("type")) + os.Exit(1) + } + + return listNotifications(ctx, states, types) } diff --git a/cmd/notifications/mark_as.go b/cmd/notifications/mark_as.go new file mode 100644 index 0000000..f38f3ac --- /dev/null +++ b/cmd/notifications/mark_as.go @@ -0,0 +1,43 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package notifications + +import ( + "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/flags" + "github.com/urfave/cli/v2" +) + +// CmdNotificationsRead represents a sub command of notifications to list read notifications +var CmdNotificationsMarkRead = cli.Command{ + Name: "read", + Aliases: []string{}, + Usage: "Show read notifications only", + Description: `Show read notifications only`, + Action: RunNotificationsRead, + Flags: flags.NotificationFlags, +} + +// RunNotificationsRead will show notifications with status read. +func RunNotificationsRead(ctx *cli.Context) error { + var statuses = []gitea.NotifyStatus{gitea.NotifyStatusRead} + return listNotifications(ctx, statuses, []gitea.NotifySubjectType{}) +} + +// CmdNotificationsUnread represents a sub command of notifications to list unread notifications. +var CmdNotificationsMarkUnread = cli.Command{ + Name: "unread", + Aliases: []string{}, + Usage: "Show unread notifications only", + Description: `Show unread notifications only`, + Action: RunNotificationsUnread, + Flags: flags.NotificationFlags, +} + +// RunNotificationsUnread will show notifications with status unread. +func RunNotificationsUnread(ctx *cli.Context) error { + var statuses = []gitea.NotifyStatus{gitea.NotifyStatusUnread} + return listNotifications(ctx, statuses, []gitea.NotifySubjectType{}) +} diff --git a/cmd/notifications/notifications_list.go b/cmd/notifications/notifications_list.go index 90f6a8e..dd896c3 100644 --- a/cmd/notifications/notifications_list.go +++ b/cmd/notifications/notifications_list.go @@ -1,4 +1,4 @@ -// Copyright 2020 The Gitea Authors. All rights reserved. +// Copyright 2021 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -15,7 +15,7 @@ import ( ) // listNotifications will get the notifications based on status -func listNotifications(cmd *cli.Context, status []gitea.NotifyStatus) error { +func listNotifications(cmd *cli.Context, status []gitea.NotifyStatus, subjects []gitea.NotifySubjectType) error { var news []*gitea.NotificationThread var err error @@ -31,15 +31,16 @@ func listNotifications(cmd *cli.Context, status []gitea.NotifyStatus) error { if all { news, _, err = client.ListNotifications(gitea.ListNotificationOptions{ - ListOptions: listOpts, - Status: status, - // TODO: SubjectTypes + ListOptions: listOpts, + Status: status, + SubjectTypes: subjects, }) } else { ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) news, _, err = client.ListRepoNotifications(ctx.Owner, ctx.Repo, gitea.ListNotificationOptions{ - ListOptions: listOpts, - Status: status, + ListOptions: listOpts, + Status: status, + SubjectTypes: subjects, }) } if err != nil { diff --git a/cmd/notifications/pin.go b/cmd/notifications/pin.go new file mode 100644 index 0000000..39082be --- /dev/null +++ b/cmd/notifications/pin.go @@ -0,0 +1,36 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package notifications + +import ( + "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/flags" + "github.com/urfave/cli/v2" +) + +// CmdNotificationsUnpin represents a sub command of notifications to unpin a notification +var CmdNotificationsUnpin = cli.Command{ + Name: "unpin", + Usage: "Remove a notification from pins", + Description: `Remove a notification from pins`, + Action: RunNotificationsPinned, + Flags: flags.NotificationFlags, +} + +// CmdNotificationsPin represents a sub command of notifications to pin a notification +var CmdNotificationsPin = cli.Command{ + Name: "pin", + Aliases: []string{"p"}, + Usage: "Save a notification as pin", + Description: `Save a notification as pin`, + Action: RunNotificationsPinned, + Flags: flags.NotificationFlags, +} + +// RunNotificationsPinned will show notifications with status pinned. +func RunNotificationsPinned(ctx *cli.Context) error { + var statuses = []gitea.NotifyStatus{gitea.NotifyStatusPinned} + return listNotifications(ctx, statuses, []gitea.NotifySubjectType{}) +} diff --git a/cmd/notifications/pinned.go b/cmd/notifications/pinned.go deleted file mode 100644 index 9904c2f..0000000 --- a/cmd/notifications/pinned.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2020 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package notifications - -import ( - "code.gitea.io/sdk/gitea" - "code.gitea.io/tea/cmd/flags" - "github.com/urfave/cli/v2" -) - -// CmdNotificationsPinned represents a sub command of notifications to list pinned notifications -var CmdNotificationsPinned = cli.Command{ - Name: "pinned", - Aliases: []string{"pd"}, - Usage: "show pinned notifications", - Description: `show pinned notifications`, - Action: RunNotificationsPinned, - Flags: flags.NotificationFlags, -} - -// RunNotificationsPinned will show notifications with status pinned. -func RunNotificationsPinned(ctx *cli.Context) error { - var statuses = []gitea.NotifyStatus{gitea.NotifyStatusPinned} - return listNotifications(ctx, statuses) -} diff --git a/cmd/notifications/read.go b/cmd/notifications/read.go deleted file mode 100644 index 40cb428..0000000 --- a/cmd/notifications/read.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2020 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package notifications - -import ( - "code.gitea.io/sdk/gitea" - "code.gitea.io/tea/cmd/flags" - "github.com/urfave/cli/v2" -) - -// CmdNotificationsRead represents a sub command of notifications to list read notifications -var CmdNotificationsRead = cli.Command{ - Name: "read", - Aliases: []string{}, - Usage: "show read notifications instead", - Description: `show read notifications instead`, - Action: RunNotificationsRead, - Flags: flags.NotificationFlags, -} - -// RunNotificationsRead will show notifications with status read. -func RunNotificationsRead(ctx *cli.Context) error { - var statuses = []gitea.NotifyStatus{gitea.NotifyStatusRead} - return listNotifications(ctx, statuses) -} diff --git a/cmd/notifications/unread.go b/cmd/notifications/unread.go deleted file mode 100644 index 262665e..0000000 --- a/cmd/notifications/unread.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2020 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package notifications - -import ( - "code.gitea.io/sdk/gitea" - "code.gitea.io/tea/cmd/flags" - "github.com/urfave/cli/v2" -) - -// CmdNotificationsUnread represents a sub command of notifications to list unread notifications. -var CmdNotificationsUnread = cli.Command{ - Name: "unread", - Aliases: []string{}, - Usage: "show unread notifications", - Description: `show unread notifications`, - Action: RunNotificationsUnread, - Flags: flags.NotificationFlags, -} - -// RunNotificationsUnread will show notifications with status unread. -func RunNotificationsUnread(ctx *cli.Context) error { - var statuses = []gitea.NotifyStatus{gitea.NotifyStatusUnread} - return listNotifications(ctx, statuses) -} -- 2.40.1 From 5ee01374bfdd69b509359809dbc1d10dcb3db03b Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Tue, 24 Aug 2021 02:31:47 +0200 Subject: [PATCH 12/17] refactor FieldsFlag as CsvFlag for reusability to be used in --state + --type of notifications listing --- cmd/flags/csvflag.go | 46 ++++++++++++++++++++++++++++++++++++++++ cmd/flags/flags.go | 28 +++--------------------- cmd/issues/list.go | 12 +++++------ cmd/milestones/issues.go | 10 +++++---- cmd/repos/list.go | 10 +++++---- cmd/repos/search.go | 6 ++---- cmd/times/list.go | 20 +++++++++++------ 7 files changed, 82 insertions(+), 50 deletions(-) create mode 100644 cmd/flags/csvflag.go diff --git a/cmd/flags/csvflag.go b/cmd/flags/csvflag.go new file mode 100644 index 0000000..9ff0108 --- /dev/null +++ b/cmd/flags/csvflag.go @@ -0,0 +1,46 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package flags + +import ( + "fmt" + "strings" + + "code.gitea.io/tea/modules/utils" + "github.com/urfave/cli/v2" +) + +// CsvFlag is a wrapper around cli.StringFlag, with an added GetValues() method +// to retrieve comma separated string values as a slice. +type CsvFlag struct { + cli.StringFlag + AvailableFields []string +} + +func NewCsvFlag(name, usage string, aliases, availableFields, defaultFields []string) *CsvFlag { + return &CsvFlag{ + AvailableFields: availableFields, + StringFlag: cli.StringFlag{ + Name: name, + Aliases: aliases, + Value: strings.Join(defaultFields, ","), + Usage: fmt.Sprintf(`Comma-separated list of %s. Available values: + %s + `, usage, strings.Join(availableFields, ",")), + }, + } +} + +func (f CsvFlag) GetValues(ctx *cli.Context) ([]string, error) { + selection := strings.Split(ctx.String(f.Name), ",") + if f.AvailableFields != nil { + for _, field := range selection { + if !utils.Contains(f.AvailableFields, field) { + return nil, fmt.Errorf("Invalid field '%s'", field) + } + } + } + return selection, nil +} diff --git a/cmd/flags/flags.go b/cmd/flags/flags.go index 4ba3069..c25c84a 100644 --- a/cmd/flags/flags.go +++ b/cmd/flags/flags.go @@ -11,7 +11,6 @@ import ( "code.gitea.io/sdk/gitea" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/task" - "code.gitea.io/tea/modules/utils" "github.com/araddon/dateparse" "github.com/urfave/cli/v2" @@ -189,28 +188,7 @@ func GetIssuePREditFlags(ctx *context.TeaContext) (*gitea.CreateIssueOption, err } // FieldsFlag generates a flag selecting printable fields. -// To retrieve the value, use GetFields() -func FieldsFlag(availableFields, defaultFields []string) *cli.StringFlag { - return &cli.StringFlag{ - Name: "fields", - Aliases: []string{"f"}, - Usage: fmt.Sprintf(`Comma-separated list of fields to print. Available values: - %s - `, strings.Join(availableFields, ",")), - Value: strings.Join(defaultFields, ","), - } -} - -// GetFields parses the values provided in a fields flag, and -// optionally validates against valid values. -func GetFields(ctx *cli.Context, validFields []string) ([]string, error) { - selection := strings.Split(ctx.String("fields"), ",") - if validFields != nil { - for _, field := range selection { - if !utils.Contains(validFields, field) { - return nil, fmt.Errorf("Invalid field '%s'", field) - } - } - } - return selection, nil +// To retrieve the value, use f.GetValues() +func FieldsFlag(availableFields, defaultFields []string) *CsvFlag { + return NewCsvFlag("fields", "fields to print", []string{"f"}, availableFields, defaultFields) } diff --git a/cmd/issues/list.go b/cmd/issues/list.go index d9ec8ab..3089af3 100644 --- a/cmd/issues/list.go +++ b/cmd/issues/list.go @@ -13,6 +13,10 @@ import ( "github.com/urfave/cli/v2" ) +var issueFieldsFlag = flags.FieldsFlag(print.IssueFields, []string{ + "index", "title", "state", "author", "milestone", "labels", +}) + // CmdIssuesList represents a sub command of issues to list issues var CmdIssuesList = cli.Command{ Name: "list", @@ -20,11 +24,7 @@ var CmdIssuesList = cli.Command{ Usage: "List issues of the repository", Description: `List issues of the repository`, Action: RunIssuesList, - Flags: append([]cli.Flag{ - flags.FieldsFlag(print.IssueFields, []string{ - "index", "title", "state", "author", "milestone", "labels", - }), - }, flags.IssuePRFlags...), + Flags: append([]cli.Flag{issueFieldsFlag}, flags.IssuePRFlags...), } // RunIssuesList list issues @@ -52,7 +52,7 @@ func RunIssuesList(cmd *cli.Context) error { return err } - fields, err := flags.GetFields(cmd, print.IssueFields) + fields, err := issueFieldsFlag.GetValues(cmd) if err != nil { return err } diff --git a/cmd/milestones/issues.go b/cmd/milestones/issues.go index 11a353d..9affb09 100644 --- a/cmd/milestones/issues.go +++ b/cmd/milestones/issues.go @@ -16,6 +16,10 @@ import ( "github.com/urfave/cli/v2" ) +var msIssuesFieldsFlag = flags.FieldsFlag(print.IssueFields, []string{ + "index", "kind", "title", "state", "updated", "labels", +}) + // CmdMilestonesIssues represents a sub command of milestones to manage issue/pull of an milestone var CmdMilestonesIssues = cli.Command{ Name: "issues", @@ -40,9 +44,7 @@ var CmdMilestonesIssues = cli.Command{ }, &flags.PaginationPageFlag, &flags.PaginationLimitFlag, - flags.FieldsFlag(print.IssueFields, []string{ - "index", "kind", "title", "state", "updated", "labels", - }), + msIssuesFieldsFlag, }, flags.AllDefaultFlags...), } @@ -110,7 +112,7 @@ func runMilestoneIssueList(cmd *cli.Context) error { return err } - fields, err := flags.GetFields(cmd, print.IssueFields) + fields, err := msIssuesFieldsFlag.GetValues(cmd) if err != nil { return err } diff --git a/cmd/repos/list.go b/cmd/repos/list.go index 78ed1e4..86a84fe 100644 --- a/cmd/repos/list.go +++ b/cmd/repos/list.go @@ -13,6 +13,10 @@ import ( "github.com/urfave/cli/v2" ) +var repoFieldsFlag = flags.FieldsFlag(print.RepoFields, []string{ + "owner", "name", "type", "ssh", +}) + // CmdReposListFlags contains all flags needed for repo listing var CmdReposListFlags = append([]cli.Flag{ &cli.BoolFlag{ @@ -27,9 +31,7 @@ var CmdReposListFlags = append([]cli.Flag{ Required: false, Usage: "List your starred repos instead", }, - flags.FieldsFlag(print.RepoFields, []string{ - "owner", "name", "type", "ssh", - }), + repoFieldsFlag, &typeFilterFlag, &flags.PaginationPageFlag, &flags.PaginationLimitFlag, @@ -82,7 +84,7 @@ func RunReposList(cmd *cli.Context) error { reposFiltered = filterReposByType(rps, typeFilter) } - fields, err := flags.GetFields(cmd, print.RepoFields) + fields, err := repoFieldsFlag.GetValues(cmd) if err != nil { return err } diff --git a/cmd/repos/search.go b/cmd/repos/search.go index e9264fc..3743458 100644 --- a/cmd/repos/search.go +++ b/cmd/repos/search.go @@ -50,9 +50,7 @@ var CmdReposSearch = cli.Command{ Required: false, Usage: "Filter archived repos (true|false)", }, - flags.FieldsFlag(print.RepoFields, []string{ - "owner", "name", "type", "ssh", - }), + repoFieldsFlag, &flags.PaginationPageFlag, &flags.PaginationLimitFlag, }, flags.LoginOutputFlags...), @@ -125,7 +123,7 @@ func runReposSearch(cmd *cli.Context) error { return err } - fields, err := flags.GetFields(cmd, nil) + fields, err := repoFieldsFlag.GetValues(cmd) if err != nil { return err } diff --git a/cmd/times/list.go b/cmd/times/list.go index b84ec47..b91e18a 100644 --- a/cmd/times/list.go +++ b/cmd/times/list.go @@ -19,6 +19,17 @@ import ( "github.com/urfave/cli/v2" ) +// NOTE: not using NewCsvFlag, as we don't want an alias & default value. +var timeFieldsFlag = &flags.CsvFlag{ + AvailableFields: print.TrackedTimeFields, + StringFlag: cli.StringFlag{ + Name: "fields", + Usage: fmt.Sprintf(`Comma-separated list of fields to print. Available values: + %s +`, strings.Join(print.TrackedTimeFields, ",")), + }, +} + // CmdTrackedTimesList represents a sub command of times to list them var CmdTrackedTimesList = cli.Command{ Name: "list", @@ -53,12 +64,7 @@ Depending on your permissions on the repository, only your own tracked times mig Aliases: []string{"m"}, Usage: "Show all times tracked by you across all repositories (overrides command arguments)", }, - &cli.StringFlag{ - Name: "fields", - Usage: fmt.Sprintf(`Comma-separated list of fields to print. Available values: - %s - `, strings.Join(print.TrackedTimeFields, ",")), - }, + timeFieldsFlag, }, flags.AllDefaultFlags...), } @@ -116,7 +122,7 @@ func RunTimesList(cmd *cli.Context) error { } if ctx.IsSet("fields") { - if fields, err = flags.GetFields(cmd, print.TrackedTimeFields); err != nil { + if fields, err = timeFieldsFlag.GetValues(cmd); err != nil { return err } } -- 2.40.1 From 4acda8d1113d76200abf2b489f53e60f7e696d39 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Tue, 24 Aug 2021 02:35:16 +0200 Subject: [PATCH 13/17] rename --for-user flag to --mine for consistency with `tea time --mine`. also move the flag to the list subcommand only, as it's not needed for the other subcmds --- cmd/flags/flags.go | 11 ----------- cmd/notifications/list.go | 10 ++++++++-- cmd/notifications/mark_as.go | 4 ++-- cmd/notifications/notifications_list.go | 2 +- cmd/notifications/pin.go | 4 ++-- 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/cmd/flags/flags.go b/cmd/flags/flags.go index c25c84a..8194f71 100644 --- a/cmd/flags/flags.go +++ b/cmd/flags/flags.go @@ -100,17 +100,6 @@ var IssuePRFlags = append([]cli.Flag{ &PaginationLimitFlag, }, AllDefaultFlags...) -// NotificationFlags defines flags that should be available on notifications. -var NotificationFlags = append([]cli.Flag{ - &cli.BoolFlag{ - Name: "for-user", - Aliases: []string{"u"}, - Usage: "Show notifications across all your repositories instead of the current repository only", - }, - &PaginationPageFlag, - &PaginationLimitFlag, -}, AllDefaultFlags...) - // IssuePREditFlags defines flags for properties of issues and PRs var IssuePREditFlags = append([]cli.Flag{ &cli.StringFlag{ diff --git a/cmd/notifications/list.go b/cmd/notifications/list.go index 5b0a292..3f4f377 100644 --- a/cmd/notifications/list.go +++ b/cmd/notifications/list.go @@ -22,7 +22,11 @@ var CmdNotificationsList = cli.Command{ Description: `List notifications`, Action: RunNotificationsList, Flags: append([]cli.Flag{ - // FIXME: shouldn't --for-user only be applied to the ls command here, instead of all notification operations? + &cli.BoolFlag{ + Name: "mine", + Aliases: []string{"m"}, + Usage: "Show notifications across all your repositories instead of the current repository only", + }, &cli.StringFlag{ Name: "state", Aliases: []string{"s"}, @@ -34,7 +38,9 @@ var CmdNotificationsList = cli.Command{ Aliases: []string{"t"}, Usage: "filter by subject type (repo,issue,pr,commit)", }, - }, flags.NotificationFlags...), + &flags.PaginationPageFlag, + &flags.PaginationLimitFlag, + }, flags.AllDefaultFlags...), } // RunNotificationsList list notifications diff --git a/cmd/notifications/mark_as.go b/cmd/notifications/mark_as.go index f38f3ac..77c8c13 100644 --- a/cmd/notifications/mark_as.go +++ b/cmd/notifications/mark_as.go @@ -17,7 +17,7 @@ var CmdNotificationsMarkRead = cli.Command{ Usage: "Show read notifications only", Description: `Show read notifications only`, Action: RunNotificationsRead, - Flags: flags.NotificationFlags, + Flags: flags.AllDefaultFlags, } // RunNotificationsRead will show notifications with status read. @@ -33,7 +33,7 @@ var CmdNotificationsMarkUnread = cli.Command{ Usage: "Show unread notifications only", Description: `Show unread notifications only`, Action: RunNotificationsUnread, - Flags: flags.NotificationFlags, + Flags: flags.AllDefaultFlags, } // RunNotificationsUnread will show notifications with status unread. diff --git a/cmd/notifications/notifications_list.go b/cmd/notifications/notifications_list.go index dd896c3..5279749 100644 --- a/cmd/notifications/notifications_list.go +++ b/cmd/notifications/notifications_list.go @@ -21,7 +21,7 @@ func listNotifications(cmd *cli.Context, status []gitea.NotifyStatus, subjects [ ctx := context.InitCommand(cmd) client := ctx.Login.Client() - all := ctx.Bool("for-user") + all := ctx.Bool("mine") // This enforces pagination (see https://github.com/go-gitea/gitea/issues/16733) listOpts := ctx.GetListOptions() diff --git a/cmd/notifications/pin.go b/cmd/notifications/pin.go index 39082be..6e81516 100644 --- a/cmd/notifications/pin.go +++ b/cmd/notifications/pin.go @@ -16,7 +16,7 @@ var CmdNotificationsUnpin = cli.Command{ Usage: "Remove a notification from pins", Description: `Remove a notification from pins`, Action: RunNotificationsPinned, - Flags: flags.NotificationFlags, + Flags: flags.AllDefaultFlags, } // CmdNotificationsPin represents a sub command of notifications to pin a notification @@ -26,7 +26,7 @@ var CmdNotificationsPin = cli.Command{ Usage: "Save a notification as pin", Description: `Save a notification as pin`, Action: RunNotificationsPinned, - Flags: flags.NotificationFlags, + Flags: flags.AllDefaultFlags, } // RunNotificationsPinned will show notifications with status pinned. -- 2.40.1 From 1860bc66fcb7e3c5bfaa9df89e12408d0b103f18 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Tue, 24 Aug 2021 02:51:02 +0200 Subject: [PATCH 14/17] notifications list: refactor filters as CsvFlag --- cmd/flags/csvflag.go | 5 +-- cmd/notifications/list.go | 67 ++++++++++++--------------------------- 2 files changed, 24 insertions(+), 48 deletions(-) diff --git a/cmd/flags/csvflag.go b/cmd/flags/csvflag.go index 9ff0108..ffece36 100644 --- a/cmd/flags/csvflag.go +++ b/cmd/flags/csvflag.go @@ -34,8 +34,9 @@ func NewCsvFlag(name, usage string, aliases, availableFields, defaultFields []st } func (f CsvFlag) GetValues(ctx *cli.Context) ([]string, error) { - selection := strings.Split(ctx.String(f.Name), ",") - if f.AvailableFields != nil { + val := ctx.String(f.Name) + selection := strings.Split(val, ",") + if f.AvailableFields != nil && val != "" { for _, field := range selection { if !utils.Contains(f.AvailableFields, field) { return nil, fmt.Errorf("Invalid field '%s'", field) diff --git a/cmd/notifications/list.go b/cmd/notifications/list.go index 3f4f377..ce27768 100644 --- a/cmd/notifications/list.go +++ b/cmd/notifications/list.go @@ -5,15 +5,18 @@ package notifications import ( - "fmt" - "os" - "code.gitea.io/tea/cmd/flags" "code.gitea.io/sdk/gitea" "github.com/urfave/cli/v2" ) +var notifStateFlag = flags.NewCsvFlag("states", "notification states to filter by", []string{"s"}, + []string{"pinned", "unread", "read"}, []string{"pinned", "unread"}) + +var notifTypeFlag = flags.NewCsvFlag("types", "subject types to filter by", []string{"t"}, + []string{"issue", "pull", "repository", "commit"}, nil) + // CmdNotificationsList represents a sub command of notifications to list notifications var CmdNotificationsList = cli.Command{ Name: "ls", @@ -22,22 +25,13 @@ var CmdNotificationsList = cli.Command{ Description: `List notifications`, Action: RunNotificationsList, Flags: append([]cli.Flag{ + notifStateFlag, + notifTypeFlag, &cli.BoolFlag{ Name: "mine", Aliases: []string{"m"}, Usage: "Show notifications across all your repositories instead of the current repository only", }, - &cli.StringFlag{ - Name: "state", - Aliases: []string{"s"}, - Usage: "filter by notification state (pinned,read,unread,all)", - DefaultText: "pinned + unread", - }, - &cli.StringFlag{ - Name: "type", - Aliases: []string{"t"}, - Usage: "filter by subject type (repo,issue,pr,commit)", - }, &flags.PaginationPageFlag, &flags.PaginationLimitFlag, }, flags.AllDefaultFlags...), @@ -46,40 +40,21 @@ var CmdNotificationsList = cli.Command{ // RunNotificationsList list notifications func RunNotificationsList(ctx *cli.Context) error { var states []gitea.NotifyStatus - var types []gitea.NotifySubjectType - - // FIXME: make these commaseparated slice flags..? - - switch ctx.String("state") { - case "": - states = []gitea.NotifyStatus{} - case "unread": - states = []gitea.NotifyStatus{gitea.NotifyStatusUnread} - case "pinned": - states = []gitea.NotifyStatus{gitea.NotifyStatusPinned} - case "read": - states = []gitea.NotifyStatus{gitea.NotifyStatusRead} - case "all": - states = []gitea.NotifyStatus{gitea.NotifyStatusPinned, gitea.NotifyStatusRead, gitea.NotifyStatusUnread} - default: - fmt.Printf("Unknown state '%s'\n", ctx.String("state")) - os.Exit(1) + statesStr, err := notifStateFlag.GetValues(ctx) + if err != nil { + return err + } + for _, s := range statesStr { + states = append(states, gitea.NotifyStatus(s)) } - switch ctx.String("type") { - case "": - types = []gitea.NotifySubjectType{} - case "commit": - types = []gitea.NotifySubjectType{gitea.NotifySubjectCommit} - case "issue": - types = []gitea.NotifySubjectType{gitea.NotifySubjectIssue} - case "pull", "pr": - types = []gitea.NotifySubjectType{gitea.NotifySubjectPull} - case "repo": - types = []gitea.NotifySubjectType{gitea.NotifySubjectRepository} - default: - fmt.Printf("Unknown type '%s'\n", ctx.String("type")) - os.Exit(1) + var types []gitea.NotifySubjectType + typesStr, err := notifTypeFlag.GetValues(ctx) + if err != nil { + return err + } + for _, t := range typesStr { + types = append(types, gitea.NotifySubjectType(t)) } return listNotifications(ctx, states, types) -- 2.40.1 From 2398f5680af55fbb08f232f27eaeb6c1a5ae3f56 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Tue, 24 Aug 2021 04:11:54 +0200 Subject: [PATCH 15/17] implement mark-notification-as handlers --- cmd/flags/flags.go | 21 ++++ cmd/notifications.go | 4 +- cmd/notifications/list.go | 58 +++++++--- cmd/notifications/mark_as.go | 140 ++++++++++++++++++++---- cmd/notifications/notifications_list.go | 52 --------- cmd/notifications/pin.go | 36 ------ 6 files changed, 184 insertions(+), 127 deletions(-) delete mode 100644 cmd/notifications/notifications_list.go delete mode 100644 cmd/notifications/pin.go diff --git a/cmd/flags/flags.go b/cmd/flags/flags.go index 8194f71..c3de546 100644 --- a/cmd/flags/flags.go +++ b/cmd/flags/flags.go @@ -100,6 +100,27 @@ var IssuePRFlags = append([]cli.Flag{ &PaginationLimitFlag, }, AllDefaultFlags...) +// NotificationFlags defines flags that should be available on notifications. +var NotificationFlags = append([]cli.Flag{ + NotificationStateFlag, + &cli.BoolFlag{ + Name: "mine", + Aliases: []string{"m"}, + Usage: "Show notifications across all your repositories instead of the current repository only", + }, + &PaginationPageFlag, + &PaginationLimitFlag, +}, AllDefaultFlags...) + +// NotificationStateFlag is a csv flag applied to all notification subcommands as filter +var NotificationStateFlag = NewCsvFlag( + "states", + "notification states to filter by", + []string{"s"}, + []string{"pinned", "unread", "read"}, + []string{"unread", "pinned"}, +) + // IssuePREditFlags defines flags for properties of issues and PRs var IssuePREditFlags = append([]cli.Flag{ &cli.StringFlag{ diff --git a/cmd/notifications.go b/cmd/notifications.go index e7daf10..3b07c0b 100644 --- a/cmd/notifications.go +++ b/cmd/notifications.go @@ -20,10 +20,10 @@ var CmdNotifications = cli.Command{ Action: notifications.RunNotificationsList, Subcommands: []*cli.Command{ ¬ifications.CmdNotificationsList, - ¬ifications.CmdNotificationsPin, - ¬ifications.CmdNotificationsUnpin, ¬ifications.CmdNotificationsMarkRead, ¬ifications.CmdNotificationsMarkUnread, + ¬ifications.CmdNotificationsMarkPinned, + ¬ifications.CmdNotificationsUnpin, }, Flags: notifications.CmdNotificationsList.Flags, } diff --git a/cmd/notifications/list.go b/cmd/notifications/list.go index ce27768..7d96ab5 100644 --- a/cmd/notifications/list.go +++ b/cmd/notifications/list.go @@ -5,15 +5,16 @@ package notifications import ( + "log" + "code.gitea.io/tea/cmd/flags" + "code.gitea.io/tea/modules/context" + "code.gitea.io/tea/modules/print" "code.gitea.io/sdk/gitea" "github.com/urfave/cli/v2" ) -var notifStateFlag = flags.NewCsvFlag("states", "notification states to filter by", []string{"s"}, - []string{"pinned", "unread", "read"}, []string{"pinned", "unread"}) - var notifTypeFlag = flags.NewCsvFlag("types", "subject types to filter by", []string{"t"}, []string{"issue", "pull", "repository", "commit"}, nil) @@ -24,23 +25,13 @@ var CmdNotificationsList = cli.Command{ Usage: "List notifications", Description: `List notifications`, Action: RunNotificationsList, - Flags: append([]cli.Flag{ - notifStateFlag, - notifTypeFlag, - &cli.BoolFlag{ - Name: "mine", - Aliases: []string{"m"}, - Usage: "Show notifications across all your repositories instead of the current repository only", - }, - &flags.PaginationPageFlag, - &flags.PaginationLimitFlag, - }, flags.AllDefaultFlags...), + Flags: append([]cli.Flag{notifTypeFlag}, flags.NotificationFlags...), } // RunNotificationsList list notifications func RunNotificationsList(ctx *cli.Context) error { var states []gitea.NotifyStatus - statesStr, err := notifStateFlag.GetValues(ctx) + statesStr, err := flags.NotificationStateFlag.GetValues(ctx) if err != nil { return err } @@ -59,3 +50,40 @@ func RunNotificationsList(ctx *cli.Context) error { return listNotifications(ctx, states, types) } + +// listNotifications will get the notifications based on status and subject type +func listNotifications(cmd *cli.Context, status []gitea.NotifyStatus, subjects []gitea.NotifySubjectType) error { + var news []*gitea.NotificationThread + var err error + + ctx := context.InitCommand(cmd) + client := ctx.Login.Client() + all := ctx.Bool("mine") + + // This enforces pagination (see https://github.com/go-gitea/gitea/issues/16733) + listOpts := ctx.GetListOptions() + if listOpts.Page == 0 { + listOpts.Page = 1 + } + + if all { + news, _, err = client.ListNotifications(gitea.ListNotificationOptions{ + ListOptions: listOpts, + Status: status, + SubjectTypes: subjects, + }) + } else { + ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) + news, _, err = client.ListRepoNotifications(ctx.Owner, ctx.Repo, gitea.ListNotificationOptions{ + ListOptions: listOpts, + Status: status, + SubjectTypes: subjects, + }) + } + if err != nil { + log.Fatal(err) + } + + print.NotificationsList(news, ctx.Output, all) + return nil +} diff --git a/cmd/notifications/mark_as.go b/cmd/notifications/mark_as.go index 77c8c13..e1391ab 100644 --- a/cmd/notifications/mark_as.go +++ b/cmd/notifications/mark_as.go @@ -5,39 +5,135 @@ package notifications import ( + "fmt" + "code.gitea.io/sdk/gitea" "code.gitea.io/tea/cmd/flags" + "code.gitea.io/tea/modules/context" + "code.gitea.io/tea/modules/utils" "github.com/urfave/cli/v2" ) -// CmdNotificationsRead represents a sub command of notifications to list read notifications +// CmdNotificationsMarkRead represents a sub command of notifications to list read notifications var CmdNotificationsMarkRead = cli.Command{ Name: "read", - Aliases: []string{}, - Usage: "Show read notifications only", - Description: `Show read notifications only`, - Action: RunNotificationsRead, - Flags: flags.AllDefaultFlags, + Aliases: []string{"r"}, + Usage: "Mark all filtered or a specific notification as read", + Description: "Mark all filtered or a specific notification as read", + ArgsUsage: "[all | ]", + Flags: flags.NotificationFlags, + Action: func(ctx *cli.Context) error { + cmd := context.InitCommand(ctx) + filter, err := flags.NotificationStateFlag.GetValues(ctx) + if err != nil { + return err + } + if !flags.NotificationStateFlag.IsSet() { + filter = []string{string(gitea.NotifyStatusUnread)} + } + return markNotificationAs(cmd, filter, gitea.NotifyStatusRead) + }, } -// RunNotificationsRead will show notifications with status read. -func RunNotificationsRead(ctx *cli.Context) error { - var statuses = []gitea.NotifyStatus{gitea.NotifyStatusRead} - return listNotifications(ctx, statuses, []gitea.NotifySubjectType{}) -} - -// CmdNotificationsUnread represents a sub command of notifications to list unread notifications. +// RunNotificationsMarkUnread will mark notifications as unread. var CmdNotificationsMarkUnread = cli.Command{ Name: "unread", - Aliases: []string{}, - Usage: "Show unread notifications only", - Description: `Show unread notifications only`, - Action: RunNotificationsUnread, - Flags: flags.AllDefaultFlags, + Aliases: []string{"u"}, + Usage: "Mark all filtered or a specific notification as unread", + Description: "Mark all filtered or a specific notification as unread", + ArgsUsage: "[all | ]", + Flags: flags.NotificationFlags, + Action: func(ctx *cli.Context) error { + cmd := context.InitCommand(ctx) + filter, err := flags.NotificationStateFlag.GetValues(ctx) + if err != nil { + return err + } + if !flags.NotificationStateFlag.IsSet() { + filter = []string{string(gitea.NotifyStatusRead)} + } + return markNotificationAs(cmd, filter, gitea.NotifyStatusUnread) + }, } -// RunNotificationsUnread will show notifications with status unread. -func RunNotificationsUnread(ctx *cli.Context) error { - var statuses = []gitea.NotifyStatus{gitea.NotifyStatusUnread} - return listNotifications(ctx, statuses, []gitea.NotifySubjectType{}) +// RunNotificationsMarkPinned will mark notifications as unread. +var CmdNotificationsMarkPinned = cli.Command{ + Name: "pin", + Aliases: []string{"p"}, + Usage: "Mark all filtered or a specific notification as pinned", + Description: "Mark all filtered or a specific notification as pinned", + ArgsUsage: "[all | ]", + Flags: flags.NotificationFlags, + Action: func(ctx *cli.Context) error { + cmd := context.InitCommand(ctx) + filter, err := flags.NotificationStateFlag.GetValues(ctx) + if err != nil { + return err + } + if !flags.NotificationStateFlag.IsSet() { + filter = []string{string(gitea.NotifyStatusUnread)} + } + return markNotificationAs(cmd, filter, gitea.NotifyStatusPinned) + }, +} + +// RunNotificationsUnpin will mark pinned notifications as unread. +var CmdNotificationsUnpin = cli.Command{ + Name: "unpin", + Usage: "Unpin all pinned or a specific notification", + Description: "Marks all pinned or a specific notification as read", + ArgsUsage: "[all | ]", + Flags: flags.NotificationFlags, + Action: func(ctx *cli.Context) error { + cmd := context.InitCommand(ctx) + filter := []string{string(gitea.NotifyStatusPinned)} + // NOTE: we implicitly mark it as read, to match web UI semantics. marking as unread might be more useful? + return markNotificationAs(cmd, filter, gitea.NotifyStatusRead) + }, +} + +func markNotificationAs(cmd *context.TeaContext, filterStates []string, targetState gitea.NotifyStatus) (err error) { + client := cmd.Login.Client() + subject := cmd.Args().First() + allRepos := cmd.Bool("mine") + + states := []gitea.NotifyStatus{} + for _, s := range filterStates { + states = append(states, gitea.NotifyStatus(s)) + } + + switch subject { + case "", "all": + opts := gitea.MarkNotificationOptions{Status: states, ToStatus: targetState} + + if allRepos { + _, err = client.ReadNotifications(opts) + } else { + cmd.Ensure(context.CtxRequirement{RemoteRepo: true}) + _, err = client.ReadRepoNotifications(cmd.Owner, cmd.Repo, opts) + } + + // TODO: print all affected notification subject URLs + // (not supported by API currently, https://github.com/go-gitea/gitea/issues/16797) + + default: + id, err := utils.ArgToIndex(subject) + if err != nil { + return err + } + _, err = client.ReadNotification(id, targetState) + if err != nil { + return err + } + + n, _, err := client.GetNotification(id) + if err != nil { + return err + } + // FIXME: this is an API URL, we want to display a web ui link.. + fmt.Println(n.Subject.URL) + return nil + } + + return err } diff --git a/cmd/notifications/notifications_list.go b/cmd/notifications/notifications_list.go deleted file mode 100644 index 5279749..0000000 --- a/cmd/notifications/notifications_list.go +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2021 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package notifications - -import ( - "log" - - "code.gitea.io/tea/modules/context" - "code.gitea.io/tea/modules/print" - - "code.gitea.io/sdk/gitea" - "github.com/urfave/cli/v2" -) - -// listNotifications will get the notifications based on status -func listNotifications(cmd *cli.Context, status []gitea.NotifyStatus, subjects []gitea.NotifySubjectType) error { - var news []*gitea.NotificationThread - var err error - - ctx := context.InitCommand(cmd) - client := ctx.Login.Client() - all := ctx.Bool("mine") - - // This enforces pagination (see https://github.com/go-gitea/gitea/issues/16733) - listOpts := ctx.GetListOptions() - if listOpts.Page == 0 { - listOpts.Page = 1 - } - - if all { - news, _, err = client.ListNotifications(gitea.ListNotificationOptions{ - ListOptions: listOpts, - Status: status, - SubjectTypes: subjects, - }) - } else { - ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) - news, _, err = client.ListRepoNotifications(ctx.Owner, ctx.Repo, gitea.ListNotificationOptions{ - ListOptions: listOpts, - Status: status, - SubjectTypes: subjects, - }) - } - if err != nil { - log.Fatal(err) - } - - print.NotificationsList(news, ctx.Output, all) - return nil -} diff --git a/cmd/notifications/pin.go b/cmd/notifications/pin.go deleted file mode 100644 index 6e81516..0000000 --- a/cmd/notifications/pin.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2021 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package notifications - -import ( - "code.gitea.io/sdk/gitea" - "code.gitea.io/tea/cmd/flags" - "github.com/urfave/cli/v2" -) - -// CmdNotificationsUnpin represents a sub command of notifications to unpin a notification -var CmdNotificationsUnpin = cli.Command{ - Name: "unpin", - Usage: "Remove a notification from pins", - Description: `Remove a notification from pins`, - Action: RunNotificationsPinned, - Flags: flags.AllDefaultFlags, -} - -// CmdNotificationsPin represents a sub command of notifications to pin a notification -var CmdNotificationsPin = cli.Command{ - Name: "pin", - Aliases: []string{"p"}, - Usage: "Save a notification as pin", - Description: `Save a notification as pin`, - Action: RunNotificationsPinned, - Flags: flags.AllDefaultFlags, -} - -// RunNotificationsPinned will show notifications with status pinned. -func RunNotificationsPinned(ctx *cli.Context) error { - var statuses = []gitea.NotifyStatus{gitea.NotifyStatusPinned} - return listNotifications(ctx, statuses, []gitea.NotifySubjectType{}) -} -- 2.40.1 From 8414c23ed5cbc7cb747d675e893b4360acc27e8f Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Tue, 24 Aug 2021 04:21:42 +0200 Subject: [PATCH 16/17] print notification id to be able to mark single notifications --- modules/print/notification.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/modules/print/notification.go b/modules/print/notification.go index 1d45530..9be86c2 100644 --- a/modules/print/notification.go +++ b/modules/print/notification.go @@ -5,6 +5,7 @@ package print import ( + "fmt" "strings" "code.gitea.io/sdk/gitea" @@ -13,6 +14,8 @@ import ( // NotificationsList prints a listing of notification threads func NotificationsList(news []*gitea.NotificationThread, output string, showRepository bool) { headers := []string{ + "ID", + "Status", "Type", "State", "Index", @@ -39,7 +42,21 @@ func NotificationsList(news []*gitea.NotificationThread, output string, showRepo index = "#" + index } - item := []string{string(n.Subject.Type), string(n.Subject.State), index, n.Subject.Title} + status := "read" + if n.Pinned { + status = "pinned" + } else if n.Unread { + status = "unread" + } + + item := []string{ + fmt.Sprint(n.ID), + status, + string(n.Subject.Type), + string(n.Subject.State), + index, + n.Subject.Title, + } if showRepository { item = append(item, n.Repository.FullName) } -- 2.40.1 From 4b874147872d08457a848acbb02f79d0730c18bf Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Tue, 24 Aug 2021 04:52:52 +0200 Subject: [PATCH 17/17] lint --- cmd/flags/csvflag.go | 10 ++++++---- cmd/notifications/mark_as.go | 6 +++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/cmd/flags/csvflag.go b/cmd/flags/csvflag.go index ffece36..99f80fb 100644 --- a/cmd/flags/csvflag.go +++ b/cmd/flags/csvflag.go @@ -19,20 +19,22 @@ type CsvFlag struct { AvailableFields []string } -func NewCsvFlag(name, usage string, aliases, availableFields, defaultFields []string) *CsvFlag { +// NewCsvFlag creates a CsvFlag, while setting its usage string and default values +func NewCsvFlag(name, usage string, aliases, availableValues, defaults []string) *CsvFlag { return &CsvFlag{ - AvailableFields: availableFields, + AvailableFields: availableValues, StringFlag: cli.StringFlag{ Name: name, Aliases: aliases, - Value: strings.Join(defaultFields, ","), + Value: strings.Join(defaults, ","), Usage: fmt.Sprintf(`Comma-separated list of %s. Available values: %s - `, usage, strings.Join(availableFields, ",")), + `, usage, strings.Join(availableValues, ",")), }, } } +// GetValues returns the value of the flag, parsed as a commaseparated list func (f CsvFlag) GetValues(ctx *cli.Context) ([]string, error) { val := ctx.String(f.Name) selection := strings.Split(val, ",") diff --git a/cmd/notifications/mark_as.go b/cmd/notifications/mark_as.go index e1391ab..1c77899 100644 --- a/cmd/notifications/mark_as.go +++ b/cmd/notifications/mark_as.go @@ -35,7 +35,7 @@ var CmdNotificationsMarkRead = cli.Command{ }, } -// RunNotificationsMarkUnread will mark notifications as unread. +// CmdNotificationsMarkUnread will mark notifications as unread. var CmdNotificationsMarkUnread = cli.Command{ Name: "unread", Aliases: []string{"u"}, @@ -56,7 +56,7 @@ var CmdNotificationsMarkUnread = cli.Command{ }, } -// RunNotificationsMarkPinned will mark notifications as unread. +// CmdNotificationsMarkPinned will mark notifications as unread. var CmdNotificationsMarkPinned = cli.Command{ Name: "pin", Aliases: []string{"p"}, @@ -77,7 +77,7 @@ var CmdNotificationsMarkPinned = cli.Command{ }, } -// RunNotificationsUnpin will mark pinned notifications as unread. +// CmdNotificationsUnpin will mark pinned notifications as unread. var CmdNotificationsUnpin = cli.Command{ Name: "unpin", Usage: "Unpin all pinned or a specific notification", -- 2.40.1