2020-07-17 15:30:02 +00:00
|
|
|
// 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 cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
2020-09-30 05:11:33 +00:00
|
|
|
"code.gitea.io/tea/cmd/flags"
|
|
|
|
"code.gitea.io/tea/modules/config"
|
|
|
|
"code.gitea.io/tea/modules/print"
|
|
|
|
|
2020-07-17 15:30:02 +00:00
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CmdNotifications is the main command to operate with notifications
|
|
|
|
var CmdNotifications = cli.Command{
|
|
|
|
Name: "notifications",
|
2020-10-01 03:35:03 +00:00
|
|
|
Aliases: []string{"notif"},
|
2020-09-27 14:35:53 +00:00
|
|
|
Usage: "Show notifications",
|
|
|
|
Description: "Show notifications, by default based of the current repo and unread one",
|
2020-07-17 15:30:02 +00:00
|
|
|
Action: runNotifications,
|
|
|
|
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",
|
|
|
|
},
|
2020-09-16 02:01:41 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "pinned",
|
|
|
|
Aliases: []string{"pd"},
|
|
|
|
Usage: "show pinned notifications instead unread",
|
|
|
|
},
|
2020-09-30 05:11:33 +00:00
|
|
|
&flags.PaginationPageFlag,
|
|
|
|
&flags.PaginationLimitFlag,
|
|
|
|
}, flags.AllDefaultFlags...),
|
2020-07-17 15:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func runNotifications(ctx *cli.Context) error {
|
|
|
|
var news []*gitea.NotificationThread
|
|
|
|
var err error
|
|
|
|
|
2020-09-30 05:11:33 +00:00
|
|
|
listOpts := flags.GetListOptions(ctx)
|
2020-09-27 12:07:46 +00:00
|
|
|
if listOpts.Page == 0 {
|
|
|
|
listOpts.Page = 1
|
2020-07-17 15:30:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 02:01:41 +00:00
|
|
|
var status []gitea.NotifyStatus
|
|
|
|
if ctx.Bool("read") {
|
|
|
|
status = []gitea.NotifyStatus{gitea.NotifyStatusRead}
|
|
|
|
}
|
|
|
|
if ctx.Bool("pinned") {
|
|
|
|
status = append(status, gitea.NotifyStatusPinned)
|
|
|
|
}
|
|
|
|
|
2020-07-17 15:30:02 +00:00
|
|
|
if ctx.Bool("all") {
|
2020-09-30 05:11:33 +00:00
|
|
|
login := config.InitCommandLoginOnly(flags.GlobalLoginValue)
|
2020-09-16 02:01:41 +00:00
|
|
|
news, _, err = login.Client().ListNotifications(gitea.ListNotificationOptions{
|
2020-07-17 15:30:02 +00:00
|
|
|
ListOptions: listOpts,
|
2020-09-16 02:01:41 +00:00
|
|
|
Status: status,
|
2020-07-17 15:30:02 +00:00
|
|
|
})
|
|
|
|
} else {
|
2020-09-30 05:11:33 +00:00
|
|
|
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
|
2020-09-16 02:01:41 +00:00
|
|
|
news, _, err = login.Client().ListRepoNotifications(owner, repo, gitea.ListNotificationOptions{
|
2020-07-17 15:30:02 +00:00
|
|
|
ListOptions: listOpts,
|
2020-09-16 02:01:41 +00:00
|
|
|
Status: status,
|
2020-07-17 15:30:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
headers := []string{
|
|
|
|
"Type",
|
|
|
|
"Index",
|
|
|
|
"Title",
|
|
|
|
}
|
|
|
|
if ctx.Bool("all") {
|
|
|
|
headers = append(headers, "Repository")
|
|
|
|
}
|
|
|
|
|
|
|
|
var values [][]string
|
|
|
|
|
|
|
|
for _, n := range news {
|
|
|
|
if n.Subject == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// if pull or Issue get Index
|
|
|
|
var index string
|
|
|
|
if n.Subject.Type == "Issue" || n.Subject.Type == "Pull" {
|
|
|
|
index = n.Subject.URL
|
|
|
|
urlParts := strings.Split(n.Subject.URL, "/")
|
|
|
|
if len(urlParts) != 0 {
|
|
|
|
index = urlParts[len(urlParts)-1]
|
|
|
|
}
|
|
|
|
index = "#" + index
|
|
|
|
}
|
|
|
|
|
|
|
|
item := []string{n.Subject.Type, index, n.Subject.Title}
|
|
|
|
if ctx.Bool("all") {
|
|
|
|
item = append(item, n.Repository.FullName)
|
|
|
|
}
|
|
|
|
values = append(values, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(values) != 0 {
|
2020-09-30 05:11:33 +00:00
|
|
|
print.OutputList(flags.GlobalOutputValue, headers, values)
|
2020-07-17 15:30:02 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|