可以使用GitHub API来获取最新的pull request和与之关联的状态。当状态失败时,触发一条评论通知PR作者。以下是一个Python示例脚本:
import requests
import json
# GitHub API URL
api_url = "https://api.github.com"
# Get latest pull request and its statuses
pull_url = api_url + "/repos/{owner}/{repo}/pulls/{number}"
pull_url = pull_url.format(owner="your_org", repo="your_repo", number=123)
headers = {"Authorization": "token YOUR_TOKEN_HERE"}
response = requests.get(pull_url, headers=headers)
pull_request = json.loads(response.content)
statuses_url = pull_request["statuses_url"]
statuses_response = requests.get(statuses_url, headers=headers)
statuses = json.loads(statuses_response.content)
# Check if any status failed
failed_statuses = [s["state"] for s in statuses if s["state"] == "failure"]
if failed_statuses:
# Comment on the pull request
comments_url = pull_request["comments_url"]
message = "One or more status checks failed. Please address and push fixes."
data = {"body": message}
response = requests.post(comments_url, headers=headers, json=data)
此脚本使用GitHub API获取与最新拉取请求相关联的状态,并检查是否有状态失败。如果有,脚本将在拉取请求上发表注释,通知作者。