Skip to content

Commit

Permalink
Feat/add sonarqube GitHub (#46)
Browse files Browse the repository at this point in the history
* ci: modify github workflow to run sq analysis

* test: add example lambda tests

* ci: run sq analysis only when push to main

* fix: typo
  • Loading branch information
pawelTshDev authored Apr 8, 2024
1 parent 82940da commit baef6a3
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 19 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Serverless Boilerplate

on:
push:
branches:
- main

jobs:
tests:
runs-on: ubuntu-latest
name: Build
strategy:
matrix:
node-version: [20]

steps:
- uses: actions/checkout@v4
- run: cp .env.dist .env
- run: npm ci
- run: npm run lint
- run: npm run test-coverage
- name: Upload artifacts
uses: actions/upload-artifact@v2
with:
name: coverage
path: coverage/

sonarqube-analysis:
runs-on: ubuntu-latest
needs: tests
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Download artifact
uses: actions/download-artifact@v2
with:
name: coverage
path: coverage/
- name: SonarQube Scan
uses: sonarsource/sonarqube-scan-action@master
env:
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
7 changes: 5 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: Serverless Boilerplate

on: [push]
on:
push:
branches-ignore:
- main

jobs:
tests:
Expand All @@ -15,4 +18,4 @@ jobs:
- run: cp .env.dist .env
- run: npm ci
- run: npm run lint
# - run: npm run test
- run: npm run test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ We support ASL for Step Functions. Make sure to install AWS Toolkit so you can r

### SonarQube configuration

Before deployment make sure to create a new SonarQube project. After that set proper repository variables (`SONAR_TOKEN` and `SONAR_HOST_URL`) and properties in `sonar-project.properties` file.
Before deployment, please ensure that a related SonarQube project has been created. After that set proper repository variables (`SONAR_TOKEN` and `SONAR_HOST_URL`) and properties in `sonar-project.properties` file.

##

Expand Down
5 changes: 4 additions & 1 deletion bitbucket-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ definitions:
pipelines:
default:
- step: *compile
- step: *sonarqube-analysis

custom:
deploy-to-dev:
- step: *compile
Expand All @@ -43,6 +43,9 @@ pipelines:
name: Deploy to DEV

branches:
main:
- step: *compile
- step: *sonarqube-analysis
master:
- step: *compile
- step:
Expand Down
5 changes: 2 additions & 3 deletions functions/example-lambda/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ import { httpErrorHandlerConfigured } from "../../shared/middleware/http-error-h
import { createFindManyOptions, makePaginationResult } from "../../shared/pagination-utils/pagination-utils";
import { Like } from "typeorm";

const connectToDb = dataSource.initialize();
const config = createConfig(process.env);
const userRepository = dataSource.getRepository(ExampleModel);

const lambdaHandler = async (event: ExampleLambdaPayload) => {
export const lambdaHandler = async (event: ExampleLambdaPayload) => {
const queryParams = event.queryStringParameters;
winstonLogger.info(`Hello from ${config.appName}. Example param is: ${queryParams.exampleParam}`);

await connectToDb;
await dataSource.initialize();

const findOptions = createFindManyOptions(userRepository, queryParams);

Expand Down
91 changes: 81 additions & 10 deletions functions/example-lambda/tests/handler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,90 @@
import request from "supertest";
import sinon from "sinon";
import { DataSource, Repository } from "typeorm";
import { lambdaHandler } from "../handler";
import assert from "assert";
import { StatusCodes } from "http-status-codes";

describe("test endpoint", () => {
const server = request("http://localhost:1337/");
let sandbox: sinon.SinonSandbox;
let userRepositoryStub: sinon.SinonStub;
let dataSourceStub: sinon.SinonStub;
let metadataStub: sinon.SinonStub;

describe("Test obligatory query parameter", () => {
it("GET `dev/?exampleParam=test` returns 200", () => {
return server.get("dev/users").query("exampleParam=test").expect(200);
});
const data = [
{
id: "50ece88a-61ac-4435-8457-051693716276",
firstName: "John",
lastName: "Doe",
email: "[email protected]",
},
{
id: "8eb91b5f-63ef-4ca8-9db4-3fa6f173f1ae",
firstName: "Mark",
lastName: "Smith",
email: "[email protected]",
}
];

const pagination = {
page: 1,
limit: 3,
total: 2,
totalPages: 1
};

const params = {
exampleParam: "example",
page: "1",
limit: "3",
};

it("GET `dev` returns bad request", () => {
return server.get("dev/users").expect(400);
beforeEach(() => {
sandbox = sinon.createSandbox();
userRepositoryStub = sandbox.stub(Repository.prototype, "findAndCount").resolves([data, 2]);
dataSourceStub = sandbox.stub(DataSource.prototype, "initialize").resolves();
metadataStub = sandbox.stub(Repository.prototype, "metadata");
});

afterEach(() => {
sandbox.restore();
});

describe("GET dev/users", () => {
it("should return users", async () => {
metadataStub.value({
columns: [{ propertyName: "email", }, { propertyName: "firstName" }]
});
const filters = {
sort: { email: "ASC" },
filter: { firstName: "John" },
search: "Doe",
};

const response = await lambdaHandler({
queryStringParameters: { ...params, ...filters }
});

const parsedResponse = JSON.parse(response.body);
assert.equal(response.statusCode, StatusCodes.OK);
assert.deepEqual(parsedResponse, { meta: { pagination, ...filters }, data });
});

it("GET 'any' returns not found", () => {
return server.get("any").expect(404);
it("shouldn't return filters if they are not available", async () => {
metadataStub.value({
columns: [{ propertyName: "id", }, { propertyName: "lastName" }]
});

const response = await lambdaHandler({
queryStringParameters: {
...params,
sort: { email: "ASC", id: "ASC" },
filter: { firstName: "John", lastName: "Doe" },
}
});

const parsedResponse = JSON.parse(response.body);
assert.equal(response.statusCode, StatusCodes.OK);
assert.deepEqual(parsedResponse, { meta: { pagination, sort: { id: "ASC" }, filter: { lastName: "Doe" }}, data });
});
});
});
Loading

0 comments on commit baef6a3

Please sign in to comment.