Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github API: Getting topics of a Github repository

Github API documentation give instructions to filter repositories by topics. Is there a way to use the API to get the topics from a specific repo?

like image 397
F Mariane Avatar asked Apr 06 '17 15:04

F Mariane


People also ask

How do I find topics on GitHub?

To search for a specific file or content within a repository, you can use the file finder or code-specific search qualifiers. For more information, see "Finding files on GitHub" and "Searching code." octocat in:readme matches repositories mentioning "octocat" in the repository's README file.

How do I retrieve data from GitHub repository?

Use git fetch to retrieve new work done by other people. Fetching from a repository grabs all the new remote-tracking branches and tags without merging those changes into your own branches. Otherwise, you can always add a new remote and then fetch. For more information, see "Managing remote repositories."

How do I fetch API in GitHub?

To get the list of random users, you have to make a request to the /user endpoint, and to get the details of a specific user, you make a request to the /user/{username} endpoint. You can try out the following commands quickly in your terminal with curl to see the results.


4 Answers

You can do this easily with Github API (it's currently in "preview mode"):

curl -H "Accept: application/vnd.github.mercy-preview+json" https://api.github.com/repos/twbs/bootstrap/topics
{
  "names": [
    "css",
    "bootstrap",
    "javascript",
    "html",
    "jekyll-site",
    "scss",
    "css-framework",
    "sass"
  ]
}

You need to include extra header Accept: application/vnd.github.mercy-preview+json.

There is one "but", since it's in "preview mode" it's not supported for production use (please read "Note" and "Warning" sections in link below).

See also:

  • https://docs.github.com/en/rest/reference/repos#get-a-repository
like image 185
tsg Avatar answered Oct 06 '22 01:10

tsg


I don't know that there is a way to just get the topics for a repository, but if you do a get for a repository, the repository json object that is returned will have a topics property that is an array of that repositories topics.

At the top of that page of documentation, you will notice that in order to have the topics returned you will need to add a specific header in your GET request: "Accept":"application/vnd.github.mercy-preview+json"

Hope this helps!

like image 26
peinearydevelopment Avatar answered Oct 06 '22 01:10

peinearydevelopment


You can do this with the Github GraphQL API

Query:

{
  repository(owner: "twbs", name: "bootstrap") {
    repositoryTopics(first: 10) {
      edges {
        node {
          topic {
            name
          }
        }
      }
    }
  }
}

This will return the first 10 topics and the name for each as shown below.
Response:

{
  "data": {
    "repository": {
      "repositoryTopics": {
        "edges": [
          {
            "node": {
              "topic": {
                "name": "css"
              }
            }
          },
          {
            "node": {
              "topic": {
                "name": "bootstrap"
              }
            }
          },
          {
            "node": {
              "topic": {
                "name": "javascript"
              }
            }
          },
          {
            "node": {
              "topic": {
                "name": "html"
              }
            }
          }
        ]
      }
    }
  }
}

Test it out in the GitHub GraphQL Explorer

like image 40
Wilhelm Klopp Avatar answered Oct 05 '22 23:10

Wilhelm Klopp


I added the fetch with Accept Headers:

fetch("https://api.github.com/users/lucksp/repos", 
   {
    method: "GET",
    headers: {
    Accept: "application/vnd.github.mercy-preview+json"
   }
})
like image 25
Phil Lucks Avatar answered Oct 05 '22 23:10

Phil Lucks