Add --fields to notification & milestone listings #422

Merged
6543 merged 22 commits from noerw/tea:fields-flag into main 2022-09-13 19:08:19 +00:00
9 changed files with 120 additions and 11 deletions
Showing only changes of commit efc2ef0370 - Show all commits

View File

@ -9,13 +9,15 @@ import (
"io/ioutil"
"strings"
"code.gitea.io/tea/modules/interact"
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/modules/config"
"code.gitea.io/tea/modules/context"
"code.gitea.io/tea/modules/interact"
"code.gitea.io/tea/modules/print"
"code.gitea.io/tea/modules/utils"
"code.gitea.io/sdk/gitea"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
)
@ -54,7 +56,11 @@ func runAddComment(cmd *cli.Context) error {
body = strings.Join([]string{body, string(bodyStdin)}, "\n\n")
}
} else if len(body) == 0 {
if body, err = interact.PromptMultiline("Content"); err != nil {
if err = survey.AskOne(interact.NewMultiline(interact.Multiline{
Message: "Comment:",
Syntax: "md",
UseEditor: config.GetPreferences().Editor,
}), &body); err != nil {
return err
}
}

View File

@ -27,6 +27,7 @@ var CmdRepos = cli.Command{
&repos.CmdReposList,
&repos.CmdReposSearch,
&repos.CmdRepoCreate,
&repos.CmdRepoFork,
},
Flags: repos.CmdReposListFlags,
}

57
cmd/repos/fork.go Normal file
View File

@ -0,0 +1,57 @@
// Copyright 2021 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 repos
import (
"fmt"
"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/urfave/cli/v2"
)
// CmdRepoFork represents a sub command of repos to fork an existing repo
var CmdRepoFork = cli.Command{
Name: "fork",
Aliases: []string{"f"},
Usage: "Fork an existing repository",
Description: "Create a repository from an existing repo",
Action: runRepoFork,
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "owner",
Aliases: []string{"O"},
Usage: "name of fork's owner, defaults to current user",
},
}, flags.LoginRepoFlags...),
}
func runRepoFork(cmd *cli.Context) error {
ctx := context.InitCommand(cmd)
client := ctx.Login.Client()
opts := gitea.CreateForkOption{}
if ctx.IsSet("owner") {
owner := ctx.String("owner")
opts.Organization = &owner
}
repo, _, err := client.CreateFork(ctx.Owner, ctx.Repo, opts)
if err != nil {
return err
}
topics, _, err := client.ListRepoTopics(repo.Owner.UserName, repo.Name, gitea.ListRepoTopicsOptions{})
if err != nil {
return err
}
print.RepoDetails(repo, topics)
fmt.Printf("%s\n", repo.HTMLURL)
return nil
}

View File

@ -17,9 +17,16 @@ import (
"gopkg.in/yaml.v2"
)
// Preferences that are stored in and read from the config file
type Preferences struct {
// Prefer using an external text editor over inline multiline prompts
Editor bool `yaml:"editor"`
}
// LocalConfig represents local configurations
type LocalConfig struct {
Logins []Login `yaml:"logins"`
Logins []Login `yaml:"logins"`
Prefs Preferences `yaml:"preferences"`
}
var (
@ -55,6 +62,11 @@ func GetConfigPath() string {
return configFilePath
}
// GetPreferences returns preferences based on the config file
func GetPreferences() Preferences {
return config.Prefs
}
// loadConfig load config from file
func loadConfig() (err error) {
loadConfigOnce.Do(func() {

View File

@ -11,6 +11,7 @@ import (
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/modules/context"
"code.gitea.io/tea/modules/print"
"github.com/AlecAivazis/survey/v2"
"golang.org/x/crypto/ssh/terminal"
)

View File

@ -43,7 +43,12 @@ func promptIssueProperties(login *config.Login, owner, repo string, o *gitea.Cre
}
// description
promptD := &survey.Multiline{Message: "Issue description:", Default: o.Body}
promptD := NewMultiline(Multiline{
Message: "Issue description:",
Default: o.Body,
Syntax: "md",
UseEditor: config.GetPreferences().Editor,
})
if err = survey.AskOne(promptD, &o.Body); err != nil {
return err
}

View File

@ -33,7 +33,11 @@ func CreateMilestone(login *config.Login, owner, repo string) error {
}
// description
promptM := &survey.Multiline{Message: "Milestone description:"}
promptM := NewMultiline(Multiline{
Message: "Milestone description:",
Syntax: "md",
UseEditor: config.GetPreferences().Editor,
})
if err := survey.AskOne(promptM, &description); err != nil {
return err
}

View File

@ -14,9 +14,26 @@ import (
"github.com/araddon/dateparse"
)
// PromptMultiline runs a textfield-style prompt and blocks until input was made.
func PromptMultiline(message string) (content string, err error) {
err = survey.AskOne(&survey.Multiline{Message: message}, &content)
// Multiline represents options for a prompt that expects multiline input
type Multiline struct {
Message string
Default string
Syntax string
UseEditor bool
}
// NewMultiline creates a prompt that switches between the inline multiline text
// and a texteditor based prompt
func NewMultiline(opts Multiline) (prompt survey.Prompt) {
if opts.UseEditor {
prompt = &survey.Editor{
Message: opts.Message,
Default: opts.Default,
FileName: "*." + opts.Syntax,
}
} else {
prompt = &survey.Multiline{Message: opts.Message, Default: opts.Default}
}
return
}

View File

@ -8,6 +8,7 @@ import (
"fmt"
"os"
"code.gitea.io/tea/modules/config"
"code.gitea.io/tea/modules/context"
"code.gitea.io/tea/modules/task"
@ -55,7 +56,11 @@ func ReviewPull(ctx *context.TeaContext, idx int64) error {
if (state == gitea.ReviewStateComment && len(codeComments) == 0) || state == gitea.ReviewStateRequestChanges {
promptOpts = survey.WithValidator(survey.Required)
}
err = survey.AskOne(&survey.Multiline{Message: "Concluding comment:"}, &comment, promptOpts)
err = survey.AskOne(NewMultiline(Multiline{
Message: "Concluding comment:",
Syntax: "md",
UseEditor: config.GetPreferences().Editor,
}), &comment, promptOpts)
if err != nil {
return err
}
@ -65,6 +70,7 @@ func ReviewPull(ctx *context.TeaContext, idx int64) error {
// DoDiffReview (1) fetches & saves diff in tempfile, (2) starts $VISUAL or $EDITOR to comment on diff,
// (3) parses resulting file into code comments.
// It doesn't really make sense to use survey.Editor() here, as we'd read the file content at least twice.
func DoDiffReview(ctx *context.TeaContext, idx int64) ([]gitea.CreatePullReviewComment, error) {
tmpFile, err := task.SavePullDiff(ctx, idx)
if err != nil {