Skip to content

Commit

Permalink
chore: annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Oct 8, 2024
1 parent 19b73b2 commit 57077f6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
41 changes: 12 additions & 29 deletions .github/empty-string-checker.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Octokit } from "@octokit/rest";
import simpleGit from "simple-git";
import * as core from "@actions/core";

const token = process.env.GITHUB_TOKEN;
const [owner, repo] = process.env.GITHUB_REPOSITORY?.split("/") || [];
const pullNumber = process.env.GITHUB_PR_NUMBER || process.env.PULL_REQUEST_NUMBER || "0";
const baseRef = process.env.GITHUB_BASE_REF;

if (!token || !owner || !repo || pullNumber === "0" || !baseRef) {
console.error("Missing required environment variables.");
core.setFailed("Missing required environment variables.");
process.exit(1);
}

Expand All @@ -16,7 +17,6 @@ const git = simpleGit();

async function main() {
try {
// Get the base and head SHAs for the pull request
const { data: pullRequest } = await octokit.pulls.get({
owner,
repo,
Expand All @@ -26,28 +26,26 @@ async function main() {
const baseSha = pullRequest.base.sha;
const headSha = pullRequest.head.sha;

// Fetch all remote branches and tags
await git.fetch(["origin", baseSha, headSha]);

// Get the diff of the pull request using the SHAs
const diff = await git.diff([`${baseSha}...${headSha}`]);

console.log("Checking for empty strings...");
core.info("Checking for empty strings...");
const emptyStrings = parseDiffForEmptyStrings(diff);

if (emptyStrings.length > 0) {
console.error("Empty strings found:");
emptyStrings.forEach(({ file, line }) => {
console.error(`${file}:${line}`);
emptyStrings.forEach(({ file, line, content }) => {
core.warning(`Empty string found: ${content}`, {
file,
startLine: parseInt(line.toString()),
});
});
await createReview(emptyStrings);
process.exit(1); // This line is causing the non-zero exit code
core.setFailed(`${emptyStrings.length} empty string${emptyStrings.length > 1 ? "s" : ""} detected in the code.`);
} else {

Check warning on line 44 in .github/empty-string-checker.ts

View workflow job for this annotation

GitHub Actions / check-for-empty-strings

Empty string found: core.setFailed(`${emptyStrings.length} empty string${emptyStrings.length > 1 ? "s" : ""} detected in the code.`);
console.log("No empty strings found.");
core.info("No empty strings found.");
}
} catch (error) {
console.error("An error occurred:", error);
process.exit(1); // This could also be causing the non-zero exit code
core.setFailed(`An error occurred: ${error instanceof Error ? error.message : String(error)}`);
}
}

Expand Down Expand Up @@ -79,21 +77,6 @@ function parseDiffForEmptyStrings(diff: string) {
return violations;
}

async function createReview(violations: Array<{ file: string; line: number; content: string }>) {
const annotationsBody = violations
.map((v) => `${v.file}#L${v.line}\n\`\`\`suggestion\n${v.content.trim().replace('""', "/* TODO: Replace empty string */")}\n\`\`\``)
.join("\n\n");

await octokit.pulls.createReview({
owner,
repo,
pull_number: parseInt(pullNumber),
event: "COMMENT",
body: `> [!WARNING]\n> ${violations.length} empty string${violations.length > 1 ? "s" : ""} detected in the code.\n\n${annotationsBody}\n\n[Read more about empty string issues](https://www.github.com/ubiquity/ts-template/issues/31).`,
});
}

main().catch((error) => {
console.error("Error running empty string check:", error);
process.exit(1);
core.setFailed(`Error running empty string check: ${error instanceof Error ? error.message : String(error)}`);
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dotenv": "^16.4.4"
},
"devDependencies": {
"@actions/core": "^1.11.1",
"@commitlint/cli": "^18.6.1",
"@commitlint/config-conventional": "^18.6.2",
"@cspell/dict-node": "^4.0.3",
Expand Down
45 changes: 45 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@
resolved "https://registry.yarnpkg.com/@aashutoshrathi/word-wrap/-/word-wrap-1.2.6.tgz#bd9154aec9983f77b3a034ecaa015c2e4201f6cf"
integrity sha512-1Yjs2SvM8TflER/OD3cOjhWWOZb58A2t7wpE2S9XfBYTiIl+XFhQG2bjy4Pu1I+EAlCNUzRDYDdFwFYUKvXcIA==

"@actions/core@^1.11.1":
version "1.11.1"
resolved "https://registry.yarnpkg.com/@actions/core/-/core-1.11.1.tgz#ae683aac5112438021588030efb53b1adb86f172"
integrity sha512-hXJCSrkwfA46Vd9Z3q4cpEpHB1rL5NG04+/rbqW9d3+CSvtB1tYe8UTpAlixa1vj0m/ULglfEK2UKxMGxCxv5A==
dependencies:
"@actions/exec" "^1.1.1"
"@actions/http-client" "^2.0.1"

"@actions/exec@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@actions/exec/-/exec-1.1.1.tgz#2e43f28c54022537172819a7cf886c844221a611"
integrity sha512-+sCcHHbVdk93a0XT19ECtO/gIXoxvdsgQLzb2fE2/5sIZmWQuluYyjPQtrtTHdU1YzTZ7bAPN4sITq2xi1679w==
dependencies:
"@actions/io" "^1.0.1"

"@actions/http-client@^2.0.1":
version "2.2.3"
resolved "https://registry.yarnpkg.com/@actions/http-client/-/http-client-2.2.3.tgz#31fc0b25c0e665754ed39a9f19a8611fc6dab674"
integrity sha512-mx8hyJi/hjFvbPokCg4uRd4ZX78t+YyRPtnKWwIl+RzNaVuFpQHfmlGVfsKEJN8LwTCvL+DfVgAM04XaHkm6bA==
dependencies:
tunnel "^0.0.6"
undici "^5.25.4"

"@actions/io@^1.0.1":
version "1.1.3"
resolved "https://registry.yarnpkg.com/@actions/io/-/io-1.1.3.tgz#4cdb6254da7962b07473ff5c335f3da485d94d71"
integrity sha512-wi9JjgKLYS7U/z8PPbco+PvTb/nRWjeoFlJ1Qer83k/3C5PHQi28hiVdeE2kHXmIL99mQFawx8qt/JPjZilJ8Q==

"@ampproject/remapping@^2.2.0":
version "2.3.0"
resolved "https://registry.yarnpkg.com/@ampproject/remapping/-/remapping-2.3.0.tgz#ed441b6fa600072520ce18b43d2c8cc8caecc7f4"
Expand Down Expand Up @@ -1147,6 +1175,11 @@
resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.56.0.tgz#ef20350fec605a7f7035a01764731b2de0f3782b"
integrity sha512-gMsVel9D7f2HLkBma9VbtzZRehRogVRfbr++f06nL2vnCGCNlzOD+/MUov/F4p8myyAHspEhVobgjpX64q5m6A==

"@fastify/busboy@^2.0.0":
version "2.1.1"
resolved "https://registry.yarnpkg.com/@fastify/busboy/-/busboy-2.1.1.tgz#b9da6a878a371829a0502c9b6c1c143ef6663f4d"
integrity sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA==

"@humanwhocodes/config-array@^0.11.13":
version "0.11.14"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.11.14.tgz#d78e481a039f7566ecc9660b4ea7fe6b1fec442b"
Expand Down Expand Up @@ -7083,6 +7116,11 @@ tunnel-agent@^0.6.0:
dependencies:
safe-buffer "^5.0.1"

tunnel@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/tunnel/-/tunnel-0.0.6.tgz#72f1314b34a5b192db012324df2cc587ca47f92c"
integrity sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg==

tweetnacl@^0.14.3, tweetnacl@~0.14.0:
version "0.14.5"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64"
Expand Down Expand Up @@ -7206,6 +7244,13 @@ undici-types@~5.26.4:
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==

undici@^5.25.4:
version "5.28.4"
resolved "https://registry.yarnpkg.com/undici/-/undici-5.28.4.tgz#6b280408edb6a1a604a9b20340f45b422e373068"
integrity sha512-72RFADWFqKmUb2hmmvNODKL3p9hcB6Gt2DOQMis1SEBaV6a4MH8soBvzg+95CYhCKPFedut2JY9bMfrDl9D23g==
dependencies:
"@fastify/busboy" "^2.0.0"

unescape-js@^1.0.5:
version "1.1.4"
resolved "https://registry.yarnpkg.com/unescape-js/-/unescape-js-1.1.4.tgz#4bc6389c499cb055a98364a0b3094e1c3d5da395"
Expand Down

0 comments on commit 57077f6

Please sign in to comment.