How to Build a Simple GitHub Bot with Cursor in 1 Hour
How to Build a Simple GitHub Bot with Cursor in 1 Hour
If you’re like me, you’ve probably found yourself wishing for a way to automate those repetitive tasks on GitHub. Maybe you want to label issues automatically, respond to pull requests, or even send reminders for code reviews. Building a GitHub bot can sound daunting, but with the right tools, it can be done in just one hour. In this guide, I’ll walk you through how to create a simple GitHub bot using Cursor—an AI coding tool that has been making waves for its ease of use and efficiency.
Why Use Cursor for Your GitHub Bot?
Cursor is an AI coding assistant that helps you write and debug code faster. It’s particularly good for automating tasks and can integrate directly with your development environment. Here’s why I recommend it:
- Time Efficiency: Cursor can generate code snippets, which saves you time writing boilerplate code.
- Error Reduction: It helps catch bugs early, especially useful when working with GitHub APIs.
- User-Friendly: Even if you’re not a coding expert, Cursor makes it easier to implement complex logic.
Prerequisites
Before diving in, make sure you have the following:
- GitHub Account: You’ll need access to create a repository.
- Node.js Installed: Download from nodejs.org if you don’t have it.
- Cursor Account: Sign up at cursor.so.
- Basic JavaScript Knowledge: Familiarity with JavaScript will help, but I’ll guide you through the tricky parts.
Step-by-Step Guide to Building Your GitHub Bot
Step 1: Set Up Your GitHub Repository
- Go to GitHub and create a new repository.
- Name it something like
github-bot-example. - Initialize it with a README file.
Step 2: Create Your Bot Script
- Open your terminal and navigate to your repository folder.
- Run
npm init -yto create apackage.jsonfile. - Install the required packages:
npm install axios dotenv - Create a new file named
bot.js.
Step 3: Write Your Bot Logic
Here’s a simple bot that labels issues based on keywords in the title:
require('dotenv').config();
const axios = require('axios');
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const REPO_OWNER = 'your-github-username';
const REPO_NAME = 'github-bot-example';
async function labelIssues() {
const response = await axios.get(`https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/issues`, {
headers: {
Authorization: `token ${GITHUB_TOKEN}`
}
});
const issues = response.data;
for (let issue of issues) {
if (issue.title.includes('bug')) {
await axios.post(`https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/issues/${issue.number}/labels`, {
labels: ['bug']
}, {
headers: {
Authorization: `token ${GITHUB_TOKEN}`
}
});
}
}
}
labelIssues();
Step 4: Configure Your Environment Variables
- Create a
.envfile in your project directory. - Add your GitHub token:
GITHUB_TOKEN=your_github_token
Step 5: Run Your Bot
- In your terminal, run:
node bot.js - Check your GitHub issues to see if the labels were applied correctly.
Troubleshooting Common Issues
- Invalid Token Error: Make sure your GitHub token has the correct permissions (repo access).
- Network Issues: Ensure that your internet connection is stable.
- API Rate Limiting: GitHub APIs have rate limits. If you hit a limit, you’ll need to wait before making more requests.
What's Next?
Now that you have a basic GitHub bot, consider expanding its functionality. You could add features like:
- Responding to pull requests with comments.
- Automatically closing issues after a certain period.
- Integrating with Slack for notifications.
Conclusion: Start Here
Building a GitHub bot using Cursor is a practical way to automate your workflows. This guide should take you about an hour to complete. Remember, while this bot is simple, the potential for automation is vast.
If you’re looking to dive deeper into AI coding tools, check out our podcast, Built This Week, where we discuss more tools and techniques for builders like you!
Follow Our Building Journey
Weekly podcast episodes on tools we're testing, products we're shipping, and lessons from building in public.