|
| 1 | +const path = require('path'); |
| 2 | +const { readPackageUp } = require('read-package-up'); |
| 3 | + |
| 4 | +// @todo |
| 5 | +// Check if maintainer can modify the head |
| 6 | + |
| 7 | +const getFormattedCommits = async (pullRequest, github) => { |
| 8 | + const commitOpts = github.rest.pulls.listCommits.endpoint.merge({ |
| 9 | + owner: pullRequest.base.repo.owner.login, |
| 10 | + repo: pullRequest.base.repo.name, |
| 11 | + pull_number: pullRequest.number, |
| 12 | + }); |
| 13 | + |
| 14 | + const commits = await github.paginate(commitOpts); |
| 15 | + |
| 16 | + // Filter merge commits and commits by asyncapi-bot |
| 17 | + const filteredCommits = commits.filter((commit) => { |
| 18 | + return !commit.commit.message.startsWith('Merge pull request') && !commit.commit.message.startsWith('Merge branch') && !commit.commit.author.name.startsWith('asyncapi-bot') && !commit.commit.author.name.startsWith('dependabot[bot]'); |
| 19 | + }); |
| 20 | + |
| 21 | + return filteredCommits.map((commit) => { |
| 22 | + return { |
| 23 | + commit_sha: commit.sha.slice(0, 7), // first 7 characters of the commit sha is enough to identify the commit |
| 24 | + commit_message: commit.commit.message, |
| 25 | + }; |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +const getReleasedPackages = async (pullRequest, github) => { |
| 30 | + const files = await github.paginate(github.rest.pulls.listFiles.endpoint.merge({ |
| 31 | + owner: pullRequest.base.repo.owner.login, |
| 32 | + repo: pullRequest.base.repo.name, |
| 33 | + pull_number: pullRequest.number, |
| 34 | + })); |
| 35 | + |
| 36 | + const releasedPackages = []; |
| 37 | + const ignoredFiles = ['README.md', 'CHANGELOG.md', './changeset/README.md', 'package.json', 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml']; |
| 38 | + for (const file of files) { |
| 39 | + if (!ignoredFiles.includes(file.filename)) { |
| 40 | + const cwd = path.resolve(path.dirname(file.filename)); |
| 41 | + const pack = await readPackageUp({ cwd }); |
| 42 | + if (pack && pack?.packageJson?.name && !releasedPackages.includes(pack.packageJson.name)) { |
| 43 | + releasedPackages.push(pack.packageJson.name); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + console.debug('Filenames', files.map((file) => file.filename)); |
| 49 | + return releasedPackages; |
| 50 | +} |
| 51 | + |
| 52 | +const getReleaseNotes = async (pullRequest, github) => { |
| 53 | + const commits = await getFormattedCommits(pullRequest, github); |
| 54 | + /** |
| 55 | + * Release notes are generated from the commits. |
| 56 | + * Format: |
| 57 | + * - title |
| 58 | + * - commit_sha: commit_message (Array of commits) |
| 59 | + */ |
| 60 | + const releaseNotes = pullRequest.title + '\n\n' + commits.map((commit) => { |
| 61 | + return `- ${commit.commit_sha}: ${commit.commit_message}`; |
| 62 | + }).join('\n'); |
| 63 | + |
| 64 | + return releaseNotes; |
| 65 | +} |
| 66 | + |
| 67 | +const getChangesetContents = async (pullRequest, github) => { |
| 68 | + const title = pullRequest.title; |
| 69 | + const releaseType = title.split(':')[0]; |
| 70 | + let releaseVersion = 'patch'; |
| 71 | + switch (releaseType) { |
| 72 | + case 'fix': |
| 73 | + releaseVersion = 'patch'; |
| 74 | + case 'feat': |
| 75 | + releaseVersion = 'minor'; |
| 76 | + case 'fix!': |
| 77 | + releaseVersion = 'major'; |
| 78 | + case 'feat!': |
| 79 | + releaseVersion = 'major'; |
| 80 | + default: |
| 81 | + releaseVersion = 'patch'; |
| 82 | + } |
| 83 | + |
| 84 | + const releaseNotes = await getReleaseNotes(pullRequest, github); |
| 85 | + const releasedPackages = await getReleasedPackages(pullRequest, github); |
| 86 | + |
| 87 | + if (releasedPackages.length === 0) { |
| 88 | + console.debug('No packages released'); |
| 89 | + return ''; |
| 90 | + } |
| 91 | + console.debug('Released packages', releasedPackages); |
| 92 | + console.debug('Release notes', releaseNotes); |
| 93 | + |
| 94 | + const changesetContents = `---\n` + releasedPackages.map((pkg) => { |
| 95 | + return `'${pkg}': ${releaseVersion}`; |
| 96 | + }).join('\n') + `\n---\n\n${releaseNotes}\n\n` |
| 97 | + |
| 98 | + return changesetContents; |
| 99 | +}; |
| 100 | + |
| 101 | +/** |
| 102 | + * This function checks if a comment has already been created by the workflow. |
| 103 | + * If not, it creates a comment with the changeset. |
| 104 | + * If it is already created, it updates the comment with the new changeset. |
| 105 | + */ |
| 106 | +const commentWorkflow = async (pullRequest, github, changesetContents) => { |
| 107 | + const body = `#### Changeset has been generated for this PR as part of auto-changeset workflow.\n\n<details><summary>Please review the changeset before merging the PR.</summary>\n\n\`\`\`\n${changesetContents}\`\`\`\n\n</details>\n\n[If you are a maintainer or the author of the PR, you can change the changeset by clicking here](https://github.com/${pullRequest.head.repo.full_name}/edit/${pullRequest.head.ref}/.changeset/${pullRequest.number}.md)\n\n> [!TIP]\n> If you don't want auto-changeset to run on this PR, you can add the label \`skip-changeset\` to the PR or remove the changeset and change PR title to something other than \`fix:\` or \`feat:\`.`; |
| 108 | + |
| 109 | + const comments = await github.rest.issues.listComments({ |
| 110 | + owner: pullRequest.base.repo.owner.login, |
| 111 | + repo: pullRequest.base.repo.name, |
| 112 | + issue_number: pullRequest.number, |
| 113 | + }); |
| 114 | + |
| 115 | + const comment = comments.data.find((comment) => comment.body.includes('Changeset has been generated for this PR as part of auto-changeset workflow.')); |
| 116 | + if (comment) { |
| 117 | + await github.rest.issues.updateComment({ |
| 118 | + owner: pullRequest.base.repo.owner.login, |
| 119 | + repo: pullRequest.base.repo.name, |
| 120 | + comment_id: comment.id, |
| 121 | + body: body, |
| 122 | + }); |
| 123 | + } else { |
| 124 | + await github.rest.issues.createComment({ |
| 125 | + owner: pullRequest.base.repo.owner.login, |
| 126 | + repo: pullRequest.base.repo.name, |
| 127 | + issue_number: pullRequest.number, |
| 128 | + body: body, |
| 129 | + user: 'asyncapi-bot', |
| 130 | + }); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +module.exports = { |
| 135 | + getChangesetContents, |
| 136 | + commentWorkflow, |
| 137 | +}; |
0 commit comments