Skip to content

Commit e8fbce9

Browse files
authored
Show links to access generated code easily in PR (#76)
In the `Show diff` step, all jobs display the differences of the generated code. While some members have navigated by opening the GitHub Actions job URL, it should be more convenient to comment directly on the PR. This change supports that. (Additional Notes on the Comments) 1. Since the generated code may be updated with each additional commit, the job deletes old comments containing old job URLs if present. 2. Some generators are incomplete, so the comment shows tasks directed to the person who updated the OpenAPI YAML, especially when `webhook.yml` is updated.
1 parent ae45c77 commit e8fbce9

File tree

7 files changed

+441
-5
lines changed

7 files changed

+441
-5
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: 'Post and Manage PR Comments'
2+
description: 'Posts comments to PR and deletes previous ones for the same job'
3+
inputs:
4+
language:
5+
description: 'The language of the SDK'
6+
required: true
7+
github-token:
8+
description: 'The GitHub token'
9+
required: true
10+
11+
outputs: {}
12+
13+
runs:
14+
using: 'composite'
15+
steps:
16+
- name: check current directory
17+
run: pwd
18+
shell: bash
19+
- name: Install dependencies
20+
run: npm ci
21+
shell: bash
22+
working-directory: ${{ github.action_path }}
23+
24+
- name: Post PR comment
25+
run: node ${{ github.action_path }}/main.js
26+
shell: bash
27+
env:
28+
LANGUAGE: ${{ inputs.language }}
29+
GITHUB_TOKEN: ${{ inputs.github-token }}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const github = require('@actions/github');
2+
const core = require('@actions/core');
3+
4+
async function run() {
5+
try {
6+
const token = process.env.GITHUB_TOKEN;
7+
const language = process.env.LANGUAGE;
8+
9+
const octokit = github.getOctokit(token);
10+
const context = github.context;
11+
12+
const stepName = 'Show diff'
13+
14+
const prNumber = context.payload.pull_request.number;
15+
const runId = context.runId;
16+
const jobName = context.job;
17+
18+
// Delete the previous comment if it exists
19+
const { data: comments } = await octokit.rest.issues.listComments({
20+
owner: context.repo.owner,
21+
repo: context.repo.repo,
22+
issue_number: prNumber,
23+
});
24+
25+
const medadataForComment = `<!-- ${jobName}-comment -->`;
26+
27+
for (const comment of comments) {
28+
if (
29+
comment.user.login === 'github-actions[bot]' &&
30+
comment.body.includes(medadataForComment)
31+
) {
32+
await octokit.rest.issues.deleteComment({
33+
owner: context.repo.owner,
34+
repo: context.repo.repo,
35+
comment_id: comment.id,
36+
});
37+
}
38+
}
39+
40+
// Post the new comment
41+
const { data: { jobs } } = await octokit.rest.actions.listJobsForWorkflowRun({
42+
owner: context.repo.owner,
43+
repo: context.repo.repo,
44+
run_id: runId,
45+
});
46+
47+
const currentJob = jobs.find(job => job.name === jobName);
48+
if (!currentJob) {
49+
throw new Error(`Job ${jobName} not found`);
50+
}
51+
52+
const jobId = currentJob.id;
53+
54+
const step = currentJob.steps.find(step => step.name === stepName);
55+
if (!step) {
56+
throw new Error(`Step '${stepName}' not found`);
57+
}
58+
59+
const stepNumber = currentJob.steps.indexOf(step) + 1;
60+
61+
const url = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}/job/${jobId}?pr=${prNumber}#step:${stepNumber}:1`;
62+
63+
// Create comment as markdown
64+
let fullCommentBody = `## ${language.toUpperCase()} \n` +
65+
`You can check generated code in ${language}\n\n` +
66+
`[Check the diff here](${url})`;
67+
68+
// for warning
69+
if (language === 'ruby' || language === 'php') {
70+
const { data: files } = await octokit.rest.pulls.listFiles({
71+
owner: context.repo.owner,
72+
repo: context.repo.repo,
73+
pull_number: prNumber,
74+
});
75+
76+
const isWebhookEventAdded = files.some(file => file.filename.includes('webhook.yml'));
77+
if (isWebhookEventAdded) {
78+
fullCommentBody += "\n\n⚠️You may need to modify code when a webhook event is added, even when tests are passed." +
79+
`Parser in line-bot-sdk-${language} in not generated automatically.` +
80+
"Please add tests and modify parser manually in each repository before release.";
81+
}
82+
}
83+
84+
await octokit.rest.issues.createComment({
85+
owner: context.repo.owner,
86+
repo: context.repo.repo,
87+
issue_number: prNumber,
88+
body: `${medadataForComment}\n${fullCommentBody}`,
89+
});
90+
} catch (error) {
91+
console.error(error);
92+
core.setFailed(`Error in this script: ${error.message}`);
93+
}
94+
}
95+
96+
run();

.github/actions/post-comment-action/package-lock.json

Lines changed: 268 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)