2020-12-14 20:05:31 +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 interact
|
|
|
|
|
|
|
|
import (
|
2021-03-08 11:48:03 +00:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2020-12-14 20:05:31 +00:00
|
|
|
"code.gitea.io/tea/modules/config"
|
|
|
|
"code.gitea.io/tea/modules/task"
|
|
|
|
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
)
|
|
|
|
|
2020-12-17 18:50:07 +00:00
|
|
|
// CreateIssue interactively creates an issue
|
2020-12-14 20:05:31 +00:00
|
|
|
func CreateIssue(login *config.Login, owner, repo string) error {
|
|
|
|
owner, repo, err := promptRepoSlug(owner, repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-08 11:48:03 +00:00
|
|
|
var opts gitea.CreateIssueOption
|
|
|
|
if err := promptIssueProperties(login, owner, repo, &opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return task.CreateIssue(login, owner, repo, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func promptIssueProperties(login *config.Login, owner, repo string, o *gitea.CreateIssueOption) error {
|
|
|
|
var milestoneName string
|
|
|
|
var labels []string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
selectableChan := make(chan (issueSelectables), 1)
|
|
|
|
go fetchIssueSelectables(login, owner, repo, selectableChan)
|
|
|
|
|
2020-12-14 20:05:31 +00:00
|
|
|
// title
|
|
|
|
promptOpts := survey.WithValidator(survey.Required)
|
2021-03-08 11:48:03 +00:00
|
|
|
promptI := &survey.Input{Message: "Issue title:", Default: o.Title}
|
|
|
|
if err = survey.AskOne(promptI, &o.Title, promptOpts); err != nil {
|
2020-12-14 20:05:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// description
|
2021-10-14 14:36:08 +00:00
|
|
|
promptD := NewMultiline(Multiline{
|
|
|
|
Message: "Issue description:",
|
|
|
|
Default: o.Body,
|
|
|
|
Syntax: "md",
|
|
|
|
UseEditor: config.GetPreferences().Editor,
|
|
|
|
})
|
2021-03-08 11:48:03 +00:00
|
|
|
if err = survey.AskOne(promptD, &o.Body); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait until selectables are fetched
|
|
|
|
selectables := <-selectableChan
|
|
|
|
if selectables.Err != nil {
|
|
|
|
return selectables.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip remaining props if we don't have permission to set them
|
|
|
|
if !selectables.Repo.Permissions.Push {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assignees
|
2022-03-19 11:08:58 +00:00
|
|
|
if o.Assignees, err = promptMultiSelect("Assignees:", selectables.Assignees, "[other]"); err != nil {
|
2021-03-08 11:48:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// milestone
|
|
|
|
if len(selectables.MilestoneList) != 0 {
|
|
|
|
if milestoneName, err = promptSelect("Milestone:", selectables.MilestoneList, "", "[none]"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.Milestone = selectables.MilestoneMap[milestoneName]
|
|
|
|
}
|
|
|
|
|
|
|
|
// labels
|
|
|
|
if len(selectables.LabelList) != 0 {
|
|
|
|
promptL := &survey.MultiSelect{Message: "Labels:", Options: selectables.LabelList, VimMode: true, Default: o.Labels}
|
|
|
|
if err := survey.AskOne(promptL, &labels); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.Labels = make([]int64, len(labels))
|
|
|
|
for i, l := range labels {
|
|
|
|
o.Labels[i] = selectables.LabelMap[l]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// deadline
|
|
|
|
if o.Deadline, err = promptDatetime("Due date:"); err != nil {
|
2020-12-14 20:05:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-08 11:48:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type issueSelectables struct {
|
|
|
|
Repo *gitea.Repository
|
2022-03-19 11:08:58 +00:00
|
|
|
Assignees []string
|
2021-03-08 11:48:03 +00:00
|
|
|
MilestoneList []string
|
|
|
|
MilestoneMap map[string]int64
|
|
|
|
LabelList []string
|
|
|
|
LabelMap map[string]int64
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchIssueSelectables(login *config.Login, owner, repo string, done chan issueSelectables) {
|
|
|
|
// TODO PERF make these calls concurrent
|
|
|
|
r := issueSelectables{}
|
|
|
|
c := login.Client()
|
|
|
|
|
|
|
|
r.Repo, _, r.Err = c.GetRepo(owner, repo)
|
|
|
|
if r.Err != nil {
|
|
|
|
done <- r
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// we can set the following properties only if we have write access to the repo
|
|
|
|
// so we fastpath this if not.
|
|
|
|
if !r.Repo.Permissions.Push {
|
|
|
|
done <- r
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-19 11:08:58 +00:00
|
|
|
assignees, _, err := c.GetAssignees(owner, repo)
|
2021-03-08 11:48:03 +00:00
|
|
|
if err != nil {
|
|
|
|
r.Err = err
|
|
|
|
done <- r
|
|
|
|
return
|
|
|
|
}
|
2022-03-19 11:08:58 +00:00
|
|
|
r.Assignees = make([]string, len(assignees))
|
|
|
|
for i, u := range assignees {
|
|
|
|
r.Assignees[i] = u.UserName
|
2021-03-08 11:48:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
milestones, _, err := c.ListRepoMilestones(owner, repo, gitea.ListMilestoneOption{})
|
|
|
|
if err != nil {
|
|
|
|
r.Err = err
|
|
|
|
done <- r
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.MilestoneMap = make(map[string]int64)
|
|
|
|
r.MilestoneList = make([]string, len(milestones))
|
|
|
|
for i, m := range milestones {
|
|
|
|
r.MilestoneMap[m.Title] = m.ID
|
|
|
|
r.MilestoneList[i] = m.Title
|
|
|
|
}
|
|
|
|
|
2022-04-07 19:44:16 +00:00
|
|
|
labels, _, err := c.ListRepoLabels(owner, repo, gitea.ListLabelsOptions{
|
|
|
|
ListOptions: gitea.ListOptions{Page: -1},
|
|
|
|
})
|
2021-03-08 11:48:03 +00:00
|
|
|
if err != nil {
|
|
|
|
r.Err = err
|
|
|
|
done <- r
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.LabelMap = make(map[string]int64)
|
|
|
|
r.LabelList = make([]string, len(labels))
|
|
|
|
for i, l := range labels {
|
|
|
|
r.LabelMap[l.Name] = l.ID
|
|
|
|
r.LabelList[i] = l.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
done <- r
|
2020-12-14 20:05:31 +00:00
|
|
|
}
|