Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 0 additions & 3 deletions .mocharc.json

This file was deleted.

2,322 changes: 1,172 additions & 1,150 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
"name": "slack-health-score",
"version": "0.1.1",
"description": "A rough health score reporter",
"type": "module",
"main": "dist/index.js",
"scripts": {
"build": "ncc build src/index.js -o dist --license licenses.txt --source-map",
"lint": "biome check .",
"lint:fix": "biome check --write .",
"local": "act public --eventpath .github/workflows/local/event.json --secret-file .github/workflows/local/.env --platform ubuntu-latest=node:20-buster",
"test": "npm run lint && c8 npm run test:mocha",
"test:mocha": "mocha --config .mocharc.json test/*-test.js test/**/*-test.js"
"test": "npm run lint && c8 npm run test:node",
"test:node": "node --test test/*-test.js test/**/*-test.js"
},
"repository": {
"type": "git",
Expand All @@ -33,16 +34,14 @@
},
"homepage": "https://github.com/slackapi/slack-health-score#readme",
"dependencies": {
"@actions/core": "^2.0.1",
"@actions/github": "^6.0.1",
"@actions/core": "^3.0.0",
"@actions/github": "^9.0.0",
"@api/codecov": "file:.api/apis/codecov"
},
"devDependencies": {
"@biomejs/biome": "2.3.13",
"@vercel/ncc": "^0.38.4",
"c8": "^10.1.3",
"chai": "^6.2.2",
"mocha": "^11.7.5",
"sinon": "^21.0.1"
"esmock": "^2.7.3"
}
}
4 changes: 2 additions & 2 deletions src/get-sha.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param {import('@actions/github')} github `@actions/github` GitHub Actions core helper utility
* @returns {string} SHA of the commit being inspected, regardless of underlying GitHub event
*/
module.exports = function getCommitSHA(core, github) {
export default function getCommitSHA(core, github) {
// Get GitHub-event-relevant contextual details, like commit SHA
const ctx = github.context;
core.debug(
Expand All @@ -29,4 +29,4 @@ module.exports = function getCommitSHA(core, github) {
}
core.info(`Using SHA: ${sha}`);
return sha;
};
}
18 changes: 10 additions & 8 deletions src/health-score.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const findProblematicComments = require('./score_components/find-problematic-comments');
const retrieveCodeCoverage = require('./score_components/coverage');
const reportStatus = require('./report');
const { parseYamlArray } = require('./helpers/helper-functions');
import { parseYamlArray } from './helpers/helper-functions.js';
import reportStatus from './report.js';
import retrieveCodeCoverage from './score_components/coverage.js';
import findProblematicComments from './score_components/find-problematic-comments.js';

module.exports = {
const hs = {
/**
* @description Compiles the health score components
* @param {import('@actions/core')} core `@actions/core` GitHub Actions core helper utility
* @param {import('@actions/github')} github `@actions/github` GitHub Actions core helper utility
* @returns {Promise<import('./types').HealthScore>} score Health score details object
* @returns {Promise<import('./types.js').HealthScore>} score Health score details object
*/
compile: async function compileScore(core, github) {
// TODO: wire up action outputs

Check warning on line 14 in src/health-score.js

View workflow job for this annotation

GitHub Actions / Health Score

src/health-score.js#L14

Problematic comment ("TODO") identified
const extensionInput = core.getInput('extension');
const includeInput = core.getInput('include');
const excludeInput = core.getInput('exclude');
Expand All @@ -21,13 +21,13 @@
const excludes = parseYamlArray(excludeInput);

let com = '';
const misses = await module.exports.coverage(core, github); // uncovered LoC
const misses = await hs.coverage(core, github); // uncovered LoC
if (extensions.length === 0) {
core.error('Extensions not specified');
} else {
if (includes.length === 0)
core.warning('Directories to be included not specified');
com = module.exports.grep(core, extensions, includes, excludes); // to-do et al comments
com = hs.grep(core, extensions, includes, excludes); // to-do et al comments
}
return {
comments: com,
Expand All @@ -38,3 +38,5 @@
coverage: retrieveCodeCoverage,
report: reportStatus,
};

export default hs;
83 changes: 41 additions & 42 deletions src/helpers/helper-functions.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
module.exports = {
/**
*
* @param input
* @returns []
*/
parseYamlArray(input) {
if (!input) {
return [];
}
const arr = input.trim().replace(/\[|\]/g, '');
/**
*
* @param input
* @returns []
*/
export function parseYamlArray(input) {
if (!input) {
return [];
}
const arr = input.trim().replace(/\[|\]/g, '');

return arr
.split(/,\s*|\n/)
.map((item) => item.trim().replace(/^- */, ''))
.filter(Boolean)
.map((item) => {
if (
(item.startsWith('"') && item.endsWith('"')) ||
(item.startsWith("'") && item.endsWith("'"))
) {
return item.slice(1, -1);
}
return item;
});
},
/**
*
* @param comments
* @returns []
*/
getAnnotations(comments) {
return comments.map((c) => ({
path: c.path,
start_line: c.line_no ? c.line_no : 1,
end_line: c.line_no ? c.line_no : 1,
annotation_level: 'warning',
message: c.commentType
? `Problematic comment ("${c.commentType}") identified`
: 'Problematic comment ("TODO", "HACK", "FIXME") identified',
}));
},
};
return arr
.split(/,\s*|\n/)
.map((item) => item.trim().replace(/^- */, ''))
.filter(Boolean)
.map((item) => {
if (
(item.startsWith('"') && item.endsWith('"')) ||
(item.startsWith("'") && item.endsWith("'"))
) {
return item.slice(1, -1);
}
return item;
});
}

/**
*
* @param comments
* @returns []
*/
export function getAnnotations(comments) {
return comments.map((c) => ({
path: c.path,
start_line: c.line_no ? c.line_no : 1,
end_line: c.line_no ? c.line_no : 1,
annotation_level: 'warning',
message: c.commentType
? `Problematic comment ("${c.commentType}") identified`
: 'Problematic comment ("TODO", "HACK", "FIXME") identified',
}));
}
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const core = require('@actions/core');
const github = require('@actions/github');
const hs = require('./health-score');
import * as core from '@actions/core';
import * as github from '@actions/github';
import hs from './health-score.js';

const startTime = new Date();
hs.compile(core, github)
Expand Down
10 changes: 5 additions & 5 deletions src/report.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const getSHA = require('./get-sha');
const { getAnnotations } = require('./helpers/helper-functions');
import getSHA from './get-sha.js';
import { getAnnotations } from './helpers/helper-functions.js';

const UNCOVERED_LINE_PENALTY = 1;
const PROBLEMATIC_COMMENT_PENALTY = 100;
Expand All @@ -8,10 +8,10 @@
* @param {Date} startTime the JavaScript Date of the start of this action's run
* @param {import('@actions/core')} core `@actions/core` GitHub Actions core helper utility
* @param {import('@actions/github')} github `@actions/github` GitHub Actions core helper utility
* @param {import('./types').HealthScore} score The health score to be reported
* @param {import('./types.js').HealthScore} score The health score to be reported
* @returns {Promise<number>} Total calculated health score
*/
module.exports = async function reportStatus(startTime, core, github, score) {
export default async function reportStatus(startTime, core, github, score) {
const gh = core.getInput('github_token');
if (!gh) {
core.warning(
Expand Down Expand Up @@ -46,7 +46,7 @@
According to [the code coverage for this project](https://app.codecov.io/gh/${ctx.repo.owner}/${ctx.repo.repo}), there are ${score.coverageMisses} uncovered lines of code. Each uncovered line of code contributes -${UNCOVERED_LINE_PENALTY} to the health score.`;
}
const annotations = getAnnotations(score.comments);
// TODO: handle API call erroring out

Check warning on line 49 in src/report.js

View workflow job for this annotation

GitHub Actions / Health Score

src/report.js#L49

Problematic comment ("TODO") identified
try {
await octokit.rest.checks.create({
name: 'Health Score',
Expand All @@ -69,4 +69,4 @@
core.error('Octokit checks creation call failed');
}
return points;
};
}
8 changes: 4 additions & 4 deletions src/score_components/coverage.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const codecov = require('@api/codecov');
const getSHA = require('../get-sha');
import codecov from '@api/codecov';
import getSHA from '../get-sha.js';

/**
* @description Compiles the health score components
* @param {import('@actions/core')} core `@actions/core` GitHub Actions core helper utility
* @param {import('@actions/github')} github `@actions/github` GitHub Actions core helper utility
* @returns {Promise<number>} Number of uncovered lines of code, or 0 in the case of no codecov token specified
*/
module.exports = async function retrieveCodeCoverage(core, github) {
export default async function retrieveCodeCoverage(core, github) {
// See if we can get a coverage overview for this commit from codecov
const codecovToken = core.getInput('codecov_token');
const maxAttempts =
Expand Down Expand Up @@ -74,7 +74,7 @@ module.exports = async function retrieveCodeCoverage(core, github) {
core.info('No codecov token provided, skipping coverage retrieval.');
}
return misses;
};
}

function sleep(ms) {
return new Promise((res) => {
Expand Down
8 changes: 4 additions & 4 deletions src/score_components/find-problematic-comments.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const fs = require('node:fs');
const child_process = require('node:child_process');
import child_process from 'node:child_process';
import fs from 'node:fs';

const CommentType = Object.freeze({
TODO: 'TODO',
FIXME: 'FIXME',
HACK: 'HACK',
});

module.exports = function grepForProblematicComments(
export default function grepForProblematicComments(
core,
ext,
include,
Expand Down Expand Up @@ -45,7 +45,7 @@
find += ` -not -path "*/${ex}"`;
}
}
const commentPattern = `\\s*(//|/\\*|\\*).*\\b(${CommentType.TODO}|${CommentType.HACK}|${CommentType.FIXME})\\b`;

Check warning on line 48 in src/score_components/find-problematic-comments.js

View workflow job for this annotation

GitHub Actions / Health Score

src/score_components/find-problematic-comments.js#L48

Problematic comment ("TODO", "HACK", "FIXME") identified

// Modify the grep command to search within comments only
find += ` -exec sh -c 'grep -EHn "${commentPattern}" "$0"' {} \\;`;
Expand All @@ -60,7 +60,7 @@
.split('\n')
.filter(Boolean)
.map((line) => {
// example line output = "./src/report.js:47: // TODO: handle API call erroring out"

Check warning on line 63 in src/score_components/find-problematic-comments.js

View workflow job for this annotation

GitHub Actions / Health Score

src/score_components/find-problematic-comments.js#L63

Problematic comment ("TODO", "HACK", "FIXME") identified
const [path, lineNo, ...data] = line.split(':');

const lineData = data.join(':');
Expand All @@ -77,7 +77,7 @@
};
});
return result;
};
}

/**
* Get the comment type.
Expand Down
2 changes: 1 addition & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* @property {number} coverageMisses Number of lines of code not covered, according to codecov
*/

module.exports = {};
export default {};
15 changes: 0 additions & 15 deletions test/.eslintrc.js

This file was deleted.

Loading