From 44062b6548f503e8e94db7d040ba9e40150226ac Mon Sep 17 00:00:00 2001 From: error418 Date: Thu, 19 Sep 2019 21:00:40 +0200 Subject: [PATCH] fix(ui): correctly display metadata --- src/core/github/commit-status-sender.ts | 2 +- src/core/pages/page-routes.ts | 9 +++++- src/nebula/status-emitter.ts | 25 ++++++++++------ src/util.ts | 20 +++++++++++++ swingletree.conf.yaml | 2 +- test/util.spec.ts | 38 +++++++++++++++++++++++++ views/builds.pug | 5 ++-- 7 files changed, 88 insertions(+), 13 deletions(-) create mode 100644 src/util.ts create mode 100644 test/util.spec.ts diff --git a/src/core/github/commit-status-sender.ts b/src/core/github/commit-status-sender.ts index 114cfeb1..a4d0bab3 100644 --- a/src/core/github/commit-status-sender.ts +++ b/src/core/github/commit-status-sender.ts @@ -81,7 +81,7 @@ class CommitStatusSender { name: event.payload.sender, output: { title: event.payload.title, - summary: event.markdown || event.payload.shortMessage + summary: event.markdown || event.payload.shortMessage || "" } }; diff --git a/src/core/pages/page-routes.ts b/src/core/pages/page-routes.ts index 25ca9538..0e80d049 100644 --- a/src/core/pages/page-routes.ts +++ b/src/core/pages/page-routes.ts @@ -9,7 +9,7 @@ import HealthService, { HealthState } from "../health-service"; import { CoreConfig } from "../core-config"; import { HistoryService } from "../history/history-service"; import { LOGGER } from "../../logger"; -import { query } from "winston"; +import { SwingletreeUtil } from "../../util"; @injectable() class PageRoutes { @@ -93,6 +93,12 @@ class PageRoutes { }); } + private flatten(object: any) { + const result = SwingletreeUtil.flattenObject(object); + console.log(JSON.stringify(result, null, 2)); + return result; + } + public getRoute(): Router { const router = Router(); @@ -102,6 +108,7 @@ class PageRoutes { res.locals.healthStates = this.healthService.getStates(HealthState.DOWN); res.locals.isBuildHistoryEnabled = this.isBuildHistoryEnabled; res.locals.path = req.path; + res.locals.flatten = this.flatten; res.locals.componentIcon = this.componentIcon; res.locals.moment = require("moment"); diff --git a/src/nebula/status-emitter.ts b/src/nebula/status-emitter.ts index f2fdc340..919dfa34 100644 --- a/src/nebula/status-emitter.ts +++ b/src/nebula/status-emitter.ts @@ -4,7 +4,6 @@ import { NebulaConfig } from "./config"; import EventBus from "../core/event/event-bus"; import { NotificationEvent } from "../core/event/event-model"; import { Swingletree } from "../core/model"; -import { TemplateEngine, Templates } from "../core/template/template-engine"; import { NebulaEvents } from "./events"; import { NebulaModel } from "./model"; @@ -12,16 +11,13 @@ import { NebulaModel } from "./model"; @injectable() export class NebulaStatusEmitter { private readonly eventBus: EventBus; - private readonly templateEngine: TemplateEngine; private readonly context: string; constructor( @inject(EventBus) eventBus: EventBus, - @inject(ConfigurationService) configurationService: ConfigurationService, - @inject(TemplateEngine) templateEngine: TemplateEngine + @inject(ConfigurationService) configurationService: ConfigurationService ) { this.eventBus = eventBus; - this.templateEngine = templateEngine; this.context = configurationService.get(NebulaConfig.CONTEXT); eventBus.register(NebulaEvents.EventType.REPORT_RECEIVED, this.reportReceivedHandler, this); @@ -30,20 +26,33 @@ export class NebulaStatusEmitter { public getAnnotations(report: NebulaModel.Report): Swingletree.Annotation[] { const annotations: Swingletree.Annotation[] = []; - // TODO: implement - return annotations; } public reportReceivedHandler(event: NebulaEvents.ReportReceivedEvent) { const annotations = this.getAnnotations(event.report); + const build = event.report.payload.build; const notificationData: Swingletree.AnalysisReport = { sender: this.context, source: event.source, checkStatus: event.report.payload.build.result.status == NebulaModel.ResultValue.SUCCESS ? Swingletree.Conclusion.PASSED : Swingletree.Conclusion.BLOCKED, - title: `Gradle Build`, + title: `${event.report.payload.build.testCount} Tests`, metadata: { + project: build.project, + java: { + version: build.info.javaVersion, + detailVersion: build.info.detailedJavaVersion + }, + gradle: { + version: build.info.build.gradle.version + }, + build: { + elapsedTime: build.elapsedTime + }, + test: { + count: build.testCount + } }, annotations: annotations }; diff --git a/src/util.ts b/src/util.ts new file mode 100644 index 00000000..17ee3dec --- /dev/null +++ b/src/util.ts @@ -0,0 +1,20 @@ +export class SwingletreeUtil { + public static flattenObject(obj: any) { + const segment: any = {}; + + for (const i in obj) { + if (!obj.hasOwnProperty(i)) continue; + + if (obj[i] instanceof Object && obj[i] !== null) { + const flatObject = this.flattenObject(obj[i]); + for (const x in flatObject) { + if (!flatObject.hasOwnProperty(x)) continue; + segment[i + "." + x] = flatObject[x]; + } + } else { + segment[i] = obj[i]; + } + } + return segment; + } +} \ No newline at end of file diff --git a/swingletree.conf.yaml b/swingletree.conf.yaml index c75a0b39..12408c0b 100644 --- a/swingletree.conf.yaml +++ b/swingletree.conf.yaml @@ -28,7 +28,7 @@ github: nebula: enabled: false secret: # basic auth password protecting the webhook - context: gradle/zap # defines the check status name + context: gradle/nebula # defines the check status name debug: false # log zap webhook event requests on debug level # SonarQube specific configuration diff --git a/test/util.spec.ts b/test/util.spec.ts new file mode 100644 index 00000000..9c6e8dc9 --- /dev/null +++ b/test/util.spec.ts @@ -0,0 +1,38 @@ +"use strict"; + +import { suite, test, describe } from "mocha"; +import { expect, assert } from "chai"; +import * as chai from "chai"; +import * as sinon from "sinon"; +import { SwingletreeUtil } from "../src/util"; + +chai.use(require("sinon-chai")); + +const sandbox = sinon.createSandbox(); + +describe("ConfigurationService", () => { + + describe("Utilities", () => { + it("should flatten objects", () => { + const testObj = { + a: { + b: "c", + d: { + e: "f", + g: "h", + array: [ 1, 2, 3 ] + } + } + }; + + const result = SwingletreeUtil.flattenObject(testObj); + + expect(result["a.d.e"]).to.be.equal("f"); + expect(result["a.d.g"]).to.be.equal("h"); + expect(result["a.b"]).to.be.equal("c"); + expect(result["a.d.array.0"]).to.be.equal(1); + expect(result["a.d.array.1"]).to.be.equal(2); + expect(result["a.d.array.2"]).to.be.equal(3); + }); + }); +}); \ No newline at end of file diff --git a/views/builds.pug b/views/builds.pug index 3580b9ba..6ab16ecb 100644 --- a/views/builds.pug +++ b/views/builds.pug @@ -62,10 +62,11 @@ block content if item.metadata table(class="table table-sm build-list") tbody - each key, index in Object.keys(item.metadata) + -var flatMetadata = flatten(item.metadata) + each key in Object.keys(flatMetadata) tr td=key - td=item.metadata[key] + td=flatMetadata[key] else span None div.col-md-7