Authentication-Results: mail-b.sr.ht; dkim=pass header.d=emersion.fr header.i=@emersion.fr Received: from mail-40136.proton.ch (mail-40136.proton.ch [185.70.40.136]) by mail-b.sr.ht (Postfix) with ESMTPS id 9B92D11F2C6 for <~emersion/hut-dev@lists.sr.ht>; Fri, 18 Feb 2022 13:52:12 +0000 (UTC) Date: Fri, 18 Feb 2022 13:52:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=emersion.fr; s=protonmail2; t=1645192330; bh=4Pc8DOY4PoElctaQipT7aVKn+EhX0IYG1f2LH2Nc8+E=; h=Date:To:From:Reply-To:Subject:Message-ID:From:To:Cc:Date:Subject: Reply-To:Feedback-ID:Message-ID; b=aZI23vB1GsRCdYJoKQ37vlQXDGV324Zr0oUgEs1VfJV1HhGOoeKApbwV+9Tgu4Sgi Sdjzo42PdM3zaGeJiCstcIfz2A7zwDgWuVi+7Nmp+vVS/njktYfnlxGZvC05mIoW/G PFxSHYyvMZYBKdvqLZ2l27yXUZTy/90wXKrozKVIfI7lcucuVT2ZRYmA1iICkJRiY6 6OzoFKK5BMFDjnB9EY+n7F2Nd+dZEKpDVXB/4jLlWfKEhLCRESAWaWRa3Tc2RD/NqF bvBzzSxaF3kl3AKKBU6QCfREHDjq8nLL+wICj1uno0zUuNKeacMSRiik4uDPZ62juB GlaQV6kMVH7VQ== To: ~emersion/hut-dev@lists.sr.ht From: Simon Ser Reply-To: Simon Ser Subject: [PATCH] todo ticket create: new command Message-ID: <20220218135156.614905-1-contact@emersion.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.2 required=10.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on mailout.protonmail.ch Pretty similar to `hut todo ticket comment`, except we need to provide both a subject and a body. Add commented instructions when using an editor, similar to git-commit's behavior. --- srht/todosrht/gql.go | 11 ++++ srht/todosrht/operations.graphql | 6 +++ todo.go | 87 ++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) diff --git a/srht/todosrht/gql.go b/srht/todosrht/gql.go index e6cb2bfbdaa7..d1708fabe870 100644 --- a/srht/todosrht/gql.go +++ b/srht/todosrht/gql.go @@ -549,3 +549,14 @@ func UpdateTicketStatus(client *gqlclient.Client, ctx = context.Context, trackerId =09err =3D client.Execute(ctx, op, &respData) =09return respData.UpdateTicketStatus, err } + +func SubmitTicket(client *gqlclient.Client, ctx context.Context, trackerId= int32, input SubmitTicketInput) (submitTicket *Ticket, err error) { +=09op :=3D gqlclient.NewOperation("mutation submitTicket ($trackerId: Int!= , $input: SubmitTicketInput!) {\n\tsubmitTicket(trackerId: $trackerId, inpu= t: $input) {\n\t\tid\n\t}\n}\n") +=09op.Var("trackerId", trackerId) +=09op.Var("input", input) +=09var respData struct { +=09=09SubmitTicket *Ticket +=09} +=09err =3D client.Execute(ctx, op, &respData) +=09return respData.SubmitTicket, err +} diff --git a/srht/todosrht/operations.graphql b/srht/todosrht/operations.gr= aphql index 883276f0ec8d..2d31a42d0331 100644 --- a/srht/todosrht/operations.graphql +++ b/srht/todosrht/operations.graphql @@ -98,3 +98,9 @@ mutation updateTicketStatus( } } } + +mutation submitTicket($trackerId: Int!, $input: SubmitTicketInput!) { + submitTicket(trackerId: $trackerId, input: $input) { + id + } +} diff --git a/todo.go b/todo.go index 5860c3f739df..f4a060cf24b6 100644 --- a/todo.go +++ b/todo.go @@ -1,12 +1,14 @@ package main =20 import ( +=09"bufio" =09"context" =09"fmt" =09"io" =09"log" =09"os" =09"strings" +=09"unicode" =20 =09"git.sr.ht/~emersion/hut/srht/todosrht" =09"git.sr.ht/~emersion/hut/termfmt" @@ -110,6 +112,7 @@ func newTodoTicketCommand() *cobra.Command { =09cmd.AddCommand(newTodoTicketListCommand()) =09cmd.AddCommand(newTodoTicketCommentCommand()) =09cmd.AddCommand(newTodoTicketStatusCommand()) +=09cmd.AddCommand(newTodoTicketCreateCommand()) =09return cmd } =20 @@ -296,6 +299,90 @@ func newTodoTicketStatusCommand() *cobra.Command { =09return cmd } =20 +const todoTicketPrefill =3D ` +` + +func newTodoTicketCreateCommand() *cobra.Command { +=09var stdin bool +=09run :=3D func(cmd *cobra.Command, args []string) { +=09=09ctx :=3D cmd.Context() +=09=09name, owner, instance :=3D getTrackerName(ctx, cmd) +=09=09c :=3D createClientWithInstance("todo", cmd, instance) +=09=09trackerID :=3D getTrackerID(c, ctx, name, owner) + +=09=09var input todosrht.SubmitTicketInput +=09=09if stdin { +=09=09=09br :=3D bufio.NewReader(os.Stdin) +=09=09=09fmt.Printf("Subject: ") + +=09=09=09var err error +=09=09=09input.Subject, err =3D br.ReadString('\n') +=09=09=09if err !=3D nil { +=09=09=09=09log.Fatalf("failed to read subject: %v", err) +=09=09=09} +=09=09=09input.Subject =3D strings.TrimSpace(input.Subject) +=09=09=09if input.Subject =3D=3D "" { +=09=09=09=09fmt.Println("Aborting due to empty subject.") +=09=09=09=09os.Exit(1) +=09=09=09} + +=09=09=09fmt.Printf("Description %s:\n", termfmt.Dim.String("(Markdown sup= ported)")) +=09=09=09bodyBytes, err :=3D io.ReadAll(br) +=09=09=09if err !=3D nil { +=09=09=09=09log.Fatalf("failed to read description: %v", err) +=09=09=09} +=09=09=09if body :=3D strings.TrimSpace(string(bodyBytes)); body !=3D "" { +=09=09=09=09input.Body =3D &body +=09=09=09} +=09=09} else { +=09=09=09text, err :=3D getInputWithEditor("hut_ticket*.md", todoTicketPre= fill) +=09=09=09if err !=3D nil { +=09=09=09=09log.Fatalf("failed to read ticket subject and description: %v"= , err) +=09=09=09} + +=09=09=09// Drop our prefilled comment, but without stripping leading +=09=09=09// whitespace +=09=09=09text =3D strings.TrimRightFunc(text, unicode.IsSpace) +=09=09=09text =3D strings.TrimSuffix(text, todoTicketPrefill) +=09=09=09text =3D strings.TrimRightFunc(text, unicode.IsSpace) + +=09=09=09parts :=3D strings.SplitN(text, "\n", 2) +=09=09=09input.Subject =3D strings.TrimSpace(parts[0]) +=09=09=09if len(parts) > 1 { +=09=09=09=09body :=3D strings.TrimSpace(parts[1]) +=09=09=09=09input.Body =3D &body +=09=09=09} +=09=09} + +=09=09if input.Subject =3D=3D "" { +=09=09=09fmt.Println("Aborting due to empty subject.") +=09=09=09os.Exit(1) +=09=09} + +=09=09ticket, err :=3D todosrht.SubmitTicket(c.Client, ctx, trackerID, inp= ut) +=09=09if err !=3D nil { +=09=09=09log.Fatal(err) +=09=09} else if ticket =3D=3D nil { +=09=09=09log.Fatal("failed to create ticket") +=09=09} + +=09=09fmt.Printf("Created new ticket %v\n", termfmt.DarkYellow.Sprintf("#%= v", ticket.Id)) +=09} + +=09cmd :=3D &cobra.Command{ +=09=09Use: "create", +=09=09Short: "Create a new ticket", +=09=09Args: cobra.ExactArgs(0), +=09=09Run: run, +=09} +=09cmd.Flags().BoolVar(&stdin, "stdin", false, "read ticket from stdin") +=09return cmd +} + func getTrackerID(c *Client, ctx context.Context, name, owner string) int3= 2 { =09var ( =09=09tracker *todosrht.Tracker base-commit: f7519c5d49a8287a0e47f9459cb859304d1e8b0c --=20 2.35.1