mirror of
https://github.com/karma-riuk/crab.git
synced 2025-07-05 13:38:12 +02:00
finally get the overlapping commits and comments
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
import os, requests, re
|
import os, requests, re
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
import itertools
|
||||||
|
|
||||||
COMMON_HEADERS = {
|
COMMON_HEADERS = {
|
||||||
'Accept': 'application/vnd.github+json',
|
'Accept': 'application/vnd.github+json',
|
||||||
@ -15,9 +16,22 @@ def get_comments(repo_url: str, pr_number: str) -> list[dict]:
|
|||||||
response = github_call(f'{repo_url}/pulls/{pr_number}/comments')
|
response = github_call(f'{repo_url}/pulls/{pr_number}/comments')
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
|
def get_commit(repo_url: str, commit_sha: str) -> dict:
|
||||||
|
response = github_call(f'{repo_url}/commits/{commit_sha}')
|
||||||
|
return response.json()
|
||||||
|
|
||||||
def get_commits(repo_url: str, pr_number: str) -> list[dict]:
|
def get_commits(repo_url: str, pr_number: str) -> list[dict]:
|
||||||
response = github_call(f'{repo_url}/pulls/{pr_number}/commits')
|
response = github_call(f'{repo_url}/pulls/{pr_number}/commits')
|
||||||
return response.json()
|
commits = response.json()
|
||||||
|
for commit in commits:
|
||||||
|
detailed_commit = get_commit(repo_url, commit['sha'])
|
||||||
|
if "files" not in detailed_commit:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for file in detailed_commit['files']:
|
||||||
|
file["patch_range"] = parse_hunk_header(file['patch'])
|
||||||
|
commit["files"] = detailed_commit["files"]
|
||||||
|
return commits
|
||||||
|
|
||||||
def parse_date(date: str) -> datetime:
|
def parse_date(date: str) -> datetime:
|
||||||
return datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ")
|
return datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ")
|
||||||
@ -67,6 +81,26 @@ def augment_comments(comments: list[dict]) -> list[dict]:
|
|||||||
ret.append(new_comment)
|
ret.append(new_comment)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
def is_range_overlapping(range1: dict, range2: dict) -> bool:
|
||||||
|
return range1["start"] <= range2["start"] <= range1["end"] or range2["start"] <= range1["start"] <= range2["end"]
|
||||||
|
|
||||||
|
def get_overlapping_commits_and_comments(commits: list[dict], comments: list[dict]) -> list[tuple[dict, dict]]:
|
||||||
|
ret = []
|
||||||
|
for commit, comment in itertools.product(commits, comments):
|
||||||
|
if "hunk_range" not in comment:
|
||||||
|
continue
|
||||||
|
if "files" not in commit:
|
||||||
|
continue
|
||||||
|
if parse_date(commit['commit']['author']['date']) < parse_date(comment['created_at']):
|
||||||
|
# we can't address a comment if that comment was made after the commit
|
||||||
|
continue
|
||||||
|
for file in commit["files"]:
|
||||||
|
if "patch_range" not in file:
|
||||||
|
continue
|
||||||
|
if file["filename"] == comment["path"]:
|
||||||
|
if is_range_overlapping(file["patch_range"]["old_range"], comment["hunk_range"]["new_range"]):
|
||||||
|
ret.append((commit, comment))
|
||||||
|
return ret
|
||||||
|
|
||||||
def process_pull_request(repo_url: str, pr_number: str) -> bool:
|
def process_pull_request(repo_url: str, pr_number: str) -> bool:
|
||||||
tmp_comments = get_comments(repo_url, pr_number)
|
tmp_comments = get_comments(repo_url, pr_number)
|
||||||
@ -89,18 +123,15 @@ def process_pull_request(repo_url: str, pr_number: str) -> bool:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
for commit in commits:
|
overlapping_commits_and_comments = get_overlapping_commits_and_comments(commits, comments)
|
||||||
print(f"Commit: {commit['sha']}")
|
for commit, comment in overlapping_commits_and_comments:
|
||||||
print(f"Author: {commit['author']['login']}")
|
print(f"Commit: {commit['sha']} address comment {comment['id']}")
|
||||||
print(f"Date: {commit['commit']['author']['date']}")
|
print(f"Commit message: {commit['commit']['message']}")
|
||||||
print(f"Message: {commit['commit']['message']}")
|
print(f"Comment: {comment['body']}")
|
||||||
print("")
|
print()
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
response = github_call('https://api.github.com/repos/cdk/cdk/pulls/1140/commits')
|
response = github_call('https://api.github.com/repos/cdk/cdk/pulls/1140/commits')
|
||||||
response = github_call('https://api.github.com/repos/cdk/cdk/pulls/1140/commits')
|
response = github_call('https://api.github.com/repos/cdk/cdk/pulls/1140/commits')
|
||||||
|
Reference in New Issue
Block a user