Skip to content

Commit

Permalink
feat: contextual comments (#2530)
Browse files Browse the repository at this point in the history
* feat: contextual comments

* chore: textual change for testing

* fix: remove redundant step

* fix: remove column & make the command single line

* fix: reorder GH params

* fix: variable command params

* fix: better comment view

* chore: cleanup

* chore: remove test textual changes
  • Loading branch information
undead404 authored Nov 23, 2023
1 parent 01bdb2a commit 148de83
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
20 changes: 6 additions & 14 deletions .github/workflows/spellcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,11 @@ 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 }}
- name: Exit with error
run: echo "Spelling errors found" && cat result.json && exit 1
54 changes: 33 additions & 21 deletions scripts/create-message.js → scripts/send-comments.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// Read LanguageTool output from ./results.json
// and txt-to-markdown mapping from ./mapping.json
// and print an errorformat message for each error
// to stdout.

import { execSync } from "child_process";
import { readFileSync } from "fs";

const results = JSON.parse(readFileSync("./result.json"));
Expand Down Expand Up @@ -57,24 +53,40 @@ for (const match of results.matches) {
throw new Error(`Column not found in source file: ${sentence}`);
}
// const errorformatLine = `${markdownFile}:${startLine}:${startColumn}:${endLine}:${endColumn}: ${message}`;
// console.log(errorformatLine);
console.log(`## ${message}\n`);
console.log(`\`${markdownFile}:${startLine}:${startColumn}\n\``);
console.log(`${rule.description}:\n`);
console.log(
`> ${context.text.slice(0, context.offset)}**${context.text.slice(
context.offset,
context.offset + context.length,
)}**${context.text.slice(context.offset + context.length)}_`,
);
console.log("Варіанти заміни:");
if (replacements.length > 0) {
let comment = `### ${message}\n${rule.description}\n${rule.category.id}/${rule.id}: ${rule.description}\n`;
// console.log(`\`${markdownFile}:${startLine}:${startColumn}\n\``);
comment += `> ${context.text.slice(0, context.offset)}**${context.text.slice(
context.offset,
context.offset + context.length,
)}**${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}`);
// console.log(`- ${replacement.value}`);
comment += `- ${replacement.value}\n`;
}
} else {
console.log("Немає");
}
console.log("\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);
console.log(`GH_TOKEN=${process.env.GH_TOKEN} ${command}`);
execSync(command, { stdio: "inherit" });
}

0 comments on commit 148de83

Please sign in to comment.