|
| 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(); |
0 commit comments