reposync/milestone_sync/milestone_gitee_to_gitlink.py

135 lines
5.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import urllib
import requests
import mysql.connector
import json
def main():
conn = mysql.connector.connect(
host="localhost",
user="root",
password="185102xmy",
database="db1"
)
cursor = conn.cursor()
cursor.execute('DROP TABLE IF EXISTS gitee_milestone_list')
# 创建一个表
cursor.execute('''CREATE TABLE IF NOT EXISTS gitee_milestone_list
(id INT AUTO_INCREMENT PRIMARY KEY, number VARCHAR(10), title VARCHAR(100), description VARCHAR(100), due_on VARCHAR(15))''')
# 你的仓库信息
owner = 'xumingyang21'
repo = 'reposyncer2'
url = f"https://gitee.com/api/v5/repos/{owner}/{repo}/milestones"
# 构造请求头
token = 'f2be2313581c1fde50b16bf35bb655c5'
headers = {'Authorization': f'token {token}'}
# 发送POST请求 获取状态为open的issue
data = {
"access_token": token,
"owner": owner,
"repo": repo,
# "state":'all',
}
# 将字典转换为查询字符串
query_string = urllib.parse.urlencode(data)
# 完整的请求 URL包括查询字符串
full_url1 = f"{url}?{query_string}"
response = requests.get(full_url1, headers=headers, json=data)
if response.status_code == 200:
milestone_info = response.json()
print(milestone_info)
for single_milestone in milestone_info:
cursor.execute(
'INSERT INTO gitee_milestone_list (number, title, description, due_on) VALUES (%s, %s, %s, %s)',
(single_milestone['number'], single_milestone['title'], single_milestone['description'], single_milestone['due_on']))
else:
print(f"创建issue失败12状态码{response.status_code},错误信息:{response.text}")
# 提交事务
conn.commit()
# 关闭游标和连接
cursor.close()
conn.close()
#获取gitlink中的milestone
conn = mysql.connector.connect(
host="localhost",
user="root",
password="185102xmy",
database="db1"
)
cursor = conn.cursor()
cursor.execute('DROP TABLE IF EXISTS gitlink_milestone_list')
# 创建一个表
cursor.execute('''CREATE TABLE IF NOT EXISTS gitlink_milestone_list
(id INT AUTO_INCREMENT PRIMARY KEY, milestone_index VARCHAR(100), name VARCHAR(100), description VARCHAR(100), due_on VARCHAR(100))''')
url = "https://gitlink.org.cn/api/v1/xumingyang21/reposyncer2/milestones.json"
payload = {}
access_token = 'zo07-W-W6vnU1mvQhVhIdfyyVY3K1R1piil_jfvS1bg'
headers = {
'Authorization': f'Bearer {access_token}', # 使用 Bearer 标记
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
'Content-Type': 'application/json'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
gitlink_milestone = response.json()['milestones']
for gitlink_single_milestone in gitlink_milestone:
cursor.execute(
'INSERT INTO gitlink_milestone_list (milestone_index, name, description, due_on) VALUES (%s, %s, %s, %s)',
(gitlink_single_milestone['id'], gitlink_single_milestone['name'], gitlink_single_milestone['description'],
gitlink_single_milestone['effective_date']))
#删除gitlink中的milestone
url = f"https://gitlink.org.cn/api/v1/xumingyang21/reposyncer2/milestones/{gitlink_single_milestone['id']}.json"
payload = json.dumps({
"name": "string",
"description": "string",
"effective_date": "string"
})
access_token = 'zo07-W-W6vnU1mvQhVhIdfyyVY3K1R1piil_jfvS1bg'
headers = {
'Authorization': f'Bearer {access_token}', # 使用 Bearer 标记
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
'Content-Type': 'application/json'
}
response = requests.request("DELETE", url, headers=headers, data=payload)
print(response.text)
#重新创建gitlink上的milestone
cursor.execute('SELECT title, description, due_on FROM gitee_milestone_list')
milestone_result = cursor.fetchall()
for row in milestone_result:
url = "https://gitlink.org.cn/api/v1/xumingyang21/reposyncer2/milestones.json"
milestone_name = row[0]
milestone_description = row[1]
milestone_effect_date = row[2]
payload = json.dumps({
"name": milestone_name,
"description": milestone_description,
"effective_date": milestone_effect_date
})
access_token = 'zo07-W-W6vnU1mvQhVhIdfyyVY3K1R1piil_jfvS1bg'
headers = {
'Authorization': f'Bearer {access_token}', # 使用 Bearer 标记
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
# 提交事务
conn.commit()
# 关闭游标和连接
cursor.close()
conn.close()
if __name__ == '__main__':
main()