Skip to content

Commit

Permalink
cmd/gg: add a blank line between PR editor template and comments
Browse files Browse the repository at this point in the history
The blank line ensures that preview works correctly.

To make this easier to work with,
I turned the template into an embedded Markdown data file
that uses the `text/template` package.
  • Loading branch information
zombiezen committed Aug 5, 2023
1 parent bbf2530 commit 8749130
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/cmd/gg/pr_editor_template.md eol=lf
11 changes: 11 additions & 0 deletions cmd/gg/pr_editor_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{ .Title }}
{{- with .Body }}

{{ . }}
{{- end }}

[comment]: # (Please enter the pull request message.)
[comment]: # (Lines formatted like this will be ignored,)
[comment]: # (and an empty message aborts the pull request.)
[comment]: # (The first line will be used as the title and must not be empty.)
[comment]: # ({{ .BaseOwner }}/{{ .BaseRepo }}: merge into {{ .BaseOwner }}:{{ .BaseBranch }} from {{ .HeadOwner }}:{{ .Branch }})
38 changes: 23 additions & 15 deletions cmd/gg/requestpull.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package main
import (
"bytes"
"context"
_ "embed"
"encoding/json"
"errors"
"fmt"
Expand All @@ -26,6 +27,7 @@ import (
"net/url"
"os"
"strings"
"text/template"
"unicode"

"gg-scm.io/pkg/git"
Expand All @@ -34,6 +36,9 @@ import (

const requestPullSynopsis = "create a GitHub pull request"

//go:embed pr_editor_template.md
var requestPullEditorTemplate string

func requestPull(ctx context.Context, cc *cmdContext, args []string) error {
f := flag.NewFlagSet(true, "gg requestpull [-n] [-e=0] [--title=MSG [--body=MSG]] [--draft] [-R user1[,user2]] [BRANCH]", requestPullSynopsis+`
Expand Down Expand Up @@ -179,18 +184,23 @@ aliases: pr
return nil
}
if *edit && *titleFlag == "" {
tmpl, err := template.New("pr_editor_template.md").Parse(requestPullEditorTemplate)
if err != nil {
return err
}
editorInit := new(bytes.Buffer)
editorInit.WriteString(title)
if body != "" {
editorInit.WriteString("\n\n")
editorInit.WriteString(body)
err = tmpl.Execute(editorInit, map[string]any{
"Title": title,
"Body": body,
"BaseOwner": baseOwner,
"BaseRepo": baseRepo,
"BaseBranch": baseBranch,
"HeadOwner": headOwner,
"Branch": branch,
})
if err != nil {
return err
}
editorInit.WriteString("\n" + prCommentPrefix + "Please enter the pull request message." + prCommentSuffix + "\n" +
prCommentPrefix + "Lines formatted like this will be ignored," + prCommentSuffix + "\n" +
prCommentPrefix + "and an empty message aborts the pull request." + prCommentSuffix + "\n" +
prCommentPrefix + "The first line will be used as the title and must not be empty." + prCommentSuffix + "\n")
fmt.Fprintf(editorInit, "%s%s/%s: merge into %s:%s from %s:%s%s\n",
prCommentPrefix, baseOwner, baseRepo, baseOwner, baseBranch, headOwner, branch, prCommentSuffix)
newMsg, err := cc.editor.open(ctx, "PR_EDITMSG.md", editorInit.Bytes())
if err != nil {
return err
Expand Down Expand Up @@ -306,12 +316,10 @@ func readPullRequestTemplate(ctx context.Context, g *git.Git) string {
return ""
}

const (
prCommentPrefix = "[comment]: # ("
prCommentSuffix = ")"
)

func parseEditedPullRequestMessage(b []byte) (title, body string, _ error) {
const prCommentPrefix = "[comment]: # ("
const prCommentSuffix = ")"

// Split into lines.
lines := bytes.Split(b, []byte{'\n'})
// Strip comment lines.
Expand Down

0 comments on commit 8749130

Please sign in to comment.