Skip to content
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

Changed printing of simulation results #397

Merged
merged 8 commits into from
Nov 5, 2024
25 changes: 18 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,31 @@ jobs:
USER_UID=1000
USER_GID=1000

- name: Save PR ID
- name: Save merge artifact
if: github.event_name != 'pull_request'
run: |
mkdir -p ./artifact
printf '{
"is_pr": false,
"pr_id": -1
}' >> ./artifact/artifact.json

- name: Save pull request artifact
if: github.event_name == 'pull_request'
env:
PR_ID: ${{ github.event.number }}
run: |
mkdir -p ./pr
echo $PR_ID > ./pr/pr_id
mkdir -p ./artifact
printf '{
"is_pr": true,
"pr_id": $PR_ID
}' >> ./artifact/artifact.json
Comment on lines +89 to +98
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix shell script to properly expand environment variable.

The logic is correct, but there's a potential issue with variable expansion due to single quotes.

Apply this fix to ensure proper variable expansion:

       - name: Save pull request artifact
         if: github.event_name == 'pull_request'
         env:
           PR_ID: ${{ github.event.number }}
         run: |
           mkdir -p ./artifact
-          printf '{
-            "is_pr": true,
-            "pr_id": $PR_ID
-          }' >> ./artifact/artifact.json
+          cat << EOF > ./artifact/artifact.json
+          {
+            "is_pr": true,
+            "pr_id": $PR_ID
+          }
+          EOF
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- name: Save pull request artifact
if: github.event_name == 'pull_request'
env:
PR_ID: ${{ github.event.number }}
run: |
mkdir -p ./pr
echo $PR_ID > ./pr/pr_id
mkdir -p ./artifact
printf '{
"is_pr": true,
"pr_id": $PR_ID
}' >> ./artifact/artifact.json
- name: Save pull request artifact
if: github.event_name == 'pull_request'
env:
PR_ID: ${{ github.event.number }}
run: |
mkdir -p ./artifact
cat << EOF > ./artifact/artifact.json
{
"is_pr": true,
"pr_id": $PR_ID
}
EOF
🧰 Tools
🪛 actionlint

99-99: shellcheck reported issue in this script: SC2016:info:2:8: Expressions don't expand in single quotes, use double quotes for that

(shellcheck)


- name: Upload PR ID
if: github.event_name == 'pull_request'
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: pr_id
path: pr/
name: artifact.json
path: ./artifact/
retention-days: 1

- name: Clean up cache
Expand Down
49 changes: 28 additions & 21 deletions .github/workflows/drive.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ jobs:
run: |
echo "AGENT_VERSION=${AGENT_VERSION}"
echo "COMPOSE_FILE=${COMPOSE_FILE}"
- name: 'Download artifact (PR ID)'
if: github.event_name == 'pull_request'
- name: Download artifact
uses: actions/github-script@v6
with:
script: |
Expand All @@ -31,10 +30,10 @@ jobs:
run_id: context.payload.workflow_run.id,
});
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
return artifact.name == "pr_id"
return artifact.name == "artifact.json"
})[0];
if (!matchArtifact) {
core.setFailed('No pr_id artifact found from the build workflow');
core.setFailed('No artifact found from the build workflow');
return;
}
let download = await github.rest.actions.downloadArtifact({
Expand All @@ -44,23 +43,20 @@ jobs:
archive_format: 'zip',
});
let fs = require('fs');
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr_id.zip`, Buffer.from(download.data));
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/artifact.zip`, Buffer.from(download.data));

- name: 'Unzip artifact (PR ID)'
if: github.event_name == 'pull_request'
- name: Unzip artifact
run: |
unzip pr_id.zip
unzip artifact.zip

- name: Check PR ID
if: github.event_name == 'pull_request'
- name: Return artifact JSON
id: return-artifact-json
uses: actions/github-script@v6
with:
script: |
let issue_number = fs.readFileSync('./pr_id');
if (!issue_number || isNaN(Number(issue_number))) {
core.setFailed(`Invalid PR ID: ${prIdContent}`);
return;
}
let fs = require('fs');
let data = JSON.parse(fs.readFileSync('./artifacts/artifacts.json'));
return data;

- name: Run docker-compose
run: |
Expand All @@ -73,12 +69,10 @@ jobs:
if: always()
run: docker compose down -v
# add rendered JSON as comment to the pull request
- name: Add simulation results as comment
if: github.event_name == 'pull_request'
- name: Create simulation results table
id: simulation-results
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
# this script reads the simulation_results.json and creates a comment on the pull request with the results.
script: |
const fs = require('fs');
// read the simulation results
Expand All @@ -94,13 +88,26 @@ jobs:
let resultsTableDivider = `| --- | --- |`;
// add everything to the resultsTable
resultsTable = resultsTableHeader + '\n' + resultsTableDivider + '\n' + resultsTable.join('\n');
return resultsTable;
result-encoding: string
- name: Print simulation results
run: |
echo "Simulation results\n"
echo "${{ steps.simulation-results.outputs.result }}"
Comment on lines +94 to +96
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix echo command to properly handle escape sequences.

The current echo command may not properly expand the newline character.

Update the command to use printf:

-  echo "Simulation results\n"
-  echo "${{ steps.simulation-results.outputs.result }}"
+  printf "Simulation results\n\n%s\n" "${{ steps.simulation-results.outputs.result }}"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
run: |
echo "Simulation results\n"
echo "${{ steps.simulation-results.outputs.result }}"
run: |
printf "Simulation results\n\n%s\n" "${{ steps.simulation-results.outputs.result }}"
🧰 Tools
🪛 actionlint

94-94: shellcheck reported issue in this script: SC2028:info:1:6: echo may not expand escape sequences. Use printf

(shellcheck)

- name: Add simulation results as comment
if: ${{ steps.return-artifact-json.outputs.result.is_pr }}
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
# this script reads the simulation_results.json and creates a comment on the pull request with the results.
script: |
let issue_number = Number(${{ steps.return-artifact-json.outputs.pr_id }});
// add the results as a comment to the pull request
let issue_number = Number(fs.readFileSync('./pr_id'));
github.rest.issues.createComment({
issue_number: issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
body: "## Simulation results\n" + resultsTable
body: "## Simulation results\n" + ${{ steps.simulation-results.outputs.result }}
});
- name: Prune all images older than 30 days from self-hosted runner
run: docker image prune -a --force --filter "until=720h"
Loading