momocow / semantic-release-gitmoji

✨🐛💥 A semantic-release plugin for gitmojis. Different from conventional changelog, Gitmoji commits are used to determine a release type and generate release notes.
MIT License
86 stars 20 forks source link

Question: How to "compare" the gitmoji or emoji value in a Helper function #92

Open arkadioz opened 5 months ago

arkadioz commented 5 months ago

Hi @momocow, this question is related to my attempt to do this "Other section" https://github.com/momocow/semantic-release-gitmoji/issues/87 while you implemente an enhacement, I was thinking of a workaround meanwhile, that if we could compare what is the value of the emoji maybe we could do some logic, but I do not know how to compare the value of the emoji, let me elaborate:

When you have the following in the default hbar template:

# 🎉 [v{{nextRelease.version}}](https://{{source}}/{{owner}}/{{repo}}/compare/v{{lastRelease.version}}..v{{nextRelease.version}}) ({{datetime "UTC:yyyy-mm-dd"}}) 🔖

{{#with commits}}
{{#if sparkles}}
## ✨ New Features
{{#each sparkles}}
- {{> commitTemplate}}
{{/each}}
{{/if}}

{{#if bug}}
## 🐛 Bug Fixes
{{#each bug}}
- {{> commitTemplate}}
{{/each}}
{{/if}}

{{checkIfItsNotABug <what_would_go_in_here?>}}
## Other changes that are not a bug emoji

{{/with}}

I was imagining that maybe with a Helper function that could check what is the "value" of the emoji we could do some logic like so:

               helpers: {
                  checkIfItsNotABug: function(emoji) {
                    if(emoji === ':bug:'){
                   // it is a bug emoji
                   return false;
                    }
                    // not a bug emoji
                    return true;
                  }
               }

but it does not work ofcourse, I was wondering how could you check the value of the emoji? I tried different ways, but I guess it will not work checking the equality against a String, because the emoji is an object that I believe is one of this objects from node-emoji? So based on that I think that maybe we could do some logic, but first I would like to confirm is it is possible to do what im trying to explain, which would be to create a helper function that can tell if it is a bug emoji or sparkles or any type. How could you do it from the helper? Also notice in the template <what_would_go_in_here?> not sure what should I pass there to get the object, should it be this or how could it be done?

Hopefully you understood what I am trying to do, thank you

momocow commented 5 months ago

{{#with commits}} is not an iteration, and commits is an object instead of an array.

commits object looks like this:

{
  bug: [
    {/* commit context 1 */},
    {/* commit context 2 */},
  ],
  sparkles: [
    {/* commit context 1 */},
    {/* commit context 2 */},
  ]
}

In the current template, for example {{#if bug}} checks if there the bug key exists on the commits object and render all bug commits in the value array if so. Thus there is no way to gather "Other changes" without explicitly specifying emojis in hbs templates.

momocow commented 5 months ago

Furthermore, if we rewrite {{#with commits}} with {{#each commits}} to iterate over an object, we will find it unfeasible since this approach will generate multiple "Other changes" parts.

{{#each commits}}
  {{#if (eq @key "bug")}}
    ## Bugs
  {{else if (eq @key "sparkles")}}
    ## Features
  {{else}}
    ## Other changes
  {{/if}}
{{/each}}
momocow commented 5 months ago

I think the best solution is to propose a helper to omit properties on the commits object.

             helpers: {
                omit: function(commits, ...keys) {
                  return Object.fromEntries(Object.entries(commits).filter(([emoji]) => !keys.includes(emoji)));
                },
             }

and rewrite your example as the following one

{{#with commits}}
{{#if sparkles}}
## ✨ New Features
{{#each sparkles}}
- {{> commitTemplate}}
{{/each}}
{{/if}}

{{#if bug}}
## 🐛 Bug Fixes
{{#each bug}}
- {{> commitTemplate}}
{{/each}}
{{/if}}
{{/with}}

{{#each (omit commits "sparkles" "bug")}}
## Other changes that are not a bug emoji
{{#each this}}
- {{> commitTemplate}}
{{/each}}
{{/each}}
arkadioz commented 5 months ago

Hey @momocow sorry for taking a while to feedback, gonna test it this weekend and give you some feedback, thanks a lot for all the help

arkadioz commented 5 months ago

@momocow Hi, tried it but I got the following error:

[8:50:25 AM] [semantic-release] [semantic-release-gitmoji] › ℹ  Release notes are generated.
[8:50:25 AM] [semantic-release] › ✘  Failed step "generateNotes" of plugin "semantic-release-gitmoji"
[8:50:25 AM] [semantic-release] › ✘  An error occurred while running semantic-release: TypeError: Cannot convert undefined or null to object
    at Function.entries (<anonymous>)
    at Proxy.omit (/tmp/build/169762bf/my-repo/.releaserc.js:50:52)
    at Proxy.wrapper (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/internal/wrapHelper.js:15:19)
    at eval (eval at createFunctionContext (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:262:23), <anonymous>:28:168)
    at prog (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/runtime.js:268:12)
    at Object.<anonymous> (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/helpers/with.js:32:14)
    at Object.wrapper (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/internal/wrapHelper.js:15:19)
    at Object.eval [as main] (eval at createFunctionContext (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:262:23), <anonymous>:25:49)
    at main (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/runtime.js:208:32)
    at ReleaseNotes.ret (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/runtime.js:212:12) {
  pluginName: 'semantic-release-gitmoji'
}
TypeError: Cannot convert undefined or null to object
    at Function.entries (<anonymous>)
    at Proxy.omit (/tmp/build/169762bf/my-repo/.releaserc.js:50:52)
    at Proxy.wrapper (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/internal/wrapHelper.js:15:19)
    at eval (eval at createFunctionContext (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:262:23), <anonymous>:28:168)
    at prog (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/runtime.js:268:12)
    at Object.<anonymous> (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/helpers/with.js:32:14)
    at Object.wrapper (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/internal/wrapHelper.js:15:19)
    at Object.eval [as main] (eval at createFunctionContext (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:262:23), <anonymous>:25:49)
    at main (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/runtime.js:208:32)
    at ReleaseNotes.ret (/tmp/build/169762bf/my-repo/node_modules/handlebars/dist/cjs/handlebars/runtime.js:212:12) {
  pluginName: 'semantic-release-gitmoji'
}

I copy pasted your suggestion but still here is how it looks:

Helper:

omit: function(commits, ...keys) {
                  return Object.fromEntries(Object.entries(commits).filter(([emoji]) => !keys.includes(emoji)));
}

Using it at default-template.hbs:

{{#each (omit commits "sparkles" "bug")}}
## Other changes that are not a bug emoji or sparkles
{{#each this}}
- {{> commitTemplate}}
{{/each}}
{{/each}}

For what I can see, it looks like maybe some entries were undefined or null, or maybe im not properly passing the object, if you know how to fix it I appreciate your help, this is a very nice enhancement not just for me but any future users :)