@@ -17,6 +17,7 @@ package main
1717import (
1818 "bytes"
1919 "context"
20+ _ "embed"
2021 "encoding/json"
2122 "errors"
2223 "fmt"
@@ -26,6 +27,7 @@ import (
2627 "net/url"
2728 "os"
2829 "strings"
30+ "text/template"
2931 "unicode"
3032
3133 "gg-scm.io/pkg/git"
@@ -34,6 +36,9 @@ import (
3436
3537const requestPullSynopsis = "create a GitHub pull request"
3638
39+ //go:embed pr_editor_template.md
40+ var requestPullEditorTemplate string
41+
3742func requestPull (ctx context.Context , cc * cmdContext , args []string ) error {
3843 f := flag .NewFlagSet (true , "gg requestpull [-n] [-e=0] [--title=MSG [--body=MSG]] [--draft] [-R user1[,user2]] [BRANCH]" , requestPullSynopsis + `
3944
@@ -179,18 +184,23 @@ aliases: pr
179184 return nil
180185 }
181186 if * edit && * titleFlag == "" {
187+ tmpl , err := template .New ("pr_editor_template.md" ).Parse (requestPullEditorTemplate )
188+ if err != nil {
189+ return err
190+ }
182191 editorInit := new (bytes.Buffer )
183- editorInit .WriteString (title )
184- if body != "" {
185- editorInit .WriteString ("\n \n " )
186- editorInit .WriteString (body )
192+ err = tmpl .Execute (editorInit , map [string ]any {
193+ "Title" : title ,
194+ "Body" : body ,
195+ "BaseOwner" : baseOwner ,
196+ "BaseRepo" : baseRepo ,
197+ "BaseBranch" : baseBranch ,
198+ "HeadOwner" : headOwner ,
199+ "Branch" : branch ,
200+ })
201+ if err != nil {
202+ return err
187203 }
188- editorInit .WriteString ("\n " + prCommentPrefix + "Please enter the pull request message." + prCommentSuffix + "\n " +
189- prCommentPrefix + "Lines formatted like this will be ignored," + prCommentSuffix + "\n " +
190- prCommentPrefix + "and an empty message aborts the pull request." + prCommentSuffix + "\n " +
191- prCommentPrefix + "The first line will be used as the title and must not be empty." + prCommentSuffix + "\n " )
192- fmt .Fprintf (editorInit , "%s%s/%s: merge into %s:%s from %s:%s%s\n " ,
193- prCommentPrefix , baseOwner , baseRepo , baseOwner , baseBranch , headOwner , branch , prCommentSuffix )
194204 newMsg , err := cc .editor .open (ctx , "PR_EDITMSG.md" , editorInit .Bytes ())
195205 if err != nil {
196206 return err
@@ -306,12 +316,10 @@ func readPullRequestTemplate(ctx context.Context, g *git.Git) string {
306316 return ""
307317}
308318
309- const (
310- prCommentPrefix = "[comment]: # ("
311- prCommentSuffix = ")"
312- )
313-
314319func parseEditedPullRequestMessage (b []byte ) (title , body string , _ error ) {
320+ const prCommentPrefix = "[comment]: # ("
321+ const prCommentSuffix = ")"
322+
315323 // Split into lines.
316324 lines := bytes .Split (b , []byte {'\n' })
317325 // Strip comment lines.
0 commit comments