Skip to content

Commit

Permalink
Merge branch 'master' into translation/web-css-syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
undead404 committed Nov 23, 2023
2 parents 828cf47 + 9c8a25b commit a8d70ea
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 97 deletions.
29 changes: 12 additions & 17 deletions .github/workflows/spellcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,16 @@ jobs:
name: Convert markdown to plain text
run: |
file=${{ needs.prepare-translation.outputs.translation }}
tmpHtmlFile=$(echo $file | sed 's/\.md/\.html/')
pandoc -f markdown -t html -o $tmpHtmlFile $file
newFileName=$(echo $file | sed 's/\.md/\.txt/')
pandoc -f markdown -t plain -o $newFileName $file
pandoc -f html -t plain -o $newFileName $tmpHtmlFile
echo "translation=$newFileName" >> $GITHUB_OUTPUT
echo $newFileName
# Error if translation file is not found
- if: steps.md2txt.outputs.translation == ''
name: Check translation is found
run: echo "No translation file found" && exit 1
- uses: actions/setup-java@v2
- uses: actions/setup-java@v3
with:
distribution: "temurin"
java-version: "8"
Expand All @@ -98,6 +99,7 @@ jobs:
run: |
cd LanguageTool-6.3
java -jar languagetool-commandline.jar -d ${{steps.disabled-rules.outputs.disabled_rules}} -l uk --json ../${{ steps.md2txt.outputs.translation }} > ../result.json
cat ../result.json
matches=$(cat ../result.json | jq '.matches')
# Check if matches equal []
echo "has_matches=$(if [ "$matches" == "[]" ]; then echo "false"; else echo "true"; fi)" >> $GITHUB_OUTPUT
Expand Down Expand Up @@ -136,19 +138,12 @@ jobs:
path: .
- name: Create mapping
run: node scripts/create-file-mapping.js ./index.txt ${{ needs.prepare-translation.outputs.translation }} > ./mapping.json && cat ./mapping.json
- id: create-message
name: Create message
run: node scripts/create-message.js ${{ needs.prepare-translation.outputs.translation }} > message.txt && cat ./message.txt
# - uses: reviewdog/action-setup@v1
# - name: Send results
# run: |
# export REVIEWDOG_GITHUB_API_TOKEN=${{ secrets.GITHUB_TOKEN }}
# cat ./message.txt | reviewdog -efm="%A%f:%l:%c:%e:%k: %m%Z" -fail-on-error -reporter=github-pr-review -filter-mode=nofilter -name="LanguageTool" -level=info
- name: Send results
uses: mshick/add-pr-comment@v2
with:
message-id: ${{ needs.prepare-translation.outputs.translation }}
message-path: ./message.txt
refresh-message-position: true
- env:
COMMIT_ID: ${{ github.event.pull_request.head.sha }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
name: Send results
run: node scripts/send-comments.js ${{ needs.prepare-translation.outputs.translation }}
timeout-minutes: 5
- name: Exit with error
run: echo "Spelling errors found" && cat result.json && exit 1
80 changes: 0 additions & 80 deletions scripts/create-message.js

This file was deleted.

137 changes: 137 additions & 0 deletions scripts/send-comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { execSync } from "child_process";
import { readFileSync } from "fs";

const MARKDOWN_ESCAPE_REGEX = /([!"#'()*+.[\\\]_`{}-])/g;
function escapeTextForMarkdown(text) {
return text.replaceAll(MARKDOWN_ESCAPE_REGEX, "\\$1");
}

function convertOffsetToLineAndColumn(markdownRunes, offset) {
let line = 1;
let column = 1;
for (let index = 0; index < offset; index += 1) {
const rune = markdownRunes[index];
if (rune === "\n") {
line += 1;
column = 1;
} else {
column += 1;
}
}
return [line, column];
}
try {
const results = JSON.parse(readFileSync("./result.json"));
const MARKDOWN_FILE_ARG_INDEX = 2;
/**
* @type {Record<number, number>}
*/
const mapping = JSON.parse(readFileSync("./mapping.json"));
const markdownFile = process.argv[MARKDOWN_FILE_ARG_INDEX];
const markdown = readFileSync(markdownFile, "utf8");

const markdownRunes = Array.from(markdown);

const TOTAL_TIMEOUT = 180_000;
const timeout = setTimeout(() => {
throw new Error("Timeout");
}, TOTAL_TIMEOUT);
const mappingMaxOffset = Math.max(...Object.keys(mapping));

// eslint-disable-next-line no-restricted-syntax
for (const match of results.matches) {
const { context, message, replacements, rule, sentence, shortMessage } =
match;
let { length, offset } = match;
let start;
length += 1;
while (!start && offset <= mappingMaxOffset) {
offset += 1;
length -= 1;
start = Number.parseInt(mapping[offset], 10);
}
let endOffset = offset + length;
let end;
endOffset -= 1;
length -= 1;
while (!end && endOffset <= mappingMaxOffset) {
endOffset += 1;
length += 1;
end = Number.parseInt(mapping[endOffset], 10);
}
console.error(start, end);
const [startLine, startColumn] = convertOffsetToLineAndColumn(
markdownRunes,
start,
);
const [endLine, endColumn] = convertOffsetToLineAndColumn(
markdownRunes,
end,
);
if (endLine < startLine) {
console.error(startLine, endLine);
throw new Error(`Line not found in source file: ${sentence}`);
}
if (endLine === startLine && endColumn < startColumn) {
console.error(startColumn, endColumn);
throw new Error(`Column not found in source file: ${sentence}`);
}
// const errorformatLine = `${markdownFile}:${startLine}:${startColumn}:${endLine}:${endColumn}: ${message}`;
let comment = "";
if (shortMessage || rule.description) {
comment += `### ${escapeTextForMarkdown(
shortMessage || rule.description,
)}\n\n`;
}
comment += `${escapeTextForMarkdown(message)}\n${rule.category.id}/${
rule.id
}: ${escapeTextForMarkdown(rule.description)}\n`;
// console.log(`\`${markdownFile}:${startLine}:${startColumn}\n\``);
comment += `> ${escapeTextForMarkdown(
context.text.slice(0, context.offset),
)}**${escapeTextForMarkdown(
context.text.slice(context.offset, context.offset + context.length),
)}**${escapeTextForMarkdown(
context.text.slice(context.offset + context.length),
)}`;
if (replacements?.length) {
comment += "\n\n#### Варіанти заміни\n";
// eslint-disable-next-line no-restricted-syntax
for (const replacement of replacements) {
// console.log(`- ${replacement.value}`);
comment += `- ${escapeTextForMarkdown(replacement.value)}\n`;
}
}
const parameters = {
body: comment,
commit_id: process.env.COMMIT_ID,
line: endLine,
path: markdownFile,
side: "RIGHT",
};
if (startLine !== endLine) {
parameters.start_line = startLine;
parameters.start_side = "RIGHT";
}
let command = `gh api repos/${process.env.GITHUB_REPOSITORY}/pulls/${process.env.PR_NUMBER}/comments`;
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of Object.entries(parameters)) {
command +=
typeof value === "number"
? ` -F ${key}=${value}`
: ` -f ${key}="${value}"`;
}
console.log(command);
// command = command.replaceAll("\n", "\\\n");
console.log(`GH_TOKEN=${process.env.GH_TOKEN} ${command}`);
execSync(command, { stdio: "inherit", timeout: 60_000 });
}
clearTimeout(timeout);
} catch (error) {
console.error(error);
process.exit(1);
}
process.on("unhandledRejection", (error) => {
console.error(error);
process.exit(1);
});

0 comments on commit a8d70ea

Please sign in to comment.