Skip to content

Commit f7148bc

Browse files
authored
Fix last commit info (#36)
Fix last commit info Fix incorrect datestamp for last commit in docs repositories Reviewed-by: Vladimir Vshivkov
1 parent 891e7c5 commit f7148bc

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

last_commit_info.py

+32-31
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
import shutil
33
import tempfile
44
import psycopg2
5-
from git import Repo
65
from github import Github
76
from datetime import datetime
87
import time
9-
import subprocess
108

119
start_time = time.time()
1210

@@ -66,14 +64,15 @@ def create_commits_table(conn, cur, table_name):
6664
print(f"Tables creating: an error occurred while trying to create a table {table_name} in the database: {e}")
6765

6866

69-
def get_last_commit_url(github_repo, git_repo, path):
70-
gh_repo = github_repo.full_name
71-
try:
72-
last_commit_sha = subprocess.check_output(['git', 'log', '-1', '--pretty=format:%H', '--', f':(exclude)**/conf.py', path], cwd=git_repo.working_dir).decode().strip()
73-
last_commit_url = f"https://github.com/{gh_repo}/commit/{last_commit_sha}"
74-
return last_commit_url
75-
except Exception as e:
76-
print(f"SHA: an error occurred while getting last commit URL: {e}")
67+
def get_last_commit_url(github_repo, path):
68+
commits = github_repo.get_commits(path=path)
69+
70+
for commit in commits:
71+
files_changed = commit.files
72+
if not any(file.filename.endswith('conf.py') for file in files_changed):
73+
return commit.html_url, commit.commit.author.date # Return the commit URL and its date
74+
75+
return None, None
7776

7877

7978
def get_last_commit(org, conn, cur, doctype, string, table_name):
@@ -87,27 +86,29 @@ def get_last_commit(org, conn, cur, doctype, string, table_name):
8786
tmp_dir = tempfile.mkdtemp()
8887

8988
try:
90-
cloned_repo = Repo.clone_from(repo.clone_url, tmp_dir)
91-
92-
for path in {doctype}:
93-
try:
94-
last_commit_url = get_last_commit_url(repo, cloned_repo, path)
95-
last_commit_str = cloned_repo.git.log('-1', '--pretty=format:%cd', '--date=short', f':(exclude)*conf.py {path}')
96-
last_commit = datetime.strptime(last_commit_str, '%Y-%m-%d')
97-
now = datetime.utcnow()
98-
duration = now - last_commit
99-
duration_days = duration.days
100-
if doctype == "umn/source":
101-
doc_type = "UMN"
102-
else:
103-
doc_type = "API"
104-
service_name = repo.name
105-
cur.execute(
106-
f'INSERT INTO {table_name} ("Service Name", "Doc Type", "Last commit at", "Days passed", "Commit URL") VALUES (%s, %s, %s, %s, %s);',
107-
(service_name, doc_type, last_commit_str, duration_days, last_commit_url,))
108-
conn.commit()
109-
except Exception as e:
110-
print(f"Last commit: an error occurred while running git log for path {path}: {str(e)}")
89+
90+
path = doctype
91+
last_commit_url, last_commit_date = get_last_commit_url(repo, path)
92+
if last_commit_url and last_commit_date:
93+
# print("*************************************************************************new block of commit")
94+
last_commit_url, _ = get_last_commit_url(repo, path)
95+
# print("last commit url------------------------------------------", last_commit_url)
96+
formatted_commit_date = last_commit_date.strftime('%Y-%m-%d')
97+
# print("LAST COMMIT DATE--------------------------------------", formatted_commit_date)
98+
now = datetime.utcnow()
99+
# print("NOW----------------------------------------", now)
100+
duration = now - last_commit_date
101+
duration_days = duration.days
102+
# print("DURATION DAYS______________________________________________", duration_days)
103+
if doctype == "umn/source":
104+
doc_type = "UMN"
105+
else:
106+
doc_type = "API"
107+
service_name = repo.name
108+
cur.execute(
109+
f'INSERT INTO {table_name} ("Service Name", "Doc Type", "Last commit at", "Days passed", "Commit URL") VALUES (%s, %s, %s, %s, %s);',
110+
(service_name, doc_type, formatted_commit_date, duration_days, last_commit_url,))
111+
conn.commit()
111112

112113
except Exception as e:
113114
print(f"Last commit: an error occurred while processing repo {repo.name}: {str(e)}")

0 commit comments

Comments
 (0)