Skip to content

Commit

Permalink
Fix issue #39: [example] Create github PR discord agent
Browse files Browse the repository at this point in the history
  • Loading branch information
openhands-agent committed Jan 29, 2025
1 parent a7bb540 commit 15c6c36
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"discord_token": "NDk5MzE5MjYyMzQwODM4NTY4.XYZ123.abc",
"github_token": "ghp_1234567890abcdef1234567890",
"github_repository": "your-username/your-repository"
}
74 changes: 74 additions & 0 deletions discord_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import discord
from discord.ext import commands
import requests
import json
import os
from dotenv import load_dotenv

load_dotenv()

class GithubAgent(commands.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.github_token = os.getenv("GITHUB_TOKEN")
self.github_repo = os.getenv("GITHUB_REPO")
self.run(os.getenv("DISCORD_TOKEN"))

async def on_ready(self):
print(f'{self.user} has connected to Discord!')

async def submit_issue(self, title, body):
"""Submit an issue to GitHub repository"""
headers = {
"Authorization": f"Bearer {self.github_token}",
"Content-Type": "application/json"
}
data = {
"title": title,
"body": body,
"labels": ["bug"]
}
response = requests.post(
f"https://api.github.com/repos/{self.github_repo}/issues",
headers=headers,
json=data
)
if response.status_code == 201:
return "Issue submitted successfully!"
return f"Failed to submit issue: {response.text}"

async def get_urgent_issues(self):
"""Get urgent issues from GitHub repository"""
headers = {
"Authorization": f"Bearer {self.github_token}"
}
response = requests.get(
f"https://api.github.com/repos/{self.github_repo}/issues?labels=urgent",
headers=headers
)
if response.status_code == 200:
issues = response.json()
return "Urgent Issues:\n" + "\n".join([f"- {issue['title']}" for issue in issues])
return "No urgent issues found or failed to retrieve issues"

intents = discord.Intents.default()
intents.typing = False
intents.presences = False

bot = GithubAgent(command_prefix="!", intents=intents)

@bot.event
async def on_message(message):
if message.author == bot.user:
return

if message.content.startswith("!submit_issue"):
_, title, body = message.content.split(" ", 2)
result = await bot.submit_issue(title, body)
await message.channel.send(result)

if message.content.startswith("!get_urgent"):
result = await bot.get_urgent_issues()
await message.channel.send(result)

bot.run("YOUR_DISCORD_TOKEN")
62 changes: 62 additions & 0 deletions tests/test_discord_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pytest
from discord_agent import GithubAgent
import requests
from unittest.mock import patch

@pytest.fixture
def mock_discord():
with patch('discord.Bot') as mock_bot:
yield mock_bot

def test_submit_issue_success(mock_discord, mocker):
# Mock successful API response
mock_response = mocker.Mock()
mock_response.status_code = 201
mock_response.json.return_value = {"number": 123}

mocker.patch('requests.post', return_value=mock_response)

agent = GithubAgent(command_prefix="!", intents=discord.Intents.default())
result = agent.submit_issue("Test Issue", "This is a test issue")
assert "Issue submitted successfully!" in result

def test_submit_issue_failure(mock_discord, mocker):
# Mock failed API response
mock_response = mocker.Mock()
mock_response.status_code = 401
mock_response.text = "Unauthorized"

mocker.patch('requests.post', return_value=mock_response)

agent = GithubAgent(command_prefix="!", intents=discord.Intents.default())
result = agent.submit_issue("Test Issue", "This is a test issue")
assert "Failed to submit issue" in result

def test_get_urgent_issues_success(mock_discord, mocker):
# Mock successful API response with sample data
mock_response = mocker.Mock()
mock_response.status_code = 200
mock_response.json.return_value = [
{"number": 1, "title": "Issue 1"},
{"number": 2, "title": "Issue 2"}
]

mocker.patch('requests.get', return_value=mock_response)

agent = GithubAgent(command_prefix="!", intents=discord.Intents.default())
result = agent.get_urgent_issues()
assert "Urgent Issues:" in result
assert "Issue 1" in result
assert "Issue 2" in result

def test_get_urgent_issues_failure(mock_discord, mocker):
# Mock failed API response
mock_response = mocker.Mock()
mock_response.status_code = 404
mock_response.text = "Not Found"

mocker.patch('requests.get', return_value=mock_response)

agent = GithubAgent(command_prefix="!", intents=discord.Intents.default())
result = agent.get_urgent_issues()
assert "No urgent issues found" in result

0 comments on commit 15c6c36

Please sign in to comment.