当有人将拉取请求合并到 GitHub 中的私有存储库时,我想在另一个位置(Slack)显示拉取请求的详细信息,包括描述中的图像。通常这些是有关已更改内容的短视频或屏幕截图,因此如果能够在 slack 中让每个人都能看到连续的更改流,那就太好了。
据我所知,查看 GitHub API 文档,无法通过 API 下载这些图像。
图像存储在不可公开访问的 URL(如
https://github.com/owner/project-name/assets/*
)中。因此,您必须登录浏览器才能真正访问图像。
当您do在浏览器中查看图像时,GitHub 会将您重定向到一个类似于
https://private-user-images.githubusercontent.com/123456/251885706-e74af325-a947-47f7-8dad-61129ad62f11.png?jwt=eyJ...
的短期 URL。这个 URL 是公开的,但我想在不登录浏览器的情况下生成该 URL,以便我可以响应 Webhook 来执行此操作。
例如,PR 描述可能是这样的:
Did a bunch of cool stuff in this one...
## What it looks like
<img width="1238" alt="Screenshot 2023-07-07 at 6 28 14 PM"
src="https://github.com/owner/project-name/assets/123456/e74af324-a944-47f4-8da4-61129ad62f14">
我想知道的是如何使用脚本远程下载位于 https://github.com/owner/project-name/assets/123456/e74af324-a944-47f4-8da4-61129ad62f14 的图像。
从浏览器获取您的
user_session
Cookie 并提供令牌以访问 Github API。
export GH_TOKEN="<token>"
export GH_SESSION_COOKIE="<session_cookie>"
python download.py "<owner>/<repo>/pulls/<pr_number>"
#!/usr/bin/env python3
import os
import sys
import urllib.request
import urllib.request
import json
import re
from urllib.parse import urlparse
def main():
# Read GH_TOKEN and GH_SESSION_COOKIE from environment variables
gh_token = os.environ["GH_TOKEN"]
gh_session_cookie = os.environ["GH_SESSION_COOKIE"]
# Set pull request number & repo name
path_segment = sys.argv[1]
# Get URL regexp
url_regexp = re.compile(r"https?://[^\"]+")
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {gh_token}",
"X-GitHub-Api-Version": "2022-11-28"
}
# Download the pull request body
req = urllib.request.Request(
f"https://api.github.com/repos/{path_segment}", headers=headers)
resp = urllib.request.urlopen(req)
# Get all occurrences of URL like patterns using RegExp
body = json.loads(resp.read().decode('utf-8'))['body']
urls = url_regexp.findall(body)
# Download files from URLs
for url in urls:
headers = {
"cookie": f"user_session={gh_session_cookie};"
}
req = urllib.request.Request(url, headers=headers)
with urllib.request.urlopen(req) as u:
# Get the file name from the URL
filename = urlparse(u.geturl()).path.split('/')[-1]
with open(filename, 'wb') as f:
f.write(u.read())
if __name__ == "__main__":
main()
session_cookie
有效期仅2周。必须格外小心地保密,因为该 cookie 允许冒充您的 Github 帐户。
当我查看 VSCode PR 187953(其中有图片)时,我看到它的 json 为 https://api.github.com/repos/microsoft/vscode/pulls/187953
它给出了,使用gh pr view(但
curl
也可以工作):
gh pr view -R microsoft/vscode 187953 --jq ".body" --json body
So that there isn't such a large gap at the bottom.
![image](https://github.com/microsoft/vscode/assets/2644648/dee6499a-cbec-460f-893b-cc5bccadc853)
<!-- Thank you for submitting a Pull Request. Please:
* Read our Pull Request guidelines:
https://github.com/microsoft/vscode/wiki/How-to-Contribute#pull-requests
* Associate an issue with the Pull Request.
* Ensure that the code is up-to-date with the `main` branch.
* Include a description of the proposed changes and how to test them.
-->
然后
curl -kL https://github.com/microsoft/vscode/assets/2644648/dee6499a-cbec-460f-893b-cc5bccadc853 -o res.png
给了我 PNG 文件。我没有设置任何身份验证标头。