2
2
import shutil
3
3
import tempfile
4
4
import psycopg2
5
- from git import Repo
6
5
from github import Github
7
6
from datetime import datetime
8
7
import time
9
- import subprocess
10
8
11
9
start_time = time .time ()
12
10
@@ -66,14 +64,15 @@ def create_commits_table(conn, cur, table_name):
66
64
print (f"Tables creating: an error occurred while trying to create a table { table_name } in the database: { e } " )
67
65
68
66
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
77
76
78
77
79
78
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):
87
86
tmp_dir = tempfile .mkdtemp ()
88
87
89
88
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 ()
111
112
112
113
except Exception as e :
113
114
print (f"Last commit: an error occurred while processing repo { repo .name } : { str (e )} " )
0 commit comments