-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.py
72 lines (61 loc) · 2.02 KB
/
sync.py
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
import requests as r
import re
import time
import json
# configuration.py contains the authentication credentials. See configuration_example.py
import configuration as cfg
API_endpoint = 'https://api.github.com'
## SYNC forks to upstream
# GET https://api.github.com/repos/gb-archive/awesome-gbdev
# f.json()['clone_url'][:-4]
#
url = API_endpoint + '/orgs' + '/gb-archive' + '/repos'
repos = []
currentPage = 0
while True:
params = {'page' : currentPage}
f = r.request('GET', url=url, params=params, auth=(cfg.credentials['user'], cfg.credentials['token']))
if (f.status_code != 200):
print(f.json())
break
if (f.json() == []):
print('Found',len(repos),'repositories')
break
currentPage += 1
for repo in f.json():
repos.append(repo["full_name"])
print('Added',len(f.json()),'repos')
a = False
for repo in repos:
if (repo == 'gb-archive/ems-flasher2'):
a = True
if (a == False):
print('skipped')
continue
print('\nSyncing', repo)
# Get the upstream parent repository
url = API_endpoint + '/repos/' + repo
f = r.request('GET', url=url, auth=(cfg.credentials['user'], cfg.credentials['token']))
try:
parentRepo = f.json()['source']['full_name']
except KeyError:
print('Source repository not found. Trying with the parent')
try:
parentRepo = f.json()['parent']['full_name']
except KeyError:
print('No parent or source repo found, skipping.')
continue
print('Found parent repository', parentRepo)
# Fetch upstream ref of last commit
url = API_endpoint + '/repos/' + parentRepo + '/git/refs/heads/master'
f = r.request('GET', url=url, auth=(cfg.credentials['user'], cfg.credentials['token']))
if (f.status_code != 200):
print('Upstream repo not available')
continue
ref = f.json()['object']['sha']
print("Got upstream ref", ref)
# Fast-forward our repo
url = API_endpoint + '/repos/' + repo + '/git/refs/heads/master'
f = r.request('PATCH', url=url, data = json.dumps({'sha':ref}), auth=(cfg.credentials['user'], cfg.credentials['token']))
if (f.status_code == 200):
print('Fork synced succesfully')