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.go b/cmd/notifications.go index 3cab988..73e3630 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,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{ + ¬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.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 { @@ -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 +} diff --git a/cmd/notifications/list.go b/cmd/notifications/list.go new file mode 100644 index 0000000..1ae8a76 --- /dev/null +++ b/cmd/notifications/list.go @@ -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, + // 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 { + // --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) +} diff --git a/cmd/notifications/notifications_list.go b/cmd/notifications/notifications_list.go new file mode 100644 index 0000000..d6cc060 --- /dev/null +++ b/cmd/notifications/notifications_list.go @@ -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 +} diff --git a/cmd/notifications/pinned.go b/cmd/notifications/pinned.go new file mode 100644 index 0000000..9904c2f --- /dev/null +++ b/cmd/notifications/pinned.go @@ -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", + 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 new file mode 100644 index 0000000..40cb428 --- /dev/null +++ b/cmd/notifications/read.go @@ -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) +} diff --git a/cmd/notifications/unread.go b/cmd/notifications/unread.go new file mode 100644 index 0000000..262665e --- /dev/null +++ b/cmd/notifications/unread.go @@ -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) +}