Skip to content

Commit

Permalink
initial issue search
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillip Miller committed Apr 11, 2022
1 parent 0315927 commit d325e3a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
14 changes: 11 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ func NewJiraClient(opts *Options) (*JiraClient, error) {
return j, nil
}

// ProcessTickets searches for matching ticket types and completes them as needed.
func (jc *JiraClient) ProcessTickets() error {
// PrintFoundTickets searches for matching ticket types and prints them
func (jc *JiraClient) PrintFoundTickets() error {
// Ensure that there is a matching ticket for the current project with this JQL query.
jql := fmt.Sprintf("text ~ \"%s\" ORDER BY created ASC", jc.opts.JQLTextSearch)
var jql string
if jc.opts.JQLRawSearch != "" {
jql = fmt.Sprintf("%s", jc.opts.JQLRawSearch)
} else if jc.opts.MyJiraIssues {
jql = fmt.Sprintf("status in (\"Ready for work\", \"In Progress\", \"Deploy Ready\") AND assignee in ( %s ) ORDER BY created ASC", jc.opts.JiraAccountID)
} else {
jql = fmt.Sprintf("text ~ \"%s\" ORDER BY created ASC", jc.opts.JQLTextSearch)
}
//jql := fmt.Sprintf("text ~ \"%s\" ORDER BY created ASC", jc.opts.JQLTextSearch)
issues, resp, err := jc.client.Issue.Search(jql, &jira.SearchOptions{
Fields: []string{
"attachment",
Expand Down
22 changes: 22 additions & 0 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Options struct {
JiraUserName string
JiraProjectName string
JQLTextSearch string
JQLRawSearch string
MyJiraIssues bool
JiraClientID string
JiraClientSecret string
}
Expand All @@ -28,12 +30,20 @@ func ConfigureCommand(cmd *cobra.Command) {
cmd.PersistentFlags().StringP("jira-username", "", "", "jira username")
cmd.PersistentFlags().StringP("jira-project-name", "", "", "a project name in jira you want to search")
cmd.PersistentFlags().StringP("jql-text-search", "", "", "a string of text that you want to search all issues for")
cmd.PersistentFlags().StringP("jql-raw-search", "", "", "a string of text that you want to search all issues for")
cmd.PersistentFlags().BoolP("my-jira-issues", "", false, "find all issues assigned and in progress assigned to JIRA_ACCOUNT_ID from config.yaml")
cmd.PersistentFlags().StringP("jira-client-id", "", "", "jira client id for oauth2")
cmd.PersistentFlags().StringP("jira-client-secret", "", "", "jira client secret for oauth2")
}

// LoadFromCommand loads all the command flag opts from cli and config file into Options struct
func (opts *Options) LoadFromCommand(cmd *cobra.Command) error {
myJiraIssues, err := cmd.Flags().GetBool("my-jira-issues")
if err != nil {
return err
}
opts.MyJiraIssues = myJiraIssues

jiraClientID, err := utils.ConfigureFlagOpts(cmd, &utils.LoadFromCommandOpts{
Flag: "jira-client-id",
Opts: opts.JiraClientID,
Expand Down Expand Up @@ -132,5 +142,17 @@ func (opts *Options) LoadFromCommand(cmd *cobra.Command) error {
return err
}
opts.JQLTextSearch = jqlTextSearch.(string)

jqlRawSearch, err := utils.ConfigureFlagOpts(cmd, &utils.LoadFromCommandOpts{
Flag: "jql-raw-search",
IsFilePath: false,
Prefix: "",
Opts: opts.JQLRawSearch,
})
if err != nil {
return err
}
opts.JQLRawSearch = jqlRawSearch.(string)

return nil
}
2 changes: 1 addition & 1 deletion cmd/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var RootSearchCommand = &cobra.Command{
log.Panic(err)
}

if err = jc.ProcessTickets(); err != nil {
if err = jc.PrintFoundTickets(); err != nil {
log.Panic(err)
}

Expand Down
12 changes: 8 additions & 4 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,17 @@ func getPriorityColor(priority string) tablewriter.Colors {
}
}

func getFieldString(name string) string {
if name != "" {
return name
func getFieldString(name interface{}) string {
if name != nil {
return name.(string)
}
return "N/A"
}

// PrintTable ...
func PrintTable(issues []jira.Issue, host string) error {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Issue", "Summary", "Status", "Priority", "URL", "Reporter"})
table.SetHeader([]string{"Issue", "Summary", "Project", "Status", "Priority", "URL", "Reporter"})
table.SetBorder(false)

table.SetHeaderColor(
Expand All @@ -165,6 +165,7 @@ func PrintTable(issues []jira.Issue, host string) error {
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiYellowColor, tablewriter.BgBlackColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiYellowColor, tablewriter.BgBlackColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiYellowColor, tablewriter.BgBlackColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiYellowColor, tablewriter.BgBlackColor},
)

table.SetColumnColor(
Expand All @@ -174,18 +175,21 @@ func PrintTable(issues []jira.Issue, host string) error {
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiWhiteColor, tablewriter.BgBlackColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiWhiteColor, tablewriter.BgBlackColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiWhiteColor, tablewriter.BgBlackColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiWhiteColor, tablewriter.BgBlackColor},
)

for _, issue := range issues {
colorData := []string{issue.Key,
issue.Fields.Summary,
issue.Fields.Project.Name,
issue.Fields.Status.Name,
issue.Fields.Priority.Name,
fmt.Sprintf("%s/browse/%s", host, issue.Key),
getFieldString(issue.Fields.Reporter.DisplayName),
}

table.Rich(colorData, []tablewriter.Colors{
{tablewriter.Normal},
{tablewriter.Normal},
{tablewriter.Normal},
isDone(issue.Fields.Status.Name),
Expand Down

0 comments on commit d325e3a

Please sign in to comment.