Skip to content

Commit

Permalink
refactor: remove obsolete formatting and setup scripts; introduce pla…
Browse files Browse the repository at this point in the history
…ywright command handling
  • Loading branch information
DE7924 committed Dec 19, 2024
1 parent 022bcb6 commit 898ffd7
Show file tree
Hide file tree
Showing 10 changed files with 403 additions and 578 deletions.
439 changes: 164 additions & 275 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

232 changes: 146 additions & 86 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,158 +1,218 @@
import { getBooleanInput, getInput, setFailed, debug } from "@actions/core";

Check failure on line 1 in src/main.ts

View workflow job for this annotation

GitHub Actions / code-quality

'debug' is defined but never used
import { exec } from "@actions/exec";
import { getOctokit, context } from "@actions/github";
import { analyze, eslint, litAnalyzer } from "./scripts/analyze";
import { formatting } from "./scripts/formatting";
import { testing } from "./scripts/testing";
import { eslint, litAnalyzer } from "./scripts/analyze";
import { playwright, testing } from "./scripts/testing";
import { comment } from "./scripts/comment";
import { cwd, chdir } from "process";
// import { coverage } from './scripts/coverage'
import minimist from "minimist";
import { setup } from "./scripts/setup";
import { execSync } from "child_process";
import { checkModifiedFiles } from "./scripts/post";

export type Command = { label: string; command: string };

export type stepResponse = { output: string; error: boolean };
export type StepResponse = { output: string; error: boolean };
export const failedEmoji = "❌";
export const passedEmoji = "✅";

export const buildComment = async (
commands: { label: string; command: string }[],
): Promise<string[]> => {
let commentBody = "\n";
let errorMessages = "";
for (const { label, command } of commands) {
const result = await runCommand(command, label);
if (result) {
commentBody += `${failedEmoji} - ${label}
<details><summary>See details</summary>${result}</details>`;
errorMessages += `${result}`;
} else {
commentBody += `${passedEmoji} - ${label}\n`;
export const runBashCommand = async (command: string): Promise<string> => {
return new Promise((resolve, reject) => {
try {
const output = execSync(command, { encoding: "utf-8" });
resolve(output);
} catch (error) {
reject(error);

Check failure on line 25 in src/main.ts

View workflow job for this annotation

GitHub Actions / code-quality

Expected the Promise rejection reason to be an Error
}
}
return [commentBody, errorMessages];
});
};

export const runCommand = async (
command: string,
label: string,
): Promise<string | boolean> => {
let output = "";
command: Command,
): Promise<[StepResponse, string]> => {
let response: StepResponse = { output: "", error: false };

Check failure on line 33 in src/main.ts

View workflow job for this annotation

GitHub Actions / code-quality

'response' is never reassigned. Use 'const' instead
let outputStr = "";
try {
await exec(command, [], {
await exec(command.command, [], {
listeners: {
stdout: (data) => {
output += data.toString();
outputStr += data.toString();
},
},
});
return false;
} catch (error: unknown) {
if (error instanceof Error) {
debug(`${label} failed: ${error.message}`);
return output;
} else if (typeof error === "string") {
debug(`${label} failed: ${error}`);
return output;
} else {
return true;
}
} catch (error) {
response.error = true;
setFailed(`Failed ${command.label}: ${error as string}`);
}
return [response, outputStr];
};

export const buildComment = async (
response: StepResponse,
outputStr: string,
label: string,
): Promise<StepResponse> => {

Check failure on line 54 in src/main.ts

View workflow job for this annotation

GitHub Actions / code-quality

Async arrow function 'buildComment' has no 'await' expression
if (response.error == true) {
response.output = `${failedEmoji} - ${label}\n<details><summary>See Details</summary>${outputStr}</details>`;
} else {
response.output = `${passedEmoji} - ${label}\n`;
}
return response;
};

export const commandComment = async (
command: Command,
): Promise<StepResponse> => {
const [response, outputStr] = await runCommand(command);
return await buildComment(response, outputStr, command.label);
};

const checkIfLocal = (): boolean => {
const argv = minimist(process.argv.slice(2));

return argv._.findLast((x: string) => x == "--local") == "--local"
? true
: false;
};

const getInputs = (
isLocal: boolean,
): [string, string, boolean, boolean, boolean, boolean] => {
// get the token and octokit
let token = "";
if (process.env.GITHUB_TOKEN && isLocal) {
token = process.env.GITHUB_TOKEN;
} else {
token = getInput("token");
}

const workingDirectory = getInput("working-directory");

// get static analysis input
const doStaticAnalysis: boolean = isLocal
? true
: getBooleanInput("run-static-analysis");

// get code formatting input
const doCodeFormatting: boolean = isLocal
? true
: getBooleanInput("run-code-formatting");

// get tests input
const doTests: boolean = isLocal ? true : getBooleanInput("run-tests");

// const runCoverage: boolean = getBooleanInput('run-coverage');
// const coveragePassScore: string = getInput('coverage-pass-score');
// get comment input
const createComment: boolean = isLocal
? true
: getBooleanInput("create-comment");

return [
token,
workingDirectory,
doStaticAnalysis,
doCodeFormatting,
doTests,
createComment,
];
};

/**
* The main function for the action.
* @returns {Promise<void>} Resolves when the action is complete.
*/
export async function run(): Promise<void> {
const argv = minimist(process.argv.slice(2));

const isLocal =
argv._.findLast((x: string) => x == "--local") == "--local" ? true : false;
const isLocal = checkIfLocal();

try {
const workingDirectory = getInput("working-directory");
const [
token,
workingDirectory,
doStaticAnalysis,
doCodeFormatting,
doTests,
createComment,
] = getInputs(isLocal);

// Check if the working directory is different from the current directory
const currentDirectory = cwd();
if (workingDirectory && workingDirectory !== currentDirectory) {
chdir(workingDirectory);
}

// get the token and octokit
let token = "";
if (process.env.GITHUB_TOKEN && isLocal) {
token = process.env.GITHUB_TOKEN;
} else {
token = getInput("token");
}
const octokit = getOctokit(token);

// get static analysis input
const doStaticAnalysis: boolean = isLocal
? true
: getBooleanInput("run-static-analysis");

// get code formatting input
const doCodeFormatting: boolean = isLocal
? true
: getBooleanInput("run-code-formatting");

// get tests input
const doTests: boolean = isLocal ? true : getBooleanInput("run-tests");

// const runCoverage: boolean = getBooleanInput('run-coverage');
// const coveragePassScore: string = getInput('coverage-pass-score');
// get comment input
const createComment: boolean = isLocal
? true
: getBooleanInput("create-comment");

// run set up
const setupStr: stepResponse | undefined = await setup();
const npmIStr: StepResponse | undefined = await commandComment({
label: "Install Dependencies",
command: "npm i --ignore-scripts",
});

// run Static Analysis
const analyzeStr: stepResponse | undefined = doStaticAnalysis
? await analyze()
: undefined;
const cemStr: StepResponse | undefined = await commandComment({
label: "Custom Elements Manifest",
command: "npm run analyze",
});

const eslintStr: stepResponse | undefined = doStaticAnalysis
const eslintStr: StepResponse | undefined = doStaticAnalysis
? await eslint({ label: "ESLint", command: "npm run lint" })
: undefined;

const litAnalyzerStr: stepResponse | undefined = doStaticAnalysis
const litAnalyzerStr: StepResponse | undefined = doStaticAnalysis
? await litAnalyzer({
label: "Lit Analyzer",
command: "npm run lint:lit-analyzer -- --format markdown",
})
: undefined;

// run Code Formatting
const codeFormattingStr: stepResponse | undefined = doCodeFormatting
? await formatting()
const prettierStr: StepResponse | undefined = doCodeFormatting
? await commandComment({ label: "Prettier", command: "npm run prettier" })
: undefined;

const playwrightStr: StepResponse | undefined = doTests
? await playwright({
label: "Install PlayWright Browsers",
command: "npx playwright install --with-deps",
})
: undefined;

// run Tests
const testingStr: stepResponse | undefined = doTests
? await testing()
const testingStr: StepResponse | undefined = doTests
? await testing({
label: "Testing",
command: "npm run test -- --coverage",
})
: undefined;

const tsDocStr: StepResponse | undefined = doTests
? await commandComment({ label: "TSDoc", command: "npm run docs" })
: undefined;

const checkModifiedFilesStr: StepResponse | undefined =

Check failure on line 191 in src/main.ts

View workflow job for this annotation

GitHub Actions / code-quality

'checkModifiedFilesStr' is assigned a value but never used
await checkModifiedFiles({
label: "Check for modified files",
command:
'echo "modified=$(if [ -n "$(git status --porcelain)" ]; then echo "true"; else echo "false"; fi)" >> $GITHUB_ENV',
});

// runCoverage
// const coverageStr: stepResponse | undefined = runCoverage
// const coverageStr: StepResponse | undefined = runCoverage
// ? await coverage()
// : undefined

// createComment
if (createComment) {
await comment(
octokit,
getOctokit(token),
context,
setupStr,
analyzeStr,
npmIStr,
cemStr,
eslintStr,
litAnalyzerStr,
codeFormattingStr,
prettierStr,
playwrightStr,
testingStr,
tsDocStr,
);
}
} catch (error) {
Expand Down
Loading

0 comments on commit 898ffd7

Please sign in to comment.