Skip to content

build(fpb-lint): linting errors as PR comments, resolved #4416 #6914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/workflows/comment-pr.yml
Original file line number Diff line number Diff line change
@@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
upload:
upload:
permissions:
pull-requests: write

This job would need to explicitly declare write access for pull-request here, I think?
Either that, or in the repo settings someone would have to enable write access by default for all workflows, but that's probably not a good idea for a public repo.

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 $(<PRurl) -r -b "Linter failed, fix the error(s):
\`\`\`
$(cat error.log)
\`\`\`"
gh pr edit $(<PRurl) --add-label "linter error"
else
gh pr review $(<PRurl) -a
gh pr edit $(<PRurl) --remove-label "linter error"
fi
83 changes: 79 additions & 4 deletions .github/workflows/fpb-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,82 @@ jobs:
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/
Comment on lines +21 to +24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fpb-lint ./books/
fpb-lint ./casts/
fpb-lint ./courses/
fpb-lint ./more/
fpb-lint books casts courses 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..."
Comment on lines +30 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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..."
fpb-lint books casts courses more &> output.log

The latest release of fpb-lint allows this to be done in one command.

I don't think it's worthwhile to echo Analyzing, since it will be performed after it's already finished anyway.


touch error.log
Comment on lines +34 to +35
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
touch error.log

If my other suggestions are merged, these line can be deleted.


- 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: Upload artifact
if: ${{ always() &&
github.event_name == 'pull_request' }}
run: |
mkdir -p ./pr
echo ${{ github.event.pull_request.html_url }} > ./pr/PRurl
mv error.log ./pr/error.log
Comment on lines +37 to +91
Copy link
Member

@SethFalco SethFalco Oct 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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: Upload artifact
if: ${{ always() &&
github.event_name == 'pull_request' }}
run: |
mkdir -p ./pr
echo ${{ github.event.pull_request.html_url }} > ./pr/PRurl
mv error.log ./pr/error.log
- name: Clean output and create artifacts
if: ${{ always() &&
github.event_name == 'pull_request' }}
run: |
mkdir -p ./pr
echo ${{ github.event.pull_request.html_url }} > ./pr/PRurl
cat output.log | sed -E 's:/home/runner/work/free-programming-books/|⚠.+::' | uniq > ./pr/error.log

I think this would be a lot simpler with bash instead of JavaScript, what do you think? The suggestion should produce the same output. Furthermore, we can merge it with the Upload artifact task below now that, since the cleanup is just a single line.

Command Description
cat output.log Get the contents of output.log.
sed -E 's:/home/runner/work/free-programming-books/|⚠.+::' Replace /home/runner/work/free-programming-books/ and the warning symbol () and all characters after it until the end of the line with nothing.
uniq Remove redundant line breaks.

- uses: actions/upload-artifact@v3
with:
name: pr
path: pr/