add more issue / pr creation params #331

Merged
6543 merged 18 commits from noerw/tea:issue-create-opts into master 2021-03-08 11:48:04 +00:00
3 changed files with 108 additions and 75 deletions
Showing only changes of commit ded112c158 - Show all commits

View File

@ -5,25 +5,18 @@
package interact
import (
"fmt"
"strings"
"time"
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/modules/config"
"code.gitea.io/tea/modules/task"
"code.gitea.io/tea/modules/utils"
"github.com/AlecAivazis/survey/v2"
"github.com/araddon/dateparse"
)
const nilVal = "[none]"
const customVal = "[other]"
// CreateIssue interactively creates an issue
func CreateIssue(login *config.Login, owner, repo string) error {
var title, description, dueDate, milestone string
var title, description, milestone string
var assignees, labels []string
var deadline *time.Time
@ -56,25 +49,12 @@ func CreateIssue(login *config.Login, owner, repo string) error {
}
// assignees
promptA := &survey.MultiSelect{Message: "Assignees:", Options: selectables.Collaborators, VimMode: true}
if err := survey.AskOne(promptA, &assignees); err != nil {
if assignees, err = promptMultiSelect("Assignees:", selectables.Collaborators, "[other]"); err != nil {
return err
}
// check for custom value & prompt again with text input
// HACK until https://github.com/AlecAivazis/survey/issues/339 is implemented
if otherIndex := utils.IndexOf(assignees, customVal); otherIndex != -1 {
var customAssignees string
promptA := &survey.Input{Message: "Assignees:", Help: "comma separated usernames"}
if err := survey.AskOne(promptA, &customAssignees); err != nil {
return err
}
assignees = append(assignees[:otherIndex], assignees[otherIndex+1:]...)
assignees = append(assignees, strings.Split(customAssignees, ",")...)
}
// milestone
promptM := &survey.Select{Message: "Milestone:", Options: selectables.MilestoneList, VimMode: true, Default: nilVal}
if err := survey.AskOne(promptM, &milestone); err != nil {
if milestone, err = promptSelect("Milestone:", selectables.MilestoneList, "", "[none]"); err != nil {
return err
}
@ -89,26 +69,9 @@ func CreateIssue(login *config.Login, owner, repo string) error {
}
// deadline
promptI = &survey.Input{Message: "Due date [no due date]:"}
err = survey.AskOne(
promptI,
&dueDate,
survey.WithValidator(func(input interface{}) error {
if str, ok := input.(string); ok {
if len(str) == 0 {
return nil
}
t, err := dateparse.ParseAny(str)
if err != nil {
return err
}
deadline = &t
} else {
return fmt.Errorf("invalid result type")
}
return nil
}),
)
if deadline, err = promptDatetime("Due date:"); err != nil {
return err
}
return task.CreateIssue(
login,
@ -146,11 +109,10 @@ func fetchIssueSelectables(login *config.Login, owner, repo string, done chan is
done <- r
return
}
r.Collaborators = make([]string, len(colabs)+2)
r.Collaborators = make([]string, len(colabs)+1)
r.Collaborators[0] = login.User
r.Collaborators[1] = customVal
for i, u := range colabs {
r.Collaborators[i+2] = u.UserName
r.Collaborators[i+1] = u.UserName
}
milestones, _, err := c.ListRepoMilestones(owner, repo, gitea.ListMilestoneOption{})
@ -160,12 +122,10 @@ func fetchIssueSelectables(login *config.Login, owner, repo string, done chan is
return
}
r.MilestoneMap = make(map[string]int64)
r.MilestoneList = make([]string, len(milestones)+1)
r.MilestoneList[0] = nilVal
r.MilestoneMap[nilVal] = 0
r.MilestoneList = make([]string, len(milestones))
for i, m := range milestones {
r.MilestoneMap[m.Title] = m.ID
r.MilestoneList[i+1] = m.Title
r.MilestoneList[i] = m.Title
}
labels, _, err := c.ListRepoLabels(owner, repo, gitea.ListLabelsOptions{})

View File

@ -5,7 +5,6 @@
package interact
import (
"fmt"
"time"
"code.gitea.io/tea/modules/config"
@ -13,12 +12,11 @@ import (
"code.gitea.io/sdk/gitea"
"github.com/AlecAivazis/survey/v2"
"github.com/araddon/dateparse"
)
// CreateMilestone interactively creates a milestone
func CreateMilestone(login *config.Login, owner, repo string) error {
var title, description, dueDate string
var title, description string
var deadline *time.Time
// owner, repo
@ -41,28 +39,7 @@ func CreateMilestone(login *config.Login, owner, repo string) error {
}
// deadline
promptI = &survey.Input{Message: "Milestone deadline [no due date]:"}
err = survey.AskOne(
promptI,
&dueDate,
survey.WithValidator(func(input interface{}) error {
if str, ok := input.(string); ok {
if len(str) == 0 {
return nil
}
t, err := dateparse.ParseAny(str)
if err != nil {
return err
}
deadline = &t
} else {
return fmt.Errorf("invalid result type")
}
return nil
}),
)
if err != nil {
if deadline, err = promptDatetime("Milestone deadline:"); err != nil {
return err
}

View File

@ -7,8 +7,11 @@ package interact
import (
"fmt"
"strings"
"time"
"code.gitea.io/tea/modules/utils"
"github.com/AlecAivazis/survey/v2"
"github.com/araddon/dateparse"
)
// PromptMultiline runs a textfield-style prompt and blocks until input was made.
@ -67,3 +70,96 @@ func promptRepoSlug(defaultOwner, defaultRepo string) (owner, repo string, err e
}
return
}
// promptDatetime prompts for a date or datetime string.
// Supports all formats understood by araddon/dateparse.
func promptDatetime(prompt string) (val *time.Time, err error) {
var input string
err = survey.AskOne(
&survey.Input{Message: prompt},
&input,
survey.WithValidator(func(input interface{}) error {
if str, ok := input.(string); ok {
if len(str) == 0 {
return nil
}
t, err := dateparse.ParseAny(str)
if err != nil {
return err
}
val = &t
} else {
return fmt.Errorf("invalid result type")
}
return nil
}),
)
return
}
// promptSelect creates a generic multiselect prompt, with processing of custom values.
func promptMultiSelect(prompt string, options []string, customVal string) ([]string, error) {
var selection []string
promptA := &survey.MultiSelect{
Message: prompt,
Options: makeSelectOpts(options, customVal, ""),
VimMode: true,
}
if err := survey.AskOne(promptA, &selection); err != nil {
return nil, err
}
return promptCustomVal(prompt, customVal, selection)
}
// promptSelect creates a generic select prompt, with processing of custom values or none-option.
func promptSelect(prompt string, options []string, customVal, noneVal string) (string, error) {
var selection string
promptA := &survey.Select{
Message: prompt,
Options: makeSelectOpts(options, customVal, noneVal),
VimMode: true,
Default: noneVal,
}
if err := survey.AskOne(promptA, &selection); err != nil {
return "", err
}
if noneVal != "" && selection == noneVal {
return "", nil
}
if customVal != "" {
sel, err := promptCustomVal(prompt, customVal, []string{selection})
if err != nil {
return "", err
}
selection = sel[0]
}
return selection, nil
}
// makeSelectOpts adds cusotmVal & noneVal to opts if set.
func makeSelectOpts(opts []string, customVal, noneVal string) []string {
if customVal != "" {
opts = append(opts, customVal)
}
if noneVal != "" {
opts = append(opts, noneVal)
}
return opts
}
// promptCustomVal checks if customVal is present in selection, and prompts
// for custom input to add to the selection instead.
func promptCustomVal(prompt, customVal string, selection []string) ([]string, error) {
// check for custom value & prompt again with text input
// HACK until https://github.com/AlecAivazis/survey/issues/339 is implemented
if otherIndex := utils.IndexOf(selection, customVal); otherIndex != -1 {
var customAssignees string
promptA := &survey.Input{Message: prompt, Help: "comma separated list"}
if err := survey.AskOne(promptA, &customAssignees); err != nil {
return nil, err
}
selection = append(selection[:otherIndex], selection[otherIndex+1:]...)
selection = append(selection, strings.Split(customAssignees, ",")...)
}
return selection, nil
}