2022-03-30 17:32:04 +01:00
|
|
|
name: Code size comment
|
|
|
|
|
|
|
|
on:
|
|
|
|
workflow_run:
|
|
|
|
workflows: [Check code size]
|
|
|
|
types: [completed]
|
|
|
|
|
2022-12-13 18:57:34 +00:00
|
|
|
concurrency:
|
|
|
|
group: ${{ github.workflow }}-${{ github.ref }}
|
|
|
|
cancel-in-progress: true
|
|
|
|
|
2022-03-30 17:32:04 +01:00
|
|
|
jobs:
|
|
|
|
comment:
|
|
|
|
runs-on: ubuntu-20.04
|
|
|
|
steps:
|
|
|
|
- name: 'Download artifact'
|
|
|
|
id: download-artifact
|
|
|
|
uses: actions/github-script@v6
|
|
|
|
with:
|
2022-12-06 17:53:32 +00:00
|
|
|
result-encoding: string
|
2022-03-30 17:32:04 +01:00
|
|
|
script: |
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
run_id: context.payload.workflow_run.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
const matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
|
|
|
|
return artifact.name == "code-size-report"
|
|
|
|
});
|
|
|
|
|
|
|
|
if (matchArtifact.length === 0) {
|
2022-12-06 17:53:32 +00:00
|
|
|
console.log('no matching artifact found');
|
|
|
|
console.log('result: "skip"');
|
|
|
|
|
|
|
|
return 'skip';
|
2022-03-30 17:32:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const download = await github.rest.actions.downloadArtifact({
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
artifact_id: matchArtifact[0].id,
|
|
|
|
archive_format: 'zip',
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/code-size-report.zip`, Buffer.from(download.data));
|
|
|
|
|
2022-12-06 17:53:32 +00:00
|
|
|
console.log('artifact downloaded to `code-size-report.zip`');
|
|
|
|
console.log('result: "ok"');
|
|
|
|
|
|
|
|
return 'ok';
|
2022-03-30 17:32:04 +01:00
|
|
|
- name: 'Unzip artifact'
|
2022-12-06 17:53:32 +00:00
|
|
|
if: steps.download-artifact.outputs.result == 'ok'
|
2022-03-30 17:32:04 +01:00
|
|
|
run: unzip code-size-report.zip
|
|
|
|
- name: Post comment to pull request
|
2022-12-06 17:53:32 +00:00
|
|
|
if: steps.download-artifact.outputs.result == 'ok'
|
2022-03-30 17:32:04 +01:00
|
|
|
uses: actions/github-script@v6
|
|
|
|
with:
|
|
|
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
|
|
|
script: |
|
|
|
|
const fs = require('fs');
|
|
|
|
|
2022-12-19 02:17:30 +00:00
|
|
|
const prNumber = Number(fs.readFileSync('pr_number'));
|
|
|
|
const codeSizeReport = `Code size report:
|
2022-03-30 17:32:04 +01:00
|
|
|
|
|
|
|
\`\`\`
|
|
|
|
${fs.readFileSync('diff')}
|
|
|
|
\`\`\`
|
2022-12-19 02:17:30 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
const comments = await github.paginate(
|
|
|
|
github.rest.issues.listComments,
|
|
|
|
{
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
issue_number: prNumber,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
comments.reverse();
|
|
|
|
|
|
|
|
const previousComment = comments.find(comment =>
|
|
|
|
comment.user.login === 'github-actions[bot]'
|
|
|
|
)
|
|
|
|
|
|
|
|
// if github-actions[bot] already made a comment, update it,
|
|
|
|
// otherwise create a new comment.
|
|
|
|
|
|
|
|
if (previousComment) {
|
|
|
|
await github.rest.issues.updateComment({
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
comment_id: previousComment.id,
|
|
|
|
body: codeSizeReport,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await github.rest.issues.createComment({
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
issue_number: prNumber,
|
|
|
|
body: codeSizeReport,
|
|
|
|
});
|
|
|
|
}
|