replace nunjucks with handlebars for templating

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2018-12-13 23:17:03 +01:00
parent f0d8eaa5f6
commit 3556d6e7a7
No known key found for this signature in database
GPG Key ID: 4EF064D0E6D63020
3 changed files with 24 additions and 17 deletions

View File

@ -6,6 +6,7 @@
"main": "index.js",
"dependencies": {
"@octokit/rest": "^16.2.0",
"handlebars": "^4.0.12",
"joi": "^14.3.0",
"koa": "^2.5.3",
"koa-bodyparser": "^4.2.1",
@ -13,8 +14,7 @@
"koa-json": "^2.0.2",
"koa-json-error": "^3.1.2",
"koa-logger": "^3.2.0",
"lokijs": "^1.5.5",
"nunjucks": "^3.1.4"
"lokijs": "^1.5.5"
},
"devDependencies": {
"@babel/cli": "^7.1.2",

View File

@ -1,4 +1,4 @@
import nunjucks from 'nunjucks'
import Handlebars from 'handlebars'
import template from './template'
/* in theory the jenkins build comment should be the first one */
@ -11,11 +11,18 @@ class Comments {
this.db = builds
this.repo = repo /* name of repo to query */
this.owner = owner /* name of user who makes the comments */
/* setup templating for comments */
this.template = template
this.nj = new nunjucks.Environment()
/* add helper for formatting dates */
this.nj.addFilter('date', (data) => (new Date(data)).toLocaleTimeString('utc'))
Handlebars.registerHelper('date', (data) =>
new Handlebars.SafeString((new Date(data)).toLocaleTimeString('utc'))
)
/* add helper for checking change in commit */
Handlebars.registerHelper('commitChanged', (data, index, options) => {
if (index == 0) { return options.inverse(this); }
if (data[index].commit !== data[index-1].commit) { return options.fn(this); }
return options.inverse(this);
})
/* setup templating for comments */
this.template = Handlebars.compile(template);
}
async renderComment (pr) {
@ -23,7 +30,7 @@ class Comments {
if (builds.length == 0) {
throw Error('No builds exist for this PR')
}
return this.nj.renderString(this.template, {builds})
return this.template({builds})
}
async postComment (pr) {

View File

@ -2,14 +2,14 @@ module.exports = `
### Jenkins Builds
| :grey_question: | Commit | :hash: | Finished | Duration | Platform | Result |
|-|-|-|-|-|-|-|
{% for b in builds -%}
{%- if b.commit != builds[loop.index0-1].commit -%}
{{#each builds}}
{{#commitChanged ../builds @index}}
| | | | | | | |
{% endif -%}
{%- if b.success -%}
| :heavy_check_mark: | {{ b.commit }} | [{{ b.id }}]({{ b.url }}) | {{ b.meta.created | date }} | {{ b.duration }} | \`{{ b.platform }}\` | [:package: package]({{ b.pkg_url }}) |
{% else -%}
| :x: | {{ b.commit }} | [{{ b.id }}]({{ b.url }}) | {{ b.meta.created | date }} | {{ b.duration }} | \`{{ b.platform }}\` | [:page_facing_up: build log]({{ b.pkg_url }}consoleText) |
{% endif -%}
{%- endfor -%}
{{/commitChanged}}
{{#if this.success}}
| :heavy_check_mark: | {{ this.commit }} | [{{ this.id }}]({{ this.url }}) | {{date this.meta.created }} | {{ this.duration }} | \`{{ this.platform }}\` | [:package: package]({{ this.pkg_url }}) |
{{else}}
| :x: | {{ this.commit }} | [{{ this.id }}]({{ this.url }}) | {{date this.meta.created }} | {{ this.duration }} | \`{{ this.platform }}\` | [:page_facing_up: build log]({{ this.pkg_url }}consoleText) |
{{/if}}
{{/each}}
`