|
3 | 3 | from datetime import datetime |
4 | 4 | from time import sleep |
5 | 5 | from typing import Generator |
6 | | - |
| 6 | +import os |
| 7 | +from typing import Optional |
7 | 8 | import pytz |
8 | 9 | import requests |
9 | 10 |
|
@@ -43,81 +44,70 @@ class IssueDataWithComment(IssueData): |
43 | 44 | comment_author_email: str = '' |
44 | 45 |
|
45 | 46 |
|
46 | | -def get_connected_pulls(issue_number, repo_owner, repo_name, token): |
47 | | - # TODO как-то заменить |
48 | | - return |
49 | | - access_token = token |
50 | | - repo_owner = repo_owner.login |
51 | | - # Формирование запроса GraphQL |
52 | | - query = """ |
53 | | - { |
54 | | - repository(owner: "%s", name: "%s") { |
55 | | - issue(number: %d) { |
56 | | - timelineItems(first: 50, itemTypes:[CONNECTED_EVENT,CROSS_REFERENCED_EVENT]) { |
57 | | - filteredCount |
58 | | - nodes { |
59 | | - ... on ConnectedEvent { |
60 | | - ConnectedEvent: subject { |
61 | | - ... on PullRequest { |
62 | | - number |
63 | | - title |
64 | | - url |
65 | | - } |
66 | | - } |
67 | | - } |
68 | | - ... on CrossReferencedEvent { |
69 | | - CrossReferencedEvent: source { |
70 | | - ... on PullRequest { |
71 | | - number |
72 | | - title |
73 | | - url |
74 | | - } |
75 | | - } |
76 | | - } |
77 | | - } |
78 | | - } |
79 | | - } |
80 | | - } |
81 | | - }""" % ( |
82 | | - repo_owner, |
83 | | - repo_name, |
84 | | - issue_number, |
85 | | - ) |
86 | | - |
87 | | - # Формирование заголовков запроса |
| 47 | +def get_connected_pulls( |
| 48 | + issue_number: int, |
| 49 | + repo_owner: str, |
| 50 | + repo_name: str, |
| 51 | + forgejo_token: Optional[str] = None |
| 52 | +) -> str: |
| 53 | + |
| 54 | + base_url = os.getenv('FORGEJO_BASE_URL') |
| 55 | + if not base_url: |
| 56 | + raise ValueError("FORGEJO_BASE_URL environment variable must be set") |
| 57 | + |
| 58 | + token = forgejo_token or os.getenv('FORGEJO_TOKEN') |
| 59 | + if not token: |
| 60 | + raise ValueError( |
| 61 | + "Forgejo API token is required. " |
| 62 | + "Set FORGEJO_TOKEN environment variable or pass forgejo_token parameter" |
| 63 | + ) |
| 64 | + |
88 | 65 | headers = { |
89 | | - "Authorization": f"Bearer {access_token}", |
90 | | - "Content-Type": "application/json", |
| 66 | + "Authorization": f"token {token}", |
| 67 | + "Accept": "application/json" |
91 | 68 | } |
| 69 | + |
| 70 | + connected_prs = set() |
| 71 | + api_base = f"{base_url}/api/v1/repos/{repo_owner}/{repo_name}" |
| 72 | + |
| 73 | + try: |
| 74 | + comments_response = requests.get( |
| 75 | + f"{api_base}/issues/{issue_number}/comments", |
| 76 | + headers=headers |
| 77 | + ) |
| 78 | + comments_response.raise_for_status() |
| 79 | + |
| 80 | + for comment in comments_response.json(): |
| 81 | + body = comment.get("body", "") |
| 82 | + if not body: |
| 83 | + continue |
| 84 | + |
| 85 | + for word in body.split(): |
| 86 | + clean_word = word.strip(".,:;!?()[]{}") |
| 87 | + if len(clean_word) > 1 and clean_word[1:].isdigit(): |
| 88 | + if clean_word.startswith('#'): |
| 89 | + pr_num = clean_word[1:] |
| 90 | + connected_prs.add(f"{base_url}/{repo_owner}/{repo_name}/pulls/{pr_num}") |
| 91 | + elif clean_word.startswith('!'): |
| 92 | + pr_num = clean_word[1:] |
| 93 | + connected_prs.add(f"{base_url}/{repo_owner}/{repo_name}/pulls/{pr_num}") |
| 94 | + |
| 95 | + prs_response = requests.get( |
| 96 | + f"{api_base}/pulls?state=all", |
| 97 | + headers=headers |
| 98 | + ) |
| 99 | + prs_response.raise_for_status() |
| 100 | + |
| 101 | + for pr in prs_response.json(): |
| 102 | + if f"#{issue_number}" in pr.get("body", ""): |
| 103 | + connected_prs.add(pr.get("html_url")) |
| 104 | + |
| 105 | + except requests.exceptions.RequestException as e: |
| 106 | + print(f"[Warning] Failed to fetch connected PRs: {str(e)}") |
| 107 | + return 'Empty field' |
| 108 | + |
| 109 | + return ';'.join(sorted(connected_prs)) if connected_prs else 'Empty field' |
92 | 110 |
|
93 | | - # Отправка запроса GraphQL |
94 | | - response = requests.post( |
95 | | - "https://api.github.com/graphql", |
96 | | - headers=headers, |
97 | | - data=json.dumps({"query": query}), |
98 | | - ) |
99 | | - response_data = response.json() |
100 | | - # Обработка полученных данных |
101 | | - pull_request_data = response_data["data"]["repository"]["issue"] |
102 | | - list_url = [] |
103 | | - if pull_request_data is not None: |
104 | | - issues_data = pull_request_data["timelineItems"]["nodes"] |
105 | | - for pulls in issues_data: |
106 | | - if ( |
107 | | - pulls.get("CrossReferencedEvent") is not None |
108 | | - and pulls.get("CrossReferencedEvent").get("url") not in list_url |
109 | | - ): |
110 | | - list_url.append(pulls.get("CrossReferencedEvent").get("url")) |
111 | | - if ( |
112 | | - pulls.get("ConnectedEvent") is not None |
113 | | - and pulls.get("ConnectedEvent").get("url") not in list_url |
114 | | - ): |
115 | | - list_url.append(pulls.get("ConnectedEvent").get("url")) |
116 | | - if list_url == []: |
117 | | - return 'Empty field' |
118 | | - else: |
119 | | - return ';'.join(list_url) |
120 | | - return 'Empty field' |
121 | 111 |
|
122 | 112 |
|
123 | 113 | def log_repository_issues( |
|
0 commit comments