-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathforksyncer1.py
More file actions
148 lines (128 loc) · 4.7 KB
/
Copy pathforksyncer1.py
File metadata and controls
148 lines (128 loc) · 4.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
"""
MIT License
Copyright (c) 2022 rtldg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
# /// script
# dependencies = [
# "PyGithub",
# ]
# ///
# Github fork syncer thing...
import github
import datetime
import os
def create_branch(repo, branch_name, sha):
repo.create_git_ref("refs/heads/"+branch_name, sha)
def timey():
return datetime.datetime.now().strftime("_%Y%m%d_%H%M%S")
def sync_branch(repo, branch_name):
try:
comparison = repo.compare(f"{repo.parent.owner.login}:{branch_name}", branch_name)
except github.GithubException as ex:
print(repo.name)
if ex.status != 404:
raise ex
# "No common ancestor between parentrepo:branch and repo:branch..."
old_branch_name = branch_name + timey()
sha = repo.parent.get_branch(branch_name).commit.sha
print(f"{repo.name} syncing {branch_name}. comparison 404, moving to {old_branch_name}. {branch_name} at {sha}")
create_branch(repo, branch_name+timey(), repo.get_branch(branch_name).commit.sha)
ref = repo.get_git_ref(f"heads/{branch_name}")
ref.edit(sha, force=True)
return
if comparison.ahead_by > 0:
if True:
print(f"we're ahead by {comparison.ahead_by} in {repo.name}/{branch_name} so maybe the parent was deleted?")
return
old_branch_name = branch_name + timey()
print(f"{repo.name} syncing {branch_name}. commits AHEAD by: {comparison.ahead_by}. moving to {old_branch_name}")
create_branch(repo, old_branch_name, repo.get_branch(branch_name).commit.sha)
if comparison.behind_by > 0 or comparison.ahead_by > 0:
ref = repo.get_git_ref(f"heads/{branch_name}")
print(f"{repo.name} syncing {branch_name}. behind by: {comparison.behind_by}. ahead by {comparison.ahead_by}")
ref.edit(comparison.base_commit.sha, force=True)
def iter_repos(g, repos, skip_till):
skip = skip_till != ""
for repo in repos:
if repo.name == skip_till:
skip = False
if skip:
continue
try:
if repo.parent == None:
continue
except github.GithubException as ex:
print(f"failed to fetch repo.parent on {repo.name} (status = {ex.status})")
continue
my_branches = {}
try:
for branch in repo.get_branches():
my_branches[branch.name] = branch
except:
print(f"branches from {repo.name} are fucked? skipping")
continue
parent_branches = []
try:
parent_branches = [branch for branch in repo.parent.get_branches()]
except:
print(f"parent_branches from {repo.name} are fucked?")
continue;
for branch in parent_branches:
if branch.name in my_branches:
sync_branch(repo, branch.name)
else:
print(f"{repo.name} creating new branch {branch.name} at {branch.commit.sha}")
create_branch(repo, branch.name, branch.commit.sha)
def main():
if not "GH_ACCESS_TOKEN" in os.environ:
with open("../forksyncertoken.secret") as f:
os.environ["GH_ACCESS_TOKEN"] = f.read().strip()
g = github.Github(os.environ["GH_ACCESS_TOKEN"])
if False:
disable_all_actions(g)
return
skip_till = ""
if skip_till == "":
iter_repos(g, g.get_organization("PMArkive2").get_repos(), skip_till)
iter_repos(g, g.get_user().get_repos(), skip_till)
def disable_all_actions(g):
last_actions_disable = datetime.datetime(2025,3,25)
login = g.get_user().login
skip = True
for repo in g.get_user().get_repos():
"""
if repo.name == "SurfReplays":
skip = False
if skip:
continue
"""
#"""
if repo.created_at < last_actions_disable:
continue
#"""
# TODO: repo.full_name (but also don't nuke org repo actions elsewhere...)
print(f"/repos/{login}/{repo.name}/actions/permissions")
try:
j = g.requester.requestJson("PUT", f"/repos/{login}/{repo.name}/actions/permissions", input={"enabled": False})
if j[0] != 204:
print(j)
except Exception as e:
print(e)
print(" bad for some reason... continuing though...")
if __name__ == "__main__":
main()