Search
Learn and practice the functionalities of search in the GitHub API.
We'll cover the following
Overview
GitHub has integrated search for numerous types including users, repositories, pull requests, commits, and code. We can search globally across GitHub by choosing “All GitHub” in the search drop-down menu located on the top of a page. We can also search locally within a particular repository or organization by navigating into the repository.
This time we’ll search using API endpoints. GitHub uses the ElasticSearch cluster to index projects whenever a new change is made. Issues or pull requests are indexed at the time of creation or modification.
Search code
This is a complex feature and to reduce its complexity, there are some restrictions. These restrictions are as follows:
- Files smaller than 384KB are searchable.
- The default branch is searchable.
- At least one search time is required while searching the source code.
We need to construct a search query to find our desired result. Let’s suppose we need to search for all the repositories owned by Laura
that contain the word programming
and python
in the README
file. Our query looks like this:
q=programming+python+in:README+user:Laura
Another example of a query can be the one used in the code below:
headers = {'Authorization': 'token {{ACCESS_TOKEN}}','Accept': 'application/vnd.github.v3+json'}url='https://api.github.com/search/code?q=addClass+in:file+language:js+repo:jquery/jquery'response = requests.get(url, headers=headers)print(json.dumps(response.json(), indent=4))
Search repositories
This endpoint helps us search for repositories. We need to construct a query, as we did above.
Let’s interpret the query written in the code below:
- It will search for popular Tetris repositories written in the assembly language.
- Then, it sorts the items by the number of stars.
- Finally, the query is displayed in descending order.
Run the code below to further understand the concept:
headers = {'Authorization': 'token {{ACCESS_TOKEN}}','Accept': 'application/vnd.github.v3.text-match+json'}url='https://api.github.com/search/repositories?q=tetris+language:assembly&sort=stars&order=desc'response = requests.get(url, headers=headers)print(json.dumps(response.json(), indent=4))
Search commits
Like repositories, we can also search for commits using the GitHub API. The following code searches for CSS-related commits in the octat/Spoon-Knife
repository.
headers = {'Authorization': 'token {{ACCESS_TOKEN}}','Accept': 'application/vnd.github.v3+json'}url='https://api.github.com/search/commits?q=repo:octocat/Spoon-Knife+css'response = requests.get(url, headers=headers)print(json.dumps(response.json(), indent=4))
Search pull request
The following code uses the GitHub API to search for the pull requests. The query in line 6 will display the Python bugs that are open (or unresolved) in Windows. As the order is set to asc
, the oldest bugs will be displayed first.
headers = {'Authorization': 'token {{ACCESS_TOKEN}}','Accept': 'application/vnd.github.v3+json'}url='https://api.github.com/search/issues?q=windows+label:bug+language:python+state:open&sort=created&order=asc'response = requests.get(url, headers=headers)print(json.dumps(response.json(), indent=4))
Search users
Finally, let's use the GitHub API to search for users. The following code searches for a user named Kate that has repositories greater than 10
and followers over 50
.
headers = {'Authorization': 'token {{ACCESS_TOKEN}}','Accept': 'application/vnd.github.v3+json'}url='https://api.github.com/search/users?q=kate+repos:%3E10+followers:%3E50'response = requests.get(url, headers=headers)print(json.dumps(response.json(), indent=4))