From 7568a646eae6b01a9d7926b5c49aaa6cb2ffe3c8 Mon Sep 17 00:00:00 2001 From: ImVector <59611597+LuigiImVector@users.noreply.github.com> Date: Wed, 13 Jul 2022 19:47:27 +0200 Subject: [PATCH 1/4] build(fpb-lint): linting errors as PR comments, resolved #4416 --- .github/workflows/fpb-lint.yml | 98 ++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 4 deletions(-) diff --git a/.github/workflows/fpb-lint.yml b/.github/workflows/fpb-lint.yml index 3762ea6607b3a..e921218d61e64 100644 --- a/.github/workflows/fpb-lint.yml +++ b/.github/workflows/fpb-lint.yml @@ -2,6 +2,11 @@ name: free-programming-books-lint on: [push, pull_request] +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_RUN_ID: ${{ github.run_id }} + PR: ${{ github.event.pull_request.html_url }} + jobs: build: @@ -9,12 +14,97 @@ jobs: steps: - uses: actions/checkout@v3 + - name: Use Python + uses: actions/setup-python@v4 + with: + python-version: '3.x' - name: Use Node.js uses: actions/setup-node@v3 with: node-version: '16.x' - run: npm install -g free-programming-books-lint - - run: fpb-lint ./books/ - - run: fpb-lint ./casts/ - - run: fpb-lint ./courses/ - - run: fpb-lint ./more/ + + - name: Push + if: ${{ github.event_name == 'push' }} + run: | + fpb-lint ./books/ + fpb-lint ./casts/ + fpb-lint ./courses/ + fpb-lint ./more/ + + - name: Pull Request + if: ${{ always() && + github.event_name == 'pull_request' }} + run: | + fpb-lint ./books/ &>> output.log || echo "Analyzing..." + fpb-lint ./casts/ &>> output.log || echo "Analyzing..." + fpb-lint ./courses/ &>> output.log || echo "Analyzing..." + fpb-lint ./more/ &>> output.log || echo "Analyzing..." + + cat > error.log + + - name: Clean output + if: ${{ always() && + github.event_name == 'pull_request' }} + uses: actions/github-script@v6 + with: + script: | + const fs = require('fs'); + const readline = require('readline'); + + const file = readline.createInterface({ + input: fs.createReadStream('output.log'), + output: process.stdout, + terminal: false, + }); + + let lastLine = ''; + file.on('line', (line) => { + if (lastLine) { + fs.appendFile('error.log', lastLine, (err) => { + if (err) { + console.error(err); + } + }); + } + + if (line.includes('/home/runner/work/free-programming-books/')) { + lastLine = line.replace('/home/runner/work/free-programming-books/', '') + "\r\n"; + } else if (line.includes('\u26a0')) { + lastLine = '\r\n\r\n'; + } else if (line.includes('remark-lint')) { + lastLine = line + '\r\n'; + } else { + lastLine = null; + } + }); + + file.on('close', () => { + if (!lastLine || lastLine === '\r\n\r\n') { + return; + } + + fs.appendFile('error.log', lastLine, (err) => { + if (err) { + console.error(err); + } + }); + }); + + - name: Print output + if: ${{ always() && + github.event_name == 'pull_request' }} + run: | + cat error.log + + if [ -s error.log ] + then + gh pr review $PR -r -b "Linter failed, fix the error(s): + \`\`\` + $(cat error.log) + \`\`\`" + gh pr edit $PR --add-label "linter error" + else + gh pr review $PR -a + gh pr edit $PR --remove-label "linter error" + fi From 1007933e14d027c196375d414678c8841bb3bba6 Mon Sep 17 00:00:00 2001 From: ImVector <59611597+LuigiImVector@users.noreply.github.com> Date: Wed, 13 Jul 2022 20:19:45 +0200 Subject: [PATCH 2/4] build(fpb-lint): removed unused code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: David Ordás <3125580+davorpa@users.noreply.github.com> --- .github/workflows/fpb-lint.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/fpb-lint.yml b/.github/workflows/fpb-lint.yml index e921218d61e64..89fc45137b670 100644 --- a/.github/workflows/fpb-lint.yml +++ b/.github/workflows/fpb-lint.yml @@ -14,10 +14,6 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Use Python - uses: actions/setup-python@v4 - with: - python-version: '3.x' - name: Use Node.js uses: actions/setup-node@v3 with: From 35b40817fc698cb64ce542c6ca99afd30804f81b Mon Sep 17 00:00:00 2001 From: ImVector <59611597+LuigiImVector@users.noreply.github.com> Date: Wed, 13 Jul 2022 20:37:10 +0200 Subject: [PATCH 3/4] build(fpb-lint): edited command to create an empty file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: David Ordás <3125580+davorpa@users.noreply.github.com> --- .github/workflows/fpb-lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/fpb-lint.yml b/.github/workflows/fpb-lint.yml index 89fc45137b670..8c4ec8f3bca6f 100644 --- a/.github/workflows/fpb-lint.yml +++ b/.github/workflows/fpb-lint.yml @@ -37,7 +37,7 @@ jobs: fpb-lint ./courses/ &>> output.log || echo "Analyzing..." fpb-lint ./more/ &>> output.log || echo "Analyzing..." - cat > error.log + touch error.log - name: Clean output if: ${{ always() && From f16f1e48f8a6d3e7d80060fd0159fd69206fd61e Mon Sep 17 00:00:00 2001 From: ImVector <59611597+LuigiImVector@users.noreply.github.com> Date: Wed, 13 Jul 2022 23:10:23 +0200 Subject: [PATCH 4/4] build: use two different workflows --- .github/workflows/comment-pr.yml | 56 ++++++++++++++++++++++++++++++++ .github/workflows/fpb-lint.yml | 27 +++++---------- 2 files changed, 64 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/comment-pr.yml diff --git a/.github/workflows/comment-pr.yml b/.github/workflows/comment-pr.yml new file mode 100644 index 0000000000000..fd792f1da59a0 --- /dev/null +++ b/.github/workflows/comment-pr.yml @@ -0,0 +1,56 @@ +name: Comment on the pull request + +on: + workflow_run: + workflows: ["free-programming-books-lint"] + types: + - completed + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + +jobs: + upload: + runs-on: ubuntu-latest + if: > + ${{ github.event.workflow_run.event == 'pull_request' && + github.event.workflow_run.conclusion == 'success' }} + steps: + - name: 'Download artifact' + uses: actions/github-script@v6 + with: + script: | + let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: context.payload.workflow_run.id, + }); + let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { + return artifact.name == "pr" + })[0]; + let download = await github.rest.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: matchArtifact.id, + archive_format: 'zip', + }); + let fs = require('fs'); + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr.zip`, Buffer.from(download.data)); + + - name: 'Unzip artifact' + run: unzip pr.zip + + - name: 'Comment on PR' + run: | + cat error.log + if [ -s error.log ] + then + gh pr review $( ./pr/PRurl + mv error.log ./pr/error.log + - uses: actions/upload-artifact@v3 + with: + name: pr + path: pr/