From 63d39fcb87fadd4370ce43f94b33e94b1f434eb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Tue, 15 Jan 2019 22:56:56 +0100 Subject: [PATCH] add folding of builds when there are more than 3 commits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- src/comments.js | 28 ++++++++++++++++++++++++---- src/template.js | 23 +++++++++++++++++------ test/comments.js | 29 +++++++++++++++++++++++++++-- test/sample.js | 20 ++++++++++++++++++++ 4 files changed, 88 insertions(+), 12 deletions(-) diff --git a/src/comments.js b/src/comments.js index bd7e168..b0912df 100644 --- a/src/comments.js +++ b/src/comments.js @@ -2,8 +2,8 @@ const log = require('loglevel') const Handlebars = require('handlebars') const template = require('./template') -/* in theory the jenkins build comment should be the first one */ -const PER_PAGE = 100 +/* how many builds to show without folding */ +const VIS_BUILDS = 3 const COMMENT_REGEX = /\#\#\# Jenkins Builds\n/ /* loop helper compares commits of build and previous build */ @@ -23,6 +23,21 @@ const dateHelper = (data) => { ) } +/* adds archive attribute to builds to mark for folding in template */ +const extractArchiveBuilds = (builds) => { + /* get unique commits */ + const commits = [...new Set(builds.map(b=>b.commit))] + /* if there's not too many don't archive any */ + if (commits.length < VIS_BUILDS) { + return {visible: builds, archived: []} + } + /* split builds into visible and archived */ + const archivedCommits = commits.slice(0, -(VIS_BUILDS-1)) + const archived = builds.filter(b => archivedCommits.includes(b.commit)) + const visible = builds.slice(archived.length) + return {visible, archived} +} + class Comments { constructor(client, owner, repo, builds) { this.gh = client @@ -33,8 +48,11 @@ class Comments { Handlebars.registerHelper('date', dateHelper) /* add helper for checking change in commit */ Handlebars.registerHelper('commitChanged', commitHelper) + /* add partis */ + Object.keys(template.partials).forEach(k=> + Handlebars.registerPartial(k, template.partials[k]) + ) /* setup templating for comments */ - Handlebars.registerPartial('build', template.build) this.template = Handlebars.compile(template.main); } @@ -43,7 +61,9 @@ class Comments { if (builds.length == 0) { throw Error('No builds exist for this PR') } - return this.template({builds}) + /* split to be able to fold if there are too many builds */ + const {visible, archived} = extractArchiveBuilds(builds) + return this.template({visible, archived}) } async postComment (pr) { diff --git a/src/template.js b/src/template.js index ba3895e..3fa56e0 100644 --- a/src/template.js +++ b/src/template.js @@ -1,19 +1,30 @@ const main = ` ### Jenkins Builds +{{#if archived.length }} +
+Click to see older builds + +{{> buildsTable archived }} +
+ +{{/if}} +{{> buildsTable visible }} +` +const buildsTable = ` | :grey_question: | Commit | :hash: | Finished (UTC) | Duration | Platform | Result | |-|-|-|-|-|-|-| -{{#each builds}} -{{#commitChanged ../builds @index}} +{{#each this}} +{{#commitChanged ../this @index}} | | | | | | | | {{/commitChanged}} -{{> build }} +{{> buildRow }} {{/each}} -` -const build = ` +`.trim() +const buildRow = ` {{#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.url }}consoleText) | {{/if}} `.trim() -module.exports = { main, build } +module.exports = { main, partials: {buildRow, buildsTable} } diff --git a/test/comments.js b/test/comments.js index de185b0..b304477 100644 --- a/test/comments.js +++ b/test/comments.js @@ -17,6 +17,25 @@ const COMMENT = ` | :x: | COMMIT-2 | [ID-2](URL-2) | 2018-12-20 08:45:28 | DURATION-2 | \`PLATFORM-2\` | [:page_facing_up: build log](URL-2consoleText) | ` +const COMMENT_FOLDED = ` +### Jenkins Builds +
+Click to see older builds + +| :grey_question: | Commit | :hash: | Finished (UTC) | Duration | Platform | Result | +|-|-|-|-|-|-|-| +| :heavy_check_mark: | COMMIT-1 | [ID-1](URL-1) | 2018-12-20 08:26:33 | DURATION-1 | \`PLATFORM-1\` | [:package: package](PKG_URL-1) | +| | | | | | | | +| :x: | COMMIT-2 | [ID-2](URL-2) | 2018-12-20 08:45:28 | DURATION-2 | \`PLATFORM-2\` | [:page_facing_up: build log](URL-2consoleText) | +
+ +| :grey_question: | Commit | :hash: | Finished (UTC) | Duration | Platform | Result | +|-|-|-|-|-|-|-| +| :heavy_check_mark: | COMMIT-3 | [ID-3](URL-3) | 2018-12-20 08:26:32 | DURATION-3 | \`PLATFORM-3\` | [:package: package](PKG_URL-3) | +| | | | | | | | +| :x: | COMMIT-4 | [ID-4](URL-4) | 2018-12-20 08:45:23 | DURATION-4 | \`PLATFORM-4\` | [:page_facing_up: build log](URL-4consoleText) | +` + describe('Comments', () => { beforeEach(() => { client = { @@ -26,7 +45,7 @@ describe('Comments', () => { }, } builds = sinon.createStubInstance(Builds, { - getBuilds: sample.BUILDS, + getBuilds: sample.BUILDS.slice(0, 2), }) comments = new Comments(client, 'owner', 'repo', builds) }) @@ -37,10 +56,16 @@ describe('Comments', () => { expect(comments.renderComment('PR-ID')).rejectedWith('No builds exist for this PR') }) - it('should render correctly', async () => { + it('should render less than 3 comments fully', async () => { let body = await comments.renderComment('PR-ID') expect(body).to.eq(COMMENT) }) + + it('should render more than 3 comments folded', async () => { + builds.getBuilds.returns(sample.BUILDS) + let body = await comments.renderComment('PR-ID') + expect(body).to.eq(COMMENT_FOLDED) + }) }) describe('postComment', () => { diff --git a/test/sample.js b/test/sample.js index 9fe975c..52db64c 100644 --- a/test/sample.js +++ b/test/sample.js @@ -30,6 +30,26 @@ const BUILDS = [ pkg_url: 'PKG_URL-2', meta: { created: 1545295528896 }, }, + { + id: 'ID-3', + commit: 'COMMIT-3', + success: true, + platform: 'PLATFORM-3', + duration: 'DURATION-3', + url: 'URL-3', + pkg_url: 'PKG_URL-3', + meta: { created: 1545294392930 }, + }, + { + id: 'ID-4', + commit: 'COMMIT-4', + success: false, + platform: 'PLATFORM-4', + duration: 'DURATION-4', + url: 'URL-4', + pkg_url: 'PKG_URL-4', + meta: { created: 1545295523333 }, + }, ] const COMMENTS = [