replace nunjucks with handlebars for templating
Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
parent
f0d8eaa5f6
commit
3556d6e7a7
|
@ -6,6 +6,7 @@
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@octokit/rest": "^16.2.0",
|
"@octokit/rest": "^16.2.0",
|
||||||
|
"handlebars": "^4.0.12",
|
||||||
"joi": "^14.3.0",
|
"joi": "^14.3.0",
|
||||||
"koa": "^2.5.3",
|
"koa": "^2.5.3",
|
||||||
"koa-bodyparser": "^4.2.1",
|
"koa-bodyparser": "^4.2.1",
|
||||||
|
@ -13,8 +14,7 @@
|
||||||
"koa-json": "^2.0.2",
|
"koa-json": "^2.0.2",
|
||||||
"koa-json-error": "^3.1.2",
|
"koa-json-error": "^3.1.2",
|
||||||
"koa-logger": "^3.2.0",
|
"koa-logger": "^3.2.0",
|
||||||
"lokijs": "^1.5.5",
|
"lokijs": "^1.5.5"
|
||||||
"nunjucks": "^3.1.4"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/cli": "^7.1.2",
|
"@babel/cli": "^7.1.2",
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import nunjucks from 'nunjucks'
|
import Handlebars from 'handlebars'
|
||||||
import template from './template'
|
import template from './template'
|
||||||
|
|
||||||
/* in theory the jenkins build comment should be the first one */
|
/* in theory the jenkins build comment should be the first one */
|
||||||
|
@ -11,11 +11,18 @@ class Comments {
|
||||||
this.db = builds
|
this.db = builds
|
||||||
this.repo = repo /* name of repo to query */
|
this.repo = repo /* name of repo to query */
|
||||||
this.owner = owner /* name of user who makes the comments */
|
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 */
|
/* 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) {
|
async renderComment (pr) {
|
||||||
|
@ -23,7 +30,7 @@ class Comments {
|
||||||
if (builds.length == 0) {
|
if (builds.length == 0) {
|
||||||
throw Error('No builds exist for this PR')
|
throw Error('No builds exist for this PR')
|
||||||
}
|
}
|
||||||
return this.nj.renderString(this.template, {builds})
|
return this.template({builds})
|
||||||
}
|
}
|
||||||
|
|
||||||
async postComment (pr) {
|
async postComment (pr) {
|
||||||
|
|
|
@ -2,14 +2,14 @@ module.exports = `
|
||||||
### Jenkins Builds
|
### Jenkins Builds
|
||||||
| :grey_question: | Commit | :hash: | Finished | Duration | Platform | Result |
|
| :grey_question: | Commit | :hash: | Finished | Duration | Platform | Result |
|
||||||
|-|-|-|-|-|-|-|
|
|-|-|-|-|-|-|-|
|
||||||
{% for b in builds -%}
|
{{#each builds}}
|
||||||
{%- if b.commit != builds[loop.index0-1].commit -%}
|
{{#commitChanged ../builds @index}}
|
||||||
| | | | | | | |
|
| | | | | | | |
|
||||||
{% endif -%}
|
{{/commitChanged}}
|
||||||
{%- if b.success -%}
|
{{#if this.success}}
|
||||||
| :heavy_check_mark: | {{ b.commit }} | [{{ b.id }}]({{ b.url }}) | {{ b.meta.created | date }} | {{ b.duration }} | \`{{ b.platform }}\` | [:package: package]({{ b.pkg_url }}) |
|
| :heavy_check_mark: | {{ this.commit }} | [{{ this.id }}]({{ this.url }}) | {{date this.meta.created }} | {{ this.duration }} | \`{{ this.platform }}\` | [:package: package]({{ this.pkg_url }}) |
|
||||||
{% else -%}
|
{{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) |
|
| :x: | {{ this.commit }} | [{{ this.id }}]({{ this.url }}) | {{date this.meta.created }} | {{ this.duration }} | \`{{ this.platform }}\` | [:page_facing_up: build log]({{ this.pkg_url }}consoleText) |
|
||||||
{% endif -%}
|
{{/if}}
|
||||||
{%- endfor -%}
|
{{/each}}
|
||||||
`
|
`
|
||||||
|
|
Loading…
Reference in New Issue