add subcomands for notifications #283

Closed
khmarbaise wants to merge 7 commits from khmarbaise/tea:issue-243-add_subcomands_for_notifications into main
7 changed files with 227 additions and 14 deletions

View File

@ -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...)

View File

@ -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,26 @@ 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{
&notifications.CmdNotificationsList,
&notifications.CmdNotificationsPinned,
&notifications.CmdNotificationsRead,
&notifications.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.StringFlag{
Name: "state",
Usage: "set milestone state (default is open)",
DefaultText: "open",
},
&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 {
Outdated
Review

I would print use --help to see all options

I would print `use --help to see all options`
@ -80,3 +81,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("Use tea notif --help to see all available commands.")
return nil
}

61
cmd/notifications/list.go Normal file
View File

@ -0,0 +1,61 @@
// 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"
"fmt"
"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 notifications`,
Action: RunNotificationsList,
noerw marked this conversation as resolved Outdated
Outdated
Review
- 	Usage:       "List notifications....",
+ 	Usage:       "List notifications",
```diff - Usage: "List notifications....", + Usage: "List notifications", ```
// Flags: flags.NotificationFlags,
Flags: append([]cli.Flag{
&cli.BoolFlag{
noerw marked this conversation as resolved Outdated
Outdated
Review

Please move these common flags into cmd/flags/flags.go as NotificationFlags

Please move these common flags into `cmd/flags/flags.go` as `NotificationFlags`

Good idea not even realized that. Thanks.

Good idea not even realized that. Thanks.
Name: "all",
Aliases: []string{"a"},
Usage: "show all notifications of related gitea instance",
noerw marked this conversation as resolved Outdated
Outdated
Review
- 			Usage:   "show all notifications of related gitea instance",
+ 			Usage:   "Show notifications across all your repos instead of the current repo only",
```diff - Usage: "show all notifications of related gitea instance", + Usage: "Show notifications across all your repos instead of the current repo only", ```
},
khmarbaise marked this conversation as resolved Outdated
Outdated
Review

@khmarbaise can you move task.ListNotifications back to cmd/notification module

I dont see a reason to add cli & flag dependency into task module

@khmarbaise can you move task.ListNotifications back to cmd/notification module I dont see a reason to add **cli** & **flag** dependency into task module

So making a file notifications_list.go which contains that. I will do and change it accordingly.

So making a file `notifications_list.go` which contains that. I will do and change it accordingly.

Done as suggested.

Done as suggested.
&cli.StringFlag{
Name: "state",
Usage: "set notification state (default is all), pinned,read,unread",
noerw marked this conversation as resolved Outdated
Outdated
Review
  • Are valid states really called all/open/closed? all/read/unread?
  • Doesn't this flag duplicate functionality from the subcommands read/unread?
  • s/milestone/notification
- Are valid states really called all/open/closed? all/read/unread? - Doesn't this flag duplicate functionality from the subcommands read/unread? - s/milestone/notification

Yes of course.

Yes of course.
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 {
// --states all
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"))
}
return listNotifications(ctx, states)
}

View File

@ -0,0 +1,54 @@
// 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 (
"fmt"
"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"
)
//listNotifications will get the notifications based on status
func listNotifications(ctx *cli.Context, status []gitea.NotifyStatus) error {
//This enforces pagination.
listOpts := flags.GetListOptions(ctx)
if listOpts.Page == 0 {
listOpts.Page = 1
}
var news []*gitea.NotificationThread
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 {
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 {
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,
})
}
if err != nil {
log.Fatal(err)
}
print.NotificationsList(news, flags.GlobalOutputValue, allRelated)
return nil
}

View File

@ -0,0 +1,27 @@
// 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",
noerw marked this conversation as resolved Outdated
Outdated
Review

"pin" makes it sound like a verb, not a read list-command.
I'd drop this alias

"pin" makes it sound like a verb, not a read list-command. I'd drop this alias

What about with an alias pd instead?

What about with an alias `pd` instead?
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)
}

27
cmd/notifications/read.go Normal file
View File

@ -0,0 +1,27 @@
// 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)
}

View File

@ -0,0 +1,27 @@
// 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)
}