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

Chore: [M3-6511] Changeset BOT #9137

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/changeset-bot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Changeset Bot

on:
pull_request:
types: [opened, reopened, synchronize]

jobs:
changeset-bot:
runs-on: ubuntu-latest
name: Changeset Bot

steps:
- uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Run Changeset Checker
uses: ./packages/manager/scripts/changelog
with:
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
pr_number: ${{ github.event.number }}
token: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions packages/manager/.changeset/pr-9137-added-1684448927783.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Added
---

Changeset bot to remind contributors to include a changeset when needed ([#9137](https://github.com/linode/manager/pull/9137))
2 changes: 2 additions & 0 deletions packages/manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"url": "https://github.com/Linode/manager.git"
},
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"@emotion/react": "^11.10.4",
"@emotion/styled": "^11.10.4",
"@linode/api-v4": "*",
Expand Down
18 changes: 18 additions & 0 deletions packages/manager/scripts/changelog/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: 'PR Changeset Action'
description: 'Adds pull request file changes as a comment to a newly opened PR'
inputs:
owner:
description: 'The owner of the repository'
required: true
repo:
description: 'The name of the repository'
required: true
pr_number:
description: 'The number of the pull request'
required: true
token:
description: 'The token to use to access the GitHub API'
required: true
runs:
using: 'node16'
main: 'compiled/index.mjs'
107 changes: 107 additions & 0 deletions packages/manager/scripts/changelog/changeset-bot.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import core from '@actions/core';
import github from '@actions/github';

const runChangesetBot = async () => {
try {
/**
* We need to fetch all the inputs that were provided to our action
* and store them in variables for us to use.
**/
const inputs = {
owner: core.getInput('owner', { required: true }),
repo: core.getInput('repo', { required: true }),
pull_number: core.getInput('pr_number', { required: true }),
token: core.getInput('token', { required: true }),
};
const octokit = new github.getOctokit(inputs.token);

/**
* We need to fetch the PR data from GitHub so we can check if the PR has a changeset.
*/
const PrData = await getCurrentPR(inputs, octokit);

/**
* Check if the PR contains a reference to a changeset
*/
const hasChangesetBotComment = PrData?.includes(`pr-${inputs.pull_number}`);
const comment = hasChangesetBotComment
? `:heavy_check_mark: **Changeset Found!**\n\nCarry on!`
: `:warning: **No Changeset Found**\n\nPR #${inputs.pull_number} does not have a changeset.\nNot every PR needs one, but if this PR is a user-facing change, or is relevant to an "Added", "Fixed", "Changed", "Removed" or "Tech Stories" type, please consider adding one by running \`yarn changeset\``;

/**
* Fetch the comments from GitHub and check if the bot has already commented on the PR.
*/
const comments = await listComments(inputs, octokit);
const hasChangeset = comments.find((comment) =>
comment.body.includes('Changeset Found')
);

await handleComment(inputs, octokit, hasChangeset, comment);
} catch (error) {
console.warn(`An error occurred trying to check for a changeset: ${error}`);
}
};

const getCurrentPR = async (inputs, octokit) => {
const { owner, repo, pull_number } = inputs;

try {
const { data: PrData } = await octokit.rest.pulls.get({
owner,
repo,
pull_number,
mediaType: {
format: 'diff',
},
});

return PrData;
} catch (error) {
console.warn(`Error fetching PR data: ${error}`);
}
};

const listComments = async (inputs, octokit) => {
const { owner, repo, pull_number } = inputs;

try {
const { data: comments } = await octokit.rest.issues.listComments({
owner,
repo,
issue_number: pull_number,
});

return comments;
} catch (error) {
console.warn(`Error fetching comments: ${error}`);
}
};

const handleComment = async (inputs, octokit, hasChangeset, comment) => {
try {
const { owner, repo, pull_number } = inputs;
/**
* If the bot has already commented on the PR, we need to update the comment with the new message,
* else we need to create a new comment.
*/
if (hasChangeset) {
await octokit.rest.issues.updateComment({
owner,
repo,
comment_id: hasChangeset.id,
body: comment,
});
} else {
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: comment,
});
}
} catch (error) {
console.warn(`Error posting/updating comments: ${error}`);
}
};

runChangesetBot();
7 changes: 7 additions & 0 deletions packages/manager/scripts/changelog/compiled/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog BOT

This is the file that the GH Action workflow will consume. Compiling avoids having to install the dependencies in the GH workflow.
It only needs to be generated if changing anything in ../changeset-bot.mjs.

You can compile the script by running `npx @vercel/ncc build scripts/changelog/changeset-bot.mjs -o scripts/changelog/dist/` and moving the
`index.mjs` file here.
Loading