|
| 1 | +import os |
| 2 | +import time |
| 3 | +from RateLimitManager import RateLimitManager |
| 4 | + |
| 5 | +class GitLabProject: |
| 6 | + """ |
| 7 | + Class to manage GitLab projects. |
| 8 | + """ |
| 9 | + |
| 10 | + def __init__(self, project_id, gitlab_url, gitlab_access_token): |
| 11 | + """ |
| 12 | + Initializes the GitLabProject class with project ID, GitLab URL, and access token. |
| 13 | +
|
| 14 | + Args: |
| 15 | + project_id (int): The project ID. |
| 16 | + gitlab_url (str): The GitLab URL. |
| 17 | + gitlab_access_token (str): The GitLab access token. |
| 18 | + """ |
| 19 | + self.project_id = project_id |
| 20 | + self.gitlab_url = gitlab_url |
| 21 | + self.gitlab_access_token = gitlab_access_token |
| 22 | + |
| 23 | + def export_project(self): |
| 24 | + """ |
| 25 | + Exports a project from GitLab. |
| 26 | +
|
| 27 | + Returns: |
| 28 | + str: The path of the exported file. |
| 29 | + """ |
| 30 | + base_url = f"{self.gitlab_url}/api/v4" |
| 31 | + export_url = f"{base_url}/projects/{self.project_id}/export" |
| 32 | + payload = {"upload[http_method]": "PUT"} |
| 33 | + headers = {"PRIVATE-TOKEN": self.gitlab_access_token} |
| 34 | + |
| 35 | + response = RateLimitManager().make_request(export_url, "POST", headers=headers, data=payload) |
| 36 | + |
| 37 | + if response.status_code == 202: |
| 38 | + print("Project export initiated successfully.") |
| 39 | + else: |
| 40 | + print(f"Failed to initiate project export. Status code: {response.status_code}") |
| 41 | + return None |
| 42 | + |
| 43 | + export_status_url = f"{base_url}/projects/{self.project_id}/export" |
| 44 | + export_status = None |
| 45 | + |
| 46 | + while export_status != "finished": |
| 47 | + time.sleep(5) |
| 48 | + status_response = RateLimitManager().make_request(export_status_url, "GET", headers=headers) |
| 49 | + |
| 50 | + if status_response.status_code == 200: |
| 51 | + export_status = status_response.json().get("export_status") |
| 52 | + if export_status == "finished": |
| 53 | + download_url = status_response.json().get("_links").get("api_url") |
| 54 | + project_name = status_response.json().get("name") |
| 55 | + break |
| 56 | + else: |
| 57 | + print("Project export still in progress. Status:", export_status) |
| 58 | + else: |
| 59 | + print(f"Failed to get export status. Status code: {status_response.status_code}") |
| 60 | + return None |
| 61 | + |
| 62 | + export_dir = "./exports" |
| 63 | + if not os.path.exists(export_dir): |
| 64 | + os.makedirs(export_dir) |
| 65 | + |
| 66 | + print("Project export completed. Downloading the exported file...") |
| 67 | + |
| 68 | + export_response = RateLimitManager().make_request(download_url, "GET", headers=headers) |
| 69 | + if export_response.status_code == 200: |
| 70 | + file_path = os.path.join(export_dir, f"{project_name}.tar.gz") |
| 71 | + with open(file_path, "wb") as file: |
| 72 | + file.write(export_response.content) |
| 73 | + |
| 74 | + print(f"Exported file downloaded as '{file_path}'.") |
| 75 | + return file_path |
| 76 | + else: |
| 77 | + print(f"Failed to download the exported file. Status code: {export_response.status_code}") |
| 78 | + return None |
0 commit comments