Skip to content

Commit

Permalink
linting actions added (openshift-knative#48)
Browse files Browse the repository at this point in the history
* added catalog api pagination(openshift-knative#28)

* added test case & initialized cursor(openshift-knative#28)

* changed cursor into local variable(openshift-knative#28)

* introduced query limit constant & fixed tests (openshift-knative#28)

* test case added (openshift-knative#28)

* pagination test case extended

* linting actions added in pr&release steps (openshift-knative#32)

* fixed path (openshift-knative#32)

* checkout added (openshift-knative#32)

* dependency action added (openshift-knative#32)

* refactoring for lint issues (openshift-knative#32)

* refactoring for lint issues (openshift-knative#32)

* revert package json changes (openshift-knative#32)

* fixed linting issues (openshift-knative#32)

* fixed linting (openshift-knative#32)
  • Loading branch information
ahmetcihank authored Apr 1, 2024
1 parent 50d548a commit c42d396
Show file tree
Hide file tree
Showing 7 changed files with 808 additions and 65 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/backstage-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Backstage lint

on:
pull_request:
branches:
- main

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: 'backstage/.nvmrc'
cache: 'yarn'
cache-dependency-path: 'backstage/yarn.lock'

- name: Print environment
run: |
node --version
yarn --version
- name: Install dependencies
shell: bash
working-directory: ./backstage
run: yarn --prefer-offline --frozen-lockfile

- name: Lint all code
shell: bash
working-directory: ./backstage
run: yarn backstage-cli repo lint
5 changes: 5 additions & 0 deletions .github/workflows/backstage-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ jobs:
working-directory: ./backstage
run: yarn --prefer-offline --frozen-lockfile

- name: lint all code
shell: bash
working-directory: ./backstage
run: yarn backstage-cli repo lint

- name: Install tooling
shell: bash
working-directory: ./backstage
Expand Down
11 changes: 8 additions & 3 deletions backstage/plugins/knative-event-mesh-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,24 @@
},
"dependencies": {
"@backstage/backend-common": "^0.19.9",
"@backstage/backend-tasks": "^0.5.21",
"@backstage/config": "^1.1.1",
"@backstage/plugin-catalog-common": "^1.0.22",
"@backstage/catalog-client": "^1.6.3",
"@backstage/catalog-model": "^1.4.5",
"@backstage/plugin-catalog-node": "^1.11.0",
"@types/express": "*",
"express": "^4.17.1",
"express-promise-router": "^4.1.0",
"winston": "^3.2.1",
"node-fetch": "^2.6.7",
"winston": "^3.2.1",
"yn": "^4.0.0"
},
"devDependencies": {
"@backstage/cli": "^0.24.0",
"@types/supertest": "^2.0.12",
"supertest": "^6.2.4",
"msw": "^1.0.0"
"msw": "^1.0.0",
"supertest": "^6.2.4"
},
"files": [
"dist"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {getVoidLogger} from '@backstage/backend-common';
import {CatalogClient} from "@backstage/catalog-client";
import {ApiEntity, Entity} from "@backstage/catalog-model";
import {CatalogProcessorRelationResult} from "@backstage/plugin-catalog-node";
import {CatalogClient} from '@backstage/catalog-client';
import {ApiEntity, Entity} from '@backstage/catalog-model';
import {CatalogProcessorRelationResult} from '@backstage/plugin-catalog-node';
import {KnativeEventMeshProcessor} from "./knativeEventMeshProcessor";

// there must be a better way to do this
const catalogApi = <any>{
const catalogApi = ({
queryEntities: jest.fn(),
} as jest.Mocked<CatalogClient>;
} as any) as jest.Mocked<CatalogClient>;

beforeEach(() => {
catalogApi.queryEntities.mockClear();
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('KnativeEventMeshProcessor', () => {
};
}

type TestCase = {
interface TestCase {
name:string;
entity:ApiEntity;
queries?:Query[];
Expand Down Expand Up @@ -430,44 +430,36 @@ describe('KnativeEventMeshProcessor', () => {
}
];

for (const testCase of testCases) {
it(testCase.name, async () => {
if (testCase.queries) {
for (const query of testCase.queries) {
let entityQueryResult = {
items: query.queryEntitiesResult.items,
totalItems: query.queryEntitiesResult.items.length,
pageInfo: query.queryEntitiesResult.pageInfo
};
catalogApi.queryEntities.mockReturnValueOnce(Promise.resolve(entityQueryResult));
}
test.each(testCases)('Name: %s', async ({name, queries, expectedRelations, entity}) => {
if (queries) {
for (const query of queries) {
const entityQueryResult = {
items: query.queryEntitiesResult.items,
totalItems: query.queryEntitiesResult.items.length,
pageInfo: query.queryEntitiesResult.pageInfo
};
catalogApi.queryEntities.mockReturnValueOnce(Promise.resolve(entityQueryResult));
}
}

const emitFn = jest.fn();
const emitFn = jest.fn();

let output = await processor.preProcessEntity(testCase.entity, <any>{}, emitFn, <any>{}, <any>{});
const output = await processor.preProcessEntity(entity, ({} as any), emitFn, ({} as any), ({} as any));

if (!testCase.expectedRelations) {
expect(emitFn).not.toHaveBeenCalled();
} else {
expect(emitFn).toHaveBeenCalledTimes(testCase.expectedRelations.length);
for (let i = 0; i < testCase.expectedRelations.length; i++) {
const relation = testCase.expectedRelations[i];
expect(emitFn).toHaveBeenNthCalledWith(i + 1, relation);
}
}
expect(emitFn).toHaveBeenCalledTimes(expectedRelations?.length || 0);

expect(output).toEqual(testCase.entity);
expectedRelations?.forEach((relation, index) => {
expect(emitFn).toHaveBeenNthCalledWith(index + 1, relation);
});

if (testCase.queries) {
expect(catalogApi.queryEntities).toHaveBeenCalledTimes(testCase.queries.length);
for (const query of testCase.queries) {
expect(catalogApi.queryEntities).toHaveBeenCalledWith(query.queryEntitiesRequest);
}
} else {
expect(catalogApi.queryEntities).not.toHaveBeenCalled();
}
expect(output).toEqual(entity);

expect(catalogApi.queryEntities).toHaveBeenCalledTimes(queries?.length || 0);

queries?.forEach(query => {
expect(catalogApi.queryEntities).toHaveBeenCalledWith(query.queryEntitiesRequest);
});
}

});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class KnativeEventMeshProcessor implements CatalogProcessor {

try {
do {
let response = await this.catalogApi.queryEntities({
const response = await this.catalogApi.queryEntities({
filter: {
kind: 'component',
'metadata.namespace': namespace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('KnativeEventMeshProvider', () => {
schedule: undefined,
};

const provider = new KnativeEventMeshProvider(providerConfig, logger, <any>null);
const provider = new KnativeEventMeshProvider(providerConfig, logger, (null as any));

type TestCase = {
name:string;
Expand Down Expand Up @@ -116,12 +116,10 @@ describe('KnativeEventMeshProvider', () => {
}
];

for (const testCase of testCases) {
it(testCase.name, async () => {
const result = provider.buildEventTypeEntity(testCase.input);
expect(result).toEqual(testCase.expected);
});
}
test.each(testCases)('Name: %s', async ({name,input, expected}) => {
const result = provider.buildEventTypeEntity(input);
expect(result).toEqual(expected);
});
});

describe('buildBrokerEntity', () => {
Expand All @@ -131,7 +129,7 @@ describe('KnativeEventMeshProvider', () => {
schedule: undefined,
};

const provider = new KnativeEventMeshProvider(providerConfig, logger, <any>null);
const provider = new KnativeEventMeshProvider(providerConfig, logger, (null as any));

type TestCase = {
name:string;
Expand Down Expand Up @@ -214,11 +212,10 @@ describe('KnativeEventMeshProvider', () => {
}
];

for (const testCase of testCases) {
it(testCase.name, async () => {
const result = provider.buildBrokerEntity(testCase.input);
expect(result).toEqual(testCase.expected);
});
}
test.each(testCases)('Name: %s', async ({name,input, expected}) => {
const result = provider.buildBrokerEntity(input);
expect(result).toEqual(expected);
});

});
});
Loading

0 comments on commit c42d396

Please sign in to comment.