Skip to content

Instantly share code, notes, and snippets.

@froi
Last active July 30, 2020 01:55
Show Gist options
  • Save froi/a99a837de54754403089d9bd216246f2 to your computer and use it in GitHub Desktop.
Save froi/a99a837de54754403089d9bd216246f2 to your computer and use it in GitHub Desktop.
playing-with-etajs

This is a quick and simple example on how we can use a template engine to create markdown strings for use in GitHub issues or Jira ticket.

Eta.js

Eta.js is a templating engine that embbeds Javascript syntax into the template it self. It seems super lite weight.

Why use it

When working with issues and PRs, a ton of the code we do are the message templates themselves. I think that extracting this into a template engine will clean up the business logic from the view logic (sound familiar 😉).

The strongest argument I can come up with, besides how the code looks, to use templates for issue and PR bodies, as well as comments is removing string concatanation from our code. I'm of the opinion that this will cut down on future fomatting issues.

const Eta = require('eta');
const fs = require('fs');
// Get the template from disk. I found that the utf-8 option is necessary
let testTemplate = fs.readFileSync("template.md", "utf-8");
let data = [
{key: "1", status: "open", statusIcon: "all", validState: "yes"},
{key: "2", status: "closed", statusIcon: "none", validState: "no"},
{key: "3", status: "open", statusIcon: "all", validState: "yes"},
{key: "4", status: "closed", statusIcon: "none", validState: "no"}
];
// After rendering the results can be passed into another template to construct more complex messages.
const result = Eta.render(testTemplate, {data});
// Here I just write to a file but this can easily be passed into another process
fs.writeFileSync("rendered.md", result); //

Pull Request body validation

<% if (it.data && it.data.length > 0) { %> The following Jira issues have been found in the body of this pull request, with validation status below

Issue Jira Status Validation
<% it.data.forEach(function(issue) { %>
<%= issue.key %> <%= issue.status %> <%= issue.statusIcon %> <%= issue.validState %>
<% }) %>

<% } else { %> :heavy_exclamation_mark: No valid issues were found in the pull request body <% } %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment