Golang – 从令牌获取Git用户名和仓库列表

huangapple go评论83阅读模式
英文:

Golang - Get Git username & repo list from token

问题

我想根据OAuth令牌提取git仓库列表。
我想为GitHub / GitLab和Bitbucket编写通用代码。

最有可能的是,首先我可能需要从令牌中提取用户名,然后我将需要提取列表。就像GitHub文档建议的那样。

https://docs.github.com/en/rest/reference/users#get-the-authenticated-user
https://api.github.com/users/<username>/repos

但是,这只能帮助我使用GitHub。
我查看了https://pkg.go.dev/github.com/go-git/go-git/v5#Repository,因为它适用于所有git平台。但是,我找不到基于OAuth令牌的仓库列表函数。

最好的方法是什么?
提前谢谢!

英文:

I want to extract the list of git repositories on the basis of oauth token.
And I want to write a common code for github / gitlab & bitbucket.

Most probably, first I may need to extract username from token and then I will need to extract the list.. Like Github documentation suggests.

https://docs.github.com/en/rest/reference/users#get-the-authenticated-user
https://api.github.com/users/&lt;username&gt;/repos

But, this will only help me with github.
I have looked into https://pkg.go.dev/github.com/go-git/go-git/v5#Repository
because it works with all git platforms. But, I couldn't find a repo listing function (based on oauth token)

What is the best way to do it ?
Thanks in advance !

答案1

得分: 1

没有标准统一的方法来获取这些信息。当你使用Git时,你提供身份验证凭据,Git使用这些凭据与服务器进行通信,无论是GitHub、GitLab、Bitbucket、你自己的Apache实例还是其他服务器,它要么接受凭据,要么拒绝凭据。凭据并不要求提供任何额外的功能。

对于GitHub,你可以使用API来获取这些信息,前提是使用的是OAuth令牌或者PAT,并且具有"user"范围。GitHub还有其他类型的令牌,可以用于克隆操作,但不能用于API,当然,任何缺少"user"范围的令牌都无法使用。我应该指出的是,对于GitHub的令牌,用户名并没有嵌入在令牌中(正如他们在博客中文档化的),所以你确实需要使用API。

GitLab可能也有一个可以使用的API来获取这些信息,但不会有一个单一的方法适用于所有仓库,并且可能无法根据令牌类型确定要使用的域。例如,github.com和GitHub Enterprise Server(本地部署选项)将发行外观相同的令牌,但这些令牌只能在发行它们的系统上使用。

英文:

There is no standard uniform way to get this information. When you use Git, you provide it authentication credentials which it uses to talk to the server, and that server, whether it is GitHub, GitLab, Bitbucket, your own Apache instance, or otherwise, either accepts it or rejects it. There is no requirement that the credentials provide any more functionality than that.

In the case of GitHub, you can use the API to get that information provided it's an OAuth token or a PAT and it has the user scope. GitHub has other kinds of tokens which can be used for a clone which cannot be used for the API, and of course any token which lacks the user scope won't work. I should point out that the for GitHub's tokens, the username isn't embedded in the token (as they've documented on their blog), so you really do have to use the API.

GitLab may also have an API for this which you can use, but there won't be a single way to work with all repositories, and it may not be possible to determine which domain to use from the token type. For example, github.com and GitHub Enterprise Server (the on-premises option) will issue tokens that look identical, but of course the tokens will only work on the system that issued them.

huangapple
  • 本文由 发表于 2021年12月13日 16:29:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/70331767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定