From 148de83fe375d5557367de57e916a6d06df5a0f4 Mon Sep 17 00:00:00 2001 From: Vitalii Perehonchuk Date: Thu, 23 Nov 2023 11:43:33 +0200 Subject: [PATCH] feat: contextual comments (#2530) * 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 --- .github/workflows/spellcheck.yml | 20 +++---- .../{create-message.js => send-comments.js} | 54 +++++++++++-------- 2 files changed, 39 insertions(+), 35 deletions(-) rename scripts/{create-message.js => send-comments.js} (57%) diff --git a/.github/workflows/spellcheck.yml b/.github/workflows/spellcheck.yml index 49fe00062f..dbeb16c5e1 100644 --- a/.github/workflows/spellcheck.yml +++ b/.github/workflows/spellcheck.yml @@ -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 diff --git a/scripts/create-message.js b/scripts/send-comments.js similarity index 57% rename from scripts/create-message.js rename to scripts/send-comments.js index 1532393854..9c3b194396 100644 --- a/scripts/create-message.js +++ b/scripts/send-comments.js @@ -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")); @@ -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" }); }