From 3556d6e7a7f406610c594938a8c099e5ed4e65b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Thu, 13 Dec 2018 23:17:03 +0100 Subject: [PATCH] replace nunjucks with handlebars for templating MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- package.json | 4 ++-- src/comments.js | 19 +++++++++++++------ src/template.js | 18 +++++++++--------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index f75bd45..ee23b7b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/comments.js b/src/comments.js index a15f5d0..9678ebb 100644 --- a/src/comments.js +++ b/src/comments.js @@ -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) { diff --git a/src/template.js b/src/template.js index 1ee1160..2c2fefd 100644 --- a/src/template.js +++ b/src/template.js @@ -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}} `