从github网址找到用户,仓库和路径 [英] Find the user, repo and path from a github url

查看:90
本文介绍了从github网址找到用户,仓库和路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够获取用户,存储库以及粘贴到表单中的github url(到存储库中的文件)的路径,以便能够对其进行api请求.例如 https://github.com/HubSpot/BuckyClient/blob/master/README.md

I need to be able to get the user, repo and path from a github url (to a file in the repo) that is pasted into a form, to be able to make api requests on it. e.g. https://github.com/HubSpot/BuckyClient/blob/master/README.md

我已经开始编写一些正则表达式来尝试查找url的各个部分,但是鉴于github url的格式很多,这似乎是一种脆弱的解决方案.

I've started writing some regex to try and just locate the parts of the url, but this seems like a brittle solution given there are many formats of github urls.

是否有我没有找到的api方法,例如getRepoFromUrl()或其他更可靠的方法,可以从提供的url中提取此信息?我正在使用node/javascript.

Is there an api method I've not found, something like getRepoFromUrl(), or another more reliable way I can extract this information from the url provided? I'm using node/javascript.

谢谢

推荐答案

不是使用正则表达式,而是使用

Instead of using a regex, could you rather use list a specific repo url using the GitHub API, in order to parse the JSON answer, getting various fields which should include the information you seek:

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

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


由于输入数据是URL,请检查该URL的html页面的元数据包含什么.
对于我的仓库
https://github.com/VonC/compileEverything ,我看到了:

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

这应该至少可以获取用户名和存储库名称.

That should allow to get back at least the name of the user and the repo name.

这篇关于从github网址找到用户,仓库和路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆