Skip to content

Commit

Permalink
Implemented PAC validation (#459)
Browse files Browse the repository at this point in the history
* Updated webpack-plugin

* Updated changelog

* Implemented PAC validation, fixed wrong path to LocalizationLoader, updated template

* Fixed tests

* updated deprecation log

* Migrated to typescript, reworked PAC validation; Updated package command logic

* Updated new, info, start commands; Updated all templates to use eslint and satisfy all PAC requirements

* updated commander.js, removed lib files,

* added lib to ignore

* Fixed tests

* Fixed naming, Implemented unit tests

* Fixed errorMessage

* test the files in repo

* test workspace

* fix path to file

* Attempt to find out the correct path to files

* Attempt to find out the correct path to files #2

* New way to build path to file using new URL

* Try to import file as json

* Updated import in pbivizNew

* Fixed one import of config.json

* Fixed name of the folder

* Removed wrong pushed files

* Reverted new way for building path to root files

* Decreased node version in build pipeline

* Decreased node version to 12x in build pipeline

* added missing @typescript-eslint/parser

* Updated actions/checkout

* Deleted redundant part of path to config

* Fixed path in case of Linux

* Fixed case sensitive test, removed Math.random

* Removed imports with type assertion

* Fixed options in start command

* Updated packages, removed redundant changes, updated changelog

* Fixed package-lock file and basic commands

* Fixed feature types, resolved review comments

* Changed way to compare objects in Package instance

* removed redundant changes

* Removed source from BaseFeature

* Declared errorMessage as getter

* Updated features' types

* Changed way to chose the visual type

* Updated package.json

---------

Co-authored-by: AleksSavelev <[email protected]>
  • Loading branch information
AleksSavelev and AleksSavelev authored Aug 22, 2023
1 parent 3151ab4 commit 29075f2
Show file tree
Hide file tree
Showing 45 changed files with 893 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ jobs:
node-version: [18.x]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm run lint
- run: npm run build --if-present
- run: npm test
env:
CI: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
node_modules
lib
*.log
/lib
spec/.tmp
/certs
/testGround
4 changes: 0 additions & 4 deletions .snyk

This file was deleted.

2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"request": "launch",
"name": "Launch Program",
"cwd": "${workspaceRoot}",
"program": "${workspaceFolder}\\bin\\pbiviz-new.js",
"program": "${workspaceFolder\bin\\pbiviz-new.js",
"args": [
"-t",
"circlecard",
Expand Down
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

This page contains information about changes to the PowerBI Visual Tools (pbiviz).

## 5.2.0
* Integrated PAC validation

## 5.1.0
* New flag `--skip-api` to skip verifying api version. It might produce different errors in visual, so use it only in some specific cases (ex. installing something during the build process brakes packages managed by monorepo managers).
* New flag `--all-locales` to disable optimization using localization loader. It's recommended not to use this flag because all locales take a huge amount of package size. If you need just a few of them follow [this guide](https://learn.microsoft.com/en-us/power-bi/developer/visuals/localization?tabs=English#step-5---add-a-resources-file-for-each-language). In this case, only declared in stringResources locales will be added to your visual package.
Expand Down
2 changes: 1 addition & 1 deletion bin/pbiviz.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ pbiviz
CommandManager.package(options, rootPath);
});

program.parse(process.argv);
program.parse(process.argv);
10 changes: 8 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "powerbi-visuals-tools",
"version": "5.1.0",
"version": "5.2.0-beta.1",
"description": "Command line tool for creating and publishing visuals for Power BI",
"main": "./bin/pbiviz.js",
"type": "module",
Expand Down Expand Up @@ -54,6 +54,7 @@
"lodash.clonedeep": "4.5.0",
"lodash.defaults": "4.2.0",
"lodash.isequal": "4.5.0",
"lodash.ismatch": "^4.4.0",
"mini-css-extract-plugin": "^2.7.6",
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
Expand Down
115 changes: 115 additions & 0 deletions spec/unit/FeatureManagerSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Power BI Visual CLI
*
* Copyright (c) Microsoft Corporation
* All rights reserved.
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the ""Software""), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

"use strict";

import * as features from '../../lib/features/index.js';
import Package from '../../lib/Package.js';
import { Stage, VisualFeatureType } from '../../lib/features/FeatureTypes.js';
import { readJsonFromRoot } from '../../lib/utils.js';

const config = readJsonFromRoot('config.json');

describe("Features", () => {
describe("Visual", () => {
const { APIVersion, ESLint, VisualVersion } = features;
it("Should support API Version", () => {
const Visual = {
doesAPIVersionMatch: (minVersion) => {
expect(minVersion).toBe(config.constants.minAPIversion);
return true;
}
}
expect(APIVersion.isSupported(Visual)).toBeTrue;
});

it("Should support ESLint", () => {
const Visual = {
doesESLlintSupported: () => true
}
expect(ESLint.isSupported(Visual)).toBeTrue;
});

it("Should support Version", () => {
const Visual = {
isVisualVersionValid: (versionLength) => {
expect(versionLength).toBe(4);
return true;
}
}
expect(VisualVersion.isSupported(Visual)).toBeTrue;
});
});

describe("Package", () => {
const featuresArray = Object.keys(features).filter(key => features[key].stage === Stage.PostBuild).map(key => features[key]);

it("Should support features with correct sources", () => {
const sourceCode = `.allowInteractions, .applySelectionFromFilter or .registerOnSelectCallback, .colorPalette,
.createDataViewWildcardSelector, .showContextMenu, .downloadService and .exportVisualsContent,
getFormattingModel, .isHighContrast, .launchUrl, .createLocalizationManager, .storageService, .openModalDialog,
.eventService and .renderingStarted and .renderingFinished, tooltipService, .displayWarningIcon`
const capabilities = {
advancedEditMode: 1,
supportsHighlight: true,
supportsKeyboardFocus: true,
supportsLandingPage: true,
supportsMultiVisualSelection: true,
supportsSynchronizingFilterState: true,
subtotals: true,
tooltips: {},
objects: {
objectCategory: 2
},
drilldown: {
roles: []
},
dataViewMappings: [
{
table: {
rows: {
dataReductionAlgorithm: {}
}
}
}
]
}
const correctPackage = new Package(sourceCode, capabilities, VisualFeatureType.All);

featuresArray.forEach(feature => {
expect(feature.isSupported(correctPackage)).toBeTrue;
})
});

it("Should not support features with empty sources", () => {
const emptyPackage = new Package('', {}, VisualFeatureType.All);

featuresArray.forEach(feature => {
expect(feature.isSupported(emptyPackage)).toBeFalse;
})
});
});
});
2 changes: 2 additions & 0 deletions src/CommandManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default class CommandManager {
const visualManager = new VisualManager(rootPath)
await visualManager
.prepareVisual()
.validateVisual()
.initializeWebpack(webpackOptions)
visualManager.startWebpackServer(options.drop)
}
Expand All @@ -68,6 +69,7 @@ export default class CommandManager {
}
new VisualManager(rootPath)
.prepareVisual()
.validateVisual()
.initializeWebpack(webpackOptions)
.then(visualManager => visualManager.generatePackage())
}
Expand Down
58 changes: 58 additions & 0 deletions src/FeatureManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Severity, Stage } from "./features/FeatureTypes.js";
import * as features from "./features/index.js";
import { Visual } from "./Visual.js";
import Package from "./Package.js";

export enum Status {
Success,
Error
}
export interface ValidationStats {
status: Status,
logs: Logs
}

export interface Logs {
errors: string[],
warnings: string[],
info: string[],
deprecation: string[]
}

export class FeatureManager {
public features = Object.keys(features).map(key => features[key]);

public validate(stage: Stage, sourceInstance: Visual | Package): ValidationStats {
const result: ValidationStats = {
status: Status.Success,
logs: {
errors: [],
warnings: [],
info: [],
deprecation: []
}
}
this.features
.filter(feature => feature.stage == stage)
.filter(feature => feature.visualFeatureType & sourceInstance.visualFeatureType)
.filter(feature => !feature.isSupported(sourceInstance))
.forEach(({ errorMessage, severity }) => {
switch(severity) {
case Severity.Error:
result.status = Status.Error;
result.logs.errors.push(errorMessage);
break;
case Severity.Warning:
result.logs.warnings.push(errorMessage);
break;
case Severity.Info:
result.logs.info.push(errorMessage);
break;
case Severity.Deprecation:
result.logs.deprecation.push(errorMessage);
break;
}
});
return result
}
}
53 changes: 53 additions & 0 deletions src/Package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Power BI Visual CLI
*
* Copyright (c) Microsoft Corporation
* All rights reserved.
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the ""Software""), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

"use strict";

import { VisualFeatureType } from "./features/FeatureTypes.js";
import isMatch from "lodash.ismatch";

/**
* Represents an instance of a visual package based on file path
*/
export default class Package {
private sourceCode: string;
private capabilities: object;
public visualFeatureType: VisualFeatureType;

constructor(sourceCode: string, capabilities: object, visualFeatureType: VisualFeatureType) {
this.sourceCode = sourceCode;
this.capabilities = capabilities;
this.visualFeatureType = visualFeatureType;
}

public contain(keyword: string) {
return this.sourceCode.includes(keyword);
}

public isCapabilityEnabled(expectedObject: object) {
return isMatch(this.capabilities, expectedObject);
}
}
40 changes: 40 additions & 0 deletions src/Visual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { compareVersions } from "compare-versions";
import { VisualFeatureType } from "./features/FeatureTypes.js";

export class Visual {
public visualFeatureType: VisualFeatureType;
private capabilities;
private config;
private packageJSON;
private visualVersion: string;

constructor(capabilities, config, packageJson) {
this.capabilities = capabilities;
this.config = config;
this.visualFeatureType = this.getVisualFeatureType();
this.packageJSON = packageJson;
this.visualVersion = config.visual.version;
}

public doesAPIVersionMatch(minAPIversion: string) {
return compareVersions(this.config.apiVersion ?? minAPIversion, minAPIversion) !== -1
}

public doesESLlintSupported() {
return Object.entries(this.packageJSON.scripts).some(([, value]) => (<string>value).includes("eslint"))
}

public isVisualVersionValid(length: number) {
return this.visualVersion.split(".").length === length
}

private getVisualFeatureType() {
const isMatrixSupported = this.capabilities?.dataViewMappings?.some(dataView => dataView.matrix)
const isSlicer = Boolean(this.capabilities?.objects?.general?.properties?.filter?.type?.filter)
let type = isSlicer ? VisualFeatureType.Slicer : VisualFeatureType.NonSlicer;
if (isMatrixSupported) {
type = type | VisualFeatureType.Matrix;
}
return type;
}
}
Loading

0 comments on commit 29075f2

Please sign in to comment.