forked from Dongjiaqi/reposync
204 lines
6.9 KiB
Python
204 lines
6.9 KiB
Python
import urllib
|
||
import requests
|
||
import mysql.connector
|
||
import json
|
||
|
||
def main():
|
||
########获取gitee里的issue并且放到数据库中#######################
|
||
# 连接到MySQL数据库
|
||
conn = mysql.connector.connect(
|
||
host="localhost",
|
||
user="rtsw",
|
||
password="123456",
|
||
database="reposyncer"
|
||
)
|
||
cursor = conn.cursor()
|
||
|
||
cursor.execute('DROP TABLE IF EXISTS gitee_issue_list')
|
||
# 创建一个表
|
||
cursor.execute('''CREATE TABLE IF NOT EXISTS gitee_issue_list
|
||
(id INT AUTO_INCREMENT PRIMARY KEY, number VARCHAR(10), title VARCHAR(100), body VARCHAR(1000), state VARCHAR(15), priority INT)''')
|
||
# 你的仓库信息
|
||
owner = 'xumingyang21'
|
||
repo = 'reposyncer2'
|
||
|
||
# Gitee获取issue的API URL
|
||
url = f"https://gitee.com/api/v5/repos/{owner}/{repo}/issues"
|
||
|
||
# 构造请求头
|
||
token = 'f2be2313581c1fde50b16bf35bb655c5'
|
||
headers = {'Authorization': f'token {token}'}
|
||
|
||
# 发送POST请求 获取状态为open的issue
|
||
data = {
|
||
"access_token": token,
|
||
"owner": owner,
|
||
"repo": repo,
|
||
"state": 'all',
|
||
"sort": 'created',
|
||
"direction": 'asc'
|
||
}
|
||
|
||
# 将字典转换为查询字符串
|
||
query_string = urllib.parse.urlencode(data)
|
||
|
||
# 完整的请求 URL,包括查询字符串
|
||
full_url1 = f"{url}?{query_string}"
|
||
|
||
response = requests.get(full_url1, headers=headers, json=data)
|
||
# 打印响应
|
||
# print(response.text)
|
||
# 检查响应状态码
|
||
if response.status_code == 200:
|
||
# 注意:这里假设响应体中包含一个'number'字段作为issue的ID,但实际上Gitee可能返回不同的结构
|
||
# 你需要根据实际的响应结构来调整以下代码
|
||
issue_info = response.json()
|
||
for issue in issue_info:
|
||
print('number1:', issue['number'])
|
||
print('state1:', issue['state'])
|
||
print('title1:', issue['title'])
|
||
print('body1:', issue['body'])
|
||
cursor.execute(
|
||
'INSERT INTO gitee_issue_list (number, title, body, state, priority) VALUES (%s, %s, %s, %s, %s)',
|
||
(issue['number'], issue['title'], issue['body'], issue['state'], issue['priority']))
|
||
|
||
|
||
else:
|
||
print(f"创建issue失败12,状态码:{response.status_code},错误信息:{response.text}")
|
||
|
||
# 提交事务
|
||
conn.commit()
|
||
# 关闭游标和连接
|
||
cursor.close()
|
||
conn.close()
|
||
|
||
########获取gitlink里的issue并且放到数据库中#######################
|
||
# 连接到MySQL数据库
|
||
conn = mysql.connector.connect(
|
||
host="localhost",
|
||
user="rtsw",
|
||
password="123456",
|
||
database="reposyncer"
|
||
)
|
||
cursor = conn.cursor()
|
||
|
||
cursor.execute('DROP TABLE IF EXISTS gitlink_issue_list')
|
||
# 创建一个表
|
||
cursor.execute('''CREATE TABLE IF NOT EXISTS gitlink_issue_list
|
||
(id INT AUTO_INCREMENT PRIMARY KEY, number VARCHAR(10), subject VARCHAR(100), description VARCHAR(1000), status_name VARCHAR(15))''')
|
||
|
||
url = "https://gitlink.org.cn/api/v1/xumingyang21/reposyncer2/issues.json?category&participant_category&keyword&author_id&milestone_id&assigner_id&status_id&sort_by&sort_direction&issue_tag_ids&page&limit&debug=admin"
|
||
|
||
payload = {}
|
||
headers = {
|
||
'Authorization': 'XtR67a232J5u1VU_8yvm7RCVfMX0XMM76m8yMAGblDY',
|
||
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)'
|
||
}
|
||
|
||
response = requests.request("GET", url, headers=headers, data=payload)
|
||
data = response.json()
|
||
# print(data)
|
||
print(data['issues'])
|
||
for issue in data['issues']:
|
||
url = f"https://gitlink.org.cn/api/v1/xumingyang21/reposyncer2/issues/{issue['id']}.json"
|
||
|
||
payload = {}
|
||
|
||
access_token = 'I9uyRzjEIObdgsD8fegdQoN2d3p3cU_7_uaNGV03S_Q'
|
||
headers = {
|
||
'Authorization': f'Bearer {access_token}',
|
||
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)'
|
||
}
|
||
|
||
response = requests.request("GET", url, headers=headers, data=payload)
|
||
data = response.json()
|
||
# print(response.text)
|
||
# print(data)
|
||
print(issue['id'])
|
||
print(issue['subject'])
|
||
print(data['description'])
|
||
print(issue['status_name'])
|
||
|
||
cursor.execute(
|
||
'INSERT INTO gitlink_issue_list (number, subject, description, status_name) VALUES (%s, %s, %s, %s)',
|
||
(issue['id'], issue['subject'], data['description'], issue['status_name']))
|
||
# 执行查询
|
||
cursor.execute('SELECT number FROM gitlink_issue_list')
|
||
# 获取所有结果
|
||
results = cursor.fetchall()
|
||
# 提取列数据到列表中
|
||
column_data = [row[0] for row in results]
|
||
|
||
##################删除gitlink仓库中的所有的issue###########################
|
||
url = "https://gitlink.org.cn/api/v1/xumingyang21/reposyncer2/issues/batch_destroy.json"
|
||
|
||
payload = json.dumps({
|
||
"ids": column_data
|
||
})
|
||
access_token = 'I9uyRzjEIObdgsD8fegdQoN2d3p3cU_7_uaNGV03S_Q'
|
||
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)
|
||
|
||
##################将gitee中的issue创建到gitlink中###########################
|
||
|
||
url = "https://gitlink.org.cn/api/v1/xumingyang21/reposyncer2/issues.json"
|
||
|
||
# 替换为您的实际访问令牌
|
||
access_token = 'I9uyRzjEIObdgsD8fegdQoN2d3p3cU_7_uaNGV03S_Q'
|
||
headers = {
|
||
'Authorization': f'Bearer {access_token}', # 使用 Bearer 标记
|
||
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
|
||
'Content-Type': 'application/json'
|
||
}
|
||
|
||
# 执行查询
|
||
cursor.execute('SELECT title,body,state,priority FROM gitee_issue_list')
|
||
# 获取所有结果
|
||
result = cursor.fetchall()
|
||
title_list = []
|
||
body_list = []
|
||
for row in result:
|
||
title_list.append(row[0])
|
||
body_list.append(row[1])
|
||
if row[2] == 'open':
|
||
status = 1
|
||
if row[2] == 'progressing':
|
||
status = 2
|
||
if row[2] == 'closed':
|
||
status = 3
|
||
if row[2] == 'rejected':
|
||
continue
|
||
if row[3] == 0:
|
||
priority = 1
|
||
if row[3] == 1:
|
||
priority = 1
|
||
if row[3] == 2:
|
||
priority = 2
|
||
if row[3] == 3:
|
||
priority = 2
|
||
if row[3] == 4:
|
||
priority = 3
|
||
|
||
payload = json.dumps({
|
||
|
||
"status_id": status, # status对应 1对应新增 2对应正在解决 3对应已解决
|
||
"priority_id": priority, # priority对应 1对应低 2对应正常 3对应高优先级
|
||
"subject": row[0],
|
||
"description": row[1],
|
||
})
|
||
response = requests.request("POST", url, headers=headers, data=payload)
|
||
print(response.text)
|
||
|
||
# 提交事务
|
||
conn.commit()
|
||
# 关闭游标和连接
|
||
cursor.close()
|
||
conn.close()
|
||
|