-
Notifications
You must be signed in to change notification settings - Fork 787
/
get_issue.go
247 lines (219 loc) · 6.05 KB
/
get_issue.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package cmd
import (
"io"
"os/user"
"regexp"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/issues"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
)
// GetIssueOptions contains the command line options
type GetIssueOptions struct {
GetOptions
Dir string
Id string
}
var (
GetIssueLong = templates.LongDesc(`
Display the status of an issue for a project.
`)
GetIssueExample = templates.Examples(`
# Get the status of an issue for a project
jx get issue --id ISSUE_ID
`)
)
// NewCmdGetIssue creates the command
func NewCmdGetIssue(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &GetIssueOptions{
GetOptions: GetOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "issue [flags]",
Short: "Display the status of an issue",
Long: GetIssueLong,
Example: GetIssueExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Id, "id", "i", "", "The issue ID")
cmd.Flags().StringVarP(&options.Dir, "dir", "d", "", "The root project directory")
options.addGetFlags(cmd)
return cmd
}
// Run implements this command
func (o *GetIssueOptions) Run() error {
tracker, err := o.createIssueProvider(o.Dir)
if err != nil {
return errors.Wrap(err, "failed to create the issue tracker")
}
issue, err := tracker.GetIssue(o.Id)
if err != nil {
return errors.Wrap(err, "issue not found")
}
table := o.CreateTable()
table.AddRow("ISSUE", "STATUS", "APPLICATION", "ENVIRONMENT")
f := o.Factory
client, ns, err := f.CreateJXClient()
if err != nil {
return errors.Wrap(err, "cannot create the JX client")
}
apisClient, err := f.CreateApiExtensionsClient()
if err != nil {
return errors.Wrap(err, "failed to create the Kube API extensions client")
}
err = kube.RegisterEnvironmentCRD(apisClient)
if err != nil {
return errors.Wrap(err, "failed to register the Environment API")
}
envList, err := client.JenkinsV1().Environments(ns).List(metav1.ListOptions{})
if err != nil {
return errors.Wrap(err, "cannot list the environments")
}
kubeClient, _, err := f.CreateClient()
if err != nil {
return errors.Wrap(err, "failed to create the Kubernetes client")
}
found := false
for _, env := range envList.Items {
envNs, err := kube.GetEnvironmentNamespace(client, ns, env.Name)
if err != nil {
continue
}
releaseList, err := client.JenkinsV1().Releases(envNs).List(metav1.ListOptions{})
if err != nil {
return errors.Wrap(err, "cannot list the releases")
}
rel := o.findRelease(tracker, issue, releaseList.Items)
if rel == nil {
continue
}
apps, err := o.getApplications(kubeClient, &env, envNs)
if err != nil {
continue
}
for _, app := range apps {
if o.match(issue.URL, app) {
table.AddRow(issue.URL, *issue.State, app, env.Name)
found = true
}
}
}
if !found {
table.AddRow(issue.URL, *issue.State, "", "")
}
table.Render()
return nil
}
func (o *GetIssueOptions) findRelease(tracker issues.IssueProvider, issue *gits.GitIssue, releases []v1.Release) *v1.Release {
for _, rel := range releases {
prs := rel.Spec.PullRequests
// checks all the PRs and the issues linked into their bodies
for _, pr := range prs {
if pr.URL == issue.URL {
return &rel
} else {
issueKind := issues.GetIssueProvider(tracker)
issueIDs := o.parseIssueIDs(pr, issueKind)
issueURLs := o.convertIssueIDsToURLs(tracker, issueIDs)
for _, issueURL := range issueURLs {
if issueURL == issue.URL {
return &rel
}
}
}
}
// checks all the issues which are not pull requests
issues := rel.Spec.Issues
for _, is := range issues {
if is.URL == issue.URL {
return &rel
}
}
}
return nil
}
func (o *GetIssueOptions) parseIssueIDs(issue v1.IssueSummary, issueKind string) []string {
regex := regexp.MustCompile(`(\#\d+)`)
if issueKind == issues.Jira {
regex = regexp.MustCompile(`[A-Z][A-Z]+-(\d+)`)
}
issues := []string{}
foundIssues := map[string]bool{}
matches := regex.FindAllStringSubmatch(issue.Body, -1)
for _, match := range matches {
for _, result := range match {
id := strings.TrimPrefix(result, "#")
found, ok := foundIssues[id]
if !found || !ok {
issues = append(issues, id)
foundIssues[id] = true
}
}
}
return issues
}
func (o *GetIssueOptions) convertIssueIDsToURLs(tracker issues.IssueProvider, issueIDs []string) []string {
issueURLs := []string{}
for _, id := range issueIDs {
issue, err := tracker.GetIssue(id)
if err != nil {
continue
}
issueURLs = append(issueURLs, issue.URL)
}
return issueURLs
}
func (o *GetIssueOptions) getApplications(client kubernetes.Interface, env *v1.Environment, envNs string) ([]string, error) {
apps := []string{}
deployments, err := kube.GetDeployments(client, envNs)
if err != nil {
return apps, errors.Wrap(err, "failed to retrieve the application deployments")
}
for k, _ := range deployments {
appName := kube.GetAppName(k, envNs)
if env.Spec.Kind == v1.EnvironmentKindTypeEdit {
if appName == kube.DeploymentExposecontrollerService {
continue
}
currUser, err := user.Current()
if err == nil {
if currUser.Username != env.Spec.PreviewGitSpec.User.Username {
continue
}
}
appName = kube.GetEditAppName(appName)
} else if env.Spec.Kind == v1.EnvironmentKindTypePreview {
appName = env.Spec.PullRequestURL
}
apps = append(apps, appName)
}
return apps, nil
}
func (o *GetIssueOptions) match(issueURL string, appName string) bool {
isssueURLParts := strings.Split(issueURL, "/")
for _, issueURLPart := range isssueURLParts {
if issueURLPart == appName {
return true
}
}
return false
}