Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new notes output to get command #153

Merged
merged 1 commit into from
Jun 11, 2024
Merged

Conversation

h0tw1r3
Copy link
Contributor

@h0tw1r3 h0tw1r3 commented May 6, 2024

Basically an alternate template to the changelog format that only works with a single "Entry" and doesn't output the changelog header or footer.
A "full" changelog style entry is usually not desired when adding to annotated tags or a Github release message.

Ideally, as this extension is GH specific, I would like to see the release notes more release friendly (automatic contributors section).

Instead of this:

### Added
- Improve sections ordering https://github.com/chelnak/gh-changelog/pull/139 ([smortex](https://github.com/smortex))

Output this for release notes:

### Added
- #139: Improve sections ordering (@smortex)

Which ends up looking like:

image

@chelnak
Copy link
Owner

chelnak commented May 7, 2024

Nice! Check out my comment in your deps PR re an upcoming change.

What do you think about this?

Rather than --notes, I wonder if a flag like --output would be better?

For example, it leaves the implementation open for future types:

  • --output notes
  • --output json
  • --output notes
  • --output standard (default)

@h0tw1r3
Copy link
Contributor Author

h0tw1r3 commented May 7, 2024

@chelnak all set I think

Copy link
Owner

@chelnak chelnak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loving this idea.

I've left a few comments/suggestions to simplify things a little bit. Have a play and let me know what you think.

Thanks!

Comment on lines +16 to +39
type outputEnum string

const (
outputStandard outputEnum = "standard"
outputNotes outputEnum = "notes"
)

func (e *outputEnum) String() string {
return string(*e)
}

func (e *outputEnum) Set(v string) error {
switch v {
case string(outputStandard), string(outputNotes):
*e = outputEnum(v)
return nil
default:
return fmt.Errorf(`must be one of %s or %s`, outputStandard, outputNotes)
}
}

func (e *outputEnum) Type() string {
return "outputEnum"
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we keep the implementation inside the writer package?

I'll explain what I mean over the next few comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only for handling the command line arguments. Open to suggestions on how to remove some of the duplication.

return "outputEnum"
}

var outputTemplate = outputStandard
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

outputTemplate could become outputType which is just a string.

Suggested change
var outputTemplate = outputStandard
var outputType string

Comment on lines +79 to +85
switch outputTemplate {
case outputStandard:
tmplSrc = writer.TmplSrcStandard
case outputNotes:
tmplSrc = writer.TmplSrcNotes
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove l79 - l85

if err != nil {
return err
}

var buf bytes.Buffer
if err := writer.Write(&buf, changelog); err != nil {
if err := writer.Write(&buf, tmplSrc, changelog); err != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writer.Write should now expect a string

Suggested change
if err := writer.Write(&buf, tmplSrc, changelog); err != nil {
if err := writer.Write(&buf, outputType, changelog); err != nil {

var changelog changelog.Changelog
var err error

if printLatest {
changelog, err = get.GetLatest(fileName)
} else if printVersion != "" {
changelog, err = get.GetVersion(fileName, printVersion)
} else if outputTemplate == outputNotes {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this comparison, we can just do a simple string one:

Suggested change
} else if outputTemplate == outputNotes {
} else if outputType == "notes" {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

outputType makes sense, but comparing to a string does not. All of this ties back to command flag validation and using constants to expose issues early, at compile time.

Comment on lines +116 to +120
getCmd.Flags().Var(
&outputTemplate,
"output",
fmt.Sprintf(`Output template. allowed: "%s" or "%s"`, outputStandard, outputNotes),
)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the flag definition, we can use StringVar and set a default value of standard.

Suggested change
getCmd.Flags().Var(
&outputTemplate,
"output",
fmt.Sprintf(`Output template. allowed: "%s" or "%s"`, outputStandard, outputNotes),
)
getCmd.Flags().StringVar(
&outputType,
"output",
"standard",
fmt.Sprintf(`Output template. allowed: "%s" or "%s"`, "standard", "notes"),
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does flag value validation happen?

@@ -45,7 +45,7 @@ var newCmd = &cobra.Command{
return err
}

if err := writer.Write(f, changelog); err != nil {
if err := writer.Write(f, writer.TmplSrcStandard, changelog); err != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple change here - writer.Write just takes standard.

Suggested change
if err := writer.Write(f, writer.TmplSrcStandard, changelog); err != nil {
if err := writer.Write(f, "standard", changelog); err != nil {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this trade compile time for run-time errors and then it needs more tests to prevent invalid values when it's implicit using types and constants?

Comment on lines +127 to +130
const (
TmplSrcStandard = tmplStandard
TmplSrcNotes = tmplNotes
)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would no longer need to expose these two consts

TmplSrcNotes = tmplNotes
)

func Write(writer io.Writer, tmplSrc string, changelog changelog.Changelog) error {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And Write would become:

func getTemplate(s string) string {
	switch s {
	case "standard":
		return tmplStandard
	case "notes":
		return tmplNotes
	default:
		return tmplStandard
	}
}

func Write(writer io.Writer, outputType string, changelog changelog.Changelog) error {
	tmplSrc := getTemplate(outputType)
	tmpl, err := template.New("changelog").Funcs(template.FuncMap{
		"getFirstCommit": func() string {
			git := gitclient.NewGitClient(exec.Command)
			commit, err := git.GetFirstCommit()
			if err != nil {
				return ""
			}
			return commit
		},
	}).Parse(tmplSrc)
	if err != nil {
		return err
	}

	return tmpl.Execute(writer, changelog)
}

@@ -36,23 +62,33 @@ This command is useful for creating and updating Release notes in GitHub.
RunE: func(command *cobra.Command, args []string) error {
fileName := configuration.Config.FileName

var tmplSrc string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tmplSrc is no longer needed.

Suggested change
var tmplSrc string

@chelnak
Copy link
Owner

chelnak commented May 7, 2024

Summing up my previous thoughts..

So yeah, I was going down the runtime route with my suggestions.

To be honest though, I'm perfectly happy with what you have in the PR.

My main concerns are always things like simplicity and whether the app follows the same patterns elsewhere!

@h0tw1r3
Copy link
Contributor Author

h0tw1r3 commented Jun 11, 2024

From the comments, I don't think anything needs to change. Can this be merged?

@chelnak
Copy link
Owner

chelnak commented Jun 11, 2024

Let's do it 🙂

@chelnak chelnak merged commit 3bd1f40 into chelnak:main Jun 11, 2024
1 check passed
@h0tw1r3 h0tw1r3 deleted the feature/notes branch June 12, 2024 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants