-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync10.py
More file actions
172 lines (142 loc) · 6.7 KB
/
Copy pathsync10.py
File metadata and controls
172 lines (142 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import os
import git
import yaml
from datetime import datetime
from github import Github, Auth
from pathlib import Path
# ===================== Config =====================
SOURCE_REPOS = [
"Cuprate/randomx-rs"
]
GH_TOKEN3 = os.getenv("GH_TOKEN3")
TARGET_REPO = os.getenv("GITHUB_REPOSITORY")
# ==================================================
auth = Auth.Token(GH_TOKEN3)
g = Github(auth=auth)
repo = git.Repo(".")
origin = repo.remote("origin")
origin.set_url(f"https://{GH_TOKEN3}@github.com/{TARGET_REPO}.git")
def safe_filename(text: str) -> str:
return text.replace("/", "_").replace("\\","_").replace(":","_").replace(" ","_")
def parse_github_object(obj):
return {
"title": obj.title,
"source_url": obj.html_url,
"author": obj.user.login if obj.user else "unknown",
"assignees": [a.login for a in obj.assignees],
"labels": [l.name for l in obj.labels],
"created_at": obj.created_at.isoformat() if obj.created_at else None,
"updated_at": obj.updated_at.isoformat() if obj.updated_at else None,
}
def save_md(file_path: Path, frontmatter: dict, content: str):
file_path.parent.mkdir(exist_ok=True, parents=True)
with open(file_path, "w", encoding="utf-8") as f:
f.write("---\n")
yaml.dump(frontmatter, f, sort_keys=False, allow_unicode=True)
f.write("---\n\n")
f.write(content)
# ===================== Sync Issues =====================
def sync_issues(repo_full_name: str):
print(f"🔍 Starting to sync Issues for {repo_full_name} (all states)")
source_repo = g.get_repo(repo_full_name)
count = 0
for issue in source_repo.get_issues(state="all"):
if hasattr(issue, 'pull_request') and issue.pull_request is not None:
continue
count += 1
print(f"✅ Captured Issue #{issue.number} | Status: {issue.state}")
fm = parse_github_object(issue)
fm.update({
"type": "issue",
"status": issue.state,
"closed_at": issue.closed_at.isoformat() if issue.closed_at else None
})
content = f"# Original Description\n{issue.body or 'No description'}\n\n# Discussion History\n"
for comment in issue.get_comments():
user = comment.user.login if comment.user else "deleted_user"
time = comment.created_at.isoformat() if comment.created_at else "No time"
content += f"## {user} | {time}\n{comment.body or 'No content'}\n\n"
content += "# Action History\n"
content += f"- Created by: {fm['author']} | {fm['created_at']}\n"
if issue.state == "closed":
content += f"- Closed at: {fm['closed_at']}\n"
save_md(Path(f"issues/issue-{issue.number}.md"), fm, content)
print(f"🎉 Sync completed! Total pure Issues captured: {count}\n")
# ===================== Sync Pull Requests =====================
def sync_pull_requests(repo_full_name: str):
print(f"🔍 Starting to sync Pull Requests for {repo_full_name}")
source_repo = g.get_repo(repo_full_name)
count = 0
for pr in source_repo.get_pulls(state="all"):
count += 1
fm = parse_github_object(pr)
fm.update({
"type": "pull_request",
"status": "merged" if pr.merged else pr.state,
"closed_at": pr.closed_at.isoformat() if pr.closed_at else None,
"merged_at": pr.merged_at.isoformat() if pr.merged else None
})
content = f"# Original Description\n{pr.body or 'No description'}\n\n# Discussion History\n"
for comment in pr.get_comments():
user = comment.user.login if comment.user else "deleted_user"
time = comment.created_at.isoformat() if comment.created_at else "No time"
content += f"## {user} | {time}\n{comment.body or 'No content'}\n\n"
content += "# Action History\n"
content += f"- Created by: {fm['author']} | {fm['created_at']}\n"
if pr.merged:
content += f"- Merged at: {fm['merged_at']}\n"
elif pr.state == "closed":
content += f"- Closed at: {fm['closed_at']}\n"
save_md(Path(f"pull_requests/pr-{pr.number}.md"), fm, content)
print(f"🎉 Sync completed! Total PRs captured: {count}\n")
# ===================== Sync Releases =====================
def sync_releases(repo_full_name: str):
print(f"🔍 Starting to sync Releases for {repo_full_name}")
source_repo = g.get_repo(repo_full_name)
releases = list(source_repo.get_releases())
for release in releases:
fm = {
"title": release.title or release.tag_name,
"type": "release",
"source_url": release.html_url,
"author": release.author.login if release.author else "unknown",
"tag_name": release.tag_name,
"published_at": release.published_at.isoformat() if release.published_at else None
}
content = f"# Version: {release.tag_name}\n\n# Release Notes\n{release.body or 'No notes'}"
save_md(Path(f"releases/release-{safe_filename(release.tag_name)}.md"), fm, content)
print(f"🎉 Sync completed! Total Releases captured: {len(releases)}\n")
# ===================== Branch Management =====================
def switch_branch(branch_name):
try:
repo.git.checkout(branch_name)
repo.git.fetch("origin", branch_name)
except:
repo.git.checkout("-b", branch_name)
print(f"🆕 Created branch: {branch_name}")
readme = Path("README.md")
if not readme.exists():
with open(readme, "w", encoding="utf-8") as f:
f.write(f"# {branch_name}\nArchive Source: https://github.com/{repo_full_name}\nAuto Sync\n")
# ===================== Main Function =====================
def main():
repo.config_writer().set_value("user", "name", "github-actions[bot]").release()
repo.config_writer().set_value("user", "email", "github-actions[bot]@users.noreply.github.com").release()
for repo_full_name in SOURCE_REPOS:
branch = repo_full_name.split("/")[-1]
print(f"\n========================================")
print(f"Syncing repo: {repo_full_name} -> Branch: {branch}")
print(f"========================================\n")
switch_branch(branch)
sync_issues(repo_full_name)
sync_pull_requests(repo_full_name)
sync_releases(repo_full_name)
repo.git.add(".")
try:
repo.index.commit(f"Update {branch} | {datetime.utcnow().strftime('%Y-%m-%d %H:%M UTC')}")
repo.git.push("--set-upstream", "origin", branch)
print(f"✅ Push successful: {branch}")
except Exception as e:
print(f"ℹ️ Data is already up to date: {branch}")
if __name__ == "__main__":
main()