reposync/pr_sync/pr_gitee_to_gitlink.py

164 lines
6.3 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.

####################首先先将gitlink中的所有pr设置成merge的#############################
import json
import urllib
import requests
import mysql.connector
# 连接到MySQL数据库
def prework():
conn = mysql.connector.connect(
host="localhost",
user="rtsw",
password="123456",
database="reposyncer"
)
cursor = conn.cursor()
cursor.execute('DROP TABLE IF EXISTS gitlink_pr_list')
cursor.execute('''CREATE TABLE IF NOT EXISTS gitlink_pr_list
(id INT AUTO_INCREMENT PRIMARY KEY, number VARCHAR(10), state VARCHAR(15), title VARCHAR(100), body VARCHAR(1000), head VARCHAR(100), base VARCHAR(15))''')
url = "https://gitlink.org.cn/api/v1/xumingyang21/reposyncer2/pulls.json?keyword&status&priority_id&issue_tag_id&version_id&reviewer_id&assign_user_id&sort_by&sort_direction"
payload = {}
access_token = 'I9uyRzjEIObdgsD8fegdQoN2d3p3cU_7_uaNGV03S_Q'
headers = {
'Authorization': f'Bearer {access_token}', # 使用 Bearer 标记
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)'
}
response = requests.request("GET", url, headers=headers, data=payload)
data = response.json()
for pr_info in data['pulls']:
print('id:', pr_info['id'])
print('state:', pr_info['status'])
print('title:', pr_info['title'])
print('body:', pr_info['body'])
print('head:', pr_info['head'])
print('base:', pr_info['base'])
cursor.execute(
'INSERT INTO gitlink_pr_list (number, state, title, body, head, base) VALUES (%s, %s, %s, %s, %s, %s)',
(pr_info['id'], pr_info['status'], pr_info['title'], pr_info['body'], pr_info['head'], pr_info['base']))
if pr_info['status'] == 'open':
index = pr_info['id']
url_update = f"https://gitlink.org.cn/api/xumingyang21/reposyncer2/pulls/{index}/refuse_merge.json"
payload = {}
access_token = 'I9uyRzjEIObdgsD8fegdQoN2d3p3cU_7_uaNGV03S_Q'
headers = {
'Authorization': f'Bearer {access_token}',
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)'
}
response_update = requests.request("POST", url_update, headers=headers, data=payload)
print(response_update.text)
print(response.text)
#######################获取gitee仓库上的pr信息##################################
cursor.execute('DROP TABLE IF EXISTS gitee_pr_list')
# 创建一个表
cursor.execute('''CREATE TABLE IF NOT EXISTS gitee_pr_list
(id INT AUTO_INCREMENT PRIMARY KEY, number VARCHAR(10), index2 VARCHAR(10), state VARCHAR(15), title VARCHAR(100), body VARCHAR(1000), head VARCHAR(100), base VARCHAR(15))''')
# 你的仓库信息
owner = 'xumingyang21'
repo = 'reposyncer2'
# Gitee获取issue的API URL
url = f'https://gitee.com/api/v5/repos/{owner}/{repo}/pulls'
# 构造请求头
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_url = f"{url}?{query_string}"
response = requests.get(full_url, headers=headers, json=data)
# 打印响应
# print(response.text)
# 检查响应状态码
if response.status_code == 200:
# 注意:这里假设响应体中包含一个'number'字段作为issue的ID但实际上Gitee可能返回不同的结构
# 你需要根据实际的响应结构来调整以下代码
pr_info = response.json()
# print(pr_info)
for pr in pr_info:
print('id:', pr['id'])
print('state:', pr['state'])
print('title:', pr['title'])
print('body:', pr['body'])
print('head:', pr['head']['label'])
print('base:', pr['base']['label'])
cursor.execute(
'INSERT INTO gitee_pr_list (number, index2, state, title, body, head, base) VALUES (%s, %s, %s, %s, %s, %s, %s)',
(
pr['number'], pr['id'], pr['state'], pr['title'], pr['body'], pr['head']['label'], pr['base']['label']))
else:
print(f"获取pr失败状态码{response.status_code},错误信息:{response.text}")
# 提交事务
conn.commit()
# 关闭游标和连接
cursor.close()
conn.close()
########################这一块应当先进行代码仓库的同步#################################################
#####################查找其中state为open的pr并将其创建到gitlink上面#####################################
def pushwork():
conn = mysql.connector.connect(
host="localhost",
user="rtsw",
password="123456",
database="reposyncer"
)
cursor = conn.cursor()
cursor.execute('SELECT state, title, body, head, base FROM gitee_pr_list')
pr_info = cursor.fetchall()
for row in pr_info:
pr_state = row[0]
pr_title = row[1]
pr_body = row[2]
pr_head = row[3]
pr_base = row[4]
if pr_state == 'open':
url = "https://gitlink.org.cn/api/xumingyang21/reposyncer2/pulls.json"
payload = json.dumps({
"title": pr_title,
"priority_id": "2",
"body": pr_body,
"head": pr_head,
"base": pr_base,
"is_original": False,
"fork_project_id": "",
"files_count": 1,
"commits_count": 1,
"reviewer_ids": [],
"receivers_login": []
})
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("POST", url, headers=headers, data=payload)
data = response.json()
print(data)
print(response.text)
# 提交事务
conn.commit()
# 关闭游标和连接
cursor.close()
conn.close()