Add action to set title

This commit is contained in:
Sebastiaan de Schaetzen 2024-10-17 21:37:48 +02:00
parent 1cf2a6d3e4
commit f12287c3e4
3 changed files with 77 additions and 22 deletions

View File

@ -353,3 +353,30 @@ func ActionSetStatus(_ []string) {
Options(options...). Options(options...).
Run() Run()
} }
func ActionSetTitle(_ []string) {
if !HasIssue() {
printError("No issue selected", nil)
return
}
issue, err := GetIssue(GetIssueKey())
if err != nil {
printError("Failed to get issue", err)
return
}
summary := issue.Fields.Summary
err = huh.NewInput().
Title("Enter a title:").
Value(&summary).
Run()
if err != nil {
printError("Failed to set title", err)
return
}
err = UpdateIssueTitle(issue.Key, summary)
if err != nil {
printError("Failed to set title", err)
return
}
println("Title changed!")
}

View File

@ -57,6 +57,7 @@ var CommandTree = []CommandArg{
).WithHelp("View data about something"), ).WithHelp("View data about something"),
option("set", option("set",
command("status", ActionSetStatus).WithHelp("Set status of a ticket"), command("status", ActionSetStatus).WithHelp("Set status of a ticket"),
command("title", ActionSetTitle).WithHelp("Set the title of a ticket"),
).WithHelp("Change something"), ).WithHelp("Change something"),
command("exit", ActionExit).WithHelp("Exit jirashell"), command("exit", ActionExit).WithHelp("Exit jirashell"),
command("logout", ActionLogout).WithHelp("Logout from Jira"), command("logout", ActionLogout).WithHelp("Logout from Jira"),

71
jira.go
View File

@ -16,55 +16,68 @@ func CreateSpinner(title string) *spinner.Spinner {
Type(spinner.Globe) Type(spinner.Globe)
} }
func RunSpinner[T any](title string, callback func() (*T, any, error)) (*T, error) { func RunSpinnerRaw(title string, callback func(ctx context.Context) error) error {
var result *T
var err error var err error
finished := false
ctx, cancel := context.WithCancel(context.Background())
err2 := CreateSpinner(title). err2 := CreateSpinner(title).
Action(func() { Action(func() {
result, _, err = callback() err = callback(ctx)
finished = true
}).Run() }).Run()
if err2 != nil { if err2 != nil {
return nil, err2 return err2
} }
if result == nil { if !finished {
return nil, errorAborted cancel()
return errorAborted
} }
return err
}
func RunSpinner[T any](title string, callback func(ctx context.Context) (*T, any, error)) (*T, error) {
var result *T
err := RunSpinnerRaw(title, func(ctx context.Context) error {
var err error
result, _, err = callback(ctx)
return err
})
return result, err return result, err
} }
func GetAllStatuses(projectId string) (*jira.IssueTypesWithStatus, error) { func GetAllStatuses(projectId string) (*jira.IssueTypesWithStatus, error) {
return RunSpinner("Fetching statuses...", func() (*jira.IssueTypesWithStatus, any, error) { return RunSpinner("Fetching statuses...", func(ctx context.Context) (*jira.IssueTypesWithStatus, any, error) {
return jiraClient.Project.GetAllStatuses(context.Background(), projectId) return jiraClient.Project.GetAllStatuses(ctx, projectId)
}) })
} }
func GetUserData() (*jira.User, error) { func GetUserData() (*jira.User, error) {
return RunSpinner("Fetching user info...", func() (*jira.User, any, error) { return RunSpinner("Fetching user info...", func(ctx context.Context) (*jira.User, any, error) {
return jiraClient.User.GetCurrentUser(context.Background()) return jiraClient.User.GetCurrentUser(ctx)
}) })
} }
func GetAllProjects() (*jira.ProjectList, error) { func GetAllProjects() (*jira.ProjectList, error) {
return RunSpinner("Fetching all projects...", func() (*jira.ProjectList, any, error) { return RunSpinner("Fetching all projects...", func(ctx context.Context) (*jira.ProjectList, any, error) {
return jiraClient.Project.GetAll(context.Background(), nil) return jiraClient.Project.GetAll(ctx, nil)
}) })
} }
func GetAllBoardsForProject(projectIdOrKey string) (*jira.BoardsList, error) { func GetAllBoardsForProject(projectIdOrKey string) (*jira.BoardsList, error) {
return RunSpinner(fmt.Sprintf("Fetching boards for project '%s'...", projectIdOrKey), func() (*jira.BoardsList, any, error) { return RunSpinner(fmt.Sprintf("Fetching boards for project '%s'...", projectIdOrKey), func(ctx context.Context) (*jira.BoardsList, any, error) {
return jiraClient.Board.GetAllBoards(context.Background(), &jira.BoardListOptions{ProjectKeyOrID: projectIdOrKey}) return jiraClient.Board.GetAllBoards(ctx, &jira.BoardListOptions{ProjectKeyOrID: projectIdOrKey})
}) })
} }
func GetBoardConfiguration(boardId int) (*jira.BoardConfiguration, error) { func GetBoardConfiguration(boardId int) (*jira.BoardConfiguration, error) {
return RunSpinner("Fetching board...", func() (*jira.BoardConfiguration, any, error) { return RunSpinner("Fetching board...", func(ctx context.Context) (*jira.BoardConfiguration, any, error) {
return jiraClient.Board.GetBoardConfiguration(context.Background(), boardId) return jiraClient.Board.GetBoardConfiguration(ctx, boardId)
}) })
} }
func GetCurrentSprint(boardId int) (*jira.Sprint, error) { func GetCurrentSprint(boardId int) (*jira.Sprint, error) {
list, err := RunSpinner("Fetching sprint...", func() (*jira.SprintsList, any, error) { list, err := RunSpinner("Fetching sprint...", func(ctx context.Context) (*jira.SprintsList, any, error) {
return jiraClient.Board.GetAllSprints(context.Background(), int64(boardId), &jira.GetAllSprintsOptions{ return jiraClient.Board.GetAllSprints(ctx, int64(boardId), &jira.GetAllSprintsOptions{
State: "active", State: "active",
}) })
}) })
@ -81,8 +94,8 @@ func GetCurrentSprint(boardId int) (*jira.Sprint, error) {
} }
func GetIssuesForSprint(sprintId int) ([]jira.Issue, error) { func GetIssuesForSprint(sprintId int) ([]jira.Issue, error) {
issues, err := RunSpinner("Fetching issues...", func() (*[]jira.Issue, any, error) { issues, err := RunSpinner("Fetching issues...", func(ctx context.Context) (*[]jira.Issue, any, error) {
list, t, err := jiraClient.Sprint.GetIssuesForSprint(context.Background(), sprintId) list, t, err := jiraClient.Sprint.GetIssuesForSprint(ctx, sprintId)
return &list, t, err return &list, t, err
}) })
if issues == nil { if issues == nil {
@ -93,7 +106,21 @@ func GetIssuesForSprint(sprintId int) ([]jira.Issue, error) {
} }
func GetIssue(issueKey string) (*jira.Issue, error) { func GetIssue(issueKey string) (*jira.Issue, error) {
return RunSpinner("Fetching issue...", func() (*jira.Issue, any, error) { return RunSpinner("Fetching issue...", func(ctx context.Context) (*jira.Issue, any, error) {
return jiraClient.Issue.Get(context.Background(), issueKey, nil) return jiraClient.Issue.Get(ctx, issueKey, nil)
})
}
func UpdateIssueTitle(issue string, title string) error {
return RunSpinnerRaw("Updating issue title...", func(ctx context.Context) error {
resp, err := jiraClient.Issue.UpdateIssue(ctx, issue, map[string]any{
"fields": map[string]any{
"summary": title,
},
})
if err != nil {
return err
}
return resp.Body.Close()
}) })
} }