-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_projects.py
48 lines (41 loc) · 1.91 KB
/
import_projects.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
import csv
import time
import requests
import os
import logging
import argparse
# Configuring the parsing of command line arguments
parser = argparse.ArgumentParser(description='Import GitLab project from CSV file.')
parser.add_argument('--token', required=True, help='Private token for GitLab API.')
parser.add_argument('--csv', required=True, help='Path to the CSV file.')
args = parser.parse_args()
# Proxy Settings
# os.environ['http_proxy'] = "http://PROXY_HOST:PROXY_PORT"
# os.environ['https_proxy'] = "https://PROXY_HOST:PROXY_PORT"
# Logging Settings
logging.basicConfig(filename='import_result.log', level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y/%m/%d %H:%M:%S')
# Opening the CSV file and reading each row
with open(args.csv, 'r') as f:
reader = csv.reader(f)
for row in reader:
file_path, namespace, path = row # CSV の各行からデータ読み取り
# Loading the export file
with open(file_path, 'rb') as f:
data = f.read()
# Sending a request to the GitLab API endpoint to upload the export data
url = "https://gitlab.com/api/v4/projects/import"
headers = {"PRIVATE-TOKEN": args.token}
payload = {"path": path, "namespace": namespace}
files = [("file", ("project.tar.gz", data, "application/gzip"))]
response = requests.post(url, headers=headers, files=files, data=payload)
# Checking the response and logging
if response.status_code != 201:
log_message = f' ERROR - File Path: {file_path}, Status Code: {response.status_code}, Response: {response.text}'
print(log_message)
logging.error(log_message)
else:
log_message = f' INFO - File Path: {file_path}, Status Code: {response.status_code}, Created'
print(log_message)
logging.info(log_message)
# Wait 12 seconds to avoid API call limit
time.sleep(12)