mirror of
https://gitea.com/gitea/tea.git
synced 2024-09-01 04:14:18 -04:00
fix lint fix lint Move print TrackedTimesList to print package Move AbsPathWithExpansion to utils/path.go rename module intern to config Move Subcomands into it's own Packages Split times subcomands into own sourcefiles Split repos subcomands into own sourcefiles Split releases subcomands into own sourcefiles Split pulls subcomands into own sourcefiles Split milestones subcomands into own sourcefiles Split login subcomands into own sourcefiles Split labels subcomands into own sourcefiles split issues subcomands into own sourcefiles mv Move Interactive Login Creation to interact package Move Add Login function to intern/login.go apply from review lint: add description to exported func smal nits Move DetailViews stdout print func to print package Refactor: * Move Config & Login routines into intern package * rename global var in cmd * Move help func to utils Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/206 Reviewed-by: Norwin <noerw@noreply.gitea.io> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
118 lines
2.8 KiB
Go
118 lines
2.8 KiB
Go
// 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"
|
|
|
|
"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"
|
|
)
|
|
|
|
// CmdNotifications is the main command to operate with notifications
|
|
var CmdNotifications = cli.Command{
|
|
Name: "notifications",
|
|
Usage: "Show notifications",
|
|
Description: "Show notifications, by default based of the current repo and unread one",
|
|
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",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "pinned",
|
|
Aliases: []string{"pd"},
|
|
Usage: "show pinned notifications instead unread",
|
|
},
|
|
&flags.PaginationPageFlag,
|
|
&flags.PaginationLimitFlag,
|
|
}, flags.AllDefaultFlags...),
|
|
}
|
|
|
|
func runNotifications(ctx *cli.Context) error {
|
|
var news []*gitea.NotificationThread
|
|
var err error
|
|
|
|
listOpts := flags.GetListOptions(ctx)
|
|
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") {
|
|
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)
|
|
}
|
|
|
|
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 {
|
|
print.OutputList(flags.GlobalOutputValue, headers, values)
|
|
}
|
|
return nil
|
|
}
|