mbigras
mbigras

Reputation: 8055

How do I make an API call to GitHub for a user's pinned repositories?

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:

enter image description here

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:

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 }

Upvotes: 11

Views: 4249

Answers (4)

eQ19
eQ19

Reputation: 10691

Try this one line command:

curl -s -X POST "${GITHUB_GRAPHQL_URL}" -H "Authorization: bearer ${TOKEN}" --data-raw '{"query":"{\n  user(login: \"'${OWNER}'\") {\n pinnedItems(first: 6, types: REPOSITORY) {\n nodes {\n ... on Repository {\n name\n }\n }\n }\n }\n}"' | jq --raw-output '.data.user.pinnedItems'

Based on GraphQL ProfileOwner the following is applied when OWNER=name_of_an_organization:

curl -s -X POST "${GITHUB_GRAPHQL_URL}" -H "Authorization: bearer ${TOKEN}" --data-raw '{"query":"{\n  organization(login: \"'${OWNER}'\") {\n pinnedItems(first: 6, types: REPOSITORY) {\n nodes {\n ... on Repository {\n name\n }\n }\n }\n }\n}"' | jq --raw-output '.data.organization.pinnedItems'

Note: Please assign the variable GITHUB_GRAPHQL_URL=https://api.github.com/graphql if you send the query outside GitHub Environtment.

Upvotes: 0

Bertrand Martel
Bertrand Martel

Reputation: 45352

Outdated

You can get pinned repository using Github GraphQL API :

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

Try it in the explorer

You can request it with with :

curl -H "Authorization: bearer TOKEN" --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" \
     -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'

Upvotes: 9

Krantlak
Krantlak

Reputation: 135

An update to the original answer:

The changelog for GitHub GraphQL schema (2019-03-30) mentions that the pinnedRepositories will be deprecated or removed by 2019-07-01. GraphQL API users are requested to use ProfileOwner.pinnedItems instead.

Examples to retrieve pinned repositories using ProfileOwner.pinnedItems:

Example 1:

query {
    user(login:"kranthilakum") {
        pinnedItems(first: 5, types: [REPOSITORY, GIST]) {
            totalCount
            edges {
                node {
                    ... on Repository {
                    name
                    }
                }
            }
        }
    }
}

Try Example 1 in the GraphQL API Explorer

Example 2:

query {
  repositoryOwner(login: "kranthilakum") {
    ... on ProfileOwner {
      pinnedItemsRemaining
      itemShowcase {
        items(first: 5) {
          totalCount
          edges {
            node {
              ... on Repository {
                name
              }
            }
          }
        }
        hasPinnedItems
      }
    }
  }
}

Try Example 2 in GraphQL API Explorer

Upvotes: 12

Sadisha
Sadisha

Reputation: 571

Go to this URL and type your username and hit the "go" button. There you will see your pinned repos as a JSON responce.

Click here

Or else, you can replace your username in the below link.

https://gh-pinned-repos-5l2i19um3.vercel.app/?username={Username}

For more info: Click here or here

Upvotes: 2

Related Questions