如何为GitHub为用户的固定存储库进行API调用? [英] How do I make an API call to GitHub for a user's pinned repositories?

查看:129
本文介绍了如何为GitHub为用户的固定存储库进行API调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GitHub上,用户可以使用



我'我希望能够做到以下几点:

  $ curl -s<某些端点带有一些参数> | <具有一些过滤的一些管道> 
str
liffy_diffy
spiralify
micro-twitter
kit
xdoc

所以我想知道:


  • 我需要的端点和参数是什么用户的固定回购?



我可以使用 nokogiri gem解析html。然而,似乎我应该通过一个简单的HTTP请求完成同样的事情:

  $ ./get_user_pinned_repos mbigras 
str
liffy_diffy
spiralify
micro-twitter
套件
xdoc

代码:

 #!/ usr / bin / env ruby​​ 
#获得用户的固定回购
需要'rubygems'
需要'nokogiri'
需要'open-uri'

如果ARGV.length!= 1
STDERR.puts用法:get_user_pinned_repos< username>
exit 1
end

username = ARGV.first
profile_url =https://github.com/#{username}
page = Nokogiri :: HTML(open(profile_url))
page.css(span.repo)。each {| repo | puts repo.text}


解决方案

GrapQL API (试一试 here ):

  {
repositoryOwner(登录:bertrandmartel){
...用户{
pinnedRepositories(first:6){
edges {
node {
名称
}
}
}
}
}
}

你可以用curl请求它:

  curl -H授权:持有者TOKEN-X POST --data-binary @  -  https://api.github.com/graphql<< EOF 
{
query:query {repositoryOwner(login:\ bertrandmartel \){... on User {pinnedRepositories(first:6){edges {node {name}}}}}}
}
EOF

您可以使用 jq JSON解析器:

  username = bertrandmartel 
curl -s -HAuthorization:bearer TOKEN\
-X POST \
-d'{query:query { repositoryOwner(login:\'$ username'\){... on User {pinnedRepositories(first:6){edges {node {name}}}}}}}'\
https://api.github.com/graphql | \
jq -r'.data.repositoryOwner.pinnedRepositories.edges []。node.name'


On GitHub, a user can have pinned repositories.

There's also the Repositories section of the API describing how to make requests that involve repos. You can also get information about the orgs a user is part of as described in another answer (which can be pinned).

However, I want to access a user's pinned repos. For example given the following profile:

I'd like to be able to do the following:

$ curl -s <some endpoint with some parameters> | <some pipeline with some filtering>
str
liffy_diffy
spiralify
micro-twitter
kit
xdoc

So I'm wondering:

  • What is the endpoint and parameters do I need to get a user's pinned repos?

I was able to use the nokogiri gem to parse the html. However, it seems like I should be api to accomplish the same thing with a simple HTTP request:

$ ./get_user_pinned_repos mbigras
str
liffy_diffy
spiralify
micro-twitter
kit
xdoc

Code:

#!/usr/bin/env ruby
# get a user's pinned repos
require 'rubygems'
require 'nokogiri'
require 'open-uri'

if ARGV.length != 1
  STDERR.puts "usage: get_user_pinned_repos <username>"
  exit 1
end

username = ARGV.first
profile_url = "https://github.com/#{username}"
page = Nokogiri::HTML(open(profile_url))
page.css("span.repo").each { |repo| puts repo.text }

解决方案

You can get pinned repository with GrapQL API (try it here):

{
  repositoryOwner(login: "bertrandmartel") {
    ... on User {
      pinnedRepositories(first:6) {
        edges {
          node {
            name
          }
        }
      }
    }
  }
}

You can request it with curl with :

curl -H "Authorization: bearer TOKEN" -X POST --data-binary @- https://api.github.com/graphql <<EOF
{
 "query": "query { repositoryOwner(login: \"bertrandmartel\") { ... on User { pinnedRepositories(first:6) { edges { node { name } } } } } }"
}
EOF

You can parse the JSON output with jq JSON parser :

username=bertrandmartel
curl -s -H "Authorization: bearer TOKEN" \
     -X POST \
     -d '{ "query": "query { repositoryOwner(login: \"'"$username"'\") { ... on User { pinnedRepositories(first:6) { edges { node { name } } } } } }" }' \
     https://api.github.com/graphql | \
     jq -r '.data.repositoryOwner.pinnedRepositories.edges[].node.name'

这篇关于如何为GitHub为用户的固定存储库进行API调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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