/
create.go
126 lines (107 loc) · 3.13 KB
/
create.go
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
package create
import (
"fmt"
"net/url"
"path"
"github.com/AlecAivazis/survey/v2"
"github.com/craftamap/bb/cmd/commands/issue/shared"
"github.com/craftamap/bb/cmd/options"
"github.com/craftamap/bb/util/logging"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
)
var (
Title string
Description string
Assignee string
Kind string
Priority string
Status string
Version string
Milestone string
Component string
)
func Add(issueCmd *cobra.Command, globalOpts *options.GlobalOptions) {
createCmd := &cobra.Command{
Use: "create",
Short: "Create a issue",
Annotations: map[string]string{
"RequiresClient": "true",
"RequiresRepository": "true",
},
Run: func(cmd *cobra.Command, args []string) {
// Initialisation
var err error
c := globalOpts.Client
bbrepo := globalOpts.BitbucketRepo
io := shared.IssueOptions{}
// Apply command line args here
if Title != "" {
io.Title = Title
}
if Description != "" {
io.Description = Description
}
if Kind != "" {
io.Kind = Kind
}
if Priority != "" {
io.Priority = Priority
}
if Status != "" {
io.Status = Status
}
if Version != "" {
io.Version = Version
}
if Milestone != "" {
io.Milestone = Milestone
}
if Component != "" {
io.Component = Component
}
fmt.Printf("Creating issue in %s\n", fmt.Sprintf("%s/%s", bbrepo.RepoOrga, bbrepo.RepoSlug))
fmt.Println()
// If the title was already specified as command line argument, don't ask for it
if Title == "" {
questionTitle := &survey.Input{
Message: "Title",
Default: io.Title,
}
err = survey.AskOne(questionTitle, &io.Title, survey.WithValidator(survey.Required))
}
if err != nil {
logging.Error(err)
return
}
io, err, cancel := shared.AskQuestionsForCreateOrUpdate(io, bbrepo, c)
if err != nil {
logging.Error(err)
return
}
if cancel {
return
}
response, err := c.IssuesCreate(bbrepo.RepoOrga, bbrepo.RepoSlug, io)
if err != nil {
fmt.Printf("%s%s%#v\n", aurora.Red(":: "), aurora.Bold("An error occurred: "), err)
return
}
link, err := url.Parse(response.Repository.Links["html"].(map[string]interface{})["href"].(string))
link.Path = path.Join(link.Path, "issues", fmt.Sprintf("%d", response.ID))
if err != nil {
logging.Error(err)
}
fmt.Printf("Take a look at your issue here: %s\n", aurora.Index(242, link.String()))
},
}
createCmd.Flags().StringVarP(&Description, "description", "b", "", "Supply a description.")
createCmd.Flags().StringVarP(&Title, "title", "t", "", "Supply a title.")
createCmd.Flags().StringVar(&Kind, "type", "", "Supply a issue type.")
createCmd.Flags().StringVar(&Priority, "priority", "", "Supply a issue priority.")
createCmd.Flags().StringVar(&Status, "status", "", "Supply a issue status.")
createCmd.Flags().StringVar(&Version, "issue-version", "", "Supply a version.")
createCmd.Flags().StringVar(&Milestone, "milestone", "", "Supply a milestone.")
createCmd.Flags().StringVar(&Component, "component", "", "Supply a component.")
issueCmd.AddCommand(createCmd)
}