GitHub operation client
| 4 | from typing import Optional, Dict, List |
| 5 | import time |
| 6 | class GitHubClient: |
| 7 | """GitHub operation client""" |
| 8 | |
| 9 | def __init__(self, token: Optional[str] = None): |
| 10 | """ |
| 11 | Initialize the GitHub client |
| 12 | |
| 13 | Args: |
| 14 | token: GitHub Personal Access Token, if None, try to get from environment variable |
| 15 | """ |
| 16 | self.token = token or os.getenv('GITHUB_AI_TOKEN') |
| 17 | if not self.token: |
| 18 | raise ValueError("GitHub Token is required, please provide it via the token parameter or set the GITHUB_AI_TOKEN environment variable.") |
| 19 | |
| 20 | self.session = requests.Session() |
| 21 | self.session.headers.update({ |
| 22 | 'Authorization': f'token {self.token}', |
| 23 | 'Accept': 'application/vnd.github.v3+json' |
| 24 | }) |
| 25 | self.api_base = 'https://api.github.com' |
| 26 | |
| 27 | def check_auth(self) -> dict: |
| 28 | """ |
| 29 | Check if the token is valid |
| 30 | """ |
| 31 | try: |
| 32 | response = self.session.get(f'{self.api_base}/user') |
| 33 | response.raise_for_status() |
| 34 | return {'status': 0, 'message': 'Authentication successful', 'user': response.json()} |
| 35 | except Exception as e: |
| 36 | return {'status': -1, 'message': f'Authentication failed: {str(e)}'} |
| 37 | |
| 38 | def create_pull_request(self, repo: str, title: str, body: str, head: str, base: str) -> dict: |
| 39 | """ |
| 40 | Create a Pull Request |
| 41 | |
| 42 | Args: |
| 43 | repo: The full name of the repository (e.g., 'owner/repo') |
| 44 | title: The PR title |
| 45 | body: The PR description |
| 46 | head: The source branch |
| 47 | base: The target branch |
| 48 | """ |
| 49 | try: |
| 50 | url = f'{self.api_base}/repos/{repo}/pulls' |
| 51 | data = { |
| 52 | 'title': title, |
| 53 | 'body': body, |
| 54 | 'head': head, |
| 55 | 'base': base |
| 56 | } |
| 57 | response = self.session.post(url, json=data) |
| 58 | response.raise_for_status() |
| 59 | pr_data = response.json() |
| 60 | return { |
| 61 | 'status': 0, |
| 62 | 'message': f'PR created successfully: {pr_data["html_url"]}', |
| 63 | 'pr_url': pr_data['html_url'] |
no outgoing calls
no test coverage detected