【发布时间】:2023-02-03 19:55:52
【问题描述】:
我写了一个 python 脚本来从“主”分支获取最新的提交以及使用该提交创建的标签。一旦我们合并到 main,就会自动创建一个标签,我想使用 python 获取它。但是找不到那个。下面是脚本:
def get_latest_hash_id(repo_owner, repo_name, branch_name, auth_token):
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/commits/{branch_name}"
headers = {
"Authorization": f"Bearer {auth_token}"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
return data['sha']
return None
def get_tag_with_hash(repo_owner, repo_name, hash_id, auth_token):
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/git/refs/tags"
headers = {
"Authorization": f"Token {auth_token}"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
print("json data = " ,data)
for tag in data:
if tag['object']['sha'] == hash_id:
return tag['ref']
repo_owner = "company"
repo_name = "app-api"
branch_name = "main"
auth_token = getpass.getpass("Enter your GitHub personal access token: ")
hash_id = get_latest_hash_id(repo_owner, repo_name, branch_name, auth_token)
if hash_id:
tag = get_tag_with_hash(repo_owner, repo_name, hash_id, auth_token)
if tag:
print(f"The latest hash ID on branch {branch_name} is {hash_id}, and the tag created from that hash ID is: {tag}")
else:
print(f"No tag found with hash ID {hash_id}")
else:
print(f"No hash ID found for branch {branch_name}")
【问题讨论】:
标签: python github github-api