Skip to content

feat: get base branch metrics from cloud #155

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

Merged
merged 8 commits into from
Mar 20, 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
79 changes: 11 additions & 68 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,10 @@ on:
pull_request:

jobs:
# checks:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - name: Use Node.js v16.11.0
# uses: actions/setup-node@v1
# with:
# node-version: v16.11.0

# - name: Install dependencies
# run: yarn

# - name: Run linting checks
# run: yarn lint

# - name: Run formatting checks
# run: yarn format-check

# - name: Install dependencies
# run: yarn test

# - name: Run test coverage
# run: yarn coverage

# # Testing beta of code coverage checks
# # This part can be removed or optimized once barecheck code coverage will be out of beta
# - name: Generate Code Coverage report
# id: code-coverage
# uses: ./
# with:
# barecheck-github-app-token: ${{ secrets.BARECHECK_GITHUB_APP_TOKEN }}
# github-token: ${{ secrets.GITHUB_TOKEN }}
# barecheck-api-key: ${{ secrets.BARECHECK_API_KEY }}
# lcov-file: "./coverage/lcov.info"
# minimum-ratio: 0
# send-summary-comment: true
# show-annotations: "warning"
# app-name: "Github Action"
base_branch_cov:
checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.base_ref }}
- name: Use Node.js v16.11.0
uses: actions/setup-node@v1
with:
Expand All @@ -59,44 +19,27 @@ jobs:
- name: Install dependencies
run: yarn

- name: Run test coverage
run: yarn coverage
- name: Run linting checks
run: yarn lint

- name: Upload code coverage for ref branch
uses: actions/upload-artifact@v2
with:
name: ref-lcov.info
path: ./coverage/lcov.info

checks:
runs-on: ubuntu-latest
needs: base_branch_cov
steps:
- uses: actions/checkout@v2
- name: Use Node.js v16.11.0
uses: actions/setup-node@v1
with:
node-version: v16.11.0

- name: Download code coverage report from base branch
uses: actions/download-artifact@v2
with:
name: ref-lcov.info
- name: Run formatting checks
run: yarn format-check

- name: Install dependencies
run: yarn
run: yarn test

- name: Run test coverage
run: yarn coverage

# Compares two code coverage files and generates report as a comment
- name: Generate Code Coverage report
id: code-coverage
uses: ./
with:
barecheck-github-app-token: ${{ secrets.BARECHECK_GITHUB_APP_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
barecheck-api-key: ${{ secrets.BARECHECK_API_KEY }}
lcov-file: "./coverage/lcov.info"
base-lcov-file: "./lcov.info"
minimum-ratio: 0 # Fails Github action once code coverage is decreasing
minimum-ratio: 0
send-summary-comment: true
show-annotations: "warning" # Possible options warning|error
show-annotations: "warning"
# app-name: "Barecheck"
148 changes: 124 additions & 24 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13240,18 +13240,97 @@ module.exports = {
};


/***/ }),

/***/ 2069:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const core = __nccwpck_require__(2186);
const { barecheckApi } = __nccwpck_require__(4044);

const { getBaseRefSha, getCurrentRefSha } = __nccwpck_require__(8383);
const { getBarecheckApiKey } = __nccwpck_require__(6);

let projectAuthState = false;

const authProject = async () => {
if (!projectAuthState) {
const apiKey = getBarecheckApiKey();

const authProjectRes = await barecheckApi.authProject({
apiKey
});

projectAuthState = {
projectId: authProjectRes.project.id,
accessToken: authProjectRes.accessToken
};
}

return projectAuthState;
};

const cleanAuthProject = () => {
projectAuthState = false;
};

const getBaseBranchCoverage = async () => {
const { ref, sha } = getBaseRefSha();

core.info(`Getting metrics from Barecheck. ref=${ref}, sha=${sha}`);

const { projectId, accessToken } = await authProject();

const coverageMetrics = await barecheckApi.coverageMetrics(accessToken, {
projectId,
ref,
sha,
take: 1
});

return coverageMetrics[0] ? coverageMetrics[0].totalCoverage : false;
};

const sendCurrentCoverage = async (totalCoverage) => {
const { ref, sha } = getCurrentRefSha();

core.info(
`Sending metrics to Barecheck. ref=${ref}, sha=${sha}, coverage=${totalCoverage}`
);

const { projectId, accessToken } = await authProject();

await barecheckApi.createCoverageMetric(accessToken, {
projectId,
ref,
sha,
totalCoverage
});
};

module.exports = {
getBaseBranchCoverage,
sendCurrentCoverage,
authProject,
cleanAuthProject
};


/***/ }),

/***/ 8383:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

/* eslint-disable no-console */
const github = __nccwpck_require__(5438);
const { githubApi } = __nccwpck_require__(4044);

const { getBarecheckGithubAppToken, getGithubToken } = __nccwpck_require__(6);

let octokit = null;

const cleanRef = (fullRef) => fullRef.replace("refs/heads/", "");

const getPullRequestContext = () => {
if (!github.context.payload.pull_request) return false;

Expand All @@ -13266,6 +13345,25 @@ const getPullRequestContext = () => {
};
};

const getBaseRefSha = () => {
const { before: baseSha, pull_request: pullRequest } = github.context.payload;
const { ref: fullRef } = github.context;

const ref = pullRequest ? pullRequest.base.ref : cleanRef(fullRef);
const sha = pullRequest ? pullRequest.base.sha : baseSha;

return { ref, sha };
};

const getCurrentRefSha = () => {
const { sha, ref: fullRef } = github.context;

const pullRequest = github.context.payload.pull_request;
const ref = pullRequest ? pullRequest.head.ref : cleanRef(fullRef);

return { ref, sha };
};

const getOctokit = async () => {
if (!octokit)
octokit = await githubApi.createOctokitClient(
Expand All @@ -13283,7 +13381,9 @@ const cleanOctokit = () => {
module.exports = {
getPullRequestContext,
getOctokit,
cleanOctokit
cleanOctokit,
getBaseRefSha,
getCurrentRefSha
};


Expand All @@ -13295,13 +13395,15 @@ module.exports = {
const core = __nccwpck_require__(2186);
const { parseLcovFile } = __nccwpck_require__(4044);

const { getBaseLcovFile } = __nccwpck_require__(6);
const { getBaseLcovFile, getBarecheckApiKey } = __nccwpck_require__(6);
const { getBaseBranchCoverage } = __nccwpck_require__(2069);

// eslint-disable-next-line max-statements
const getBasecoverageDiff = async (coverage) => {
// TODO: Get metrics from Barecheck API
const baseFile = getBaseLcovFile();
const baseMetrics = false;
const baseMetrics = getBarecheckApiKey()
? await getBaseBranchCoverage()
: false;
let baseCoveragePercentage = baseMetrics ? baseMetrics.coverage : 0;

if (!baseCoveragePercentage && baseFile) {
Expand Down Expand Up @@ -13649,48 +13751,46 @@ const { parseLcovFile } = __nccwpck_require__(4044);

const { getLcovFile } = __nccwpck_require__(6);

const { sendCurrentCoverage } = __nccwpck_require__(2069);

const sendSummaryComment = __nccwpck_require__(2599);
const showAnnotations = __nccwpck_require__(5100);
const checkMinimumRatio = __nccwpck_require__(8329);
const getBaseCoverageDiff = __nccwpck_require__(5819);
const getChangedFilesCoverage = __nccwpck_require__(3228);

const runFeatures = async (diff, coverage) => {
const runCodeCoverage = async (coverage) => {
const diff = await getBaseCoverageDiff(coverage);
core.info(`Code coverage diff: ${diff}%`);

const changedFilesCoverage = await getChangedFilesCoverage(coverage);
await sendSummaryComment(changedFilesCoverage, diff, coverage.percentage);

await checkMinimumRatio(diff);
await showAnnotations(changedFilesCoverage);
await sendCurrentCoverage(coverage.percentage);

core.setOutput("percentage", coverage.percentage);
core.setOutput("diff", diff);
};

const runCodeCoverage = async (coverage) => {
const diff = await getBaseCoverageDiff(coverage);

core.info(`Code coverage diff: ${diff}%`);

await runFeatures(diff, coverage);
};

async function main() {
const compareFile = getLcovFile();
try {
const compareFile = getLcovFile();

core.info(`lcov-file: ${compareFile}`);
core.info(`lcov-file: ${compareFile}`);

const coverage = await parseLcovFile(compareFile);
core.info(`Current code coverage: ${coverage.percentage}%`);
const coverage = await parseLcovFile(compareFile);
core.info(`Current code coverage: ${coverage.percentage}%`);

await runCodeCoverage(coverage);
await runCodeCoverage(coverage);
} catch (err) {
core.info(err);
core.setFailed(err.message);
}
}

try {
main();
} catch (err) {
core.info(err);
core.setFailed(err.message);
}
main();

})();

Expand Down
38 changes: 18 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,43 @@ const { parseLcovFile } = require("barecheck");

const { getLcovFile } = require("./input");

const { sendCurrentCoverage } = require("./lib/api");

const sendSummaryComment = require("./services/sendSummaryComment");
const showAnnotations = require("./services/showAnnotations");
const checkMinimumRatio = require("./services/minimumRatio");
const getBaseCoverageDiff = require("./services/baseCoverageDiff");
const getChangedFilesCoverage = require("./services/changedFilesCoverage");

const runFeatures = async (diff, coverage) => {
const runCodeCoverage = async (coverage) => {
const diff = await getBaseCoverageDiff(coverage);
core.info(`Code coverage diff: ${diff}%`);

const changedFilesCoverage = await getChangedFilesCoverage(coverage);
await sendSummaryComment(changedFilesCoverage, diff, coverage.percentage);

await checkMinimumRatio(diff);
await showAnnotations(changedFilesCoverage);
await sendCurrentCoverage(coverage.percentage);

core.setOutput("percentage", coverage.percentage);
core.setOutput("diff", diff);
};

const runCodeCoverage = async (coverage) => {
const diff = await getBaseCoverageDiff(coverage);

core.info(`Code coverage diff: ${diff}%`);

await runFeatures(diff, coverage);
};

async function main() {
const compareFile = getLcovFile();
try {
const compareFile = getLcovFile();

core.info(`lcov-file: ${compareFile}`);
core.info(`lcov-file: ${compareFile}`);

const coverage = await parseLcovFile(compareFile);
core.info(`Current code coverage: ${coverage.percentage}%`);
const coverage = await parseLcovFile(compareFile);
core.info(`Current code coverage: ${coverage.percentage}%`);

await runCodeCoverage(coverage);
await runCodeCoverage(coverage);
} catch (err) {
core.info(err);
core.setFailed(err.message);
}
}

try {
main();
} catch (err) {
core.info(err);
core.setFailed(err.message);
}
main();
Loading