forked from Lesin/reposync
179 lines
5.7 KiB
Python
179 lines
5.7 KiB
Python
import urllib
|
||
import requests
|
||
import mysql.connector
|
||
import json
|
||
|
||
def main():
|
||
########获取gitee里的issue并且放到数据库中#######################
|
||
# 连接到MySQL数据库
|
||
conn = mysql.connector.connect(
|
||
host="localhost",
|
||
user="root",
|
||
password="185102xmy",
|
||
database="db1"
|
||
)
|
||
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,
|
||
"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="root",
|
||
password="185102xmy",
|
||
database="db1"
|
||
)
|
||
cursor = conn.cursor()
|
||
|
||
cursor.execute('DROP TABLE IF EXISTS github_issue_list')
|
||
# 创建一个表
|
||
cursor.execute('''CREATE TABLE IF NOT EXISTS github_issue_list
|
||
(id INT AUTO_INCREMENT PRIMARY KEY, number VARCHAR(10), title VARCHAR(100), body VARCHAR(1000), status_name VARCHAR(15))''')
|
||
|
||
url = f"https://api.github.com/repos/fuxingtamu/yundingzhiyi/issues"
|
||
# 构造请求头
|
||
token = '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'
|
||
# 构造请求头
|
||
headers = {
|
||
'Authorization': f'token {token}',
|
||
'Accept': 'application/vnd.github+json'
|
||
}
|
||
# 发送POST请求
|
||
data = {
|
||
}
|
||
response = requests.get(url, headers=headers, json=data, verify=False)
|
||
issue_info = response.json()
|
||
column_data=[]
|
||
for issue in issue_info:
|
||
print('title:', issue['title'])
|
||
print('body:', issue['body'])
|
||
print('number', issue['number'])
|
||
print('id', issue['id'])
|
||
|
||
cursor.execute(
|
||
'INSERT INTO github_issue_list (number, title, body, status_name) VALUES (%s, %s, %s, %s)',
|
||
(issue['number'], issue['title'], issue['body'], issue['id']))
|
||
# 执行查询
|
||
cursor.execute('SELECT number FROM github_issue_list')
|
||
# 获取所有结果
|
||
results = cursor.fetchall()
|
||
# 提取列数据到列表中
|
||
column_data = [row[0] for row in results]
|
||
|
||
##################删除gitlink仓库中的所有的issue###########################
|
||
for number in column_data:
|
||
url = f"https://api.github.com/repos/fuxingtamu/yundingzhiyi/issues/{number}"
|
||
|
||
# 构造请求头
|
||
token = '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'
|
||
# 构造请求头
|
||
headers = {
|
||
'Authorization': f'token {token}',
|
||
'Accept': 'application/vnd.github+json'
|
||
}
|
||
|
||
# 发送POST请求
|
||
data = {
|
||
'owner': 'fuxingtamu',
|
||
'repo': 'yundingzhiyi',
|
||
'issue_number': number,
|
||
'state': 'closed'
|
||
}
|
||
|
||
response = requests.patch(url, headers=headers, json=data, verify=False)
|
||
# 打印响应
|
||
print(response.text)
|
||
|
||
##################将gitee中的issue创建到gitlink中###########################
|
||
# GitHub创建issue的API URL
|
||
url = f"https://api.github.com/repos/fuxingtamu/yundingzhiyi/issues"
|
||
|
||
# 构造请求头
|
||
token = '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'
|
||
# 构造请求头
|
||
headers = {
|
||
'Authorization': f'token {token}',
|
||
'Accept': 'application/vnd.github+json'
|
||
}
|
||
|
||
|
||
# 执行查询
|
||
cursor.execute('SELECT title,body,state,priority FROM gitee_issue_list')
|
||
# 获取所有结果
|
||
result = cursor.fetchall()
|
||
for row in result:
|
||
# 发送POST请求
|
||
data = {
|
||
"title": row[0],
|
||
"body": row[1]
|
||
}
|
||
|
||
response = requests.post(url, headers=headers, json=data, verify=False)
|
||
print(response.text)
|
||
|
||
# 提交事务
|
||
conn.commit()
|
||
# 关闭游标和连接
|
||
cursor.close()
|
||
conn.close()
|
||
|
||
if __name__ == '__main__':
|
||
main()
|
||
|