Skip to content

Commit

Permalink
Finish 0.0.5 for issue list, pull request list, commit list
Browse files Browse the repository at this point in the history
  • Loading branch information
eddie32 committed Sep 8, 2024
1 parent 5d96a34 commit 2495a1c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 47 deletions.
89 changes: 45 additions & 44 deletions api/plugins/agent/GithubDataFetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
# @Time : 2024/9/8
# @Author : liuboyuan
# @Description :
import requests
import markdown
import imgkit
from datetime import datetime
from typing import Dict, Optional

import os

from api.plugins.agent.github_client import GitHubClient
from framework.inori_plugin_manager.base_plugin import BasePlugin


Expand All @@ -17,9 +18,10 @@ class GithubDataFetcher(BasePlugin):
def __init__(self) -> None:
self.api_url = "https://api.github.com"
self.headers = {"Accept": "application/vnd.github.v3+json"}
self.query_params: Optional[Dict[str, str]] = None
self.query_params: Optional[str] = None
self.token: Optional[str] = None
self.output_dir = None
self.github_client = None

def configure(self, token: str, output_dir=None) -> None:
"""
Expand All @@ -28,10 +30,11 @@ def configure(self, token: str, output_dir=None) -> None:
"""
self.token = token
self.headers["Authorization"] = f"token {self.token}"
self.github_client = GitHubClient(api_url=self.api_url, headers=self.headers)
if output_dir:
self.output_dir = output_dir

def set_query_params(self, params: Dict[str, str]) -> None:
def set_query_params(self, params: str) -> None:
"""
设置查询参数
:param params: 字典类型的查询参数
Expand All @@ -41,61 +44,49 @@ def set_query_params(self, params: Dict[str, str]) -> None:
def run(self, *args, **kwargs) -> Optional[str]:
"""
实现GitHub API的数据抓取逻辑,并生成报告文件
:param output_dir: 保存报告的目录路径,如果未指定,则保存在当前目录
:param args: 可变参数
:param kwargs: 关键字参数
:return: 返回生成的报告文件路径
"""
import requests

# 确保查询参数已设置
if not self.query_params:
raise ValueError("查询参数未设置,请使用 set_query_params() 方法设置参数")

# 构建完整的API请求URL
url = f"{self.api_url}/search/repositories"

try:
# 发送GET请求到GitHub API,获取热门项目数据
response = requests.get(url, headers=self.headers, params=self.query_params)

# 检查请求是否成功
if response.status_code == 200:
# 获取第一个仓库的数据
repo_data = response.json().get("items", [])[0]

# 生成报告并保存为文件
report_file_path = self.generate_report(repo_data, self.output_dir)

# 返回报告文件的路径
return report_file_path
else:
# 如果请求失败,抛出异常
response.raise_for_status()

except requests.exceptions.RequestException as e:
# 处理请求异常
print(f"请求GitHub API时出错: {e}")
return None
report_file_path = self.generate_report(self.query_params, self.output_dir)
return report_file_path
# 将报告转换为图片
# self.convert_markdown_to_image(report, "report.png")
# print("报告图片已生成并保存为report.png")

# 将报告转换为图片
# self.convert_markdown_to_image(report, "report.png")
# print("报告图片已生成并保存为report.png")

def generate_report(self, repo_data: Dict, output_dir: Optional[str] = None) -> str:
def generate_report(self, query: str, output_dir: Optional[str] = None) -> str:
"""
根据GitHub项目数据生成markdown格式的报告,并保存为文件
:param repo_data: GitHub仓库的数据
:param output_dir: 保存报告的目录路径,如果未指定,则保存在当前目录
:return: 返回markdown格式的报告
"""
repo_name = repo_data.get("name", "N/A")
from datetime import datetime
print(query)
repos = self.github_client.search_repositories(query)
repo_data = None
if "items" in repos and len(repos["items"]) > 0:
repo_data = repos["items"][0]
if repo_data is None:
raise Exception("Github API Failure")
repo_name = repo_data.get("name", "UnknownRepo")
owner = repo_data.get("owner", {}).get("login", "N/A")
stars = repo_data.get("stargazers_count", "N/A")
forks = repo_data.get("forks_count", "N/A")
description = repo_data.get("description", "N/A")
url = repo_data.get("html_url", "N/A")
updated_at = repo_data.get("updated_at", "N/A")
print(repo_name)
print(owner)
# 获取 issues 列表
issues = self.github_client.get_issues(repo_name, owner)
# 获取 commits 列表
commits = self.github_client.get_commits(repo_name, owner)
# 获取 pull requests 列表
pull_requests = self.github_client.get_pull_requests(repo_name, owner)

report = f"""
# GitHub Repository Report: {repo_name}
Expand All @@ -107,23 +98,33 @@ def generate_report(self, repo_data: Dict, output_dir: Optional[str] = None) ->
**URL:** [Link to repository]({url})
**Last Updated:** {updated_at}
## Issues
{issues}
## Commits
{commits}
## Pull Requests
{pull_requests}
Report generated on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
"""

# 生成Markdown文件
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
filename = f"{repo_name}-{timestamp}.md"

print(report)
# 如果指定了路径,则将文件保存在该路径下
if output_dir:
if output_dir is not None:
output_path = f"{output_dir}/{filename}"
else:
output_path = filename

print(output_path)
with open(output_path, 'w', encoding='utf-8') as file:
file.write(report)
print(f"Markdown报告已保存为 {output_path}")

return output_path
return report

def convert_markdown_to_image(self, markdown_text: str, output_file: str) -> None:
"""
Expand All @@ -143,7 +144,7 @@ def convert_markdown_to_image(self, markdown_text: str, output_file: str) -> Non
fetcher.configure(token, output_dir=None) # 替换为你的GitHub Token

# 设置查询参数
fetcher.set_query_params({"q": "repo:langchain-ai/langchain"})
fetcher.set_query_params("repo:langchain-ai/langchain")

fetcher.run()

Expand Down
55 changes: 55 additions & 0 deletions api/plugins/agent/github_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
# @Time : 2024/9/8
# @Author : liuboyuan
# @Description :
import requests
from typing import Dict, List


class GitHubClient:
def __init__(self, api_url: str = "https://api.github.com", headers: Dict[str, str] = None):
self.api_url = api_url
self.headers = headers or {"Accept": "application/vnd.github.v3+json"}

def search_repositories(self, query: str) -> Dict:
"""
搜索 GitHub 仓库
:param query: 搜索查询字符串
:return: 返回搜索结果的 JSON 数据
"""
url = f"{self.api_url}/search/repositories"
response = requests.get(url, headers=self.headers, params={"q": query})
if response.status_code == 200:
return response.json()
else:
raise Exception(f"Unable to fetch repositories: {response.status_code} {response.text}")


def get_issues(self, repo_name: str, owner: str) -> str:
url = f"{self.api_url}/repos/{owner}/{repo_name}/issues"
response = requests.get(url, headers=self.headers)
if response.status_code == 200:
issues = response.json()
return "\n".join(f"- {issue.get('title')} (#{issue.get('number')})" for issue in issues)
else:
return "Unable to fetch issues."

def get_commits(self, repo_name: str, owner: str) -> str:
url = f"{self.api_url}/repos/{owner}/{repo_name}/commits"
response = requests.get(url, headers=self.headers)
if response.status_code == 200:
commits = response.json()
return "\n".join(
f"- {commit.get('commit', {}).get('message')} (by {commit.get('commit', {}).get('author', {}).get('name')})"
for commit in commits)
else:
return "Unable to fetch commits."

def get_pull_requests(self, repo_name: str, owner: str) -> str:
url = f"{self.api_url}/repos/{owner}/{repo_name}/pulls"
response = requests.get(url, headers=self.headers)
if response.status_code == 200:
pulls = response.json()
return "\n".join(f"- {pull.get('title')} (#{pull.get('number')})" for pull in pulls)
else:
return "Unable to fetch pull requests."
4 changes: 1 addition & 3 deletions api/view/trial_sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ def post(self):
print(pm.get_plugin_list())
plugin = pm.get_plugin("api.plugins.agent.githubdatafetcher")
plugin.configure(token=token, output_dir=local_folder)
plugin.set_query_params(
{"q": repo}
)
plugin.set_query_params(repo)
plugin.run()
return self.response_raw(
code=Codes.SUCCESS.code,
Expand Down

0 comments on commit 2495a1c

Please sign in to comment.