javascript - 从 github url 中查找用户、repo 和路径

标签 javascript github github-api

我需要能够从粘贴到表单中的 github url(到 repo 中的文件)获取用户、repo 和路径,以便能够对其发出 api 请求。例如https://github.com/HubSpot/BuckyClient/blob/master/README.md

我已经开始编写一些正则表达式来尝试定位 url 的部分,但这似乎是一个脆弱的解决方案,因为 github url 有多种格式。

有没有我没有找到的 api 方法,比如 getRepoFromUrl(),或者我可以从提供的 url 中提取此信息的其他更可靠的方法?我正在使用节点/javascript。

谢谢

最佳答案

不使用正则表达式,您是否可以使用 GitHub API 列出特定的 repo url? ,为了解析 JSON 答案,获取应包含您要查找的信息的各种字段:

"owner": {
      "login": "octocat",

"name": "Hello-World",
"full_name": "octocat/Hello-World", 

由于输入的数据是一个url,查看该url的html页面的元数据包括什么。
对于我的 repo https://github.com/VonC/compileEverything ,我明白了:

<title>VonC/compileEverything</title>
<meta content="VonC/compileEverything" property="og:title" />
...

这应该至少允许取回用户名和存储库名称。

关于javascript - 从 github url 中查找用户、repo 和路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28852056/

相关文章:

C# GitHub SCIM API//cURL和HttpClient的区别

javascript - 在 Chart.js 中隐藏 y 轴的最小值和最大值

javascript - 在谷歌图表蜡烛中以短格式显示总数

git - 同一台电脑上有多个 GitHub 帐户吗?

github-api - 如何从github api获取 "my pull requests"?

github - 通过 GitHub API 访问链接的 SAML 身份

javascript - 对 MM :SS transition 使用 d3.interpolateString

javascript - 如何在 bazaarvoice api 中使用搜索选项

git 错误 : failed to push some refs to remote

git - 如何在我的 NetBeans IDE 中设置 github 存储库?