在 Github 的“见解”选项卡下,我可以通过按“依赖关系图”按钮来查找依赖关系,这会显示该项目的所有依赖关系。
我的问题是 - 我可以编写一个 scrypt 来获取这些依赖项吗?
我尝试在 postam 中导航并对 url 发出 GET 请求 'https://github.com/-User-/-Project-/network/dependencies' 我收到了 404 回复。
但是当我在 Chrome 中输入相同的 url 时,我可以看到该项目的依赖关系图。
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_AUTH_TOKEN"\
https://api.github.com/repos/<owner>/<name>/dependency-graph/sbom
您可以使用 GraphQL 获取它:
gh api -H 'Accept: application/vnd.github.hawkgirl-preview+json' graphql --paginate -f query='query {
repository(owner:"<owner>",name:"<name>") {
dependencyGraphManifests {
totalCount
nodes {
filename
}
edges {
node {
blobPath
dependencies {
totalCount
nodes {
packageName
requirements
hasDependencies
packageManager
}
}
}
}
}
}
}'
将 和 替换为您感兴趣的存储库的所有者和名称。
这可以使用 python 库 github-dependents-info 实现:有没有针对 GitHub 的命令,可以让我获取 github 中项目的依赖项?
pip install github-dependents-info
github-dependents-info 使用 GitHub HTML 来抓取依赖项信息,而不是直接使用 GitHub API。以下是其文档中列出的两个用于命令行使用的示例:
# Outputs Text
github-dependents-info --repo nvuillam/npm-groovy-lint
# Outputs JSON
github-dependents-info --repo nvuillam/npm-groovy-lint --json
还支持输出到 markdown 文件,尽管最好查阅[官方示例](github-dependents-info --repo nvuillam/npm-groovy-lint --json)以了解所有细微差别。至于
您可以在 Python 脚本中导入并使用该库,如下所示:我的问题是 - 我可以编写一个 scrypt 来获取这些依赖项吗?
# only used for pretty-printing, not necessary
import json
from github_dependents_info import GithubDependentsInfo
REPO_NAME = 'nvuillam/npm-groovy-lint'
gh_deps_info = GithubDependentsInfo(REPO_NAME, debug=True, sort_key="stars")
# Starts scraping GitHub HTML web
repo_dependency_stats = gh_deps_info.collect()
# Outputs the JSON to terminal with 2 space indentation
print(json.dumps(repo_dependency_stats, indent=2))
我找不到有关Python使用文档的太多信息,但可以通过查看nvuillam/github-dependents-info/blob/main/tests/test_gh_dependents_info/test_gh_dependents_info.py构建上面的示例。
希望这有帮助:)。