package cmd import ( "code.gitea.io/sdk/gitea" "fmt" "gitea.com/jolheiser/sip/modules/markdown" "github.com/AlecAivazis/survey/v2" "github.com/urfave/cli/v2" "go.jolheiser.com/beaver/color" ) var IssuesCreate = cli.Command{ Name: "create", Aliases: []string{"new"}, Usage: "Create a new issue", Action: doIssueCreate, } func doIssueCreate(ctx *cli.Context) error { fmt.Println() url := color.New(color.FgYellow).Format(fmt.Sprintf("%s/%s/%s", ctx.String("url"), ctx.String("owner"), ctx.String("repo"))) fmt.Println(color.New(color.FgCyan).Format("Creating a new issue for"), url) token, err := requireToken(ctx) if err != nil { return err } var confirmed bool var title, body string for !confirmed { questions := []*survey.Question{ { Name: "title", Prompt: &survey.Input{Message: "Title", Default: title}, Validate: survey.Required, }, { Name: "body", Prompt: &survey.Multiline{Message: "Description", Default: body}, }, } answers := struct { Title string Body string }{} if err := survey.Ask(questions, &answers); err != nil { return err } title = answers.Title body = answers.Body preview, err := markdown.Render(body) if err != nil { return err } fmt.Printf("%s\n\n%s\n", title, preview) confirm := &survey.Confirm{Message: "Preview above, enter to create or 'n' to edit", Default: true} if err := survey.AskOne(confirm, &confirmed); err != nil { return err } } client := gitea.NewClient(ctx.String("url"), token) issue, err := client.CreateIssue(ctx.String("owner"), ctx.String("repo"), gitea.CreateIssueOption{Title: title, Body: body}) if err != nil { return err } info := color.Info cyan := color.New(color.FgCyan) fmt.Println(info.Format("Issue"), cyan.Format(fmt.Sprintf("#%d", issue.Index)), info.Format("created!")) fmt.Println(cyan.Format(fmt.Sprintf("%s/%s/%s/issues/%d", ctx.String("url"), ctx.String("owner"), ctx.String("repo"), issue.Index))) return nil }