File: list.go

package info (click to toggle)
tea-cli 0.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,648 kB
  • sloc: makefile: 116; sh: 17
file content (132 lines) | stat: -rw-r--r-- 3,287 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package issues

import (
	stdctx "context"
	"fmt"
	"time"

	"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/araddon/dateparse"
	"github.com/urfave/cli/v3"
)

var issueFieldsFlag = flags.FieldsFlag(print.IssueFields, []string{
	"index", "title", "state", "author", "milestone", "labels", "owner", "repo",
})

// CmdIssuesList represents a sub command of issues to list issues
var CmdIssuesList = cli.Command{
	Name:        "list",
	Aliases:     []string{"ls"},
	Usage:       "List issues of the repository",
	Description: `List issues of the repository`,
	ArgsUsage:   " ", // command does not accept arguments
	Action:      RunIssuesList,
	Flags:       append([]cli.Flag{issueFieldsFlag}, flags.IssueListingFlags...),
}

// RunIssuesList list issues
func RunIssuesList(_ stdctx.Context, cmd *cli.Command) error {
	ctx := context.InitCommand(cmd)

	state := gitea.StateOpen
	switch ctx.String("state") {
	case "all":
		state = gitea.StateAll
	case "", "open":
		state = gitea.StateOpen
	case "closed":
		state = gitea.StateClosed
	default:
		return fmt.Errorf("unknown state '%s'", ctx.String("state"))
	}

	kind := gitea.IssueTypeIssue
	switch ctx.String("kind") {
	case "", "issues", "issue":
		kind = gitea.IssueTypeIssue
	case "pulls", "pull", "pr":
		kind = gitea.IssueTypePull
	case "all":
		kind = gitea.IssueTypeAll
	default:
		return fmt.Errorf("unknown kind '%s'", ctx.String("kind"))
	}

	var err error
	var from, until time.Time
	if ctx.IsSet("from") {
		from, err = dateparse.ParseLocal(ctx.String("from"))
		if err != nil {
			return err
		}
	}
	if ctx.IsSet("until") {
		until, err = dateparse.ParseLocal(ctx.String("until"))
		if err != nil {
			return err
		}
	}
	owner := ctx.Owner
	if ctx.IsSet("owner") {
		owner = ctx.String("owner")
	}

	// ignore error, as we don't do any input validation on these flags
	labels, _ := flags.LabelFilterFlag.GetValues(cmd)
	milestones, _ := flags.MilestoneFilterFlag.GetValues(cmd)
	var issues []*gitea.Issue
	if ctx.Repo != "" {
		issues, _, err = ctx.Login.Client().ListRepoIssues(owner, ctx.Repo, gitea.ListIssueOption{
			ListOptions: flags.GetListOptions(),
			State:       state,
			Type:        kind,
			KeyWord:     ctx.String("keyword"),
			CreatedBy:   ctx.String("author"),
			AssignedBy:  ctx.String("assigned-to"),
			MentionedBy: ctx.String("mentions"),
			Labels:      labels,
			Milestones:  milestones,
			Since:       from,
			Before:      until,
		})

		if err != nil {
			return err
		}
	} else {
		issues, _, err = ctx.Login.Client().ListIssues(gitea.ListIssueOption{
			ListOptions: flags.GetListOptions(),
			State:       state,
			Type:        kind,
			KeyWord:     ctx.String("keyword"),
			CreatedBy:   ctx.String("author"),
			AssignedBy:  ctx.String("assigned-to"),
			MentionedBy: ctx.String("mentions"),
			Labels:      labels,
			Milestones:  milestones,
			Since:       from,
			Before:      until,
			Owner:       owner,
		})

		if err != nil {
			return err
		}
	}

	fields, err := issueFieldsFlag.GetValues(cmd)
	if err != nil {
		return err
	}

	print.IssuesPullsList(issues, ctx.Output, fields)
	return nil
}