-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgh_sync
More file actions
executable file
·174 lines (156 loc) · 6.71 KB
/
gh_sync
File metadata and controls
executable file
·174 lines (156 loc) · 6.71 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
173
174
#!/usr/bin/env python3
#
# spell-checker:words GHSYNC Htoken LOGDIR ghsyncrc githuborg
# spell-checker:words gitlaborg gitlabsyncrc levelname
import re
import configparser
import os
import subprocess
import logging
import time
import sys
import github
import gitlab
def readConfig(configFile='~/.ghsyncrc'):
config = configparser.ConfigParser()
config.read(configFile)
# Read in core options
conf = {}
conf['githuborg'] = config.get('global','gitHubOrgName')
conf['gitlaborg'] = config.get('global','gitLabNSpace')
conf['gitlabRepoBase'] = config.get('global','gitLabRepos')
conf['GHtoken'] = config.get('global','gitHubToken')
conf['GLtoken'] = config.get('global','gitLabToken')
conf['GLurl'] = config.get('global','gitLabUrl')
conf['logDir'] = config.get('global','logDir')
return conf
def getRepoConfig(repo, configFile='~/.gitlabsyncrc'):
config = configparser.ConfigParser()
config.read(configFile)
# Return value, with defaults:
conf = {'gitlaborg': config.get('global','gitLabNSpace'),
'gitlabRepo': repo}
if config.has_section(repo):
if config.has_option(repo,'gitLabNSpace'):
conf['gitlaborg'] = config.get(repo,'gitLabNSpace')
if config.has_option(repo,'gitLabRepoName'):
conf['gitlabRepo'] = config.get(repo,'gitLabRepoName')
return conf
def getEnv(env_var):
try:
if os.getenv(env_var, None) is not None:
return os.getenv(env_var)
except Exception as err:
print(f"Error reading environment variable \"{env_var}\": {err}",
file=sys.stderr)
raise
return None
# Main program
def main():
# Looking for multiple configuration files in the following order.
# The first file found will be used:
#
# - gh_sync.conf in the current directory
# - $HOME/.gitlabsyncrc
#
# The following Environment Variables will override the
# configuration files
#
# - GHSYNC_GITLAB_URL
# - GHSYNC_GITLAB_TOKEN
# - GHSYNC_GITLAB_GROUP
# - GHSYNC_GITHUB_ORG
# - GHSYNC_GITHUB_TOKEN
# - GHSYNC_LOGDIR
# The only default value in the configuration is th log directory
config = {
"githuborg": None,
"gitlaborg": None,
"gitlabRepoBase": None,
"GHtoken": None,
"GLtoken": None,
"GLurl": None,
"logDir": os.getcwd()}
configFiles = [
os.path.join(os.getcwd(), "gh_sync.conf"),
os.path.join(os.path.expanduser("~"), ".ghsyncrc")
]
myConfigFile = None
for conf_file in configFiles:
if os.path.exists(conf_file):
myConfigFile = conf_file
config = readConfig(myConfigFile)
break
# Override anything from the environment
config['GLurl'] = getEnv("GHSYNC_GITLAB_URL") or config['GLurl']
config['GLtoken'] = getEnv("GHSYNC_GITLAB_TOKEN") or config['GLtoken']
config['gitlaborg'] = getEnv("GHSYNC_GITLAB_GROUP") or config['gitlaborg']
config['githuborg'] = getEnv("GHSYNC_GITHUB_ORG") or config['githuborg']
config['GHtoken'] = getEnv("GHSYNC_GITHUB_TOKEN") or config['GHtoken']
config['logDir'] = getEnv("GHSYNC_LOGDIR") or config['logDir']
logging.basicConfig(filename=os.path.join(config['logDir'], "gh_sync.log"),
format='%(asctime)s [%(levelname)s] %(message)s',
datefmt='%Y-%m-%dT%H:%M:%S',
level=logging.INFO)
logging.info('*' * 72)
logging.info('Starting gitHub sync')
# Print the configuration (minus the Tokens) to the logs
logging.info(f'GitLab URL: {config['GLurl']}')
logging.info(f'GitLab Group: {config['gitlaborg']}')
logging.info(f'GitHub Organization: {config['githuborg']}')
try:
# Connect to gitHub
logging.info(f"Starting connection to GitHub")
gh = github.Github(token=config['GHtoken'])
except:
logging.critical("Failed to connect to GitHub")
raise
try:
# Connect to gitlab
gl = gitlab.Gitlab(config['GLurl'], config['GLtoken'])
except:
logging.critical(f"Failed to connect to GitLab at {config['GLurl']}")
raise
for ghRepo in gh.getRepos(name=config['githuborg'], type="orgs"):
logging.info('Starting mirror of '+ghRepo.name)
try:
repoConfig = getRepoConfig(ghRepo.name, myConfigFile)
glProject = gl.findProject(repoConfig['gitlabRepo'], repoConfig['gitlaborg'])
if glProject.id == -1:
# No gitlab project found
logging.info(f"Importing new repository {repoConfig['gitlaborg']}/{ghRepo.name} from {ghRepo.full_name}")
# Start the Import
glProject = gl.importGitHub(gh, ghRepo, repoConfig['gitlaborg'])
# A brief pause until we continue
time.sleep(5)
# The previous block will create the project.
if glProject.id != -1:
# Check if the import is complete
import_status = gl.importStatus(glProject)
if import_status == "failed":
# Something happened that needs to be addressed manually
logging.critical(f"Mirror failed for {"/".join([repoConfig['gitlaborg'], ghRepo.name])}. Skipping . . .")
else:
sleep_time = 30
# Wait for the project import to finish before continuing
while import_status != "finished" and sleep_time < 500:
time.sleep(sleep_time)
sleep_time = sleep_time + 60
import_status = gl.importStatus(glProject)
if import_status != "finished":
logging.warning(f"Import of {ghRepo.full_name} is taking too long. Skipping . . .")
else:
# Repository exists, ensure the pull sync is configured
if gl.setPullMirror(glProject, ghRepo.clone_url):
logging.info(f"Project {"/".join([repoConfig['gitlaborg'], ghRepo.name])} configured to pull mirror")
else:
logging.warning(f"Failed to configure project {"/".join([repoConfig['gitlaborg'], ghRepo.name])} for pull mirror")
# Now we should add the GitHub web hook, if we decide to do that.
# ##gh.createWebHook(config['githuborg'], ghRepo.name, gl._api_url)
except:
logging.critical(f'Error occurred during mirror of {"/".join([repoConfig['gitlaborg'], ghRepo.name])}')
else:
logging.info('Ending mirror of '+ghRepo.name)
logging.info('Ending gitHub sync')
if __name__ == "__main__":
main()