-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
159 lines (137 loc) · 3.86 KB
/
report.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
/*
Copyright © 2024 AntoninoAdornetto
*/
package cmd
import (
"bytes"
"fmt"
"os"
"os/exec"
"github.com/AntoninoAdornetto/issue-summoner/pkg/issue"
"github.com/AntoninoAdornetto/issue-summoner/pkg/scm"
"github.com/AntoninoAdornetto/issue-summoner/pkg/ui"
"github.com/AntoninoAdornetto/issue-summoner/templates"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
)
var reportCmd = &cobra.Command{
Use: "report",
Short: "Report pending issues to a source code management platform",
Long: `Report will scan your git project for comments that include issue annotations.
Issue annotations can be as simple as @TODO or any other value that you seefit.
The only requirement is that the annotation resides in a single or multi line comment.
Once issue annotations are discovered, you will be presented with a list of all the issues
that were located and you can select which ones you would like to report to a source code management
platform.`,
Run: func(cmd *cobra.Command, args []string) {
annotation, path := handleCommonFlags(cmd)
sourceCodeManager, err := cmd.Flags().GetString(flag_scm)
if err != nil {
ui.LogFatal(err.Error())
}
_, err = scm.ReadAccessToken(sourceCodeManager)
if err != nil {
if os.IsNotExist(err) {
ui.LogFatal(
"configuration file does not exist. please run <issue-summoner authorize> or see <issue-summoner authorize --help>",
)
} else {
ui.LogFatal(err.Error())
}
}
issueManager, err := issue.NewIssueManager(issue.PENDING_ISSUE, annotation)
if err != nil {
ui.LogFatal(err.Error())
}
_, err = issueManager.Walk(path)
if err != nil {
ui.LogFatal(err.Error())
}
issues := issueManager.GetIssues()
if len(issues) == 0 {
fmt.Println(ui.ErrorTextStyle.Render(no_issues))
return
}
selections := ui.Selection{
Options: make(map[string]bool),
}
options := make([]ui.Item, len(issues))
for i, is := range issues {
options[i] = ui.Item{
Title: is.Title,
Desc: is.Description,
ID: is.ID,
}
}
var quit bool
teaProgram := tea.NewProgram(
ui.InitialModelMultiSelect(
options,
&selections,
select_issues,
&quit,
),
)
if _, err := teaProgram.Run(); err != nil {
ui.LogFatal(err.Error())
}
tmpl, err := templates.LoadIssueTemplate()
if err != nil {
ui.LogFatal(err.Error())
}
reportQueue := make([]scm.GitIssue, 0)
for i, is := range issues {
if selections.Options[is.ID] {
md, err := is.ExecuteIssueTemplate(tmpl)
if err != nil {
ui.LogFatal(err.Error())
}
reportQueue = append(
reportQueue,
scm.GitIssue{Title: is.Title, Body: string(md), QueueIndex: i},
)
}
}
out := bytes.Buffer{}
remoteCmd := exec.Command("git", "remote", "-v")
remoteCmd.Stdout = &out
if err := remoteCmd.Run(); err != nil {
ui.LogFatal(err.Error())
}
userName, repoName, err := scm.ExtractUserRepoName(out.Bytes())
if err != nil {
ui.LogFatal(err.Error())
}
gitManager, err := scm.NewGitManager(sourceCodeManager, userName, repoName)
if err != nil {
ui.LogFatal(err.Error())
}
reported := gitManager.Report(reportQueue)
for ch := range reported {
if err := issueManager.WriteIssueID(ch.ID, ch.QueueIndex); err != nil {
ui.LogFatal(err.Error())
}
}
if len(reportQueue) == 0 {
return
}
fmt.Println(
ui.SuccessTextStyle.Render(
fmt.Sprintf(
"Success! Uploaded %d issue(s) to %s",
len(reportQueue),
sourceCodeManager,
),
),
)
fmt.Println(
ui.SecondaryTextStyle.Render("make sure to commit and push the annotation updates!"),
)
},
}
func init() {
rootCmd.AddCommand(reportCmd)
reportCmd.Flags().StringP(flag_path, shortflag_path, "", flag_desc_path)
reportCmd.Flags().StringP(flag_annotation, shortflag_annotation, "@TODO", flag_desc_annotation)
reportCmd.Flags().StringP(flag_scm, shortflag_scm, scm.GITHUB, flag_desc_scm)
}