-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from ubiquity/cleanup-20241022192741
revert: remove sync-template #54 (comment)
- Loading branch information
Showing
17 changed files
with
2,722 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import * as core from "@actions/core"; | ||
import { Octokit } from "@octokit/rest"; | ||
import simpleGit from "simple-git"; | ||
|
||
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) { | ||
core.setFailed("Missing required environment variables."); | ||
process.exit(1); | ||
} | ||
|
||
const octokit = new Octokit({ auth: token }); | ||
const git = simpleGit(); | ||
|
||
async function main() { | ||
try { | ||
const { data: pullRequest } = await octokit.pulls.get({ | ||
owner, | ||
repo, | ||
pull_number: parseInt(pullNumber, 10), | ||
}); | ||
|
||
const baseSha = pullRequest.base.sha; | ||
const headSha = pullRequest.head.sha; | ||
|
||
await git.fetch(["origin", baseSha, headSha]); | ||
|
||
const diff = await git.diff([`${baseSha}...${headSha}`]); | ||
|
||
core.info("Checking for empty strings..."); | ||
const violations = parseDiffForEmptyStrings(diff); | ||
|
||
if (violations.length > 0) { | ||
violations.forEach(({ file, line, content }) => { | ||
core.warning( | ||
"Detected an empty string.\n\nIf this is during variable initialization, consider using a different approach.\nFor more information, visit: https://www.github.com/ubiquity/ts-template/issues/31", | ||
{ | ||
file, | ||
startLine: line, | ||
} | ||
); | ||
}); | ||
|
||
// core.setFailed(`${violations.length} empty string${violations.length > 1 ? "s" : ""} detected in the code.`); | ||
|
||
await octokit.rest.checks.create({ | ||
owner, | ||
repo, | ||
name: "Empty String Check", | ||
head_sha: headSha, | ||
status: "completed", | ||
conclusion: violations.length > 0 ? "failure" : "success", | ||
output: { | ||
title: "Empty String Check Results", | ||
summary: `Found ${violations.length} violation${violations.length !== 1 ? "s" : ""}`, | ||
annotations: violations.map((v) => ({ | ||
path: v.file, | ||
start_line: v.line, | ||
end_line: v.line, | ||
annotation_level: "warning", | ||
message: "Empty string found", | ||
raw_details: v.content, | ||
})), | ||
}, | ||
}); | ||
} else { | ||
core.info("No empty strings found."); | ||
} | ||
} catch (error) { | ||
core.setFailed(`An error occurred: ${error instanceof Error ? error.message : String(error)}`); | ||
} | ||
} | ||
|
||
function parseDiffForEmptyStrings(diff: string) { | ||
const violations: Array<{ file: string; line: number; content: string }> = []; | ||
const diffLines = diff.split("\n"); | ||
|
||
let currentFile: string; | ||
let headLine = 0; | ||
let inHunk = false; | ||
|
||
diffLines.forEach((line) => { | ||
const hunkHeaderMatch = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/.exec(line); | ||
if (hunkHeaderMatch) { | ||
headLine = parseInt(hunkHeaderMatch[1], 10); | ||
inHunk = true; | ||
return; | ||
} | ||
|
||
if (line.startsWith("--- a/") || line.startsWith("+++ b/")) { | ||
currentFile = line.slice(6); | ||
inHunk = false; | ||
return; | ||
} | ||
|
||
// Only process TypeScript files | ||
if (!currentFile?.endsWith(".ts")) { | ||
return; | ||
} | ||
|
||
if (inHunk && line.startsWith("+")) { | ||
// Check for empty strings in TypeScript syntax | ||
if (/^\+.*""/.test(line)) { | ||
// Ignore empty strings in comments | ||
if (!line.trim().startsWith("//") && !line.trim().startsWith("*")) { | ||
// Ignore empty strings in template literals | ||
if (!/`[^`]*\$\{[^}]*\}[^`]*`/.test(line)) { | ||
violations.push({ | ||
file: currentFile, | ||
line: headLine, | ||
content: line.substring(1).trim(), | ||
}); | ||
} | ||
} | ||
} | ||
headLine++; | ||
} else if (!line.startsWith("-")) { | ||
headLine++; | ||
} | ||
}); | ||
|
||
return violations; | ||
} | ||
main().catch((error) => { | ||
core.setFailed(`Error running empty string check: ${error instanceof Error ? error.message : String(error)}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: Empty String Check | ||
|
||
on: | ||
pull_request: | ||
types: [opened, synchronize, reopened] | ||
|
||
jobs: | ||
check-for-empty-strings: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: "20.10.0" | ||
- name: Get GitHub App token | ||
uses: tibdex/[email protected] | ||
id: get_installation_token | ||
with: | ||
app_id: ${{ secrets.APP_ID }} | ||
private_key: ${{ secrets.APP_PRIVATE_KEY }} | ||
- name: Install Dependencies | ||
run: | | ||
yarn add tsx simple-git | ||
- name: Check for Empty Strings | ||
run: | | ||
yarn tsx .github/empty-string-checker.ts | ||
env: | ||
GITHUB_TOKEN: ${{ steps.get_installation_token.outputs.token }} | ||
GITHUB_REPOSITORY: ${{ github.repository }} | ||
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }} | ||
GITHUB_BASE_REF: ${{ github.base_ref }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,29 @@ | ||
import esbuild from "esbuild"; | ||
const typescriptEntries = ["static/main.ts"]; | ||
// const cssEntries = ["static/style.css"]; | ||
const entries = [ | ||
...typescriptEntries, | ||
// ...cssEntries | ||
]; | ||
import esbuild, { BuildOptions } from "esbuild"; | ||
|
||
export const esBuildContext: esbuild.BuildOptions = { | ||
const ENTRY_POINTS = { | ||
typescript: ["static/main.ts"], | ||
// css: ["static/style.css"], | ||
}; | ||
|
||
const DATA_URL_LOADERS = [".png", ".woff", ".woff2", ".eot", ".ttf", ".svg"]; | ||
|
||
export const esbuildOptions: BuildOptions = { | ||
sourcemap: true, | ||
entryPoints: entries, | ||
entryPoints: [...ENTRY_POINTS.typescript /* ...ENTRY_POINTS.css */], | ||
bundle: true, | ||
minify: false, | ||
loader: { | ||
".png": "dataurl", | ||
".woff": "dataurl", | ||
".woff2": "dataurl", | ||
".eot": "dataurl", | ||
".ttf": "dataurl", | ||
".svg": "dataurl", | ||
}, | ||
loader: Object.fromEntries(DATA_URL_LOADERS.map((ext) => [ext, "dataurl"])), | ||
outdir: "static/dist", | ||
}; | ||
|
||
esbuild | ||
.build(esBuildContext) | ||
.then(() => { | ||
async function runBuild() { | ||
try { | ||
await esbuild.build(esbuildOptions); | ||
console.log("\tesbuild complete"); | ||
}) | ||
.catch((err) => { | ||
} catch (err) { | ||
console.error(err); | ||
process.exit(1); | ||
}); | ||
} | ||
} | ||
|
||
void runBuild(); |
Oops, something went wrong.