Last active
November 9, 2022 18:08
-
-
Save tecandrew/8d58a8e5a526a79d69e01841993336cc to your computer and use it in GitHub Desktop.
GitLab Archive Using Python API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from zipfile import ZipFile | |
import os | |
import time | |
from gitlab import ( | |
Gitlab, | |
) | |
API_TOKEN = '' | |
GROUP = '' # group id | |
SUBGROUP_ID = '' # subgroup id | |
def main(): | |
print("-- gitlab-archive") | |
gl = Gitlab(private_token=API_TOKEN) | |
gl.auth() | |
print("-- getting subgroups") | |
group = gl.groups.get(GROUP) | |
subgroups = group.subgroups.list(get_all=True) | |
# print(subgroups) | |
for group in subgroups: | |
gName = group.asdict().get('name') | |
print(gName) | |
print('-- subgroup info') | |
subgroup = gl.groups.get(SUBGROUP_ID) | |
## EXPORT SUBGROUP | |
print(subgroup) | |
export = subgroup.exports.create() | |
time.sleep(5) | |
with open('subgroup.tar.gz', 'wb') as f: | |
export.download(streamed=True, action=f.write) | |
print('-- OK - exported to subgroup.tar.gz') | |
print('-- getting subgroup repos') | |
projects = subgroup.projects.list(get_all=True) | |
# print(projects) | |
for groupProject in projects: | |
pName = groupProject.asdict().get('name') | |
print(pName) | |
# cast to Project obj | |
p = gl.projects.get(groupProject.id, lazy=True) | |
# print(p) | |
print(f'-- archiving: {pName}') | |
os.mkdir(f'./{pName}') | |
## REPO | |
print(f'-- getting raw repo') | |
for i in p.repository_tree(): | |
fName = i.get('name') | |
print(f'\t{fName}') | |
pRepoName = f"{pName}/{pName}-repo.zip" | |
print(f'-- archiving repo: {pRepoName}') | |
with open(pRepoName, 'wb') as f: | |
p.repository_archive(streamed=True, action=f.write, format='zip') | |
# unzip to subfolder | |
with ZipFile(pRepoName, 'r') as z: | |
z.extractall(path=pName) | |
os.remove(pRepoName) | |
## EXPORT | |
print(f'-- creating gitlab export') | |
pExport = p.exports.create() | |
pExport.refresh() | |
while pExport.export_status != 'finished': | |
print(f"-- refreshing. export not ready...") | |
time.sleep(1) | |
pExport.refresh() | |
# download when available | |
pArchiveName = f'{pName}/{pName}.tar.gz' | |
print(f'-- ready. downloading {pArchiveName}') | |
with open(pArchiveName, 'wb') as f: | |
pExport.download(streamed=True, action=f.write) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment