|
1 | | -from time import sleep |
2 | | -import logging |
3 | | -import traceback |
4 | | - |
5 | | -import requests |
6 | | - |
7 | | -from src.GitHubRepoAPI import GitHubRepoAPI |
8 | | -from src.interface_wrapper import ( |
9 | | - RepositoryFactory, |
10 | | - IRepositoryAPI |
11 | | -) |
12 | | -from src.constants import ( |
13 | | - TIMEDELTA, |
14 | | -) |
15 | | - |
16 | | - |
17 | | -def login(token, base_url): |
18 | | - try: |
19 | | - client = RepositoryFactory.create_api(token, base_url) |
20 | | - return client |
21 | | - except Exception as e: |
22 | | - logging.error(e) |
23 | | - logging.error(traceback.format_exc()) |
24 | | - return None |
25 | | - |
26 | | - |
27 | | -def get_tokens_from_file(tokens_path: str) -> list[str]: |
28 | | - with open(tokens_path, 'r') as file: |
29 | | - tokens = [token for token in file.read().split('\n') if token] |
30 | | - |
31 | | - return tokens |
32 | | - |
33 | | - |
34 | | -def get_repos_from_file(repos_path: str) -> list[str]: |
35 | | - with open(repos_path, 'r') as file: |
36 | | - list_repos = [x for x in file.read().split('\n') if x] |
37 | | - |
38 | | - return list_repos |
39 | | - |
40 | | - |
41 | | -class Clients: |
42 | | - def __init__(self, tokens: list[str], base_url: str | None = None): |
43 | | - self.clients = [] |
44 | | - self.token_map = {} |
45 | | - |
46 | | - for token in tokens: |
47 | | - client = login(token, base_url) |
48 | | - if client: |
49 | | - self.clients.append(client) |
50 | | - self.token_map[client] = token |
51 | | - |
52 | | - if not self.clients: |
53 | | - raise Exception("No valid tokens for either GitHub or Forgejo") |
54 | | - |
55 | | - def _get_next_client(self) -> tuple[IRepositoryAPI, str]: |
56 | | - client = None |
57 | | - max_remaining_limit = -1 |
58 | | - |
59 | | - for c in self.clients: |
60 | | - remaining, _ = c.get_rate_limiting() |
61 | | - if remaining > max_remaining_limit: |
62 | | - client = c |
63 | | - max_remaining_limit = remaining |
64 | | - sleep(TIMEDELTA) |
65 | | - |
66 | | - if client is None: |
67 | | - raise Exception("No git clients available") |
68 | | - return client, self.token_map[client] |
69 | | - |
70 | | - def get_next_client(self) -> tuple[IRepositoryAPI, str]: |
71 | | - return self._get_next_client() |
72 | | - |
73 | | - |
74 | | -def get_next_binded_repo(clients: Clients, repositories: list[str]): |
75 | | - for repo_name in repositories: |
76 | | - try: |
77 | | - client, token = clients.get_next_client() |
78 | | - repo = client.get_repository(repo_name) |
79 | | - except Exception as err: |
80 | | - print(f'git_logger.get_next_binded_repo(): error {err}') |
81 | | - print(f'git_logger.get_next_binded_repo(): failed to load repository "{repo_name}"') |
82 | | - else: |
83 | | - yield client, repo, token |
84 | | - |
85 | | - |
86 | | -def get_assignee_story(git_object, client, token, repository): |
87 | | - assignee_result = "" |
88 | | - |
89 | | - try: |
90 | | - repo_owner = repository.owner.login |
91 | | - repo_name = repository.name |
92 | | - issue_index = git_object._id # Для pull request и issue одинаково |
93 | | - |
94 | | - base_url = client.get_base_url().rstrip('/') |
95 | | - |
96 | | - url = f"{base_url}/repos/{repo_owner}/{repo_name}/issues/{issue_index}/timeline" |
97 | | - headers = { |
98 | | - "Authorization": f"Bearer {token}" if client is GitHubRepoAPI else f"token {token}", |
99 | | - "Accept": "application/json" |
100 | | - } |
101 | | - |
102 | | - response = requests.get(url, headers=headers) |
103 | | - if response.status_code != 200: |
104 | | - raise Exception(f"Failed to fetch issue timeline: {response.status_code}, {response.text}") |
105 | | - |
106 | | - events = response.json() |
107 | | - |
108 | | - for event in events: |
109 | | - if event.get('event') in ["assigned", "unassigned"]: |
110 | | - date = event.get('created_at') |
111 | | - assigner = event.get('actor', {}).get('login', 'unknown') |
112 | | - assignee = event.get('assignee', {}).get('login', 'unknown') |
113 | | - |
114 | | - assignee_result += f"{date}: {assigner} -" |
115 | | - if event['event'] == "unassigned": |
116 | | - assignee_result += "/" |
117 | | - assignee_result += f"> {assignee}; " |
118 | | - |
119 | | - sleep(TIMEDELTA) |
120 | | - |
121 | | - except Exception as e: |
122 | | - print(f"get_assignee_story(): error {e}") |
123 | | - |
124 | | - return assignee_result |
| 1 | +from time import sleep |
| 2 | +import logging |
| 3 | +import traceback |
| 4 | + |
| 5 | +import requests |
| 6 | + |
| 7 | +from src.GitHubRepoAPI import GitHubRepoAPI |
| 8 | +from src.interface_wrapper import ( |
| 9 | + RepositoryFactory, |
| 10 | + IRepositoryAPI |
| 11 | +) |
| 12 | +from src.constants import ( |
| 13 | + TIMEDELTA, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +def login(token, base_url): |
| 18 | + try: |
| 19 | + client = RepositoryFactory.create_api(token, base_url) |
| 20 | + return client |
| 21 | + except Exception as e: |
| 22 | + logging.error(e) |
| 23 | + logging.error(traceback.format_exc()) |
| 24 | + return None |
| 25 | + |
| 26 | + |
| 27 | +def get_tokens_from_file(tokens_path: str) -> list[str]: |
| 28 | + with open(tokens_path, 'r') as file: |
| 29 | + tokens = [token for token in file.read().split('\n') if token] |
| 30 | + |
| 31 | + return tokens |
| 32 | + |
| 33 | + |
| 34 | +def get_repos_from_file(repos_path: str) -> list[str]: |
| 35 | + with open(repos_path, 'r') as file: |
| 36 | + list_repos = [x for x in file.read().split('\n') if x] |
| 37 | + |
| 38 | + return list_repos |
| 39 | + |
| 40 | + |
| 41 | +class Clients: |
| 42 | + def __init__(self, tokens: list[str], base_url: str | None = None): |
| 43 | + self.clients = [] |
| 44 | + self.token_map = {} |
| 45 | + |
| 46 | + for token in tokens: |
| 47 | + client = login(token, base_url) |
| 48 | + if client: |
| 49 | + self.clients.append(client) |
| 50 | + self.token_map[client] = token |
| 51 | + |
| 52 | + if not self.clients: |
| 53 | + if base_url: |
| 54 | + raise Exception("No valid tokens for either GitHub or Forgejo") |
| 55 | + raise Exception("Make sure that base_url is provided") |
| 56 | + |
| 57 | + def _get_next_client(self) -> tuple[IRepositoryAPI, str]: |
| 58 | + client = None |
| 59 | + max_remaining_limit = -1 |
| 60 | + |
| 61 | + for c in self.clients: |
| 62 | + remaining, _ = c.get_rate_limiting() |
| 63 | + if remaining > max_remaining_limit: |
| 64 | + client = c |
| 65 | + max_remaining_limit = remaining |
| 66 | + sleep(TIMEDELTA) |
| 67 | + |
| 68 | + if client is None: |
| 69 | + raise Exception("No git clients available") |
| 70 | + return client, self.token_map[client] |
| 71 | + |
| 72 | + def get_next_client(self) -> tuple[IRepositoryAPI, str]: |
| 73 | + return self._get_next_client() |
| 74 | + |
| 75 | + |
| 76 | +def get_next_binded_repo(clients: Clients, repositories: list[str]): |
| 77 | + for repo_name in repositories: |
| 78 | + try: |
| 79 | + client, token = clients.get_next_client() |
| 80 | + repo = client.get_repository(repo_name) |
| 81 | + except Exception as err: |
| 82 | + print(f'git_logger.get_next_binded_repo(): error {err}') |
| 83 | + print(f'git_logger.get_next_binded_repo(): failed to load repository "{repo_name}"') |
| 84 | + else: |
| 85 | + yield client, repo, token |
| 86 | + |
| 87 | + |
| 88 | +def get_assignee_story(git_object, client, token, repository): |
| 89 | + assignee_result = "" |
| 90 | + |
| 91 | + try: |
| 92 | + repo_owner = repository.owner.login |
| 93 | + repo_name = repository.name |
| 94 | + issue_index = git_object._id # Для pull request и issue одинаково |
| 95 | + |
| 96 | + base_url = client.get_base_url().rstrip('/') |
| 97 | + |
| 98 | + url = f"{base_url}/repos/{repo_owner}/{repo_name}/issues/{issue_index}/timeline" |
| 99 | + headers = { |
| 100 | + "Authorization": f"Bearer {token}" if client is GitHubRepoAPI else f"token {token}", |
| 101 | + "Accept": "application/json" |
| 102 | + } |
| 103 | + |
| 104 | + response = requests.get(url, headers=headers) |
| 105 | + if response.status_code != 200: |
| 106 | + raise Exception(f"Failed to fetch issue timeline: {response.status_code}, {response.text}") |
| 107 | + |
| 108 | + events = response.json() |
| 109 | + |
| 110 | + for event in events: |
| 111 | + if event.get('event') in ["assigned", "unassigned"]: |
| 112 | + date = event.get('created_at') |
| 113 | + assigner = event.get('actor', {}).get('login', 'unknown') |
| 114 | + assignee = event.get('assignee', {}).get('login', 'unknown') |
| 115 | + |
| 116 | + assignee_result += f"{date}: {assigner} -" |
| 117 | + if event['event'] == "unassigned": |
| 118 | + assignee_result += "/" |
| 119 | + assignee_result += f"> {assignee}; " |
| 120 | + |
| 121 | + sleep(TIMEDELTA) |
| 122 | + |
| 123 | + except Exception as e: |
| 124 | + print(f"get_assignee_story(): error {e}") |
| 125 | + |
| 126 | + return assignee_result |
0 commit comments