Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE-36] Added coverage report workflow #37

Merged
merged 10 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/coverage_report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
on:
push:
branches:
- main

name: coverage report

jobs:
test-coverage:
name: Publish coverage report
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup sonarqube
uses: warchant/setup-sonar-scanner@v3
- name: coverage report
uses: actions/setup-node@v1
with:
# node 12 support is deprecated in sonar
node-version: 14
- run: npm ci
- run: npm run test:report
- run: sonar-scanner
-Dsonar.organization=kiegroup
-Dsonar.projectKey=chain-status
-Dsonar.projectName="Chain Status"
-Dsonar.sources=$GITHUB_WORKSPACE
-Dsonar.host.url=${{ secrets.SONAR_URL }}
-Dsonar.login=${{ secrets.SONAR_TOKEN }}
-Dsonar.tests=test
-Dsonar.testExecutionReportPaths=packages/action/test-report.xml
-Dsonar.javascript.lcov.reportPaths=coverage/lcov.info
-Dsonar.exclusions=packages/action/test/**,packages/action/webpage/**
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,12 @@ typings/
# TernJS port file
.tern-port

# Test reports
*/**/test-report.xml

# Default nvm node version
.nvmrc

packages/webpage/build/
.vscode/

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
],
"scripts": {
"test": "lerna run test",
"test:report": "lerna run test -- --coverage --testResultsProcessor=jest-sonar-reporter",
"build": "lerna run build -- --if-present",
"predeploy": "lerna run predeploy",
"deploy": "lerna run deploy",
Expand Down Expand Up @@ -38,6 +39,7 @@
"git-pre-hooks": "^1.2.1",
"jest": "^25.5.1",
"jest-expect-message": "^1.0.2",
"jest-sonar-reporter": "^2.0.0",
"lerna": "^4.0.0",
"prettier": "^2.0.5"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/action/dist/index.js

Large diffs are not rendered by default.

52 changes: 1 addition & 51 deletions packages/action/src/lib/common.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,17 @@
const util = require("util");
const core = require("@actions/core");

class ClientError extends Error {}

class TimeoutError extends Error {}

function log(prefix, obj) {
const str = obj.map(o => (typeof o === "object" ? inspect(o) : o));
if (prefix) {
console.log.apply(console, [prefix, ...str]);
} else {
console.log.apply(console, str);
}
}

const annotationer = {
notice: (title, content) => core.notice(content, { title: `${title}` }),
warning: (title, content) => core.warning(content, { title: `${title}` }),
error: (title, content) => core.error(content, { title: `${title}` })
};

const logger = {
level: "info",

trace: (...str) => {
if (logger.level === "trace") {
log("[TRACE] ", str);
}
},

debug: (...str) => {
if (logger.isDebug()) {
log("[DEBUG] ", str);
}
},

emptyLine: () => log("", []),
info: (...str) => log("[INFO] ", str),
warn: (...str) => log("[WARN] ", str),

error: (...str) => {
if (str.length === 1) {
if (str[0] instanceof Error) {
if (logger.isDebug()) {
log(null, [str[0].stack || str[0]]);
} else {
log("[ERROR] ", [str[0].message || str[0]]);
}
}
} else {
log("[ERROR] ", str);
}
},
isDebug: () => logger.level === "trace" || logger.level === "debug"
};

function inspect(obj) {
return util.inspect(obj, false, null, true);
}

module.exports = {
ClientError,
TimeoutError,
annotationer,
logger
annotationer
};
2 changes: 1 addition & 1 deletion packages/action/src/lib/github/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ async function main(args, outputFolderPath, metadata, skipZero, isDebug) {
metadata,
projects: skipZero
? pullRequestInformation.filter(
e => e.pullRequests && e.pullRequest.length > 0
e => e.pullRequests && e.pullRequests.length > 0
)
: pullRequestInformation
},
Expand Down
12 changes: 6 additions & 6 deletions packages/action/src/utils/file.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const { formatDate } = require("./date-util");
const fs = require("fs");
const path = require("path");

const saveFiles = (data, outputFolderPath, extesion = "json") => {
logger.info(`Writting files to ${outputFolderPath}`);
const saveFiles = (data, outputFolderPath, extension = "json") => {
logger.info(`Writing files to ${outputFolderPath}`);
if (!fs.existsSync(outputFolderPath)) {
logger.info(
`Directory '${outputFolderPath}' does not exist. Creating it...`
Expand All @@ -19,13 +19,13 @@ const saveFiles = (data, outputFolderPath, extesion = "json") => {

const todayFilePath = path.join(
outputFolderPath,
`${formatDate(new Date())}.${extesion}`
`${formatDate(new Date())}.${extension}`
);
logger.info(`Writting file to ${todayFilePath}`);
logger.info(`Writing file to ${todayFilePath}`);
fs.writeFileSync(todayFilePath, data);

const latestFilePath = path.join(outputFolderPath, `latest.${extesion}`);
logger.info(`Writting file to ${latestFilePath}`);
const latestFilePath = path.join(outputFolderPath, `latest.${extension}`);
logger.info(`Writing file to ${latestFilePath}`);
fs.writeFileSync(latestFilePath, data);
return { latest: latestFilePath, today: todayFilePath };
};
Expand Down
110 changes: 110 additions & 0 deletions packages/action/test/bin/main.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
jest.mock("fs");
const { existsSync, readFileSync, writeFileSync } = require("fs");
const { main } = require("../../src/bin/main");
const { main: executeGithub } = require("../../src/lib/github/main");
jest.mock("../../src/lib/github/main");
const { main: executeJenkins } = require("../../src/lib/jenkins/main");
jest.mock("../../src/lib/jenkins/main");

afterEach(() => {
jest.clearAllMocks();
});

const defaultArgs = {
title: "Title",
subtitle: "Subtitle",
outputFolderPath: "/tmp/",
debug: true,
jenkinsUrl: undefined
};

describe("github main", () => {
test("branching github flow", async () => {
// since args.jenkinsUrl is undefined we will go in the github flow
await main({ ...defaultArgs });
expect(executeJenkins).toBeCalledTimes(0);

expect(executeGithub).toBeCalledTimes(1);
expect(executeGithub).toHaveBeenCalledWith(
expect.objectContaining({
title: "Title"
}),
"/tmp/title",
expect.objectContaining({
title: "Title",
subtitle: "Subtitle",
createdBy: undefined,
createdUrl: undefined
}),
undefined,
true
);
});

test("append new project status", async () => {
// simulate a product.json file already exists
existsSync.mockReturnValueOnce(true);
readFileSync.mockReturnValueOnce(
'{"projectStatuses": [{"id": "kiegroup-status", "name": "Kiegroup Status","folder": "kiegroup-status","date": 1660748832737}]}'
);

await main({ ...defaultArgs });

expect(executeGithub).toBeCalledTimes(1);
expect(writeFileSync).toBeCalledTimes(1);
// TODO: fix
// expect(writeFileSync).toHaveBeenCalledWith(
// "/tmp/product.json",
// JSON.stringify({
// projectStatuses: [
// {
// id: "kiegroup-status",
// name: "Kiegroup Status",
// folder: "kiegroup-status",
// date: 1660748832737
// },
// {
// id: "title",
// name: "Title",
// folder: "title",
// date: 1660915702755
// }
// ]
// }, null, 4)
// );
});
});

describe("jenkins main", () => {
test("", async () => {
// since args.jenkinsUrl is not undefined we will go in the jenkins flow
await main({
...defaultArgs,
...{
jenkinsUrl: "jenkins-url",
jobUrl: "job-url",
certFilePath: "cert-file-path"
}
});
expect(executeGithub).toBeCalledTimes(0);

expect(executeJenkins).toBeCalledTimes(1);
expect(executeJenkins).toHaveBeenCalledWith(
"jenkins-url",
"job-url",
undefined,
"cert-file-path",
"/tmp/title",
expect.objectContaining({
title: "Title",
subtitle: "Subtitle",
createdBy: undefined,
createdUrl: undefined
}),
undefined,
true
);
expect(existsSync).toBeCalledTimes(1);
expect(readFileSync).toBeCalledTimes(0);
});
});
44 changes: 44 additions & 0 deletions packages/action/test/lib/common.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const core = require("@actions/core");
jest.mock("@actions/core");
const { annotationer } = require("../../src/lib/common");

afterEach(() => {
jest.clearAllMocks();
});

const content = "logging content";

describe("annotationer", () => {
test("notice", () => {
annotationer.notice("This is the title", content);

expect(core.notice).toBeCalledWith(
content,
expect.objectContaining({
title: "This is the title"
})
);
});

test("warning", () => {
annotationer.warning("This is the title", content);

expect(core.warning).toBeCalledWith(
content,
expect.objectContaining({
title: "This is the title"
})
);
});

test("error", () => {
annotationer.error("This is the title", content);

expect(core.error).toBeCalledWith(
content,
expect.objectContaining({
title: "This is the title"
})
);
});
});
Loading