Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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 }
like image 300
mbigras Avatar asked Apr 11 '17 16:04

mbigras


People also ask

How do I make API call GitHub?

Go to Developer Settings ->Personal Access Tokens. Generate a new token. Add a name and select the scope for the API access and click on Create Token. In the next screen, make sure to copy the token and save it in a file. This token will be used in the command line to access GitHub API.

Is there an API for GitHub?

GitHub provides two APIs: a REST API and a GraphQL API. You can interact with both APIs using GitHub CLI, curl, the official Octokit libraries, and third party libraries.

What is the API endpoint for GitHub?

Github APIs( or Github ReST APIs) are the APIs that you can use to interact with GitHub. They allow you to create and manage repositories, branches, issues, pull requests, and many more. For fetching publicly available information (like public repositories, user profiles, etc.), you can call the API.

How do I get my GitHub API repository?

Hitting https://api.github.com/users/USERNAME/repos will list public repositories for the user USERNAME.


2 Answers

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

like image 111
Krantlak Avatar answered Sep 19 '22 16:09

Krantlak


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

like image 42
Sadisha Avatar answered Sep 19 '22 16:09

Sadisha