forked from Lesin/reposync
217 lines
8.2 KiB
Python
217 lines
8.2 KiB
Python
import urllib
|
||
import requests
|
||
import mysql.connector
|
||
|
||
####################################先将gitee的pr状态更改为合并#####################################
|
||
def prework():
|
||
# 连接到MySQL数据库
|
||
conn = mysql.connector.connect(
|
||
host="localhost",
|
||
user="root",
|
||
password="185102xmy",
|
||
database="db1"
|
||
)
|
||
cursor = conn.cursor()
|
||
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}")
|
||
|
||
cursor.execute('SELECT number, state FROM gitee_pr_list')
|
||
|
||
gitee_pr_info = cursor.fetchall()
|
||
for row in gitee_pr_info:
|
||
number = row[0]
|
||
state = row[1]
|
||
owner = 'xumingyang21'
|
||
repo = 'reposyncer2'
|
||
if state == 'open':
|
||
# Gitee获取issue的API URL
|
||
url = f'https://gitee.com/api/v5/repos/{owner}/{repo}/pulls/{number}'
|
||
# 构造请求头
|
||
token = 'f2be2313581c1fde50b16bf35bb655c5'
|
||
headers = {'Authorization': f'token {token}'}
|
||
# 发送POST请求 获取状态为open的issue
|
||
data = {
|
||
"access_token": token,
|
||
"owner": owner,
|
||
"repo": repo,
|
||
"state": 'closed'
|
||
}
|
||
# 将字典转换为查询字符串
|
||
query_string = urllib.parse.urlencode(data)
|
||
# 完整的请求 URL,包括查询字符串
|
||
full_url = f"{url}?{query_string}"
|
||
response = requests.patch(full_url, headers=headers, json=data)
|
||
|
||
if response.status_code == 200:
|
||
# 注意:这里假设响应体中包含一个'number'字段作为issue的ID,但实际上Gitee可能返回不同的结构
|
||
# 你需要根据实际的响应结构来调整以下代码
|
||
pr_info = response.json()
|
||
print(pr_info)
|
||
else:
|
||
print(f"获取pr失败,状态码:{response.status_code},错误信息:{response.text}")
|
||
|
||
#########################获取github中的信息#########################################
|
||
|
||
cursor.execute('DROP TABLE IF EXISTS github_pr_list')
|
||
cursor.execute('''CREATE TABLE IF NOT EXISTS github_pr_list
|
||
(id INT AUTO_INCREMENT PRIMARY KEY, number VARCHAR(10), title VARCHAR(100), body VARCHAR(1000), head VARCHAR(100), base VARCHAR(15), state VARCHAR(10))''')
|
||
|
||
# GitHub创建issue的API URL
|
||
url = f"https://api.github.com/repos/fuxingtamu/yundingzhiyi/pulls"
|
||
|
||
# 构造请求头
|
||
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)
|
||
# 打印响应
|
||
# print(response.text)
|
||
pr_info = response.json()
|
||
for pr in pr_info:
|
||
print('title:', pr['title'])
|
||
print('body:', pr['body'])
|
||
print('head:', pr['head']['label'])
|
||
print('base:', pr['base']['label'])
|
||
cursor.execute(
|
||
'INSERT INTO github_pr_list (number, title, body, head, base, state) VALUES (%s, %s, %s, %s, %s, %s)',
|
||
(
|
||
pr['number'], pr['title'], pr['body'], pr['head']['ref'], pr['base']['ref'], pr['state']))
|
||
|
||
# 检查响应状态码
|
||
if response.status_code == 200:
|
||
# 注意:这里假设响应体中包含一个'number'字段作为issue的ID,但实际上GitHub可能返回不同的结构
|
||
# 你需要根据实际的响应结构来调整以下代码
|
||
issue_info = response.json()
|
||
for pr in issue_info:
|
||
print(f'Issue created with ID: {pr["number"]}')
|
||
else:
|
||
print(f"创建issue失败,状态码:{response.status_code},错误信息:{response.text}")
|
||
# 提交事务
|
||
conn.commit()
|
||
# 关闭游标和连接
|
||
cursor.close()
|
||
conn.close()
|
||
|
||
|
||
#################################这个地方执行仓库的同步########################################
|
||
|
||
|
||
################################在gitee里面添加新的pr#######################################
|
||
def push():
|
||
# 连接到MySQL数据库
|
||
conn = mysql.connector.connect(
|
||
host="localhost",
|
||
user="root",
|
||
password="185102xmy",
|
||
database="db1"
|
||
)
|
||
cursor = conn.cursor()
|
||
owner = 'xumingyang21'
|
||
repo = 'reposyncer2'
|
||
cursor.execute('SELECT state, title, body, head, base FROM github_pr_list')
|
||
github_pr_info = cursor.fetchall()
|
||
for row in github_pr_info:
|
||
github_state = row[0]
|
||
github_title = row[1]
|
||
github_body = row[2]
|
||
github_head = row[3]
|
||
github_base = row[4]
|
||
if github_state == 'open':
|
||
# Gitee创建pr的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,
|
||
"title": github_title,
|
||
"body": github_body,
|
||
"head": github_head,
|
||
"base": github_base
|
||
}
|
||
|
||
# 将字典转换为查询字符串
|
||
query_string = urllib.parse.urlencode(data)
|
||
# 完整的请求 URL,包括查询字符串
|
||
full_url = f"{url}?{query_string}"
|
||
response = requests.post(full_url, headers=headers, json=data)
|
||
# 打印响应
|
||
# print(response.text)
|
||
# 检查响应状态码
|
||
if response.status_code == 201:
|
||
# 注意:这里假设响应体中包含一个'number'字段作为issue的ID,但实际上Gitee可能返回不同的结构
|
||
# 你需要根据实际的响应结构来调整以下代码
|
||
pr_info = response.json()
|
||
print(pr_info)
|
||
|
||
else:
|
||
print(f"获取pr失败,状态码:{response.status_code},错误信息:{response.text}")
|
||
# 提交事务
|
||
conn.commit()
|
||
# 关闭游标和连接
|
||
cursor.close()
|
||
conn.close()
|
||
|
||
if __name__ == '__main__':
|
||
prework()
|
||
push() |