-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
240 lines (229 loc) · 6.47 KB
/
main.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
package main
import (
"fmt"
"os"
"sort"
flag "github.com/spf13/pflag"
"github.com/vitor-augusto1/jira-weasel/logger"
"github.com/vitor-augusto1/jira-weasel/pkg/assert"
"github.com/vitor-augusto1/jira-weasel/pkg/colors"
"github.com/vitor-augusto1/jira-weasel/static"
)
type Flag[T any] struct {
full string
short string
defaultV T
}
var (
REPORT_FLAG_NAME Flag[bool] = Flag[bool]{full: "report", short: "r", defaultV: false}
PURGE_FLAG_NAME Flag[bool] = Flag[bool]{full: "purge", short: "p", defaultV: false}
LIST_FLAG_NAME Flag[bool] = Flag[bool]{full: "list", short: "l", defaultV: false}
QUIET_FLAG_NAME Flag[bool] = Flag[bool]{full: "quiet", short: "q", defaultV: false}
DEFAULT_KEYWORDS []string = []string{"TODO", "FIXME", "REFACTOR"}
)
func init() {
flag.Usage = static.WeaselDetails
}
func main() {
CheckIfCurrentDirectoryIsAGitRepository()
CheckIfWeaselConfigFileExists()
var conf *Config
conf, err := LoadConfigs("./weasel.toml")
assert.NotNil(conf, "conf cannot be nil", "conf", conf)
if err != nil {
logger.LogErrorExitingOne("There is an error with you config file. Please check it again.")
}
creds := NewJiraBasicAuthCreds()
assert.NotNil(creds, "creds cannot be nil", "creds", creds)
creds.username = conf.Credentials.Username
creds.password = conf.Credentials.Password
jiraClient := NewJiraClient(conf.Project.Url, creds.ReturnBasicAuthEncodedCredentials())
assert.NotNil(jiraClient, "jiraClient cannot be nil", "jiraClient", jiraClient)
keywords := conf.returnIssuesKeywordsSlice()
weasel := &Weasel{
Keywords: keywords,
baseRemoteUrl: conf.Remote,
}
weasel.LoadProjectFiles()
var (
flagVar bool
silentFlag bool
)
flag.BoolVarP(&flagVar,
REPORT_FLAG_NAME.full,
REPORT_FLAG_NAME.short,
REPORT_FLAG_NAME.defaultV,
static.REPORT_MESSAGE,
)
flag.BoolVarP(&flagVar,
PURGE_FLAG_NAME.full,
PURGE_FLAG_NAME.short,
PURGE_FLAG_NAME.defaultV,
static.PURGE_MESSAGE,
)
flag.BoolVarP(&flagVar,
LIST_FLAG_NAME.full,
LIST_FLAG_NAME.short,
LIST_FLAG_NAME.defaultV,
static.LIST_MESSAGE,
)
flag.BoolVarP(&silentFlag,
QUIET_FLAG_NAME.full,
QUIET_FLAG_NAME.short,
QUIET_FLAG_NAME.defaultV,
static.SILENT_MESSAGE,
)
flag.Parse()
if flag.Lookup(REPORT_FLAG_NAME.full).Changed {
reportCommand(weasel, jiraClient, &conf.Keywords, silentFlag, static.Banner)
} else if flag.Lookup(PURGE_FLAG_NAME.full).Changed {
purgeCommand(weasel, jiraClient, silentFlag, static.Banner)
} else if flag.Lookup(LIST_FLAG_NAME.full).Changed {
listCommand(weasel, silentFlag, static.Banner)
} else {
helperCommand()
os.Exit(1)
}
}
func reportCommand(
weasel *Weasel,
jiraClient *JiraClient,
keywordMap *map[string]string,
quiet bool,
bannerFunc func(),
) {
assert.NotNil(weasel, "weasel can't be nil", "weasel", weasel)
assert.NotNil(jiraClient, "jiraClient can't be nil", "jiraClient", jiraClient)
assert.NotNil(keywordMap, "keywordMap can't be nil", "keywordMap", keywordMap)
if !quiet {
bannerFunc()
}
issuesToReport := []*Issue{}
weasel.VisitTodosInWeaselFiles(func(td Todo) error {
if td.ReportedID != nil {
return nil
}
assert.Nil(td.ReportedID, "Trying to report an already reported todo.", "t.ReportedID", td.ReportedID)
mappedKeyword, ok := (*keywordMap)[td.Keyword]
assert.Assert(ok, "The provided todo keyword was not mapped", "t.Keyword", td.Keyword)
issue := jiraClient.CreateNewIssueFromTODO(td, mappedKeyword)
if issue != nil {
issuesToReport = append(issuesToReport, issue)
}
return nil
})
for _, issue := range issuesToReport {
if !quiet {
fmt.Fprintf(
os.Stdout,
" 📢 Reporting a new '%s' [%s %s]\n\n",
colors.Info(issue.Todo.Keyword),
colors.Success(issue.Todo.FilePath),
colors.Success(fmt.Sprint(issue.Todo.Line)),
)
}
createdIssueResp, err := jiraClient.ReportIssueAsJiraTicket(issue)
if err != nil {
logger.LogErrorExitingOne(
fmt.Sprintf(
"Can't report the following issue: '%s'. Skipping for now.\n",
issue.Summary,
),
)
}
issue.Todo.ReportedID = &createdIssueResp.Key
err = issue.Todo.ChangeTodoStatusToReported()
if err != nil {
continue
}
commitMessage := fmt.Sprintf("weasel: Report TODO (%s)", *issue.Todo.ReportedID)
err = issue.Todo.CommitTodoUpdate(commitMessage)
if err != nil {
continue
}
if !quiet {
issue.Todo.PrintCurrentStatus()
}
}
}
func purgeCommand(weasel *Weasel, jiraClient *JiraClient, quiet bool, bannerFunc func()) {
assert.NotNil(weasel, "weasel can't be nil", "weasel", weasel)
assert.NotNil(jiraClient, "jiraClient can't be nil", "jiraClient", jiraClient)
if !quiet {
bannerFunc()
fmt.Fprintf(
os.Stdout,
" 🧹 Purging %s TODOS...\n\n",
colors.Success("'DONE'"),
)
}
todosToCheckStatus := []*Todo{}
todosToPurge := []*Todo{}
weasel.VisitTodosInWeaselFiles(func(td Todo) error {
if td.ReportedID != nil {
assert.NotNil(td.ReportedID, "Trying to purge an unreported todo", "td.ReportedID", td.ReportedID)
todosToCheckStatus = append(todosToCheckStatus, &td)
}
return nil
})
for _, td := range todosToCheckStatus {
status := jiraClient.CheckJiraIssueStatusFromAnExistingTodo(*td)
if len(status) > 0 {
if status == "done" || status == "DONE" {
todosToPurge = append(todosToPurge, td)
if !quiet {
fmt.Println()
td.PrintCurrentStatus()
}
}
}
}
sort.Slice(todosToPurge, func(i, j int) bool {
if todosToPurge[i].FilePath == todosToPurge[j].FilePath {
return todosToPurge[i].Line > todosToPurge[j].Line
}
return todosToPurge[i].FilePath < todosToPurge[j].FilePath
})
if len(todosToPurge) == 0 {
fmt.Fprintf(os.Stdout, colors.Info(" There are no TODOS to be purge\n"))
os.Exit(0)
}
for _, td := range todosToPurge {
err := td.SelfPurge()
if err != nil {
fmt.Fprintf(
os.Stderr,
colors.Error(
fmt.Sprintf(
"Error trying to purge the todo of id: '%s'. Skipping for now.\n%s",
*td.ReportedID,
err,
),
),
)
}
commitMessage := fmt.Sprintf("weasel: Purge TODO (%s)", *td.ReportedID)
err = td.CommitTodoUpdate(commitMessage)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't commit changes: '%s'.\n", err)
}
}
}
func listCommand(weasel *Weasel, quiet bool, bannerFunc func()) {
if !quiet {
bannerFunc()
}
weasel.VisitTodosInWeaselFiles(func(td Todo) error {
if td.ReportedID != nil {
fmt.Println()
td.PrintCurrentStatus()
return nil
}
fmt.Println()
td.PrintCurrentStatus()
return nil
})
}
func helperCommand() {
flag.Usage()
}